Skip to content

Commit 8e91fa3

Browse files
committed
Partial fix for #13 WsjcppYamlCursor
1 parent b25e4d2 commit 8e91fa3

13 files changed

+306
-182
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 140 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -313,39 +313,6 @@ bool WsjcppYamlItem::setElementValue(
313313

314314
// ---------------------------------------------------------------------
315315

316-
bool WsjcppYamlItem::setElementValue(
317-
const std::string &sName,
318-
long nValue,
319-
WsjcppYamlQuotes nNameQuotes,
320-
WsjcppYamlQuotes nValueQuotes
321-
) {
322-
return setElementValue(sName, std::to_string(nValue), nNameQuotes, nValueQuotes);
323-
}
324-
325-
// ---------------------------------------------------------------------
326-
327-
bool WsjcppYamlItem::setElementValue(
328-
const std::string &sName,
329-
int nValue,
330-
WsjcppYamlQuotes nNameQuotes,
331-
WsjcppYamlQuotes nValueQuotes
332-
) {
333-
return setElementValue(sName, std::to_string(nValue), nNameQuotes, nValueQuotes);
334-
}
335-
336-
// ---------------------------------------------------------------------
337-
338-
bool WsjcppYamlItem::setElementValue(
339-
const std::string &sName,
340-
bool bValue,
341-
WsjcppYamlQuotes nNameQuotes,
342-
WsjcppYamlQuotes nValueQuotes
343-
) {
344-
return setElementValue(sName, (bValue ? "yes" : "no"), nNameQuotes, nValueQuotes);
345-
}
346-
347-
// ---------------------------------------------------------------------
348-
349316
bool WsjcppYamlItem::createElementMap(const std::string &sName, WsjcppYamlQuotes nNameQuotes) {
350317
if (m_nItemType != WSJCPP_YAML_ITEM_MAP ) {
351318
WsjcppLog::throw_err(TAG, "createElementMap, Element must be 'map' for " + this->getPlaceInFile().getForLogFormat());
@@ -498,7 +465,6 @@ bool WsjcppYamlItem::isValue() {
498465
// ---------------------------------------------------------------------
499466

500467
std::string WsjcppYamlItem::getValue() {
501-
WsjcppLog::warn(TAG, "getValue is deprecated try getStringValue, getBoolValue, getLongValue, getIntValue...");
502468
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
503469
WsjcppLog::throw_err(TAG, "getValue, Element must be value for " + this->getForLogFormat());
504470
}
@@ -507,55 +473,6 @@ std::string WsjcppYamlItem::getValue() {
507473

508474
// ---------------------------------------------------------------------
509475

510-
std::string WsjcppYamlItem::getStringValue() {
511-
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
512-
WsjcppLog::throw_err(TAG, "getStringValue, Element must be value for " + this->getForLogFormat());
513-
}
514-
return m_sValue;
515-
}
516-
517-
// ---------------------------------------------------------------------
518-
519-
bool WsjcppYamlItem::getBoolValue() {
520-
std::string sValue = getStringValue();
521-
sValue = WsjcppCore::toLower(sValue);
522-
if (sValue == "yes" || sValue == "true") {
523-
return true;
524-
} else if (sValue == "no" || sValue == "false") {
525-
return false;
526-
} else {
527-
WsjcppLog::throw_err(TAG, "getBoolValue, Element must be bool expected with ignore case like"
528-
"['yes','no','true','false'] for " + this->getForLogFormat());
529-
}
530-
return false;
531-
}
532-
533-
// ---------------------------------------------------------------------
534-
535-
long WsjcppYamlItem::getLongValue() {
536-
std::string sValue = getStringValue();
537-
sValue = WsjcppCore::toLower(sValue);
538-
long nValue = std::atol(sValue.c_str());
539-
if (std::to_string(nValue) != sValue) {
540-
WsjcppLog::throw_err(TAG, "getLongValue, Element must be long or int but have a string" + this->getForLogFormat());
541-
}
542-
return nValue;
543-
}
544-
545-
// ---------------------------------------------------------------------
546-
547-
int WsjcppYamlItem::getIntValue() {
548-
std::string sValue = getStringValue();
549-
sValue = WsjcppCore::toLower(sValue);
550-
int nValue = std::atoi(sValue.c_str());
551-
if (std::to_string(nValue) != sValue) {
552-
WsjcppLog::throw_err(TAG, "getLongValue, Element must be int but have a string" + this->getForLogFormat());
553-
}
554-
return nValue;
555-
}
556-
557-
// ---------------------------------------------------------------------
558-
559476
void WsjcppYamlItem::setValue(const std::string &sValue, WsjcppYamlQuotes nQuotes) {
560477
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
561478
WsjcppLog::throw_err(TAG, "setValue, Element must be value for " + this->getForLogFormat());
@@ -987,12 +904,14 @@ void WsjcppYamlParserStatus::logUnknownLine(const std::string &sPrefix) {
987904

988905
WsjcppYamlCursor::WsjcppYamlCursor(WsjcppYamlItem *pCurrentNode) {
989906
m_pCurrentNode = pCurrentNode;
907+
TAG = "WsjcppYamlCursor";
990908
}
991909

992910
// ---------------------------------------------------------------------
993911

994-
WsjcppYamlCursor::WsjcppYamlCursor() {
995-
m_pCurrentNode = nullptr;
912+
WsjcppYamlCursor::WsjcppYamlCursor()
913+
: WsjcppYamlCursor(nullptr) {
914+
// nothing
996915
}
997916

998917
// ---------------------------------------------------------------------
@@ -1009,6 +928,18 @@ bool WsjcppYamlCursor::isNull() const {
1009928

1010929
// ---------------------------------------------------------------------
1011930

931+
bool WsjcppYamlCursor::isUndefined() const {
932+
return m_pCurrentNode != nullptr && m_pCurrentNode->isUndefined();
933+
}
934+
935+
// ---------------------------------------------------------------------
936+
937+
bool WsjcppYamlCursor::isValue() const {
938+
return m_pCurrentNode != nullptr && m_pCurrentNode->isValue();
939+
}
940+
941+
// ---------------------------------------------------------------------
942+
1012943
bool WsjcppYamlCursor::isArray() const {
1013944
return m_pCurrentNode != nullptr && m_pCurrentNode->isArray();
1014945
}
@@ -1027,6 +958,130 @@ bool WsjcppYamlCursor::isMap() const {
1027958

1028959
// ---------------------------------------------------------------------
1029960

961+
std::vector<std::string> WsjcppYamlCursor::keys() const {
962+
return m_pCurrentNode != nullptr && m_pCurrentNode->isMap() ? m_pCurrentNode->getKeys() : std::vector<std::string>();
963+
}
964+
965+
// ---------------------------------------------------------------------
966+
967+
bool WsjcppYamlCursor::hasKey(const std::string &sKey) const {
968+
return m_pCurrentNode != nullptr && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sKey);
969+
}
970+
971+
// ---------------------------------------------------------------------
972+
973+
// WsjcppYamlCursor &WsjcppYamlCursor::set(const std::string &sName, const std::string &sValue) {
974+
// return *this;
975+
// }
976+
//
977+
// // ---------------------------------------------------------------------
978+
//
979+
// WsjcppYamlCursor &WsjcppYamlCursor::set(const std::string &sName, int nValue) {
980+
// return *this;
981+
// }
982+
//
983+
// // ---------------------------------------------------------------------
984+
//
985+
// WsjcppYamlCursor &WsjcppYamlCursor::set(const std::string &sName, bool bValue) {
986+
// return *this;
987+
// }
988+
//
989+
// // ---------------------------------------------------------------------
990+
//
991+
// WsjcppYamlCursor &WsjcppYamlCursor::remove(const std::string &sKey) {
992+
// return *this;
993+
// }
994+
995+
// ---------------------------------------------------------------------
996+
997+
std::string WsjcppYamlCursor::comment() {
998+
return m_pCurrentNode != nullptr ? m_pCurrentNode->getComment() : "";
999+
}
1000+
1001+
// ---------------------------------------------------------------------
1002+
1003+
WsjcppYamlCursor &WsjcppYamlCursor::comment(const std::string& sComment) {
1004+
if (m_pCurrentNode != nullptr) {
1005+
m_pCurrentNode->setComment(sComment);
1006+
}
1007+
return *this;
1008+
}
1009+
1010+
// ---------------------------------------------------------------------
1011+
1012+
std::string WsjcppYamlCursor::valStr() {
1013+
return m_pCurrentNode != nullptr ? m_pCurrentNode->getValue() : "";
1014+
}
1015+
1016+
// ---------------------------------------------------------------------
1017+
1018+
WsjcppYamlCursor &WsjcppYamlCursor::val(const std::string &sValue) {
1019+
if (m_pCurrentNode != nullptr) {
1020+
m_pCurrentNode->setValue(sValue); // TODO reserch need or not add quotes
1021+
}
1022+
return *this;
1023+
}
1024+
1025+
// ---------------------------------------------------------------------
1026+
1027+
WsjcppYamlCursor &WsjcppYamlCursor::val(const char *sValue) {
1028+
this->val(std::string(sValue));
1029+
return *this;
1030+
}
1031+
1032+
// ---------------------------------------------------------------------
1033+
1034+
int WsjcppYamlCursor::valInt() {
1035+
if (m_pCurrentNode != nullptr) {
1036+
std::string sValue = m_pCurrentNode->getValue();
1037+
sValue = WsjcppCore::toLower(sValue);
1038+
int nValue = std::atoi(sValue.c_str());
1039+
if (std::to_string(nValue) != sValue) {
1040+
WsjcppLog::throw_err(TAG, "valInt, Element must be int but have a string" + m_pCurrentNode->getForLogFormat());
1041+
}
1042+
return nValue;
1043+
}
1044+
return 0;
1045+
}
1046+
1047+
// ---------------------------------------------------------------------
1048+
1049+
WsjcppYamlCursor &WsjcppYamlCursor::val(int nValue) {
1050+
if (m_pCurrentNode != nullptr) {
1051+
m_pCurrentNode->setValue(std::to_string(nValue));
1052+
}
1053+
return *this;
1054+
}
1055+
1056+
// ---------------------------------------------------------------------
1057+
1058+
bool WsjcppYamlCursor::valBool() {
1059+
if (m_pCurrentNode != nullptr) {
1060+
std::string sValue = m_pCurrentNode->getValue();
1061+
sValue = WsjcppCore::toLower(sValue);
1062+
if (sValue == "yes" || sValue == "true") {
1063+
return true;
1064+
} else if (sValue == "no" || sValue == "false") {
1065+
return false;
1066+
} else {
1067+
WsjcppLog::throw_err(TAG, "valBool, Element must be bool expected with ignore case like"
1068+
"['yes','no','true','false'] for " + m_pCurrentNode->getForLogFormat());
1069+
}
1070+
}
1071+
return false;
1072+
}
1073+
1074+
// ---------------------------------------------------------------------
1075+
1076+
WsjcppYamlCursor &WsjcppYamlCursor::val(bool bValue) {
1077+
if (m_pCurrentNode != nullptr) {
1078+
m_pCurrentNode->setValue((bValue ? "yes" : "no"));
1079+
}
1080+
return *this;
1081+
}
1082+
1083+
// ---------------------------------------------------------------------
1084+
10301085
WsjcppYamlCursor WsjcppYamlCursor::operator[](int idx) const {
10311086
if (m_pCurrentNode != nullptr && m_pCurrentNode->isArray() && idx < m_pCurrentNode->getLength() && idx >= 0) {
10321087
return WsjcppYamlCursor(m_pCurrentNode->getElement(idx));

src/wsjcpp_yaml.h

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,6 @@ class WsjcppYamlItem { // TODO: rename to node
9999
WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE
100100
);
101101

102-
bool setElementValue(
103-
const std::string &sName, int nValue,
104-
WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE,
105-
WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE
106-
);
107-
108-
bool setElementValue(
109-
const std::string &sName, long nValue,
110-
WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE,
111-
WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE
112-
);
113-
114-
bool setElementValue(
115-
const std::string &sName, bool bValue,
116-
WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE,
117-
WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE
118-
);
119-
120102
bool createElementMap(const std::string &sName, WsjcppYamlQuotes nNameQuotes);
121103
WsjcppYamlItem *createElementMap();
122104
bool createElementArray(const std::string &sName, WsjcppYamlQuotes nNameQuotes);
@@ -130,11 +112,7 @@ class WsjcppYamlItem { // TODO: rename to node
130112

131113
bool isValue();
132114

133-
std::string getValue(); // deprecated
134-
std::string getStringValue(); // simular 'getValue'
135-
bool getBoolValue();
136-
long getLongValue();
137-
int getIntValue();
115+
std::string getValue(); // contains only strings
138116

139117
void setValue(const std::string &sValue, WsjcppYamlQuotes nQuotes = WSJCPP_YAML_QUOTES_NONE);
140118
WsjcppYamlQuotes getValueQuotes();
@@ -222,21 +200,28 @@ class WsjcppYamlCursor {
222200
// null or undefined
223201
bool isNull() const;
224202

203+
// isUndefined
204+
bool isUndefined() const;
205+
206+
// value
207+
bool isValue() const;
208+
225209
// array
226210
bool isArray() const;
227211
size_t size() const;
228-
WsjcppYamlCursor &push(const std::string &sVal);
229-
WsjcppYamlCursor &push(int nVal);
230-
WsjcppYamlCursor &push(bool bVal);
231-
WsjcppYamlCursor &remove(int nIdx);
212+
// WsjcppYamlCursor &push(const std::string &sVal);
213+
// WsjcppYamlCursor &push(int nVal);
214+
// WsjcppYamlCursor &push(bool bVal);
215+
// WsjcppYamlCursor &remove(int nIdx);
232216

233217
// map
234218
bool isMap() const;
235-
std::vector<std::string> keys();
236-
WsjcppYamlCursor &set(const std::string &sName, const std::string &sValue);
237-
WsjcppYamlCursor &set(const std::string &sName, int nValue);
238-
WsjcppYamlCursor &set(const std::string &sName, bool bValue);
239-
WsjcppYamlCursor &remove(const std::string &sKey);
219+
std::vector<std::string> keys() const;
220+
bool hasKey(const std::string &sKey) const;
221+
// WsjcppYamlCursor &set(const std::string &sName, const std::string &sValue);
222+
// WsjcppYamlCursor &set(const std::string &sName, int nValue);
223+
// WsjcppYamlCursor &set(const std::string &sName, bool bValue);
224+
// WsjcppYamlCursor &remove(const std::string &sKey);
240225

241226
// comment
242227
std::string comment();
@@ -245,7 +230,8 @@ class WsjcppYamlCursor {
245230
// val
246231
std::string valStr();
247232
WsjcppYamlCursor &val(const std::string &sValue);
248-
bool valInt();
233+
WsjcppYamlCursor &val(const char *sValue);
234+
int valInt();
249235
WsjcppYamlCursor &val(int nValue);
250236
bool valBool();
251237
WsjcppYamlCursor &val(bool bValue);
@@ -255,6 +241,7 @@ class WsjcppYamlCursor {
255241
WsjcppYamlCursor operator[](const std::string &sName) const;
256242

257243
private:
244+
std::string TAG;
258245
WsjcppYamlItem *m_pCurrentNode;
259246
};
260247

0 commit comments

Comments
 (0)