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
22using namespace std;
23
24namespace Isis {
25
31
32
38 PvlConstraints::PvlConstraints(const QString &keyListFile) {
39 addKeyToList(FileName(keyListFile));
40 return;
41 }
42
43
55 PvlConstraints constraints;
56 constraints.addExclude(excludes);
57 return (constraints);
58 }
59
60
71 PvlConstraints constraints;
72 constraints.addInclude(includes);
73 return (constraints);
74 }
75
76
82
83
92 return (m_excludes.size());
93 }
94
95
104 return (m_includes.size());
105 }
106
107
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
174 m_excludes += other;
175 }
176
177
184 m_includes += other;
185 }
186
187
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
263 return (m_excludes);
264 }
265
266
278 return (m_includes);
279 }
280
281
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
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
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
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;
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
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
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
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
This class can be used to define import/export behavior of Pvl structures when used in the PvlFlatMap...
Definition PvlFlatMap.h:150
const QStringList & includes() const
Returns the PvlObject/PvlGroup inclusion constraints.
bool isKeyInList(const QString &name) const
Determines if a PvlKeyword is included.
void addInclude(const QString &name)
Adds a PvlObject/PvlGroup inclusion constraint.
void readKeyListFile(const FileName &fname)
Reads PvlKeyword inclusion constraints from a file.
const QStringList & excludes() const
Returns the PvlObject/PvlGroup exclusion constraints.
const QStringList & keyList() const
Returns the PvlKeyword inclusion constraints.
QStringList m_excludes
The names of objects and groups excluded (Excludes keyword)
Definition PvlFlatMap.h:184
bool isIncluded(const QString &name) const
Determines if a PvlObject or PvlGroup is included.
void addExclude(const QString &name)
Adds a PvlObject/PvlGroup exclusion constraint.
int excludeSize() const
Returns the number of PvlObjects and PvlGroups to exclude.
QStringList m_includes
The names of objects and groups included (Includes keyword)
Definition PvlFlatMap.h:185
int keyListSize() const
Returns the number of PvlKeywords to include.
int includeSize() const
Returns the number of PvlObjects and PvlGroups to include.
static PvlConstraints withExcludes(const QStringList &excludes)
Static method to construct a PvlConstraints object from a list of names for the PvlObjects and PvlGro...
static PvlConstraints withIncludes(const QStringList &includes)
Static method to construct a PvlConstraints object from a list of names for the PvlObjects and PvlGro...
void addKeyToList(const QString &name)
Adds a PvlKeyword inclusion constraint.
virtual ~PvlConstraints()
Destructor.
QStringList m_keylist
The names of keywords to exclusively include (KeyListFile)
Definition PvlFlatMap.h:186
PvlConstraints()
Default constructor for an empty PvlConstraints object.
bool isExcluded(const QString &name) const
Determines if a PvlObject or PvlGroup is excluded.
Contains more than one keyword-value pair.
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
QString name() const
Returns the container name.
Provides a flat map of PvlKeywords.
Definition PvlFlatMap.h:218
void append(const PvlKeyword &key)
Appends the given PvlKeyword's values to the PvlFlatMap.
PvlKeyword keyword(const QString &key) const
Finds a keyword in the PvlFlatMap.
void add(const QString &key, const QString &value)
Adds PvlKeyword with the given name and value to the PvlFlatMap.
QString operator()(const QString &name) const
Gets the first value of a keyword in the PvlFlatMap.
QStringList allValues(const QString &key) const
Gets all the values associated with a keyword in the PvlFlatMap.
int count(const QString &key) const
Returns the number of values associated with a given keyword.
QString get(const QString &key, const int &index=0) const
Gets the value of a keyword in the PvlFlatMap.
int merge(const PvlFlatMap &other)
Adds the keywords from another PvlFlatMap.
bool erase(const QString &key)
Erases a keyword from the PvlFlatMap.
bool exists(const QString &key) const
Determines whether a given keyword exists in the PvlFlatMap.
virtual ~PvlFlatMap()
Destructor.
int loadGroups(const PvlObject &object, const PvlConstraints &constraints)
Loads PvlGroups into the PvlFlatMap.
PvlFlatMap()
Default constructor.
bool isNull(const QString &key, const int index=0) const
Determines if the value of a keyword is Null.
static QStringList keywordValues(const PvlKeyword &keyword)
Gets all of a PvlKeyword's values.
int loadKeywords(const PvlContainer &pvl, const PvlConstraints &constraints)
Loads PvlKeywords within a PvlContainer into the PvlFlatMap.
QMap< QString, PvlKeyword >::iterator PvlFlatMapIterator
An iterator for the underlying QMap that PvlFlatMap is built on.
Definition PvlFlatMap.h:224
int loadGroup(const PvlGroup &group, const PvlConstraints &constraints)
Loads a PvlGroup into the PvlFlatMap.
int loadObject(const PvlObject &object, const PvlConstraints &constraints)
Loads PvlObjects into the PvlFlatMap.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
A single keyword-value pair.
Definition PvlKeyword.h:87
QString name() const
Returns the keyword name.
Definition PvlKeyword.h:103
int size() const
Returns the number of values stored in this keyword.
Definition PvlKeyword.h:133
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
Provides access to sequential ASCII stream I/O.
Definition TextFile.h:38
This is free and unencumbered software released into the public domain.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
Namespace for the standard library.