Isis 3 Programmer Reference
PvlGroup.cpp
Go to the documentation of this file.
1 
21 #include "PvlGroup.h"
22 #include "PvlKeyword.h"
23 #include "IException.h"
24 #include "PvlFormat.h"
25 
26 using namespace std;
27 namespace Isis {
29  PvlGroup::PvlGroup() : Isis::PvlContainer("Group", "") {}
30 
35  PvlGroup::PvlGroup(const QString &name) :
36  Isis::PvlContainer("Group", name) {
37  }
38 
39 
41  PvlGroup::PvlGroup(const PvlGroup &other) : PvlContainer(other) {}
42 
43 
51  std::istream &operator>>(std::istream &is, PvlGroup &result) {
52  PvlKeyword termination("EndGroup");
53 
54  PvlKeyword errorKeywords[] = {
55  PvlKeyword("Group"),
56  PvlKeyword("Object"),
57  PvlKeyword("EndObject")
58  };
59 
60  PvlKeyword readKeyword;
61 
62  istream::pos_type beforeKeywordPos = is.tellg();
63  is >> readKeyword;
64 
65  if(readKeyword != PvlKeyword("Group")) {
66  if(is.eof() && !is.bad()) {
67  is.clear();
68  }
69 
70  is.seekg(beforeKeywordPos, ios::beg);
71 
72  QString msg = "Expected PVL keyword named [Group], found keyword named [";
73  msg += readKeyword.name();
74  msg += "] when reading PVL";
76  }
77 
78  if(readKeyword.size() == 1) {
79  result.setName(readKeyword[0]);
80  }
81  else {
82  if(is.eof() && !is.bad()) {
83  is.clear();
84  }
85 
86  is.seekg(beforeKeywordPos, ios::beg);
87 
88  QString msg = "Expected a single value for group name, found [(";
89 
90  for(int i = 0; i < readKeyword.size(); i++) {
91  if(i != 0) msg += ", ";
92 
93  msg += readKeyword[i];
94  }
95 
96  msg += ")] when reading PVL";
98  }
99 
100 
101  for(int comment = 0; comment < readKeyword.comments(); comment++) {
102  result.addComment(readKeyword.comment(comment));
103  }
104 
105  readKeyword = PvlKeyword();
106  beforeKeywordPos = is.tellg();
107 
108  is >> readKeyword;
109  while(is.good() && readKeyword != termination) {
110  for(unsigned int errorKey = 0;
111  errorKey < sizeof(errorKeywords) / sizeof(PvlKeyword);
112  errorKey++) {
113 
114  if(readKeyword == errorKeywords[errorKey]) {
115  if(is.eof() && !is.bad()) {
116  is.clear();
117  }
118 
119  is.seekg(beforeKeywordPos, ios::beg);
120 
121  QString msg = "Unexpected [";
122  msg += readKeyword.name();
123  msg += "] in Group [";
124  msg += result.name();
125  msg += "] when reading PVL";
127  }
128  }
129 
130  result.addKeyword(readKeyword);
131  readKeyword = PvlKeyword();
132  beforeKeywordPos = is.tellg();
133 
134  is >> readKeyword;
135  }
136 
137  if(readKeyword != termination) {
138  if(is.eof() && !is.bad()) {
139  is.clear();
140  is.unget();
141  }
142 
143  is.seekg(beforeKeywordPos, ios::beg);
144 
145  QString msg = "Group [" + result.name();
146  msg += "] EndGroup not found before end of file when reading PVL";
148  }
149 
150  return is;
151  }
152 
158  ostream &operator<<(std::ostream &os, PvlGroup &group) {
159 
160  // Set up a Formatter
161  bool removeFormatter = false;
162  if(group.format() == NULL) {
163  group.setFormat(new PvlFormat());
164  removeFormatter = true;
165  }
166 
167  Isis::PvlGroup temp("DEFAULT");
168  if(group.hasFormatTemplate()) temp = *(Isis::PvlGroup *)group.formatTemplate();
169 
170  // Output comment from the template
171  if(temp.comments() > 0) {
172  for(int k = 0; k < temp.comments(); k++) {
173  for(int l = 0; l < group.indent(); l++) os << " ";
174  os << temp.comment(k) << group.format()->formatEOL();
175  }
176 // os << group.format()->formatEOL();
177  }
178 
179  // Output the group comments and name
180  os << group.nameKeyword() << group.format()->formatEOL();
181  group.setIndent(group.indent() + 2);
182 
183  // Output the keywords in this group
184  if(group.keywords() > 0) {
185  os << (Isis::PvlContainer &) group << group.format()->formatEOL();
186  }
187 
188  // Output the end of the group
189  group.setIndent(group.indent() - 2);
190  for(int i = 0; i < group.indent(); i++) os << " ";
191  os << group.format()->formatEnd("End_Group", group.nameKeyword());
192 
193  if(removeFormatter) {
194  delete group.format();
195  group.setFormat(NULL);
196  }
197 
198  return os;
199  }
200 
201 
203  const PvlGroup &PvlGroup::operator=(const PvlGroup &other) {
204  this->PvlContainer::operator=(other);
205 
206  return *this;
207  }
208 
221  {
222  // Group cannot be empty - needs to have a keyword
223  if(pPvlGrp.keywords() <= 0) {
224  QString sErrMsg = "Group \"" + pPvlGrp.name() + "\" has no Keywords\n";
225  throw IException(IException::User, sErrMsg, _FILEINFO_);
226  }
227 
228  validateAllKeywords((PvlContainer &)pPvlGrp);
229  }
230 
231 } // end namespace isis
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:100
void clear()
Clears all values and units for this PvlKeyword object.
Definition: PvlKeyword.cpp:307
Contains more than one keyword-value pair.
Definition: PvlContainer.h:63
void validateAllKeywords(PvlContainer &pPvlCont)
Validate All the Keywords in a Container comparing with the Template.
Formats a Pvl name value pair to Isis standards.
Definition: PvlFormat.h:124
const PvlGroup & operator=(const PvlGroup &other)
This is an assignment operator.
Definition: PvlGroup.cpp:203
Namespace for the standard library.
void addKeyword(const PvlKeyword &keyword, const InsertMode mode=Append)
Add a keyword to the container.
void setName(const QString &name)
Set the name of the container.
Definition: PvlContainer.h:70
void validateGroup(PvlGroup &pPvlGrp)
Validate a Group comparing with the Template Group.
Definition: PvlGroup.cpp:220
const PvlContainer & operator=(const PvlContainer &other)
This is an assignment operator.
QString name() const
Returns the container name.
Definition: PvlContainer.h:77
PvlGroup()
Creates a blank PvlGroup object.
Definition: PvlGroup.cpp:29
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
std::istream & operator>>(std::istream &is, CSVReader &csv)
Input read operator for input stream sources.
Definition: CSVReader.cpp:463
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
A single keyword-value pair.
Definition: PvlKeyword.h:98
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:134
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
QDebug operator<<(QDebug debug, const Hillshade &hillshade)
Print this class out to a QDebug object.
Definition: Hillshade.cpp:308