Isis 3 Programmer Reference
PvlGroup.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7
8#include "PvlGroup.h"
9#include "PvlKeyword.h"
10#include "IException.h"
11#include "PvlFormat.h"
12
13using namespace std;
14namespace Isis {
17
22 PvlGroup::PvlGroup(const QString &name) :
23 Isis::PvlContainer("Group", name) {
24 }
25
26
28 PvlGroup::PvlGroup(const PvlGroup &other) : PvlContainer(other) {}
29
30
38 std::istream &operator>>(std::istream &is, PvlGroup &result) {
39 PvlKeyword termination("EndGroup");
40
41 PvlKeyword errorKeywords[] = {
42 PvlKeyword("Group"),
43 PvlKeyword("Object"),
44 PvlKeyword("EndObject")
45 };
46
47 PvlKeyword readKeyword;
48
49 istream::pos_type beforeKeywordPos = is.tellg();
50 is >> readKeyword;
51
52 if(readKeyword != PvlKeyword("Group")) {
53 if(is.eof() && !is.bad()) {
54 is.clear();
55 }
56
57 is.seekg(beforeKeywordPos, ios::beg);
58
59 QString msg = "Expected PVL keyword named [Group], found keyword named [";
60 msg += readKeyword.name();
61 msg += "] when reading PVL";
62 throw IException(IException::Unknown, msg, _FILEINFO_);
63 }
64
65 if(readKeyword.size() == 1) {
66 result.setName(readKeyword[0]);
67 }
68 else {
69 if(is.eof() && !is.bad()) {
70 is.clear();
71 }
72
73 is.seekg(beforeKeywordPos, ios::beg);
74
75 QString msg = "Expected a single value for group name, found [(";
76
77 for(int i = 0; i < readKeyword.size(); i++) {
78 if(i != 0) msg += ", ";
79
80 msg += readKeyword[i];
81 }
82
83 msg += ")] when reading PVL";
84 throw IException(IException::Unknown, msg, _FILEINFO_);
85 }
86
87
88 for(int comment = 0; comment < readKeyword.comments(); comment++) {
89 result.addComment(readKeyword.comment(comment));
90 }
91
92 readKeyword = PvlKeyword();
93 beforeKeywordPos = is.tellg();
94
95 is >> readKeyword;
96 while(is.good() && readKeyword != termination) {
97 for(unsigned int errorKey = 0;
98 errorKey < sizeof(errorKeywords) / sizeof(PvlKeyword);
99 errorKey++) {
100
101 if(readKeyword == errorKeywords[errorKey]) {
102 if(is.eof() && !is.bad()) {
103 is.clear();
104 }
105
106 is.seekg(beforeKeywordPos, ios::beg);
107
108 QString msg = "Unexpected [";
109 msg += readKeyword.name();
110 msg += "] in Group [";
111 msg += result.name();
112 msg += "] when reading PVL";
113 throw IException(IException::Unknown, msg, _FILEINFO_);
114 }
115 }
116
117 result.addKeyword(readKeyword);
118 readKeyword = PvlKeyword();
119 beforeKeywordPos = is.tellg();
120
121 is >> readKeyword;
122 }
123
124 if(readKeyword != termination) {
125 if(is.eof() && !is.bad()) {
126 is.clear();
127 is.unget();
128 }
129
130 is.seekg(beforeKeywordPos, ios::beg);
131
132 QString msg = "Group [" + result.name();
133 msg += "] EndGroup not found before end of file when reading PVL";
134 throw IException(IException::Unknown, msg, _FILEINFO_);
135 }
136
137 return is;
138 }
139
145 ostream &operator<<(std::ostream &os, PvlGroup &group) {
146
147 // Set up a Formatter
148 bool removeFormatter = false;
149 if(group.format() == NULL) {
150 group.setFormat(new PvlFormat());
151 removeFormatter = true;
152 }
153
154 Isis::PvlGroup temp("DEFAULT");
155 if(group.hasFormatTemplate()) temp = *(Isis::PvlGroup *)group.formatTemplate();
156
157 // Output comment from the template
158 if(temp.comments() > 0) {
159 for(int k = 0; k < temp.comments(); k++) {
160 for(int l = 0; l < group.indent(); l++) os << " ";
161 os << temp.comment(k) << group.format()->formatEOL();
162 }
163// os << group.format()->formatEOL();
164 }
165
166 // Output the group comments and name
167 os << group.nameKeyword() << group.format()->formatEOL();
168 group.setIndent(group.indent() + 2);
169
170 // Output the keywords in this group
171 if(group.keywords() > 0) {
172 os << (Isis::PvlContainer &) group << group.format()->formatEOL();
173 }
174
175 // Output the end of the group
176 group.setIndent(group.indent() - 2);
177 for(int i = 0; i < group.indent(); i++) os << " ";
178 os << group.format()->formatEnd("End_Group", group.nameKeyword());
179
180 if(removeFormatter) {
181 delete group.format();
182 group.setFormat(NULL);
183 }
184
185 return os;
186 }
187
188
190 const PvlGroup &PvlGroup::operator=(const PvlGroup &other) {
191 this->PvlContainer::operator=(other);
192
193 return *this;
194 }
195
208 {
209 // Group cannot be empty - needs to have a keyword
210 if(pPvlGrp.keywords() <= 0) {
211 QString sErrMsg = "Group \"" + pPvlGrp.name() + "\" has no Keywords\n";
212 throw IException(IException::User, sErrMsg, _FILEINFO_);
213 }
214
216 }
217
218} // end namespace isis
Isis exception class.
Definition IException.h:91
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition IException.h:118
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
Contains more than one keyword-value pair.
const PvlContainer & operator=(const PvlContainer &other)
This is an assignment operator.
int keywords() const
Returns the number of keywords contained in the PvlContainer.
void setName(const QString &name)
Set the name of the container.
QString name() const
Returns the container name.
void validateAllKeywords(PvlContainer &pPvlCont)
Validate All the Keywords in a Container comparing with the Template.
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
Formats a Pvl name value pair to Isis standards.
Definition PvlFormat.h:108
Contains multiple PvlContainers.
Definition PvlGroup.h:41
PvlGroup()
Creates a blank PvlGroup object.
Definition PvlGroup.cpp:16
const PvlGroup & operator=(const PvlGroup &other)
This is an assignment operator.
Definition PvlGroup.cpp:190
void validateGroup(PvlGroup &pPvlGrp)
Validate a Group comparing with the Template Group.
Definition PvlGroup.cpp:207
A single keyword-value pair.
Definition PvlKeyword.h:87
void clear()
Clears all values and units for this PvlKeyword object.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
Namespace for the standard library.