12 #include "IException.h"
16 #include "Constants.h"
19 #include "PvlFormatPds.h"
28 PvlFormatPds::PvlFormatPds() {
40 PvlFormatPds::PvlFormatPds(
const QString &file) : PvlFormat(file) {
52 PvlFormatPds::PvlFormatPds(Pvl &keywordType) : PvlFormat(keywordType) {
68 QString PvlFormatPds::formatValue(
const PvlKeyword &keyword,
int num) {
70 QString name = keyword.
name().toUpper();
71 if(name ==
"OBJECT" || (name ==
"GROUP")) {
72 QString val = (QString)keyword;
81 return formatString(keyword, num);
85 return formatReal(keyword, num, accuracy(keyword));
89 return formatInteger(keyword, num, accuracy(keyword));
93 return formatHex(keyword, num, accuracy(keyword));
97 return formatBinary(keyword, num, accuracy(keyword));
101 return formatEnum(keyword, num);
105 return formatBool(keyword, num);
110 return formatUnknown(keyword, num);
113 return formatUnknown(keyword, num);
128 QString PvlFormatPds::formatString(
const PvlKeyword &keyword,
int num) {
131 bool singleUnit =
false;
134 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
139 QString tmp = keyword[num][0].toUpper();
140 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
141 val +=
"\"" + tmp +
"\"";
145 val = addQuotes(val);
150 if((keyword.size() > 1) && (num == 0)) {
155 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
156 QString unit = keyword.unit(num);
159 val +=
" <" + unit +
">";
163 if(num != keyword.size() - 1) {
167 else if(keyword.size() > 1) {
172 if((singleUnit) && (num == keyword.size() - 1) &&
173 (keyword.unit(num).size() > 0)) {
174 QString unit = keyword.unit(num);
177 val +=
" <" + unit +
">";
190 QString PvlFormatPds::formatReal(
const PvlKeyword &keyword,
int num,
195 bool singleUnit =
false;
198 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
203 if((keyword.size() > 1) && (num == 0)) {
208 QString tmp = keyword[num].toUpper();
209 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
210 val +=
"\"" + tmp +
"\"";
212 else if(places >= 0) {
214 out << setiosflags(ios::fixed) << setprecision(places) <<
toDouble((QString)keyword[num]);
215 val += out.str().c_str();
222 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
223 QString unit = keyword.unit(num);
225 val +=
" <" + unit +
">";
229 if(num != keyword.size() - 1) {
233 else if(keyword.size() > 1) {
238 if((singleUnit) && (num == keyword.size() - 1) &&
239 (keyword.unit(num).size() > 0)) {
240 QString unit = keyword.unit(num);
242 val +=
" <" + unit +
">";
255 QString PvlFormatPds::formatEnum(
const PvlKeyword &keyword,
int num) {
259 bool singleUnit =
false;
262 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
267 if((keyword.size() > 1) && (num == 0)) {
272 QString tmp = keyword[num].toUpper();
273 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
274 val +=
"\"" + tmp +
"\"";
281 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
282 QString unit = keyword.unit(num);
284 val +=
" <" + unit +
">";
288 if(num != keyword.size() - 1) {
292 else if(keyword.size() > 1) {
297 if((singleUnit) && (num == keyword.size() - 1) &&
298 (keyword.unit(num).size() > 0)) {
299 QString unit = keyword.unit(num);
301 val +=
" <" + unit +
">";
319 QString PvlFormatPds::formatUnknown(
const PvlKeyword &keyword,
int num) {
323 bool singleUnit =
false;
326 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
331 QString tmp = keyword[num].toUpper();
332 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
333 val +=
"\"" + tmp +
"\"";
337 val = PvlFormat::addQuotes(val);
342 if((keyword.size() > 1) && (num == 0)) {
347 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
348 QString unit = keyword.unit(num);
350 val +=
" <" + unit +
">";
354 if(num != keyword.size() - 1) {
358 else if(keyword.size() > 1) {
363 if((singleUnit) && (num == keyword.size() - 1) &&
364 (keyword.unit(num).size() > 0)) {
365 QString unit = keyword.unit(num);
367 val +=
" <" + unit +
">";
380 QString PvlFormatPds::formatInteger(
const PvlKeyword &keyword,
int num,
int bytes) {
384 bool singleUnit =
false;
387 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
392 if((keyword.size() > 1) && (num == 0)) {
397 QString tmp = keyword[num].toUpper();
398 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
399 val +=
"\"" + tmp +
"\"";
406 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
407 QString unit = keyword.unit(num);
409 val +=
" <" + unit +
">";
413 if(num != keyword.size() - 1) {
417 else if(keyword.size() > 1) {
422 if((singleUnit) && (num == keyword.size() - 1) &&
423 (keyword.unit(num).size() > 0)) {
424 QString unit = keyword.unit(num);
426 val +=
" <" + unit +
">";
439 QString PvlFormatPds::formatBinary(
const PvlKeyword &keyword,
int num,
int bits) {
443 bool singleUnit =
false;
447 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
452 if((keyword.size() > 1) && (num == 0)) {
457 QString tmp = keyword[num].toUpper();
458 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
459 val +=
"\"" + tmp +
"\"";
464 string binDig =
"01";
466 tmp = binDig[value % 2] + tmp;
471 ss << right << setfill(
'0') << setw(bits) << tmp;
472 tmp = ss.str().c_str();
473 val +=
"2#" + tmp +
"#";
477 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
478 QString unit = keyword.unit(num);
480 val +=
" <" + unit +
">";
484 if(num != keyword.size() - 1) {
488 else if(keyword.size() > 1) {
493 if((singleUnit) && (num == keyword.size() - 1) &&
494 (keyword.unit(num).size() > 0)) {
495 QString unit = keyword.unit(num);
497 val +=
" <" + unit +
">";
512 QString PvlFormatPds::formatHex(
const PvlKeyword &keyword,
int num,
int bytes) {
516 bool singleUnit =
false;
519 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
524 if((keyword.size() > 1) && (num == 0)) {
529 QString tmp = keyword[num].toUpper();
530 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
531 val +=
"\"" + tmp +
"\"";
536 ss << hex << (
unsigned short int)
toInt((QString)keyword[num]);
538 else if(bytes == 4) {
539 ss << hex << (
unsigned int)
toInt((QString)keyword[num]);
542 ss << hex <<
toBigInt((QString)keyword[num]);
544 QString h = ss.str().c_str();
546 val +=
"16#" + h +
"#";
550 if((!singleUnit) && (keyword.unit(num).size() > 0)) {
551 QString unit = keyword.unit(num);
553 val +=
" <" + unit +
">";
557 if(num != keyword.size() - 1) {
561 else if(keyword.size() > 1) {
566 if((singleUnit) && (num == keyword.size() - 1) &&
567 (keyword.unit(num).size() > 0)) {
568 QString unit = keyword.unit(num);
570 val +=
" <" + unit +
">";
583 QString PvlFormatPds::formatBool(
const PvlKeyword &keyword,
int num) {
589 if((num >= keyword.size()) || (keyword[num].size() == 0)) {
594 if((keyword.size() > 1) && (num == 0)) {
599 QString tmp = keyword[num];
601 if((tmp ==
"N/A") || (tmp ==
"NULL") || (tmp ==
"UNK")) {
602 val +=
"\"" + tmp +
"\"";
609 if(num != keyword.size() - 1) {
613 else if(keyword.size() > 1) {
628 QString PvlFormatPds::formatName(
const PvlKeyword &keyword) {
629 QString text = keyword.name();
630 text = text.toUpper();
641 QString PvlFormatPds::formatEnd(
const QString name,
642 const PvlKeyword &keyword) {
643 QString left = name.toUpper();
645 QString right = (QString)keyword;
646 right = right.toUpper();
661 QString PvlFormatPds::addQuotes(
const QString value) {
665 bool quoteValue =
true;
666 bool singleQuoteValue =
false;
667 if(val.contains(
" ")) {
668 if(val.contains(
"\"")) {
669 singleQuoteValue =
true;
677 singleQuoteValue =
false;
680 else if(val[0] ==
'"') {
681 singleQuoteValue =
false;
684 else if(val[0] ==
'\'') {
685 singleQuoteValue =
false;
690 val =
"\"" + val +
"\"";
692 else if(singleQuoteValue) {
693 val =
"'" + val +
"'";