@@ -575,7 +575,7 @@ std::string WsjcppYamlItem::getForLogFormat() {
575575
576576WsjcppYamlParsebleLine::WsjcppYamlParsebleLine (int nLine) {
577577 TAG = " WsjcppYamlParsebleLine(line:" + std::to_string (nLine) + " )" ;
578- m_nLine = nLine;
578+ m_nLineNumber = nLine;
579579}
580580
581581// ---------------------------------------------------------------------
@@ -588,7 +588,7 @@ WsjcppYamlParsebleLine::WsjcppYamlParsebleLine()
588588// ---------------------------------------------------------------------
589589
590590int WsjcppYamlParsebleLine::getLineNumber () {
591- return m_nLine ;
591+ return m_nLineNumber ;
592592}
593593
594594// ---------------------------------------------------------------------
@@ -617,6 +617,12 @@ std::string WsjcppYamlParsebleLine::getComment() {
617617
618618// ---------------------------------------------------------------------
619619
620+ bool WsjcppYamlParsebleLine::hasComment () {
621+ return m_bHasComment;
622+ }
623+
624+ // ---------------------------------------------------------------------
625+
620626std::string WsjcppYamlParsebleLine::getName () {
621627 return m_sName;
622628}
@@ -653,15 +659,29 @@ bool WsjcppYamlParsebleLine::isEmptyValue() {
653659
654660// ---------------------------------------------------------------------
655661
656- void WsjcppYamlParsebleLine::parseLine (const std::string &sLine ) {
662+ bool WsjcppYamlParsebleLine::isEmptyLine () {
663+ return m_bEmptyLine;
664+ }
665+
666+ // ---------------------------------------------------------------------
667+
668+ bool WsjcppYamlParsebleLine::parseLine (const std::string &sLine , std::string &sError ) {
657669 // reset variables
658670 m_bArrayItem = false ;
659671 m_sPrefix = " " ;
660672 m_sComment = " " ;
661673 m_sName = " " ;
662674 m_sValue = " " ;
675+ m_bHasComment = false ;
663676 m_bNameHasQuotes = false ;
664677 m_bValueHasQuotes = false ;
678+ m_bEmptyLine = false ;
679+ std::string sLineTrim = sLine ;
680+ sLineTrim = WsjcppCore::trim (sLineTrim );
681+ if (sLineTrim .length () == 0 ) {
682+ m_bEmptyLine = true ;
683+ return true ;
684+ }
665685
666686 WsjcppYamlParserLineStates state = WsjcppYamlParserLineStates::NO;
667687 for (int i = 0 ; i < sLine .length (); i++) {
@@ -670,6 +690,7 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
670690 m_sPrefix += c;
671691 } else if (c == ' #' && (state == WsjcppYamlParserLineStates::NO || state == WsjcppYamlParserLineStates::VALUE)) {
672692 state = WsjcppYamlParserLineStates::COMMENT;
693+ m_bHasComment = true ;
673694 } else if (state == WsjcppYamlParserLineStates::COMMENT) {
674695 if (c != ' \r ' ) {
675696 m_sComment += c;
@@ -714,7 +735,8 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
714735 if (state == WsjcppYamlParserLineStates::STRING
715736 || state == WsjcppYamlParserLineStates::ESCAPING
716737 ) {
717- WsjcppLog::throw_err (TAG, " wrong format" );
738+ sError = " Line has wrong format." ;
739+ return false ;
718740 }
719741
720742 // split name and value
@@ -728,7 +750,7 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
728750 }
729751 }*/
730752
731- WsjcppCore::trim (m_sName);
753+ m_sName = WsjcppCore::trim (m_sName);
732754 if (m_sName.length () > 0 && m_sName[0 ] == ' "' ) {
733755 m_bNameHasQuotes = true ;
734756 m_sName = removeStringDoubleQuotes (m_sName);
@@ -740,7 +762,8 @@ void WsjcppYamlParsebleLine::parseLine(const std::string &sLine) {
740762 m_sValue = removeStringDoubleQuotes (m_sValue);
741763 }
742764
743- WsjcppCore::trim (m_sComment);
765+ m_sComment = WsjcppCore::trim (m_sComment);
766+ return true ;
744767}
745768
746769// ---------------------------------------------------------------------
@@ -776,10 +799,13 @@ std::string WsjcppYamlParsebleLine::removeStringDoubleQuotes(const std::string &
776799// WsjcppYamlParserStatus
777800
778801void WsjcppYamlParserStatus::logUnknownLine (const std::string &sPrefix ) {
779- WsjcppLog::warn (sPrefix , " Unknown line (" + std::to_string (placeInFile.getNumberOfLine ()) + " ): '" + placeInFile.getLine () + " ' \n "
780- + " Current Intent: " + std::to_string (nIntent) + " \n "
781- + " Current Item(line: " + std::to_string (pCurItem->getPlaceInFile ().getNumberOfLine ()) + " ): '" + pCurItem->getPlaceInFile ().getLine () + " '"
782- + " Current Item(filename: " + pCurItem->getPlaceInFile ().getFilename () + " '"
802+ WsjcppLog::warn (sPrefix , " \n "
803+ " error:\n "
804+ " desc: \" unknown_line\"\n "
805+ " line_number: " + std::to_string (pCurItem->getPlaceInFile ().getNumberOfLine ()) + " \n "
806+ " line: \" " + placeInFile.getLine () + " \"\n "
807+ " intent: " + std::to_string (nIntent) + " \n "
808+ " filename: \" " + pCurItem->getPlaceInFile ().getFilename () + " \" "
783809 );
784810}
785811
@@ -788,6 +814,7 @@ void WsjcppYamlParserStatus::logUnknownLine(const std::string &sPrefix) {
788814
789815WsjcppYaml::WsjcppYaml () {
790816 m_pRoot = new WsjcppYamlItem (nullptr , WsjcppYamlPlaceInFile (), WSJCPP_YAML_ITEM_MAP);
817+ TAG = " WsjcppYaml" ;
791818}
792819
793820// ---------------------------------------------------------------------
@@ -798,12 +825,12 @@ WsjcppYaml::~WsjcppYaml() {
798825
799826// ---------------------------------------------------------------------
800827
801- bool WsjcppYaml::loadFromFile (const std::string &sFileName ) {
828+ bool WsjcppYaml::loadFromFile (const std::string &sFileName , std::string & sError ) {
802829 std::string sTextContent ;
803830 if (!WsjcppCore::readTextFile (sFileName , sTextContent )) {
804831 return false ;
805832 }
806- return parse (sFileName , sTextContent );
833+ return parse (sFileName , sTextContent , sError );
807834}
808835
809836// ---------------------------------------------------------------------
@@ -818,14 +845,8 @@ bool WsjcppYaml::saveToFile(const std::string &sFileName) {
818845
819846// ---------------------------------------------------------------------
820847
821- bool WsjcppYaml::loadFromString (const std::string &sBuffer ) {
822- return false ;
823- }
824-
825- // ---------------------------------------------------------------------
826-
827- bool WsjcppYaml::loadFromString (std::string &sBuffer ) {
828- return parse (" " , sBuffer );
848+ bool WsjcppYaml::loadFromString (const std::string &sBufferName , const std::string &sBuffer , std::string &sError ) {
849+ return parse (sBufferName , sBuffer , sError );
829850}
830851
831852// ---------------------------------------------------------------------
@@ -864,7 +885,7 @@ std::vector<std::string> WsjcppYaml::splitToLines(const std::string &sBuffer) {
864885
865886// ---------------------------------------------------------------------
866887
867- bool WsjcppYaml::parse (const std::string &sFileName , const std::string &sBuffer ) {
888+ bool WsjcppYaml::parse (const std::string &sFileName , const std::string &sBuffer , std::string & sError ) {
868889 std::vector<std::string> vLines = this ->splitToLines (sBuffer );
869890 WsjcppYamlParserStatus st;
870891 st.pCurItem = m_pRoot; // TODO recreate again new root element
@@ -877,25 +898,32 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer)
877898 // WsjcppLog::info(TAG, "Line(" + std::to_string(nLine) + ") '" + st.sLine + "'");
878899 st.placeInFile .setNumberOfLine (nLine);
879900 st.line = WsjcppYamlParsebleLine (nLine);
880- st.line .parseLine (st.placeInFile .getLine ());
901+ if (!st.line .parseLine (st.placeInFile .getLine (), sError )) {
902+ return false ;
903+ }
881904
882905 bool isEmptyName = st.line .isEmptyName ();
883906 bool isEmptyValue = st.line .isEmptyValue ();
884907 bool isArrayItem = st.line .isArrayItem ();
885908 int nLineIntent = st.line .getIntent ();
886909 int nDiffIntent = nLineIntent - st.nIntent ;
887-
888- // TODO check comment
889- /* if (isEmptyName && isEmptyValue && isArrayItem) {
910+
911+ if (st.line .isEmptyLine ()) {
912+ WsjcppYamlItem *pItem = new WsjcppYamlItem (
913+ st.pCurItem , st.placeInFile ,
914+ WsjcppYamlItemType::WSJCPP_YAML_ITEM_EMPTY
915+ );
916+ st.pCurItem ->appendElement (pItem);
890917 continue ;
891- }*/
918+ }
892919
893920 while (nDiffIntent < 0 ) {
894921 st.pCurItem = st.pCurItem ->getParent ();
895922 st.nIntent = st.nIntent - 2 ;
896923 nDiffIntent = nLineIntent - st.nIntent ;
897924 if (st.pCurItem == nullptr ) {
898- WsjcppLog::throw_err (TAG, " cur item is nullptr" );
925+ sError = " Current item is nullptr" ;
926+ return false ;
899927 }
900928 }
901929
0 commit comments