@@ -161,7 +161,7 @@ void WsjcppYamlNode::doEmpty() {
161161 if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) {
162162 m_nItemType = WSJCPP_YAML_NODE_EMPTY;
163163 } else {
164- throw std::runtime_error (TAG + " : Element already defined as '" + this ->getItemTypeAsString () + " '" );
164+ throw std::runtime_error (TAG + " : Element already defined as '" + this ->getNodeTypeAsString () + " '" );
165165 }
166166}
167167
@@ -177,7 +177,7 @@ void WsjcppYamlNode::doArray() {
177177 if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) {
178178 m_nItemType = WSJCPP_YAML_NODE_ARRAY;
179179 } else {
180- throw std::runtime_error (TAG + " : Element already defined as '" + this ->getItemTypeAsString () + " '" );
180+ throw std::runtime_error (TAG + " : Element already defined as '" + this ->getNodeTypeAsString () + " '" );
181181 }
182182}
183183
@@ -187,7 +187,7 @@ void WsjcppYamlNode::doMap() {
187187 if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) {
188188 m_nItemType = WSJCPP_YAML_NODE_MAP;
189189 } else {
190- throw std::runtime_error (TAG + " : Element already defined as '" + this ->getItemTypeAsString () + " '" );
190+ throw std::runtime_error (TAG + " : Element already defined as '" + this ->getNodeTypeAsString () + " '" );
191191 }
192192}
193193
@@ -197,7 +197,7 @@ void WsjcppYamlNode::doValue() {
197197 if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) {
198198 m_nItemType = WSJCPP_YAML_NODE_VALUE;
199199 } else {
200- throw std::runtime_error (TAG + " : Element already defined as '" + this ->getItemTypeAsString () + " '" );
200+ throw std::runtime_error (TAG + " : Element already defined as '" + this ->getNodeTypeAsString () + " '" );
201201 }
202202}
203203
@@ -413,15 +413,20 @@ WsjcppYamlNode *WsjcppYamlNode::getElement(int i) {
413413
414414// ---------------------------------------------------------------------
415415
416- bool WsjcppYamlNode::appendElement (WsjcppYamlNode *pItem ) {
417- if (pItem ->isEmpty ()) {
418- m_vObjects.push_back (pItem ); // TODO clone object
416+ bool WsjcppYamlNode::appendElement (WsjcppYamlNode *pNode ) {
417+ if (pNode ->isEmpty ()) {
418+ m_vObjects.push_back (pNode ); // TODO clone object
419419 return true ;
420420 }
421421 if (m_nItemType != WSJCPP_YAML_NODE_ARRAY) {
422- throw std::runtime_error (TAG + " : appendElement, Element must be array for " + this ->getForLogFormat ());
423- }
424- m_vObjects.push_back (pItem); // TODO clone object
422+ throw std::runtime_error (TAG + " : appendElement, "
423+ " tring add node \n "
424+ " name='" + pNode->getName () + " '\n "
425+ " type=" + pNode->getNodeTypeAsString () + " \n "
426+ " line=" + std::to_string (pNode->getNumberOfLine ()) + " )\n "
427+ " To element (must be array) \n " + this ->getForLogFormat ());
428+ }
429+ m_vObjects.push_back (pNode); // TODO clone object
425430 return true ;
426431}
427432
@@ -613,7 +618,7 @@ std::string WsjcppYamlNode::toString(std::string sIntent) {
613618
614619// ---------------------------------------------------------------------
615620
616- std::string WsjcppYamlNode::getItemTypeAsString () {
621+ std::string WsjcppYamlNode::getNodeTypeAsString () {
617622 if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) {
618623 return " undefined" ;
619624 } else if (m_nItemType == WSJCPP_YAML_NODE_ARRAY) {
@@ -666,6 +671,18 @@ int WsjcppYamlNode::getNodeIntent() {
666671
667672// ---------------------------------------------------------------------
668673
674+ int WsjcppYamlNode::getNumberOfLine () const {
675+ return m_placeInFile.getNumberOfLine ();
676+ }
677+
678+ // ---------------------------------------------------------------------
679+
680+ void WsjcppYamlNode::setNumberOfLine (int nNumberOfLine) {
681+ m_placeInFile.setNumberOfLine (nNumberOfLine);
682+ }
683+
684+ // ---------------------------------------------------------------------
685+
669686void WsjcppYamlNode::throw_error (const std::string &sError ) {
670687 throw std::runtime_error (sError .c_str ());
671688}
@@ -1191,6 +1208,12 @@ WsjcppYamlCursor &WsjcppYamlCursor::val(bool bValue) {
11911208
11921209// ---------------------------------------------------------------------
11931210
1211+ WsjcppYamlNode *WsjcppYamlCursor::node () {
1212+ return m_pCurrentNode;
1213+ }
1214+
1215+ // ---------------------------------------------------------------------
1216+
11941217WsjcppYamlCursor WsjcppYamlCursor::operator [](int idx) const {
11951218 if (m_pCurrentNode != nullptr && m_pCurrentNode->isArray () && idx < m_pCurrentNode->getLength () && idx >= 0 ) {
11961219 return WsjcppYamlCursor (m_pCurrentNode->getElement (idx));
@@ -1240,9 +1263,10 @@ bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError)
12401263
12411264// ---------------------------------------------------------------------
12421265
1243- bool WsjcppYaml::saveToFile (const std::string &sFileName ) {
1266+ bool WsjcppYaml::saveToFile (const std::string &sFileName , std::string & sError ) {
12441267 std::string sBuffer = m_pRoot->toString ();
12451268 if (!WsjcppCore::writeFile (sFileName , sBuffer )) {
1269+ sError = " Could not save to file" ;
12461270 return false ;
12471271 }
12481272 return true ;
@@ -1256,7 +1280,7 @@ bool WsjcppYaml::loadFromString(const std::string &sBufferName, const std::strin
12561280
12571281// ---------------------------------------------------------------------
12581282
1259- bool WsjcppYaml::saveToString (std::string &sBuffer ) {
1283+ bool WsjcppYaml::saveToString (std::string &sBuffer , std::string & sError ) {
12601284 sBuffer = m_pRoot->toString ();
12611285 return true ;
12621286}
@@ -1440,38 +1464,30 @@ void WsjcppYaml::process_hasName_emptyValue_arrayItem() {
14401464// ---------------------------------------------------------------------
14411465
14421466void WsjcppYaml::process_hasName_emptyValue_noArrayItem () {
1467+ // std::cout << "process_hasName_emptyValue_noArrayItem" << std::endl;
14431468 if (m_parseLine.getIntent () == m_pParseCurrentParentNode->getNodeIntent ()) {
14441469 if (m_pParseCurrentParentNode->getParent () != nullptr ) {
14451470 m_pParseCurrentParentNode = m_pParseCurrentParentNode->getParent ();
14461471 }
14471472 }
1448- // std::cout << "process_hasName_emptyValue_noArrayItem " << std::endl;
14491473 WsjcppYamlNode *pNode = new WsjcppYamlNode (
14501474 m_pParseCurrentParentNode, m_parsePlaceInFile,
14511475 WSJCPP_YAML_NODE_UNDEFINED
14521476 );
14531477 if (m_parseLine.getValueQuotes () != WSJCPP_YAML_QUOTES_NONE) {
1478+ // std::cout << "pNode->doValue() for '" << m_parseLine.getName() << "' value: '" << m_parseLine.getValue() << "' " << std::endl;
14541479 pNode->doValue ();
14551480 pNode->setValue (m_parseLine.getValue (), m_parseLine.getValueQuotes ());
14561481 }
1457- int nDiffIntent = m_parseLine.getIntent () - m_nParseCurrentIntent;
1482+ // int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
14581483 pNode->setName (m_parseLine.getName (), m_parseLine.getNameQuotes ());
14591484 pNode->setComment (m_parseLine.getComment ());
14601485 pNode->setNodeIntents (m_vStackDiffNodeIntents);
1461- // std::cout << "current node [" << m_vStackDiffNodeIntents.back() << "]" << std::endl;
1462-
1463- /* if (nDiffIntent == 0 && m_pParseCurrentParentNode->isUndefined()) {
1464- std::cout << "shit nDiffIntent = " << nDiffIntent << std::endl;
1465- std::cout << "shit m_pParseCurrentParentNode->getName() = " << m_pParseCurrentParentNode->getName() << std::endl;
1466- if (m_pParseCurrentParentNode->getParent() != nullptr) {
1467- std::cout << "shit "
1468- << " {" << m_pParseCurrentParentNode->getPlaceInFile().getLine() << "} "
1469- << " {" << m_parsePlaceInFile.getLine() << "} " << std::endl;
1470- }
1471- }
1472- */
1486+
14731487 m_pParseCurrentParentNode->setElement (m_parseLine.getName (), pNode);
1474- m_pParseCurrentParentNode = pNode;
1488+ if (pNode->isUndefined ()) {
1489+ m_pParseCurrentParentNode = pNode;
1490+ }
14751491}
14761492
14771493// ---------------------------------------------------------------------
@@ -1481,11 +1497,18 @@ void WsjcppYaml::process_hasName_hasValue_arrayItem() {
14811497 if (m_pParseCurrentParentNode->isUndefined ()) {
14821498 m_pParseCurrentParentNode->doArray ();
14831499 }
1500+ // if (!m_pParseCurrentParentNode->isArray()) {
1501+ // std::cout << "m_pParseCurrentParentNode->getName(): " << m_pParseCurrentParentNode->getName() << std::endl;
1502+ // }
1503+
14841504 WsjcppYamlNode *pMapItem = new WsjcppYamlNode (
14851505 m_pParseCurrentParentNode, m_parsePlaceInFile,
14861506 WSJCPP_YAML_NODE_MAP
14871507 );
1508+ // std::cout << "m_parseLine.getName(): " << m_parseLine.getName() << std::endl;
1509+
14881510 m_pParseCurrentParentNode->appendElement (pMapItem);
1511+ // std::cout << "appended " << std::endl;
14891512 m_pParseCurrentParentNode = pMapItem;
14901513 pMapItem->setNodeIntents (m_vStackDiffNodeIntents);
14911514
@@ -1506,6 +1529,7 @@ void WsjcppYaml::process_hasName_hasValue_arrayItem() {
15061529// ---------------------------------------------------------------------
15071530
15081531void WsjcppYaml::process_hasName_hasValue_noArrayItem () {
1532+ // std::cout << "process_hasName_hasValue_noArrayItem" << std::endl;
15091533 WsjcppYamlNode *pNode = new WsjcppYamlNode (
15101534 m_pParseCurrentParentNode, m_parsePlaceInFile,
15111535 WSJCPP_YAML_NODE_VALUE
@@ -1550,6 +1574,7 @@ void WsjcppYaml::process_emptyName_hasValue_noArrayItem() {
15501574// ---------------------------------------------------------------------
15511575
15521576void WsjcppYaml::process_emptyName_emptyValue_arrayItem () {
1577+ // std::cout << "process_emptyName_emptyValue_arrayItem" << std::endl;
15531578 if (m_pParseCurrentParentNode->isUndefined ()) {
15541579 m_pParseCurrentParentNode->doArray ();
15551580 }
0 commit comments