7 #include "IException.h"
11 #include "PvlKeyword.h"
14 #include "PvlFormat.h"
23 PvlFormat::PvlFormat() {
36 PvlFormat::PvlFormat(
const QString &file) {
49 PvlFormat::PvlFormat(Pvl &keywordType) {
56 void PvlFormat::init() {
58 m_keywordMapFile.clear();
68 void PvlFormat::add(
const QString &file) {
69 m_keywordMapFile = file;
76 catch(IException &e) {
78 msg +=
"Unable to open or read keyword to type mapping file [";
80 throw IException(e, IException::Programmer, msg, _FILEINFO_);
90 void PvlFormat::add(Pvl &pvl) {
91 for(
int i = 0; i < pvl.keywords(); ++i) {
92 PvlKeyword &key = pvl[i];
93 QString name = key.name().toUpper();
94 QString type = key[0].toUpper();
95 PvlKeyword newKey(name, type);
96 for(
int j = 1; j < key.size(); ++j) newKey.addValue(key[j]);
98 if (m_keywordMap.hasKeyword(name)) {
99 m_keywordMap.deleteKeyword(name);
101 m_keywordMap.addKeyword(newKey);
111 KeywordType PvlFormat::type(
const PvlKeyword &keyword) {
112 QString name = keyword.name().toUpper();
113 if(m_keywordMap.hasKeyword(name)) {
114 PvlKeyword &key = m_keywordMap.findKeyword(name);
117 return NoTypeKeyword;
129 int PvlFormat::accuracy(
const PvlKeyword &keyword) {
130 QString name = keyword.name().toUpper();
131 if(m_keywordMap.hasKeyword(name)) {
132 PvlKeyword &key = m_keywordMap.findKeyword(name);
134 return toInt(key[1]);
147 QString PvlFormat::formatValue(
const PvlKeyword &keyword,
int num) {
153 bool singleUnit = isSingleUnit(keyword);
156 if(num >= keyword.size()) {
161 if(keyword[num].size() == 0) {
168 val = addQuotes(val);
171 if((keyword.size() > 1) && (num == 0)) {
176 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
177 val +=
" <" + keyword.unit(num) +
">";
181 if(num != keyword.size() - 1) {
185 else if(keyword.size() > 1) {
190 if((singleUnit) && (num == keyword.size() - 1) &&
191 (keyword.unit(num).size() > 0)) {
192 val +=
" <" + keyword.unit(num) +
">";
204 QString PvlFormat::formatName(
const PvlKeyword &keyword) {
205 return keyword.name();
215 QString PvlFormat::formatEnd(
const QString name,
216 const PvlKeyword &keyword) {
217 return "End_" + formatName(keyword);
227 QString PvlFormat::addQuotes(
const QString value) {
230 bool needQuotes =
false;
233 char existingQuoteType =
'\0';
234 for (
int pos = 0; !needQuotes && pos < val.size(); pos++) {
238 if (val[pos] ==
'(' || val[pos] ==
'{') {
241 if (val[pos] ==
'(') {
242 closePos = val.indexOf(
')');
244 if (val[pos] ==
'{') {
245 closePos = val.indexOf(
'}');
249 if (closePos == -1 || closePos != val.size() - 1) {
258 if (val[pos] ==
' ' || val[pos] ==
'(' ||
259 val[pos] ==
'(' || val[pos] ==
')' ||
260 val[pos] ==
'{' || val[pos] ==
'}' ||
261 val[pos] ==
',' || val[pos] ==
'=') {
265 if (pos == val.size() - 1 && val[pos] ==
'-') {
270 if (existingQuoteType ==
'\0') {
271 if (val[pos] ==
'"') {
272 existingQuoteType =
'"';
274 else if (val[pos] ==
'\'') {
275 existingQuoteType =
'\'';
280 if (val[pos] ==
'"' || val[pos] ==
'\'') {
281 val[pos] = existingQuoteType;
287 char quoteValue =
'"';
289 if(existingQuoteType ==
'"') {
294 val = quoteValue + val + quoteValue;
310 bool singleUnit =
true;
311 for(
int i = 0; i < keyword.
size(); i ++) {