USGS

Isis 3.0 Object Programmers' Reference

Home

PvlFlatMap.cpp
Go to the documentation of this file.
1 
24 #include "PvlFlatMap.h"
25 
26 #include <QDebug>
27 
28 // other ISIS
29 #include "IException.h"
30 #include "FileName.h"
31 #include "Pvl.h"
32 #include "PvlContainer.h"
33 #include "PvlFlatMap.h"
34 #include "PvlGroup.h"
35 #include "PvlKeyword.h"
36 #include "PvlObject.h"
37 #include "TextFile.h"
38 
39 using namespace std;
40 
41 namespace Isis {
42 
46  PvlConstraints::PvlConstraints() {
47  }
48 
49 
55  PvlConstraints::PvlConstraints(const QString &keyListFile) {
56  addKeyToList(FileName(keyListFile));
57  return;
58  }
59 
60 
71  PvlConstraints PvlConstraints::withExcludes(const QStringList &excludes) {
72  PvlConstraints constraints;
73  constraints.addExclude(excludes);
74  return (constraints);
75  }
76 
77 
87  PvlConstraints PvlConstraints::withIncludes(const QStringList &includes) {
88  PvlConstraints constraints;
89  constraints.addInclude(includes);
90  return (constraints);
91  }
92 
93 
97  PvlConstraints::~PvlConstraints() {
98  }
99 
100 
108  int PvlConstraints::excludeSize() const {
109  return (m_excludes.size());
110  }
111 
112 
120  int PvlConstraints::includeSize() const {
121  return (m_includes.size());
122  }
123 
124 
134  int PvlConstraints::keyListSize() const {
135  return (m_keylist.size());
136  }
137 
138 
151  void PvlConstraints::addExclude(const QString &name) {
152  m_excludes.append(name);
153  }
154 
155 
167  void PvlConstraints::addInclude(const QString &name) {
168  m_includes.append(name);
169  }
170 
171 
180  void PvlConstraints::addKeyToList(const QString &name) {
181  m_keylist.append(name);
182  }
183 
184 
190  void PvlConstraints::addExclude(const QStringList &other) {
191  m_excludes += other;
192  }
193 
194 
200  void PvlConstraints::addInclude(const QStringList &other) {
201  m_includes += other;
202  }
203 
204 
210  void PvlConstraints::addKeyToList(const QStringList &other) {
211  m_keylist += other;
212  }
213 
214 
220  void PvlConstraints::addKeyToList(const FileName &fileName) {
221  readKeyListFile(fileName);
222  return;
223  }
224 
225 
236  bool PvlConstraints::isExcluded(const QString &name) const {
237  return (m_excludes.contains(name, Qt::CaseInsensitive));
238  }
239 
240 
251  bool PvlConstraints::isIncluded(const QString &name) const {
252  return (m_includes.contains(name, Qt::CaseInsensitive));
253  }
254 
255 
265  bool PvlConstraints::isKeyInList(const QString &name) const {
266  return (m_keylist.contains(name, Qt::CaseInsensitive));
267  }
268 
269 
279  const QStringList &PvlConstraints::excludes() const {
280  return (m_excludes);
281  }
282 
283 
294  const QStringList &PvlConstraints::includes() const {
295  return (m_includes);
296  }
297 
298 
308  const QStringList &PvlConstraints::keyList() const {
309  return (m_keylist);
310  }
311 
312 
321  void PvlConstraints::readKeyListFile(const FileName &keyListFile) {
322  // Pvl pvl(keyListFile.expanded());
323  //
324  // if (pvl.hasObject("KeyList")) {
325  // PvlContainer keylist = pvl.findObject("KeyList");
326  // PvlContainer::ConstPvlKeywordIterator k = keylist.begin();
327  // while (k != keylist.end()) {
328  // addKeyToList(k->name());
329  // ++k;
330  // }
331  // }
332  // else {
333  // PvlContainer::ConstPvlKeywordIterator k = pvl.begin();
334  // while (k != pvl.end()) {
335  // addKeyToList(k->name());
336  // ++k;
337  // }
338  // }
339  // return;
340 
341 // Pvl pvl(keyListFile.expanded());
342 // qDebug() << "INPUT FILE = ";
343 // qDebug() << keyListFile.expanded();
344 // for (int k = 0; k < pvl.keywords(); k++) {
345 // cout << "HI!" << endl;
346 // addKeyToList(pvl[k].name());
347 // }
348  TextFile keyList(keyListFile.expanded());
349  QString keywordName = "";
350  while (keyList.GetLine(keywordName)) {
351  addKeyToList(keywordName);
352  }
353 
354  }
355 
356 
360  PvlFlatMap::PvlFlatMap() : QMap<QString, PvlKeyword> () {
361  }
362 
363 
370  QMap<QString, PvlKeyword> () {
371  merge(other);
372  }
373 
374 
391  const PvlFlatMap &pmap2) :
392  QMap<QString, PvlKeyword> () {
393  merge(pmap1);
394  merge(pmap2);
395  }
396 
397 
412  const PvlConstraints &constraints) :
413  QMap<QString, PvlKeyword> () {
414  loadObject(pvl, constraints);
415  }
416 
417 
433  const PvlConstraints &constraints) :
434  QMap<QString, PvlKeyword> () {
435  loadKeywords(pvl, constraints);
436  }
437 
438 
443  }
444 
445 
453  bool PvlFlatMap::exists(const QString &key) const {
454  return (contains(key.toLower()));
455  }
456 
457 
467  int PvlFlatMap::count(const QString &key) const {
468  if ( !exists(key) ) {
469  return (0);
470  }
471  return (keyword(key).size());
472  }
473 
474 
487  bool PvlFlatMap::isNull(const QString &key,
488  const int index) const {
489  if (exists(key)) {
490  return (keyword(key).isNull(index));
491  }
492 
493  // Always returns true if the keyword doesn't exist
494  return (true);
495  }
496 
497 
505  void PvlFlatMap::add(const QString &key,
506  const QString &value) {
507  add(PvlKeyword(key, value));
508  }
509 
510 
517  void PvlFlatMap::add(const PvlKeyword &key) {
518  insert(key.name().toLower(), key);
519  }
520 
521 
531  void PvlFlatMap::append(const QString &key,
532  const QString &value) {
533  append(PvlKeyword(key, value));
534  return;
535  }
536 
537 
546  void PvlFlatMap::append(const PvlKeyword &key) {
547  if ( exists(key.name()) ) {
548  PvlFlatMapIterator kw = find(key.name().toLower());
549  // add all values to the map
550  for (int i = 0; i < key.size(); i++) {
551  kw.value().addValue(key[i]);
552  }
553  }
554  else {
555  insert(key.name().toLower(), PvlKeyword(key.name(), key[0]));
556  // if there are more than one values in this keyword, insert the first
557  // into the map and recursively call append to add the rest.
558  for (int i = 1; i < key.size(); i++) {
559  append(key.name(), key[i]);
560  }
561  }
562  return;
563  }
564 
565 
574  bool PvlFlatMap::erase(const QString &key) {
575  return (bool (remove(key.toLower())));
576  }
577 
578 
593  QString PvlFlatMap::get(const QString &key,
594  const int &index) const {
595  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
596  if (end() == k) {
597  QString mess = "Keyword " + key + " does not exist!";
599  }
600  if (index >= k.value().size()) {
601  QString mess = "Index " + toString(index) + " does not exist for keyword " + key + "!";
603  }
604 
605  return (k.value()[index]);
606  }
607 
608 
621  QString PvlFlatMap::get(const QString &key,
622  const QString &defValue,
623  const int &index) const {
624  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
625  if (end() == k || index >= k.value().size()) {
626  return (defValue);
627  }
628  else {
629  return (k.value()[index]);
630  }
631  }
632 
633 
644  QString PvlFlatMap::operator()(const QString &name) const {
645  return (get(name));
646  }
647 
648 
658  QStringList PvlFlatMap::allValues(const QString &key) const {
659  QStringList values;
660  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
661  if (end() != k) {
662  values = keywordValues(k.value());
663  }
664  return (values);
665  }
666 
667 
677  PvlKeyword PvlFlatMap::keyword(const QString &key) const {
678  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
679  if (end() == k) {
680  QString mess = "Keyword " + key + " does not exist!";
682  }
683  return (k.value());
684  }
685 
686 
697  int PvlFlatMap::merge(const PvlFlatMap &other) {
698  int n = 0;
699  QMap<QString, PvlKeyword>::const_iterator keys = other.begin();
700  while ( keys != other.end() ) {
701  add(keys.value());
702  n++;
703  ++keys;
704  }
705 
706  return (n);
707  }
708 
709 
720  QStringList values;
721  for (int i = 0 ; i < keyword.size() ; i++) {
722  values << keyword[i];
723  }
724  return (values);
725  }
726 
727 
747  int PvlFlatMap::loadObject(const PvlObject &object,
748  const PvlConstraints &constraints) {
749  int total = 0;
750 
751  // Check constraints if specified
752  int nconsts = constraints.excludeSize() + constraints.includeSize();
753  if ( nconsts > 0 ) {
754  bool isExcluded = constraints.isExcluded(object.name());
755  bool isIncluded = constraints.isIncluded(object.name());
756  bool hasBoth = (constraints.excludeSize() > 0) &&
757  (constraints.includeSize() > 0);
758 
759  // Check constraints
760  // include object and its groups and keywords when both Includes and Excludes are specified
761  // don't include an object if it isn't in Includes keyword
762  if ( hasBoth ) {
763  if ( !isIncluded ) {
764  return (total);
765  } // At the object level
766  }
767  else if ( isExcluded ) {
768  return (total);
769  }
770  else if ( constraints.includeSize() > 0 ) {
771  if ( !isIncluded ) {
772  return (total);
773  }
774  }
775  }
776 
777  // First load keys in the object, then existing groups followed by additional
778  // objects
779  total += loadKeywords(object, constraints);
780  total += loadGroups(object, constraints);
781 
782  PvlObject::ConstPvlObjectIterator objs;
783  for (objs = object.beginObject() ; objs != object.endObject() ; ++objs) {
784  total += loadObject(*objs, constraints);
785  }
786  return (total);
787  }
788 
789 
806  const PvlConstraints &constraints) {
807  int total = 0;
808 
809  // Load the PvlGroups contained in the PvlObject
810  PvlObject::ConstPvlGroupIterator group;
811  for (group = object.beginGroup() ; group != object.endGroup() ; group++) {
812  total += loadGroup(*group, constraints);
813  }
814  return total;
815  }
816 
817 
835  int PvlFlatMap::loadGroup(const PvlGroup &group,
836  const PvlConstraints &constraints) {
837  int total = 0;
838  // Check constraints if specified
839  int nconsts = constraints.excludeSize() + constraints.includeSize();
840  if ( nconsts > 0 ) {
841  bool isExcluded = constraints.isExcluded(group.name());
842  bool isIncluded = constraints.isIncluded(group.name());
843  bool hasBoth = (constraints.excludeSize() > 0) &&
844  (constraints.includeSize() > 0);
845  // Check constraints
846  // When both Includes and Excludes provided, Excludes applies to groups
847  // If an object is included but one of its group is excluded, exclude the group
848  if ( hasBoth ) {
849  if ( isExcluded ) {
850  return (total);
851  } // at group level
852  }
853  else if ( isExcluded ) {
854  return (total);
855  }
856  else if ( constraints.includeSize() > 0 ) {
857  if ( !isIncluded ) {
858  return (total);
859  }
860  }
861  }
862 
863  // load the PvlGroup's PvlKeywords
864  total += loadKeywords(group, constraints);
865  return (total);
866  }
867 
868 
891  const PvlConstraints &constraints) {
893  int n = 0;
894 
895  // if there are any keyword constraints, only load those PvlKeywords
896  if ( constraints.keyListSize() > 0 ) {
897  for (key = pvl.begin() ; key != pvl.end() ; ++key) {
898  if ( constraints.isKeyInList(key->name()) ) {
899  add(*key);
900  n++;
901  }
902  }
903  }
904  // otherwise load all PvlKeywords in the PvlContainer provided
905  else {
906  for (key = pvl.begin() ; key != pvl.end() ; ++key) {
907  add(*key);
908  n++;
909  }
910  }
911  return (n);
912  }
913 
914 } // namespace Isis
915