@@ -556,62 +556,84 @@ std::string WsjcppYamlNode::toString(std::string sIndent) {
556556 for (int i = 0 ; i < m_vObjects.size (); i++) {
557557 WsjcppYamlNode *pNode = m_vObjects[i];
558558 if (pNode->isEmpty ()) {
559- std::string sVal = pNode->toString ();
560- sVal = WsjcppCore::trim (sVal );
561- if (sVal .length () > 0 ) { // empty string have content
562- sRet += sIndent + pNode->getStringNodeLastIndent ();
563- }
559+ // std::string sVal = pNode->toString(sIndent + pNode->getStringNodeLastIndent());
560+ std::string sVal = pNode->toString (sIndent );
564561 sRet += sVal ;
565562 } else if (pNode->isMap ()) {
566563 sRet += sIndent + pNode->getStringNodeLastIndent ();
567564 std::string s = pNode->toString (sIndent + pNode->getStringNodeLastIndent ());
568- WsjcppCore::trim (s);
565+ WsjcppCore::ltrim (s);
569566 sRet += " - " + s;
570567 } else {
571568 sRet += sIndent + pNode->getStringNodeLastIndent ();
572569 sRet += " - " + pNode->toString ();
573570 }
574571 sRet += " \n " ;
575572 }
573+ removeLastCharNewLine (sRet );
576574 } else if (this ->isMap ()) {
577- for (int i = 0 ; i < m_vObjects.size (); i++) {
578- WsjcppYamlNode *pNode = m_vObjects[i];
579- if (pNode->isEmpty () ) {
580- // sRet += " * " + pNode->toString(sIndent);
581- sRet += pNode->toString (sIndent );
582- sRet += " \n " ;
583- } else if (pNode->isUndefined ()) {
584- sRet += sIndent + pNode->getStringNodeLastIndent ()
585- + pNode->getSerializedName () + " :\n " + pNode->toString ();
586- } else if (pNode->isArray () || pNode->isMap ()) {
587- sRet += sIndent + pNode->getStringNodeLastIndent ()
588- + pNode->getSerializedName () + " :" ;
589- if (pNode->getComment ().length () > 0 ) {
590- sRet += " # " + pNode->getComment ();
591- }
592- sRet += " \n " ;
593- sRet += pNode->toString (sIndent + pNode->getStringNodeLastIndent ());
594- } else {
595- std::string sVal = pNode->toString ();
596- std::string sVal_ = sVal ;
597- sVal_ = WsjcppCore::trim (sVal_ );
598- if (sVal_ .length () > 0 ) {
599- sVal = " " + sVal ;
575+ if (m_vObjects.size () > 0 ) {
576+ // sRet += "-s-" + std::to_string(m_vObjects.size()) + "--";
577+ for (int i = 0 ; i < m_vObjects.size (); i++) {
578+ WsjcppYamlNode *pNode = m_vObjects[i];
579+ if (pNode->isEmpty () ) {
580+ std::string s = pNode->toString (sIndent );
581+ // WsjcppLog::warn(TAG, "Empty");
582+ // WsjcppCore::trim(s);
583+ // sRet += s + " # rmpty";
584+ sRet += s;
585+ } else if (pNode->isUndefined ()) {
586+ std::string s = pNode->toString ();
587+ sRet += sIndent + pNode->getStringNodeLastIndent ()
588+ + pNode->getSerializedName () + " :" ;
589+ if (pNode->hasObjects ()) {
590+ sRet += " \n " + pNode->toString ();
591+ removeLastCharNewLine (sRet );
592+ }
593+ } else if (pNode->isArray () || pNode->isMap ()) {
594+ sRet += sIndent + pNode->getStringNodeLastIndent ()
595+ + pNode->getSerializedName () + " :" ;
596+ if (pNode->getComment ().length () > 0 ) {
597+ sRet += " # " + pNode->getComment ();
598+ }
599+ std::string s = pNode->toString (sIndent + pNode->getStringNodeLastIndent ());
600+ if (pNode->isMap ()) {
601+ if (pNode->getKeys ().size () > 0 ) {
602+ sRet += " \n " ;
603+ } else {
604+ // removeLastCharNewLine(s);
605+ }
606+ }
607+ if (pNode->isArray ()) {
608+ if (pNode->getLength () > 0 ) {
609+ sRet += " \n " ;
610+ } else {
611+ // removeLastCharNewLine(s);
612+ }
613+ }
614+ sRet += s;
615+ } else {
616+ std::string sVal = pNode->toString ();
617+ std::string sVal_ = sVal ;
618+ sVal_ = WsjcppCore::trim (sVal_ );
619+ if (sVal_ .length () > 0 ) {
620+ sVal = " " + sVal ;
621+ }
622+ WsjcppCore::rtrim (sVal );
623+ sRet += sIndent + pNode->getStringNodeLastIndent ()
624+ + pNode->getSerializedName () + " :" + sVal ;
600625 }
601- sRet += sIndent + pNode->getStringNodeLastIndent ()
602- + pNode->getSerializedName () + " :" + sVal ;
603626 sRet += " \n " ;
604627 }
628+ removeLastCharNewLine (sRet );
605629 }
606630 } else {
631+ WsjcppLog::warn (TAG, " ????" );
607632 sRet = " " ; // undefined element must be empty
608633 }
609634
610635 if (m_pParent == nullptr ) {
611- int nLen = sRet .length ();
612- if (nLen > 0 && sRet [nLen - 1 ] == ' \n ' ) {
613- sRet = sRet .substr (0 , nLen - 1 );
614- }
636+ removeLastCharNewLine (sRet );
615637 }
616638 return sRet ;
617639}
@@ -675,18 +697,31 @@ int WsjcppYamlNode::getNumberOfLine() const {
675697 return m_placeInFile.getNumberOfLine ();
676698}
677699
678- // ---------------------------------------------------------------------
679-
680700void WsjcppYamlNode::setNumberOfLine (int nNumberOfLine) {
681701 m_placeInFile.setNumberOfLine (nNumberOfLine);
682702}
683703
684- // ---------------------------------------------------------------------
685-
686704void WsjcppYamlNode::throw_error (const std::string &sError ) {
687705 throw std::runtime_error (sError .c_str ());
688706}
689707
708+ void WsjcppYamlNode::removeLastCharNewLine (std::string &sLine ) {
709+ int nLen = sLine .length ();
710+ if (nLen > 0 && sLine [nLen - 1 ] == ' \n ' ) {
711+ sLine = sLine .substr (0 , nLen - 1 );
712+ }
713+ }
714+
715+ bool WsjcppYamlNode::hasContent (const std::string &sVal ) {
716+ std::string sVal_ = sVal ;
717+ sVal_ = WsjcppCore::trim (sVal_ );
718+ return sVal_ .length () > 0 ;
719+ }
720+
721+ bool WsjcppYamlNode::hasObjects () {
722+ return m_vObjects.size () > 0 ;
723+ }
724+
690725// ---------------------------------------------------------------------
691726// WsjcppYamlParsebleLine
692727
@@ -1264,7 +1299,7 @@ bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError)
12641299// ---------------------------------------------------------------------
12651300
12661301bool WsjcppYaml::saveToFile (const std::string &sFileName , std::string &sError ) {
1267- std::string sBuffer = m_pRoot->toString ();
1302+ std::string sBuffer = m_pRoot->toString () + " \n " ; // last empty line must be always
12681303 if (!WsjcppCore::writeFile (sFileName , sBuffer )) {
12691304 sError = " Could not save to file" ;
12701305 return false ;
@@ -1281,7 +1316,8 @@ bool WsjcppYaml::loadFromString(const std::string &sBufferName, const std::strin
12811316// ---------------------------------------------------------------------
12821317
12831318bool WsjcppYaml::saveToString (std::string &sBuffer , std::string &sError ) {
1284- sBuffer = m_pRoot->toString ();
1319+ // WsjcppLog::warn(TAG, "WsjcppYaml::saveToString");
1320+ sBuffer = m_pRoot->toString () + " \n " ; // last empty line must be always
12851321 return true ;
12861322}
12871323
@@ -1367,14 +1403,17 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
13671403
13681404 if (m_parseLine.isEmptyLine ()) {
13691405 if (m_pParseCurrentParentNode != nullptr ) {
1406+ // WsjcppLog::warn(TAG, "here empty line and parent exists " + m_parsePlaceInFile.getForLogFormat());
13701407 if (m_pParseCurrentParentNode->isArray () || m_pParseCurrentParentNode->isMap () || m_pParseCurrentParentNode->isUndefined ()) {
1408+ // WsjcppLog::warn(TAG, "array, map or undefined");
13711409 WsjcppYamlNode *pNode = new WsjcppYamlNode (
13721410 m_pParseCurrentParentNode, m_parsePlaceInFile,
13731411 WSJCPP_YAML_NODE_EMPTY
13741412 );
13751413 pNode->setNodeIndents (m_vStackDiffNodeIndents);
13761414 m_pParseCurrentParentNode->appendElement (pNode);
13771415 } else if (m_pParseCurrentParentNode->getParent () != nullptr && (m_pParseCurrentParentNode->getParent ()->isArray () || m_pParseCurrentParentNode->getParent ()->isMap ())) {
1416+ // WsjcppLog::warn(TAG, "parent exists and parent map or array");
13781417 WsjcppYamlNode *pNode = new WsjcppYamlNode (
13791418 m_pParseCurrentParentNode->getParent (), m_parsePlaceInFile,
13801419 WSJCPP_YAML_NODE_EMPTY
@@ -1384,6 +1423,8 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
13841423 } else {
13851424 throw std::runtime_error (TAG + " : Empty element can be added only to map or to array" );
13861425 }
1426+ } else {
1427+ WsjcppLog::warn (TAG, " here empty line" );
13871428 }
13881429 continue ;
13891430 }
0 commit comments