11 #include "IException.h"
14 #include "PvlFormat.h"
22 using json = nlohmann::json;
63 for(
auto it = jsonobj.begin(); it != jsonobj.end(); it++) {
65 if (it.value().is_array()) {
66 keyword.
setName(QString::fromStdString(it.key()));
67 for(
auto ar = it.value().begin(); ar!=it.value().end();ar++) {
68 keyword += QString::number(ar->get<
double>());
71 else if(it.value().is_number()) {
72 keyword.
setName(QString::fromStdString(it.key()));
73 keyword.
setValue(QString::number(it->get<
double>()));
75 else if(it.value().is_boolean()) {
76 keyword.
setName(QString::fromStdString(it.key()));
77 keyword.
setValue(QString(it->get<
bool>() ?
"true" :
"false"));
79 else if(it.value().is_null()) {
80 keyword.
setName(QString::fromStdString(it.key()));
84 keyword.
setName(QString::fromStdString(it.key()));
85 keyword.
setValue(QString::fromStdString(it.value()));
103 vector<PvlObject *> searchList;
104 searchList.push_back(
this);
106 while(searchList.size() > 0) {
108 searchList[0]->findGroup(
name,
111 if(it != searchList[0]->
endGroup())
return *it;
113 for(
int i = 0; i < searchList[0]->objects(); i++) {
114 searchList.push_back(&searchList[0]->
object(i));
117 searchList.erase(searchList.begin());
120 QString msg =
"Unable to find PVL group [" +
name +
"]";
138 vector<const PvlObject *> searchList;
139 searchList.push_back(
this);
141 while(searchList.size() > 0) {
142 ConstPvlGroupIterator it =
143 searchList[0]->findGroup(
name,
146 if(it != searchList[0]->
endGroup())
return *it;
148 for(
int i = 0; i < searchList[0]->objects(); i++) {
149 searchList.push_back(&searchList[0]->
object(i));
152 searchList.erase(searchList.begin());
155 QString msg =
"Unable to find PVL group [" +
name +
"]";
185 vector<PvlObject *> searchList;
186 searchList.push_back(
this);
188 while(searchList.size() > 0) {
190 searchList[0]->findKeyword(kname, searchList[0]->
begin(),
191 searchList[0]->
end());
192 if(it != searchList[0]->
end()) {
197 for(
int g = 0; g < searchList[0]->groups(); g++) {
199 searchList[0]->group(g).findKeyword(kname,
202 if(it != searchList[0]->
group(g).
end()) {
209 for(
int i = 0; i < searchList[0]->objects(); i++) {
210 searchList.push_back(&searchList[0]->
object(i));
214 searchList.erase(searchList.begin());
218 QString msg =
"Unable to find PVL keyword [" + kname +
"]";
244 vector<const PvlObject *> searchList;
245 searchList.push_back(
this);
247 while(searchList.size() > 0) {
249 searchList[0]->findKeyword(kname, searchList[0]->
begin(),
250 searchList[0]->
end());
251 if(it != searchList[0]->
end()) {
256 for(
int g = 0; g < searchList[0]->groups(); g++) {
258 searchList[0]->group(g).findKeyword(kname,
262 if(it != searchList[0]->
group(g).
end()) {
269 for(
int i = 0; i < searchList[0]->objects(); i++) {
270 searchList.push_back(&searchList[0]->
object(i));
274 searchList.erase(searchList.begin());
292 vector<PvlObject *> searchList;
293 searchList.push_back(
this);
295 while(searchList.size() > 0) {
297 searchList[0]->findObject(
name,
300 if(it != searchList[0]->
endObject())
return *it;
302 for(
int i = 0; i < searchList[0]->objects(); i++) {
303 searchList.push_back(&searchList[0]->
object(i));
306 searchList.erase(searchList.begin());
309 QString msg =
"Unable to find PVL object [" +
name +
"]";
327 vector<const PvlObject *> searchList;
328 searchList.push_back(
this);
330 while(searchList.size() > 0) {
331 ConstPvlObjectIterator it =
332 searchList[0]->findObject(
name,
341 for(
int i = 0; i < searchList[0]->objects(); i++) {
342 searchList.push_back(&searchList[0]->
object(i));
346 searchList.erase(searchList.begin());
349 QString msg =
"Unable to find PVL object [" +
name +
"]";
369 QString msg =
"Unable to find PVL object [" +
name +
"] in " +
type() +
370 " [" + this->
name() +
"]";
387 if(index >= (
int)
m_objects.size() || index < 0) {
388 QString msg =
"The specified index is out of bounds in PVL " +
type() +
395 for(
int i = 0; i < index; i++) key++;
411 QString msg =
"Unable to find PVL group [" +
name +
"] in " +
type() +
412 " [" + this->
name() +
"]";
429 if(index >= (
int)
m_groups.size() || index < 0) {
430 QString msg =
"The specified index is out of bounds in PVL " +
type() +
437 for(
int i = 0; i < index; i++) key++;
453 if(index < 0 || index >= (
int)
m_groups.size()) {
472 if(index < 0 || index >= (
int)
m_groups.size()) {
490 if(index < 0 || index >= (
int)
m_objects.size()) {
508 if(index < 0 || index >= (
int)
m_objects.size()) {
526 bool removeFormatter =
false;
527 if(
object.format() == NULL) {
529 removeFormatter =
true;
533 if(
object.hasFormatTemplate())
540 for(
int i = 0; i < outTemplate.comments(); i++) {
541 newTemp.addComment(outTemplate.comment(i));
545 for(
int i = 0; i < outTemplate.
keywords(); i++) {
546 if(outTemplate[i].isNamed(
"Isis:PvlTemplate:File")) {
547 QString filename = outTemplate[i];
550 QString message =
"Could not open the following PVL template file: ";
557 for(
int j = 0; j < include.
keywords(); j++) {
562 for(
int j = 0; j < include.
objects(); j++) {
567 for(
int j = 0; j < include.
groups(); j++) {
578 for(
int i = 0; i < outTemplate.
objects(); i++) {
584 for(
int i = 0; i < outTemplate.
groups(); i++) {
589 outTemplate = newTemp;
592 if(outTemplate.comments() > 0) {
593 for(
int k = 0; k < outTemplate.comments(); k++) {
594 for(
int l = 0; l <
object.indent(); l++) os <<
" ";
595 os << outTemplate.comment(k) <<
object.format()->formatEOL();
601 os <<
object.nameKeyword() <<
object.format()->formatEOL();
602 object.setIndent(
object.indent() + 2);
605 if(
object.keywords() > 0) {
613 for(
int i = 0; i < outTemplate.
objects(); i++) {
614 for(
int j = 0; j <
object.objects(); j++) {
615 if(outTemplate.
object(i).
name() !=
object.object(j).name())
continue;
616 if(j == 0 &&
object.keywords() > 0)
617 os <<
object.format()->formatEOL();
619 object.object(j).setIndent(
object.indent());
620 object.object(j).setFormatTemplate(outTemplate.
object(i));
621 object.object(j).setFormat(
object.format());
622 os <<
object.object(j) <<
object.format()->formatEOL();
623 object.object(j).setFormat(NULL);
624 object.object(j).setIndent(0);
626 if(++numObjects <
object.objects())
627 os <<
object.format()->formatEOL();
633 for(
int i = 0; i <
object.objects(); i++) {
634 if(outTemplate.
hasObject(
object.object(i).name()))
continue;
635 if(i == 0 &&
object.keywords() > 0)
636 os <<
object.format()->formatEOL();
638 object.object(i).setIndent(
object.indent());
639 object.object(i).setFormat(
object.format());
640 os <<
object.object(i) <<
object.format()->formatEOL();
641 object.object(i).setFormat(NULL);
642 object.object(i).setIndent(0);
644 if(++numObjects <
object.objects())
645 os <<
object.format()->formatEOL();
653 for(
int i = 0; i < outTemplate.
groups(); i++) {
654 for(
int j = 0; j <
object.groups(); j++) {
655 if(outTemplate.
group(i).
name() !=
object.group(j).name())
continue;
656 if((numgroups == 0) &&
657 (
object.objects() > 0 ||
object.keywords() > 0))
658 os <<
object.format()->formatEOL();
660 object.group(j).setIndent(
object.indent());
661 object.group(j).setFormatTemplate(outTemplate.
group(i));
662 object.group(j).setFormat(
object.format());
663 os <<
object.group(j) <<
object.format()->formatEOL();
664 object.group(j).setFormat(NULL);
665 object.group(j).setIndent(0);
666 if(++numgroups <
object.groups()) os <<
object.format()->formatEOL();
671 for(
int i = 0; i <
object.groups(); i++) {
672 if(outTemplate.
hasGroup(
object.group(i).name()))
continue;
673 if((numgroups == 0) &&
674 (
object.objects() > 0 ||
object.keywords() > 0))
675 os <<
object.format()->formatEOL();
677 object.group(i).setIndent(
object.indent());
678 object.group(i).setFormat(
object.format());
679 os <<
object.group(i) <<
object.format()->formatEOL();
680 object.group(i).setFormat(NULL);
681 object.group(i).setIndent(0);
683 if(++numgroups <
object.groups())
684 os <<
object.format()->formatEOL();
688 object.setIndent(
object.indent() - 2);
689 for(
int i = 0; i <
object.indent(); i++) os <<
" ";
690 os <<
object.format()->formatEnd(
"End_Object",
object.nameKeyword());
692 if(removeFormatter) {
693 delete object.format();
694 object.setFormat(NULL);
714 istream::pos_type beforeKeywordPos = is.tellg();
718 if(is.eof() && !is.bad()) {
722 is.seekg(beforeKeywordPos, ios::beg);
724 QString msg =
"Expected PVL keyword named [Object], found keyword named [";
725 msg += readKeyword.name();
730 if(readKeyword.size() == 1) {
731 result.
setName(readKeyword[0]);
734 is.seekg(beforeKeywordPos, ios::beg);
736 QString msg =
"Expected a single value for PVL object name, found [(";
738 for(
int i = 0; i < readKeyword.size(); i++) {
739 if(i != 0) msg +=
", ";
741 msg += readKeyword[i];
748 for(
int comment = 0; comment < readKeyword.comments(); comment++) {
749 result.addComment(readKeyword.comment(comment));
753 beforeKeywordPos = is.tellg();
756 while(readKeyword != termination) {
757 for(
unsigned int errorKey = 0;
758 errorKey <
sizeof(errorKeywords) /
sizeof(
PvlKeyword);
760 if(readKeyword == errorKeywords[errorKey]) {
761 if(is.eof() && !is.bad()) {
765 is.seekg(beforeKeywordPos, ios::beg);
767 QString msg =
"Unexpected [";
768 msg += readKeyword.name();
769 msg +=
"] in PVL Object [";
770 msg += result.
name();
777 is.seekg(beforeKeywordPos);
782 else if(readKeyword ==
PvlKeyword(
"Object")) {
783 is.seekg(beforeKeywordPos);
793 beforeKeywordPos = is.tellg();
804 if(readKeyword != termination) {
805 if(is.eof() && !is.bad()) {
809 is.seekg(beforeKeywordPos, ios::beg);
811 QString msg =
"PVL Object [" + result.
name();
812 msg +=
"] EndObject not found before end of file";
846 for(
int i=0; i<iObjSize; i++) {
849 QString sObjName = pvlTmplObj.
name();
850 bool bObjFound =
false;
862 QString sOption = sObjName +
"__Required";
866 if(pvlKeyOption[0] ==
"true") {
871 if (bObjFound ==
false) {
872 QString sErrMsg =
"Object \"" + sObjName +
"\" Not Found in the Template File\n";
878 int iTmplGrpSize =
groups();
879 for(
int i=0; i<iTmplGrpSize; i++) {
881 bool bGrpFound =
false;
882 QString sGrpName = pvlTmplGrp.
name();
895 QString sOption = sGrpName +
"__Required";
898 if(pvlKeyOption[0] ==
"true") {
903 if (bGrpFound ==
false) {
904 QString sErrMsg =
"Group \"" + sGrpName +
"\" Not Found in the Template File\n";