Isis Developer Reference
PvlKeyword.h
Go to the documentation of this file.
1#ifndef PvlKeyword_h
2#define PvlKeyword_h
8/* SPDX-License-Identifier: CC0-1.0 */
9#include <vector>
10#include <map>
11#include <iostream>
12
13#include <QString>
14#include <QVariant>
15#include <QVarLengthArray>
16
17#include "Constants.h"
18#include "IString.h"
19
20#include <nlohmann/json.hpp>
21
22namespace Isis {
23 class PvlSequence;
24 class PvlFormat;
25
87 class PvlKeyword {
88 public:
89 PvlKeyword();
90 PvlKeyword(QString name);
91 PvlKeyword(QString name, QString value,
92 QString unit = "");
93 PvlKeyword(const PvlKeyword &other);
95
96 void setName(QString name);
97
103 QString name() const {
104 if(m_name)
105 return m_name;
106 else
107 return "";
108 };
115 bool isNamed(QString name) const {
116 return stringEqual(name, this->name());
117 };
118
119 void setValue(QString value, QString unit = "");
120 void setJsonValue(nlohmann::json jsonobj, QString unit = "");
121
122 void setUnits(QString units);
123 void setUnits(QString value, QString units);
124
125 PvlKeyword &operator=(QString value);
126
127 void addValue(QString value, QString unit = "");
128 void addJsonValue(nlohmann::json jsonobj, QString unit = "");
129
130 PvlKeyword &operator+=(QString value);
131
133 int size() const {
134 return m_values.size();
135 };
136 bool isNull(const int index = 0) const;
137 void clear();
138
139 friend std::istream &operator>>(std::istream &is, PvlKeyword &result);
140 friend std::ostream &operator<<(std::ostream &os,
141 const PvlKeyword &keyword);
142
144 operator double() const {
145 return toDouble(operator[](0));
146 };
148 operator int() const {
149 return toInt(operator[](0));
150 };
152 operator Isis::BigInt() const {
153 return toBigInt(operator[](0));
154 };
155
156 operator QString() const;
157
158 const QString &operator[](int index) const;
159 QString &operator[](int index);
160 QString unit(const int index = 0) const;
161
162 void addComment(QString comment);
163 void addCommentWrapped(QString comment);
164
165 void addComments(const std::vector<QString> &comments);
166
168 int comments() const {
169 return (m_comments ? m_comments->size() : 0);
170 };
171 QString comment(const int index) const;
172 void clearComment();
173
179 bool operator==(const PvlKeyword &key) const {
180 if(!m_name && !key.m_name) return true;
181 if(!m_name || !key.m_name) return false;
182
183 return (stringEqual(m_name, key.m_name));
184 };
185
191 bool operator!=(const PvlKeyword &key) const {
192 return !(*this == key);
193 };
194
195 bool isEquivalent(QString string1, int index = 0) const;
196
202 void setWidth(int width) {
203 m_width = width;
204 };
205
211 void setIndent(int indent) {
212 m_indent = indent;
213 };
214
216 int width() const {
217 return m_width;
218 };
219
221 int indent() const {
222 return m_indent;
223 };
224
226
227 void setFormat(PvlFormat *formatter);
228 PvlFormat *format();
229
230 static bool stringEqual(const QString &string1,
231 const QString &string2);
232
233
234 static QString readLine(std::istream &is, bool insideComment);
235
236 static bool readCleanKeyword(QString keyword,
237 std::vector< QString > &keywordComments,
238 QString &keywordName,
239 std::vector< std::pair<QString, QString> >
240 &keywordValues);
241
242 static QString readValue(QString &keyword, bool &quoteProblem);
243 static QString readValue(QString &keyword, bool &quoteProblem,
244 const std::vector< std::pair<char, char> > &
245 otherDelimiters);
246
247 const PvlKeyword &operator=(const PvlKeyword &other);
248
250 void validateKeyword(PvlKeyword & pvlKwrd, QString psValueType="", PvlKeyword* pvlKwrdRange=NULL);
251
252 protected:
253 QString reform(const QString &value) const;
254 QString toPvl(const QString &value) const;
255 QString toIPvl(const QString &value) const;
256 std::ostream &writeWithWrap(std::ostream &os,
257 const QString &textToWrite,
258 int startColumn,
259 PvlFormat &format) const;
260
263
264 private:
266 char * m_name;
267
276 QVarLengthArray<QString, 1> m_values;
277
279 std::vector<QString> *m_units;
280
282 std::vector<QString> *m_comments;
283
284 void init();
285
286 void writeSpaces(std::ostream &, int) const;
287
292 int m_width;
297 int m_indent;
298 };
299};
300
301#endif
302
Formats a Pvl name value pair to Isis standards.
Definition PvlFormat.h:108
A single keyword-value pair.
Definition PvlKeyword.h:87
PvlKeyword()
Constructs a blank PvlKeyword object.
Definition PvlKeyword.cpp:22
void setIndent(int indent)
Sets the indent level when outputted(for formatting)
Definition PvlKeyword.h:211
const QString & operator[](int index) const
Gets value for this object at specified index.
Definition PvlKeyword.cpp:397
void setName(QString name)
Sets the keyword name.
Definition PvlKeyword.cpp:120
friend std::istream & operator>>(std::istream &is, PvlKeyword &result)
Read in a keyword.
Definition PvlKeyword.cpp:966
void setJsonValue(nlohmann::json jsonobj, QString unit="")
Sets new value from Json.
Definition PvlKeyword.cpp:173
QString toIPvl(const QString &value) const
Converts a value to iPVL format.
Definition PvlKeyword.cpp:539
int width() const
Returns the current set longest keyword name.
Definition PvlKeyword.h:216
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
PvlKeyword & operator+=(QString value)
Adds a value.
Definition PvlKeyword.cpp:342
bool isNull(const int index=0) const
Decides whether a value is null or not at a given index.
Definition PvlKeyword.cpp:102
static bool readCleanKeyword(QString keyword, std::vector< QString > &keywordComments, QString &keywordName, std::vector< std::pair< QString, QString > > &keywordValues)
This reads a keyword compressed back to 1 line of data (excluding comments, which are included on sep...
Definition PvlKeyword.cpp:1180
QString unit(const int index=0) const
Returns the units of measurement of the element of the array of values for the object at the specifie...
Definition PvlKeyword.cpp:414
~PvlKeyword()
Destructs a PvlKeyword object.
Definition PvlKeyword.cpp:64
void setFormat(PvlFormat *formatter)
Set the PvlFormatter used to format the keyword name and value(s)
Definition PvlKeyword.cpp:942
friend std::ostream & operator<<(std::ostream &os, const PvlKeyword &keyword)
Write out the keyword.
Definition PvlKeyword.cpp:1830
int comments() const
Returns the number of lines of comments associated with this keyword.
Definition PvlKeyword.h:168
QString comment(const int index) const
Return a comment at the specified index.
Definition PvlKeyword.cpp:498
PvlFormat * m_formatter
Formatter object.
Definition PvlKeyword.h:262
bool operator==(const PvlKeyword &key) const
Returns true of the keyword names match.
Definition PvlKeyword.h:179
int indent() const
Returns the current indent level.
Definition PvlKeyword.h:221
QString reform(const QString &value) const
Checks if the value needs to be converted to PVL or iPVL and returns it in the correct format.
Definition PvlKeyword.cpp:515
void setUnits(QString units)
Sets the unit of measure for all current values if any exist.
Definition PvlKeyword.cpp:184
void setWidth(int width)
The width of the longest keyword name (for formatting)
Definition PvlKeyword.h:202
static bool stringEqual(const QString &string1, const QString &string2)
Checks to see if two QStrings are equal.
Definition PvlKeyword.cpp:592
bool operator!=(const PvlKeyword &key) const
Returns true of the keyword names do not match.
Definition PvlKeyword.h:191
bool isNamed(QString name) const
Determines whether two PvlKeywords have the same name or not.
Definition PvlKeyword.h:115
static QString readValue(QString &keyword, bool &quoteProblem)
Definition PvlKeyword.cpp:1611
PvlFormat * format()
Get the current PvlFormat or create one.
Definition PvlKeyword.cpp:953
void addCommentWrapped(QString comment)
Automatically wraps and adds long comments to the PvlKeyword.
Definition PvlKeyword.cpp:467
QString toPvl(const QString &value) const
Converts a value to PVL format.
Definition PvlKeyword.cpp:567
void addJsonValue(nlohmann::json jsonobj, QString unit="")
Adds a value with units.
Definition PvlKeyword.cpp:302
PvlKeyword & operator=(QString value)
Sets new values.
Definition PvlKeyword.cpp:247
std::ostream & writeWithWrap(std::ostream &os, const QString &textToWrite, int startColumn, PvlFormat &format) const
Wraps output so that length doesn't exceed the character limit.
Definition PvlKeyword.cpp:668
void setValue(QString value, QString unit="")
Sets new values.
Definition PvlKeyword.cpp:155
void addComment(QString comment)
Add a comment to the PvlKeyword.
Definition PvlKeyword.cpp:433
bool isEquivalent(QString string1, int index=0) const
Checks to see if a value with a specified index is equivalent to another QString.
Definition PvlKeyword.cpp:619
void clearComment()
Clears the current comments.
Definition PvlKeyword.cpp:485
void addComments(const std::vector< QString > &comments)
This method adds multiple comments at once by calling AddComments on each element in the vector.
Definition PvlKeyword.cpp:1159
static QString readLine(std::istream &is, bool insideComment)
This method reads one line of data from the input stream.
Definition PvlKeyword.cpp:1768
void clear()
Clears all values and units for this PvlKeyword object.
Definition PvlKeyword.cpp:348
void validateKeyword(PvlKeyword &pvlKwrd, QString psValueType="", PvlKeyword *pvlKwrdRange=NULL)
Validate Keyword for type and required values.
Definition PvlKeyword.cpp:1938
void addValue(QString value, QString unit="")
Adds a value with units.
Definition PvlKeyword.cpp:268
Parse and return elements of a Pvl sequence.
Definition PvlSequence.h:46
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
int toInt(const QString &string)
Global function to convert from a string to an integer.
Definition IString.cpp:93
BigInt toBigInt(const QString &string)
Global function to convert from a string to a "big" integer.
Definition IString.cpp:115
long long int BigInt
Big int.
Definition Constants.h:49
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition IString.cpp:149