Isis 3 Programmer Reference
PvlFlatMap.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "PvlFlatMap.h"
8 
9 #include <QDebug>
10 
11 // other ISIS
12 #include "IException.h"
13 #include "FileName.h"
14 #include "Pvl.h"
15 #include "PvlContainer.h"
16 #include "PvlFlatMap.h"
17 #include "PvlGroup.h"
18 #include "PvlKeyword.h"
19 #include "PvlObject.h"
20 #include "TextFile.h"
21 
22 using namespace std;
23 
24 namespace Isis {
25 
29  PvlConstraints::PvlConstraints() {
30  }
31 
32 
38  PvlConstraints::PvlConstraints(const QString &keyListFile) {
39  addKeyToList(FileName(keyListFile));
40  return;
41  }
42 
43 
54  PvlConstraints PvlConstraints::withExcludes(const QStringList &excludes) {
55  PvlConstraints constraints;
56  constraints.addExclude(excludes);
57  return (constraints);
58  }
59 
60 
70  PvlConstraints PvlConstraints::withIncludes(const QStringList &includes) {
71  PvlConstraints constraints;
72  constraints.addInclude(includes);
73  return (constraints);
74  }
75 
76 
80  PvlConstraints::~PvlConstraints() {
81  }
82 
83 
91  int PvlConstraints::excludeSize() const {
92  return (m_excludes.size());
93  }
94 
95 
103  int PvlConstraints::includeSize() const {
104  return (m_includes.size());
105  }
106 
107 
117  int PvlConstraints::keyListSize() const {
118  return (m_keylist.size());
119  }
120 
121 
134  void PvlConstraints::addExclude(const QString &name) {
135  m_excludes.append(name);
136  }
137 
138 
150  void PvlConstraints::addInclude(const QString &name) {
151  m_includes.append(name);
152  }
153 
154 
163  void PvlConstraints::addKeyToList(const QString &name) {
164  m_keylist.append(name);
165  }
166 
167 
173  void PvlConstraints::addExclude(const QStringList &other) {
174  m_excludes += other;
175  }
176 
177 
183  void PvlConstraints::addInclude(const QStringList &other) {
184  m_includes += other;
185  }
186 
187 
193  void PvlConstraints::addKeyToList(const QStringList &other) {
194  m_keylist += other;
195  }
196 
197 
203  void PvlConstraints::addKeyToList(const FileName &fileName) {
204  readKeyListFile(fileName);
205  return;
206  }
207 
208 
219  bool PvlConstraints::isExcluded(const QString &name) const {
220  return (m_excludes.contains(name, Qt::CaseInsensitive));
221  }
222 
223 
234  bool PvlConstraints::isIncluded(const QString &name) const {
235  return (m_includes.contains(name, Qt::CaseInsensitive));
236  }
237 
238 
248  bool PvlConstraints::isKeyInList(const QString &name) const {
249  return (m_keylist.contains(name, Qt::CaseInsensitive));
250  }
251 
252 
262  const QStringList &PvlConstraints::excludes() const {
263  return (m_excludes);
264  }
265 
266 
277  const QStringList &PvlConstraints::includes() const {
278  return (m_includes);
279  }
280 
281 
291  const QStringList &PvlConstraints::keyList() const {
292  return (m_keylist);
293  }
294 
295 
304  void PvlConstraints::readKeyListFile(const FileName &keyListFile) {
305  // Pvl pvl(keyListFile.expanded());
306  //
307  // if (pvl.hasObject("KeyList")) {
308  // PvlContainer keylist = pvl.findObject("KeyList");
309  // PvlContainer::ConstPvlKeywordIterator k = keylist.begin();
310  // while (k != keylist.end()) {
311  // addKeyToList(k->name());
312  // ++k;
313  // }
314  // }
315  // else {
316  // PvlContainer::ConstPvlKeywordIterator k = pvl.begin();
317  // while (k != pvl.end()) {
318  // addKeyToList(k->name());
319  // ++k;
320  // }
321  // }
322  // return;
323 
324 // Pvl pvl(keyListFile.expanded());
325 // qDebug() << "INPUT FILE = ";
326 // qDebug() << keyListFile.expanded();
327 // for (int k = 0; k < pvl.keywords(); k++) {
328 // cout << "HI!" << endl;
329 // addKeyToList(pvl[k].name());
330 // }
331  TextFile keyList(keyListFile.expanded());
332  QString keywordName = "";
333  while (keyList.GetLine(keywordName)) {
334  addKeyToList(keywordName);
335  }
336 
337  }
338 
339 
343  PvlFlatMap::PvlFlatMap() : QMap<QString, PvlKeyword> () {
344  }
345 
346 
353  QMap<QString, PvlKeyword> () {
354  merge(other);
355  }
356 
357 
374  const PvlFlatMap &pmap2) :
375  QMap<QString, PvlKeyword> () {
376  merge(pmap1);
377  merge(pmap2);
378  }
379 
380 
395  const PvlConstraints &constraints) :
396  QMap<QString, PvlKeyword> () {
397  loadObject(pvl, constraints);
398  }
399 
400 
416  const PvlConstraints &constraints) :
417  QMap<QString, PvlKeyword> () {
418  loadKeywords(pvl, constraints);
419  }
420 
421 
426  }
427 
428 
436  bool PvlFlatMap::exists(const QString &key) const {
437  return (contains(key.toLower()));
438  }
439 
440 
450  int PvlFlatMap::count(const QString &key) const {
451  if ( !exists(key) ) {
452  return (0);
453  }
454  return (keyword(key).size());
455  }
456 
457 
470  bool PvlFlatMap::isNull(const QString &key,
471  const int index) const {
472  if (exists(key)) {
473  return (keyword(key).isNull(index));
474  }
475 
476  // Always returns true if the keyword doesn't exist
477  return (true);
478  }
479 
480 
488  void PvlFlatMap::add(const QString &key,
489  const QString &value) {
490  add(PvlKeyword(key, value));
491  }
492 
493 
500  void PvlFlatMap::add(const PvlKeyword &key) {
501  insert(key.name().toLower(), key);
502  }
503 
504 
514  void PvlFlatMap::append(const QString &key,
515  const QString &value) {
516  append(PvlKeyword(key, value));
517  return;
518  }
519 
520 
529  void PvlFlatMap::append(const PvlKeyword &key) {
530  if ( exists(key.name()) ) {
531  PvlFlatMapIterator kw = find(key.name().toLower());
532  // add all values to the map
533  for (int i = 0; i < key.size(); i++) {
534  kw.value().addValue(key[i]);
535  }
536  }
537  else {
538  insert(key.name().toLower(), PvlKeyword(key.name(), key[0]));
539  // if there are more than one values in this keyword, insert the first
540  // into the map and recursively call append to add the rest.
541  for (int i = 1; i < key.size(); i++) {
542  append(key.name(), key[i]);
543  }
544  }
545  return;
546  }
547 
548 
557  bool PvlFlatMap::erase(const QString &key) {
558  return (bool (remove(key.toLower())));
559  }
560 
561 
576  QString PvlFlatMap::get(const QString &key,
577  const int &index) const {
578  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
579  if (end() == k) {
580  QString mess = "Keyword " + key + " does not exist!";
581  throw IException(IException::Programmer, mess, _FILEINFO_);
582  }
583  if (index >= k.value().size()) {
584  QString mess = "Index " + toString(index) + " does not exist for keyword " + key + "!";
585  throw IException(IException::Programmer, mess, _FILEINFO_);
586  }
587 
588  return (k.value()[index]);
589  }
590 
591 
604  QString PvlFlatMap::get(const QString &key,
605  const QString &defValue,
606  const int &index) const {
607  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
608  if (end() == k || index >= k.value().size()) {
609  return (defValue);
610  }
611  else {
612  return (k.value()[index]);
613  }
614  }
615 
616 
627  QString PvlFlatMap::operator()(const QString &name) const {
628  return (get(name));
629  }
630 
631 
641  QStringList PvlFlatMap::allValues(const QString &key) const {
642  QStringList values;
643  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
644  if (end() != k) {
645  values = keywordValues(k.value());
646  }
647  return (values);
648  }
649 
650 
660  PvlKeyword PvlFlatMap::keyword(const QString &key) const {
661  QMap<QString, PvlKeyword>::const_iterator k = find(key.toLower());
662  if (end() == k) {
663  QString mess = "Keyword " + key + " does not exist!";
664  throw IException(IException::Programmer, mess, _FILEINFO_);
665  }
666  return (k.value());
667  }
668 
669 
680  int PvlFlatMap::merge(const PvlFlatMap &other) {
681  int n = 0;
682  QMap<QString, PvlKeyword>::const_iterator keys = other.begin();
683  while ( keys != other.end() ) {
684  add(keys.value());
685  n++;
686  ++keys;
687  }
688 
689  return (n);
690  }
691 
692 
703  QStringList values;
704  for (int i = 0 ; i < keyword.size() ; i++) {
705  values << keyword[i];
706  }
707  return (values);
708  }
709 
710 
730  int PvlFlatMap::loadObject(const PvlObject &object,
731  const PvlConstraints &constraints) {
732  int total = 0;
733 
734  // Check constraints if specified
735  int nconsts = constraints.excludeSize() + constraints.includeSize();
736  if ( nconsts > 0 ) {
737  bool isExcluded = constraints.isExcluded(object.name());
738  bool isIncluded = constraints.isIncluded(object.name());
739  bool hasBoth = (constraints.excludeSize() > 0) &&
740  (constraints.includeSize() > 0);
741 
742  // Check constraints
743  // include object and its groups and keywords when both Includes and Excludes are specified
744  // don't include an object if it isn't in Includes keyword
745  if ( hasBoth ) {
746  if ( !isIncluded ) {
747  return (total);
748  } // At the object level
749  }
750  else if ( isExcluded ) {
751  return (total);
752  }
753  else if ( constraints.includeSize() > 0 ) {
754  if ( !isIncluded ) {
755  return (total);
756  }
757  }
758  }
759 
760  // First load keys in the object, then existing groups followed by additional
761  // objects
762  total += loadKeywords(object, constraints);
763  total += loadGroups(object, constraints);
764 
765  PvlObject::ConstPvlObjectIterator objs;
766  for (objs = object.beginObject() ; objs != object.endObject() ; ++objs) {
767  total += loadObject(*objs, constraints);
768  }
769  return (total);
770  }
771 
772 
789  const PvlConstraints &constraints) {
790  int total = 0;
791 
792  // Load the PvlGroups contained in the PvlObject
793  PvlObject::ConstPvlGroupIterator group;
794  for (group = object.beginGroup() ; group != object.endGroup() ; group++) {
795  total += loadGroup(*group, constraints);
796  }
797  return total;
798  }
799 
800 
818  int PvlFlatMap::loadGroup(const PvlGroup &group,
819  const PvlConstraints &constraints) {
820  int total = 0;
821  // Check constraints if specified
822  int nconsts = constraints.excludeSize() + constraints.includeSize();
823  if ( nconsts > 0 ) {
824  bool isExcluded = constraints.isExcluded(group.name());
825  bool isIncluded = constraints.isIncluded(group.name());
826  bool hasBoth = (constraints.excludeSize() > 0) &&
827  (constraints.includeSize() > 0);
828  // Check constraints
829  // When both Includes and Excludes provided, Excludes applies to groups
830  // If an object is included but one of its group is excluded, exclude the group
831  if ( hasBoth ) {
832  if ( isExcluded ) {
833  return (total);
834  } // at group level
835  }
836  else if ( isExcluded ) {
837  return (total);
838  }
839  else if ( constraints.includeSize() > 0 ) {
840  if ( !isIncluded ) {
841  return (total);
842  }
843  }
844  }
845 
846  // load the PvlGroup's PvlKeywords
847  total += loadKeywords(group, constraints);
848  return (total);
849  }
850 
851 
874  const PvlConstraints &constraints) {
876  int n = 0;
877 
878  // if there are any keyword constraints, only load those PvlKeywords
879  if ( constraints.keyListSize() > 0 ) {
880  for (key = pvl.begin() ; key != pvl.end() ; ++key) {
881  if ( constraints.isKeyInList(key->name()) ) {
882  add(*key);
883  n++;
884  }
885  }
886  }
887  // otherwise load all PvlKeywords in the PvlContainer provided
888  else {
889  for (key = pvl.begin() ; key != pvl.end() ; ++key) {
890  add(*key);
891  n++;
892  }
893  }
894  return (n);
895  }
896 
897 } // namespace Isis
898 
Isis::PvlFlatMap::count
int count(const QString &key) const
Returns the number of values associated with a given keyword.
Definition: PvlFlatMap.cpp:450
Isis::PvlConstraints::isIncluded
bool isIncluded(const QString &name) const
Determines if a PvlObject or PvlGroup is included.
Definition: PvlFlatMap.cpp:234
Isis::PvlFlatMap::add
void add(const QString &key, const QString &value)
Adds PvlKeyword with the given name and value to the PvlFlatMap.
Definition: PvlFlatMap.cpp:488
Isis::PvlKeyword::name
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:98
Isis::PvlConstraints::isKeyInList
bool isKeyInList(const QString &name) const
Determines if a PvlKeyword is included.
Definition: PvlFlatMap.cpp:248
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::PvlContainer::ConstPvlKeywordIterator
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
Definition: PvlContainer.h:160
Isis::PvlFlatMap::operator()
QString operator()(const QString &name) const
Gets the first value of a keyword in the PvlFlatMap.
Definition: PvlFlatMap.cpp:627
Isis::PvlFlatMap::isNull
bool isNull(const QString &key, const int index=0) const
Determines if the value of a keyword is Null.
Definition: PvlFlatMap.cpp:470
Isis::PvlFlatMap
Provides a flat map of PvlKeywords.
Definition: PvlFlatMap.h:218
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::PvlFlatMap::keyword
PvlKeyword keyword(const QString &key) const
Finds a keyword in the PvlFlatMap.
Definition: PvlFlatMap.cpp:660
Isis::PvlConstraints::keyListSize
int keyListSize() const
Returns the number of PvlKeywords to include.
Definition: PvlFlatMap.cpp:117
Isis::PvlFlatMap::exists
bool exists(const QString &key) const
Determines whether a given keyword exists in the PvlFlatMap.
Definition: PvlFlatMap.cpp:436
Isis::PvlFlatMap::loadKeywords
int loadKeywords(const PvlContainer &pvl, const PvlConstraints &constraints)
Loads PvlKeywords within a PvlContainer into the PvlFlatMap.
Definition: PvlFlatMap.cpp:873
Isis::TextFile
Provides access to sequential ASCII stream I/O.
Definition: TextFile.h:38
Isis::TextFile::GetLine
bool GetLine(QString &line, const bool skipComments=true)
Gets next line from file.
Definition: TextFile.cpp:411
Isis::PvlConstraints::addExclude
void addExclude(const QString &name)
Adds a PvlObject/PvlGroup exclusion constraint.
Definition: PvlFlatMap.cpp:134
Isis::PvlConstraints::excludeSize
int excludeSize() const
Returns the number of PvlObjects and PvlGroups to exclude.
Definition: PvlFlatMap.cpp:91
QStringList
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::PvlFlatMap::append
void append(const PvlKeyword &key)
Appends the given PvlKeyword's values to the PvlFlatMap.
Definition: PvlFlatMap.cpp:529
Isis::PvlFlatMap::allValues
QStringList allValues(const QString &key) const
Gets all the values associated with a keyword in the PvlFlatMap.
Definition: PvlFlatMap.cpp:641
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::PvlFlatMap::merge
int merge(const PvlFlatMap &other)
Adds the keywords from another PvlFlatMap.
Definition: PvlFlatMap.cpp:680
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::PvlConstraints::includeSize
int includeSize() const
Returns the number of PvlObjects and PvlGroups to include.
Definition: PvlFlatMap.cpp:103
Isis::PvlFlatMap::loadGroups
int loadGroups(const PvlObject &object, const PvlConstraints &constraints)
Loads PvlGroups into the PvlFlatMap.
Definition: PvlFlatMap.cpp:788
Isis::PvlFlatMap::get
QString get(const QString &key, const int &index=0) const
Gets the value of a keyword in the PvlFlatMap.
Definition: PvlFlatMap.cpp:576
Isis::PvlConstraints::addInclude
void addInclude(const QString &name)
Adds a PvlObject/PvlGroup inclusion constraint.
Definition: PvlFlatMap.cpp:150
Isis::PvlFlatMap::~PvlFlatMap
virtual ~PvlFlatMap()
Destructor.
Definition: PvlFlatMap.cpp:425
Isis::PvlContainer::name
QString name() const
Returns the container name.
Definition: PvlContainer.h:63
Isis::PvlFlatMap::PvlFlatMapIterator
QMap< QString, PvlKeyword >::iterator PvlFlatMapIterator
An iterator for the underlying QMap that PvlFlatMap is built on.
Definition: PvlFlatMap.h:224
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::PvlConstraints::isExcluded
bool isExcluded(const QString &name) const
Determines if a PvlObject or PvlGroup is excluded.
Definition: PvlFlatMap.cpp:219
Isis::PvlFlatMap::erase
bool erase(const QString &key)
Erases a keyword from the PvlFlatMap.
Definition: PvlFlatMap.cpp:557
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
std
Namespace for the standard library.
Isis::PvlFlatMap::keywordValues
static QStringList keywordValues(const PvlKeyword &keyword)
Gets all of a PvlKeyword's values.
Definition: PvlFlatMap.cpp:702
Isis::PvlKeyword::size
int size() const
Returns the number of values stored in this keyword.
Definition: PvlKeyword.h:125
QMap
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:22
Isis::PvlContainer::end
PvlKeywordIterator end()
Return the ending iterator.
Definition: PvlContainer.h:194
Isis::PvlFlatMap::loadGroup
int loadGroup(const PvlGroup &group, const PvlConstraints &constraints)
Loads a PvlGroup into the PvlFlatMap.
Definition: PvlFlatMap.cpp:818
Isis::PvlContainer
Contains more than one keyword-value pair.
Definition: PvlContainer.h:49
Isis::PvlFlatMap::PvlFlatMap
PvlFlatMap()
Default constructor.
Definition: PvlFlatMap.cpp:343
Isis::PvlFlatMap::loadObject
int loadObject(const PvlObject &object, const PvlConstraints &constraints)
Loads PvlObjects into the PvlFlatMap.
Definition: PvlFlatMap.cpp:730
Isis::PvlContainer::begin
PvlKeywordIterator begin()
Return the beginning iterator.
Definition: PvlContainer.h:178
Isis::PvlConstraints
This class can be used to define import/export behavior of Pvl structures when used in the PvlFlatMap...
Definition: PvlFlatMap.h:150
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16