From f35b122fee7a3fcbb2ee983869b116fb332042ff Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Tue, 29 Sep 2020 17:13:12 +0700 Subject: [PATCH 1/5] Updated version to v0.1.5 --- src.wsjcpp/CMakeLists.txt | 2 +- unit-tests.wsjcpp/CMakeLists.txt | 2 +- wsjcpp.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src.wsjcpp/CMakeLists.txt b/src.wsjcpp/CMakeLists.txt index ec5b1de..d797eeb 100644 --- a/src.wsjcpp/CMakeLists.txt +++ b/src.wsjcpp/CMakeLists.txt @@ -1,7 +1,7 @@ # Automaticly generated by wsjcpp@v0.2.0 cmake_minimum_required(VERSION 3.0) -add_definitions(-DWSJCPP_APP_VERSION="v0.1.3") +add_definitions(-DWSJCPP_APP_VERSION="v0.1.5") add_definitions(-DWSJCPP_APP_NAME="wsjcpp-yaml") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") diff --git a/unit-tests.wsjcpp/CMakeLists.txt b/unit-tests.wsjcpp/CMakeLists.txt index a2008dd..e324115 100644 --- a/unit-tests.wsjcpp/CMakeLists.txt +++ b/unit-tests.wsjcpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) project(unit-tests C CXX) -add_definitions(-DWSJCPP_APP_VERSION="ut-v0.1.3") +add_definitions(-DWSJCPP_APP_VERSION="ut-v0.1.5") add_definitions(-DWSJCPP_APP_NAME="unit-tests-wsjcpp-yaml") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") diff --git a/wsjcpp.yml b/wsjcpp.yml index 68fbc4e..bb1b682 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -2,7 +2,7 @@ wsjcpp_version: v0.0.1 cmake_minimum_required: 3.0 cmake_cxx_standard: 11 name: wsjcpp-yaml -version: v0.1.3 +version: v0.1.5 description: Read/Write yaml files issues: https://github.com/wsjcpp/wsjcpp-yaml/issues repositories: From a45f212760f6b060599178f967419dd1739e2f55 Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Tue, 29 Sep 2020 17:21:54 +0700 Subject: [PATCH 2/5] Added new unit-test: ReadWsjcppHoldYml --- unit-tests.wsjcpp/CMakeLists.txt | 2 + .../read-wsjcpp-hold-yml/wsjcpp.hold.yml | 91 +++++++++++++++++++ .../src/unit_test_read_wsjcpp_hold_yaml.cpp | 49 ++++++++++ wsjcpp.yml | 2 + 4 files changed, 144 insertions(+) create mode 100644 unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml create mode 100644 unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp diff --git a/unit-tests.wsjcpp/CMakeLists.txt b/unit-tests.wsjcpp/CMakeLists.txt index e324115..484321f 100644 --- a/unit-tests.wsjcpp/CMakeLists.txt +++ b/unit-tests.wsjcpp/CMakeLists.txt @@ -51,6 +51,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cursor.cpp") list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_tag_names.cpp") list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cleanup.cpp") list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_append_elements.cpp") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp") + include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt) diff --git a/unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml b/unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml new file mode 100644 index 0000000..af61cea --- /dev/null +++ b/unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml @@ -0,0 +1,91 @@ +wsjcpp_version: v0.0.1 +cmake_cxx_standard: 11 +cmake_minimum_required: 3.0 + +name: wsjcpp-core +version: v0.2.1 +description: Basic Utils for wsjcpp +issues: https://github.com/wsjcpp/wsjcpp-core/issues +repositories: + - type: main + url: "https://github.com/wsjcpp/wsjcpp-core" +keywords: + - c++ + - wsjcpp + +authors: + - name: Evgenii Sopov + email: mrseakg@gmail.com + +distribution: + - source-file: src/wsjcpp_core.cpp + target-file: wsjcpp_core.cpp + type: "source-code" + sha1: "09ef821bbc090fc1cd8a15bc4a57a9a2ce8ae00d" + - source-file: src/wsjcpp_core.h + target-file: wsjcpp_core.h + type: "source-code" # todo must be header-file + sha1: "e6e4ab2067d3c942db08e3b79862486eaf851e4b" + - source-file: "src/wsjcpp_unit_tests.cpp" + target-file: "wsjcpp_unit_tests.cpp" + type: "unit-tests" + sha1: "fd5989d1a83c8b90bdc4d5e9bc9c3051eaa1e6d2" + - source-file: "src/wsjcpp_unit_tests.h" + target-file: "wsjcpp_unit_tests.h" + type: "unit-tests" + sha1: "83d4b6e046b6b58c42882ccae4be413e03c401c1" + - source-file: "src/wsjcpp_unit_tests_main.cpp" + target-file: "wsjcpp_unit_tests_main.cpp" + type: "unit-tests" + sha1: "388ae269b325c5e161f6c3a5c598575714a4bffc" + - source-file: "scripts.wsjcpp/generate.WsjcppUnitTest.wsjcpp-script" + target-file: "generate.WsjcppUnitTest.wsjcpp-script" + type: "safe-scripting-generate" + sha1: "a7c9c2d19bf81c5b00e659384b0b92a99319a4c1" + - source-file: "scripts.wsjcpp/generate.Class.wsjcpp-script" + target-file: "generate.Class.wsjcpp-script" + type: "safe-scripting-generate" + sha1: "de1799907c685d606b93e08b821b540c2faa2db1" + +unit-tests: + cases: + - name: CoreNormalizePath + description: Check function normalizePath + - name: CoreExtractFilename + description: Check function extract filenane from path + - name: "ToUpper" + description: "String to upper" + - name: "CreateUuid" + description: "Test generation uuids" + - name: "GetEnv" + description: "Test getEnv function" + - name: "ToLower" + description: "Test toLower" + - name: "ReplaceAll" + description: "Test replace all" + - name: "DecodeUriComponent" + description: "Check decoding" + - name: "EncodeUriComponent" + description: "Check encoding" + - name: "Uint2HexString" + description: "Test convert unsigned int to hex string" + - name: "Split" + description: "Test split function" + - name: "CreateEmptyFile" + description: "Test create empty file" + - name: "ReadFileToBuffer" + description: "test for readFileToBuffer" + - name: "Join" + description: "Test join function" + - name: "getHumanSizeBytes" + description: "Test function get human size in bytes" + - name: "TestResources" + description: "Test basic resources" + - name: "ListOfDirs" + description: "Check list of directories" + - name: "FilePermissions" + description: "" + - name: "StringPadding" + description: "" + - name: "DateTimeFormat" + description: "" diff --git a/unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp b/unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp new file mode 100644 index 0000000..930f3c2 --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_read_wsjcpp_hold_yaml.cpp @@ -0,0 +1,49 @@ + +#include +#include +#include + +// --------------------------------------------------------------------- +// UnitTestReadWsjcppHoldYml + +class UnitTestReadWsjcppHoldYml : public WsjcppUnitTestBase { + public: + UnitTestReadWsjcppHoldYml(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; +}; + +REGISTRY_WSJCPP_UNIT_TEST(UnitTestReadWsjcppHoldYml) + +UnitTestReadWsjcppHoldYml::UnitTestReadWsjcppHoldYml() + : WsjcppUnitTestBase("UnitTestReadWsjcppHoldYml") { +} + +// --------------------------------------------------------------------- + +bool UnitTestReadWsjcppHoldYml::doBeforeTest() { + // do something before test + return true; +} + +// --------------------------------------------------------------------- + +void UnitTestReadWsjcppHoldYml::executeTest() { + WsjcppYaml yaml; + std::string sFilepath = "./data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml"; + std::string sError; + if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) { + WsjcppLog::err(TAG, sError); + return; + } +} + +// --------------------------------------------------------------------- + +bool UnitTestReadWsjcppHoldYml::doAfterTest() { + // do somethig after test + return true; +} + + diff --git a/wsjcpp.yml b/wsjcpp.yml index bb1b682..7287a55 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -69,3 +69,5 @@ unit-tests: description: "" - name: "AppendElements" description: "" + - name: "ReadWsjcppHoldYml" + description: "" From 7ee46b991283ef3c141ae33bb5d932a0fed62b4f Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Wed, 30 Sep 2020 13:54:07 +0700 Subject: [PATCH 3/5] Fixed #23 Added new method WsjcppYamlCursor::node() --- src/wsjcpp_yaml.cpp | 6 ++++++ src/wsjcpp_yaml.h | 2 ++ unit-tests.wsjcpp/src/unit_test_cursor.cpp | 1 + 3 files changed, 9 insertions(+) diff --git a/src/wsjcpp_yaml.cpp b/src/wsjcpp_yaml.cpp index 2b00c59..958a907 100644 --- a/src/wsjcpp_yaml.cpp +++ b/src/wsjcpp_yaml.cpp @@ -1191,6 +1191,12 @@ WsjcppYamlCursor &WsjcppYamlCursor::val(bool bValue) { // --------------------------------------------------------------------- +WsjcppYamlNode *WsjcppYamlCursor::node() { + return m_pCurrentNode; +} + +// --------------------------------------------------------------------- + WsjcppYamlCursor WsjcppYamlCursor::operator[](int idx) const { if (m_pCurrentNode != nullptr && m_pCurrentNode->isArray() && idx < m_pCurrentNode->getLength() && idx >= 0) { return WsjcppYamlCursor(m_pCurrentNode->getElement(idx)); diff --git a/src/wsjcpp_yaml.h b/src/wsjcpp_yaml.h index 215f394..e2bd343 100644 --- a/src/wsjcpp_yaml.h +++ b/src/wsjcpp_yaml.h @@ -235,6 +235,8 @@ class WsjcppYamlCursor { bool valBool(); WsjcppYamlCursor &val(bool bValue); + // node + WsjcppYamlNode *node(); WsjcppYamlCursor operator[](int idx) const; WsjcppYamlCursor operator[](const std::string &sName) const; diff --git a/unit-tests.wsjcpp/src/unit_test_cursor.cpp b/unit-tests.wsjcpp/src/unit_test_cursor.cpp index d9ef0d6..fdf960d 100644 --- a/unit-tests.wsjcpp/src/unit_test_cursor.cpp +++ b/unit-tests.wsjcpp/src/unit_test_cursor.cpp @@ -133,6 +133,7 @@ void UnitTestCursor::executeTest() { // bool compare("array bool el 4", yaml["arr1"][4].valStr(), "true"); + compare("array bool el 4", yaml["arr1"][4].node()->getValue(), "true"); compare("array bool el 4", yaml["arr1"][4].valBool(), true); yaml["arr1"][4].val(true); compare("array bool el 4", yaml["arr1"][4].valStr(), "yes"); From ad853ddabaea09934f9465f21ac29cd97ea19d3b Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Wed, 30 Sep 2020 14:04:00 +0700 Subject: [PATCH 4/5] Fixed #16 saveToFile/saveToString will be has &sError parameter --- src/main.cpp | 4 ++-- src/wsjcpp_yaml.cpp | 5 +++-- src/wsjcpp_yaml.h | 4 ++-- unit-tests.wsjcpp/src/unit_test_cursor.cpp | 2 +- unit-tests.wsjcpp/src/unit_test_read_write_file.cpp | 2 +- unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp | 4 ++-- .../src/unit_test_yaml_parser_array_included_map.cpp | 2 +- unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp | 2 +- .../src/unit_test_yaml_parser_hierarchical_map.cpp | 2 +- unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp | 2 +- unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp | 2 +- unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp | 2 +- 12 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3be199b..6d6bb12 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,8 +22,8 @@ int main(int argc, char* argv[]) { return -1; } - if (!yaml.saveToFile(sFilePath)) { - WsjcppLog::err(TAG, "Could not save data to file"); + if (!yaml.saveToFile(sFilePath, sError)) { + WsjcppLog::err(TAG, "Could not save data to file: " + sError); return -1; } diff --git a/src/wsjcpp_yaml.cpp b/src/wsjcpp_yaml.cpp index 958a907..e9a3596 100644 --- a/src/wsjcpp_yaml.cpp +++ b/src/wsjcpp_yaml.cpp @@ -1246,9 +1246,10 @@ bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError) // --------------------------------------------------------------------- -bool WsjcppYaml::saveToFile(const std::string &sFileName) { +bool WsjcppYaml::saveToFile(const std::string &sFileName, std::string &sError) { std::string sBuffer = m_pRoot->toString(); if (!WsjcppCore::writeFile(sFileName, sBuffer)) { + sError = "Could not save to file"; return false; } return true; @@ -1262,7 +1263,7 @@ bool WsjcppYaml::loadFromString(const std::string &sBufferName, const std::strin // --------------------------------------------------------------------- -bool WsjcppYaml::saveToString(std::string &sBuffer) { +bool WsjcppYaml::saveToString(std::string &sBuffer, std::string &sError) { sBuffer = m_pRoot->toString(); return true; } diff --git a/src/wsjcpp_yaml.h b/src/wsjcpp_yaml.h index e2bd343..d1723af 100644 --- a/src/wsjcpp_yaml.h +++ b/src/wsjcpp_yaml.h @@ -255,9 +255,9 @@ class WsjcppYaml { ~WsjcppYaml(); void clear(); bool loadFromFile(const std::string &sFileName, std::string &sError); - bool saveToFile(const std::string &sFileName); + bool saveToFile(const std::string &sFileName, std::string &sError); bool loadFromString(const std::string &sBufferName, const std::string &sBuffer, std::string &sError); - bool saveToString(std::string &sBuffer); + bool saveToString(std::string &sBuffer, std::string &sError); WsjcppYamlNode *getRoot(); WsjcppYamlCursor getCursor() const; diff --git a/unit-tests.wsjcpp/src/unit_test_cursor.cpp b/unit-tests.wsjcpp/src/unit_test_cursor.cpp index fdf960d..0fb4e31 100644 --- a/unit-tests.wsjcpp/src/unit_test_cursor.cpp +++ b/unit-tests.wsjcpp/src/unit_test_cursor.cpp @@ -104,7 +104,7 @@ void UnitTestCursor::executeTest() { compare("map use as array", yaml["map1"][0].isNull(), true); compare("array use as map", yaml["arr1"]["0"].isNull(), true); - compare("array use as map", yaml["arr1"][0].isNull(), false); + compare("array use as array", yaml["arr1"][0].isNull(), false); compare("array size", yaml["arr1"].comment(), "some comment array 1"); compare("array size", yaml["arr1"].size(), 6); diff --git a/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp b/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp index 361233d..01a7e21 100644 --- a/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp +++ b/unit-tests.wsjcpp/src/unit_test_read_write_file.cpp @@ -46,7 +46,7 @@ void UnitTestReadWriteFile::executeTest() { compare("version-value", yaml.getRoot()->getElement("version")->getValue(), "3"); - if (!compare("Error parsing", yaml.saveToFile(sFilepathOutput), true)) { + if (!compare("Error parsing", yaml.saveToFile(sFilepathOutput, sError), true)) { WsjcppLog::err(TAG, sError); return; } diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp index 9f2894b..309962e 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_all.cpp @@ -66,7 +66,7 @@ void UnitTestYamlParserAll::executeTest() { } std::string sSaved1 = ""; - if (!compare("Error saving", yaml.saveToString(sSaved1), true)) { + if (!compare("Error saving", yaml.saveToString(sSaved1, sError), true)) { compare("yaml_saved 2-test", sSaved1, sTestYaml); return; } @@ -98,7 +98,7 @@ void UnitTestYamlParserAll::executeTest() { compare("test80_comment", pItem->getValue(), "opa2"); std::string sSaved2 = ""; - if (compare("saving yaml", yaml.saveToString(sSaved2), true)) { + if (compare("saving yaml", yaml.saveToString(sSaved2, sError), true)) { compare("yaml_saved 1-2", sSaved1, sSaved2); } } diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp index b160229..cd199c0 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp @@ -90,7 +90,7 @@ void UnitTestYamlParserArrayIncludedMap::executeTest() { compare("param2-value", yaml.getRoot()->getElement("param2")->getValue(), "v2"); std::string sSaved = ""; - if (compare("save yaml", yaml.saveToString(sSaved), true)) { + if (compare("save yaml", yaml.saveToString(sSaved, sError), true)) { compare("yaml_save", sSaved, "# test array included map\n" // expected "param1: none value1 # it's value for something # olala\n" diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp index 0c11427..6ca59e7 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp @@ -85,7 +85,7 @@ void UnitTestYamlParserComments::executeTest() { // compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2"); std::string sSaved = ""; - compare("save yaml", yaml.saveToString(sSaved), true); + compare("save yaml", yaml.saveToString(sSaved, sError), true); compare("yaml_save", sSaved, "# Some comment 1\n" "param1: value1 # comment 2 # comment\n" diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp index 7ea6895..ef72763 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_hierarchical_map.cpp @@ -88,7 +88,7 @@ void UnitTestYamlParserHierarchicalMap::executeTest() { compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2"); std::string sSaved = ""; - bool bResult = yaml.saveToString(sSaved); + bool bResult = yaml.saveToString(sSaved, sError); if (compare("save yaml", bResult, true)) { compare("yaml_save", sSaved, "# Some comment 1\n" diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp index dbfc4d1..6fc539f 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp @@ -76,7 +76,7 @@ void UnitTestYamlParserQuotes::executeTest() { compare("array-element0-value", yaml.getRoot()->getElement("array")->getElement(0)->getValue(), "https://github.com/wsjcpp/wsjcpp-core:v0.0.1"); std::string sSaved = ""; - compare("save yaml", yaml.saveToString(sSaved), true); + compare("save yaml", yaml.saveToString(sSaved, sError), true); compare("yaml_save", sSaved, "# Some comment 1\n" "param1: \"value1\" # v1\n" diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp index a9f5b38..b3c2dd5 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp @@ -89,7 +89,7 @@ void UnitTestYamlParserSimpleArray::executeTest() { compare("param2-comment", yaml.getRoot()->getElement("param2")->getComment(), "value 2"); std::string sSaved = ""; - bool bResult = yaml.saveToString(sSaved); + bool bResult = yaml.saveToString(sSaved, sError); compare("save yaml", bResult, true); compare("yaml_save", sSaved, "# simple array test\n" // expected diff --git a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp b/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp index 2730591..c8778bf 100644 --- a/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp +++ b/unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_map.cpp @@ -52,7 +52,7 @@ void UnitTestYamlParserSimpleMap::executeTest() { compare("param2", yaml.getRoot()->getElement("param2")->getComment(), "some comment 2"); std::string sSaved = ""; - bool bResult = yaml.saveToString(sSaved); + bool bResult = yaml.saveToString(sSaved, sError); if (compare("save yaml", bResult, true)) { compare("yaml_save", sSaved, "# Some comment 1\n" From 31730f3863701725120da33341ceae0eea30b2f2 Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Wed, 30 Sep 2020 15:01:24 +0700 Subject: [PATCH 5/5] Fixed #25 problem with parsing wsjcpp.hold.yml --- src/wsjcpp_yaml.cpp | 70 ++++++++++++++++++++++++++++----------------- src/wsjcpp_yaml.h | 10 ++++++- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/wsjcpp_yaml.cpp b/src/wsjcpp_yaml.cpp index e9a3596..63f8f57 100644 --- a/src/wsjcpp_yaml.cpp +++ b/src/wsjcpp_yaml.cpp @@ -161,7 +161,7 @@ void WsjcppYamlNode::doEmpty() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_EMPTY; } else { - throw std::runtime_error(TAG + ": Element already defined as '" + this->getItemTypeAsString() + "'"); + throw std::runtime_error(TAG + ": Element already defined as '" + this->getNodeTypeAsString() + "'"); } } @@ -177,7 +177,7 @@ void WsjcppYamlNode::doArray() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_ARRAY; } else { - throw std::runtime_error(TAG + ": Element already defined as '" + this->getItemTypeAsString() + "'"); + throw std::runtime_error(TAG + ": Element already defined as '" + this->getNodeTypeAsString() + "'"); } } @@ -187,7 +187,7 @@ void WsjcppYamlNode::doMap() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_MAP; } else { - throw std::runtime_error(TAG + ": Element already defined as '" + this->getItemTypeAsString() + "'"); + throw std::runtime_error(TAG + ": Element already defined as '" + this->getNodeTypeAsString() + "'"); } } @@ -197,7 +197,7 @@ void WsjcppYamlNode::doValue() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { m_nItemType = WSJCPP_YAML_NODE_VALUE; } else { - throw std::runtime_error(TAG + ": Element already defined as '" + this->getItemTypeAsString() + "'"); + throw std::runtime_error(TAG + ": Element already defined as '" + this->getNodeTypeAsString() + "'"); } } @@ -413,15 +413,20 @@ WsjcppYamlNode *WsjcppYamlNode::getElement(int i) { // --------------------------------------------------------------------- -bool WsjcppYamlNode::appendElement(WsjcppYamlNode *pItem) { - if (pItem->isEmpty()) { - m_vObjects.push_back(pItem); // TODO clone object +bool WsjcppYamlNode::appendElement(WsjcppYamlNode *pNode) { + if (pNode->isEmpty()) { + m_vObjects.push_back(pNode); // TODO clone object return true; } if (m_nItemType != WSJCPP_YAML_NODE_ARRAY) { - throw std::runtime_error(TAG + ": appendElement, Element must be array for " + this->getForLogFormat()); - } - m_vObjects.push_back(pItem); // TODO clone object + throw std::runtime_error(TAG + ": appendElement, " + "tring add node \n" + " name='" + pNode->getName() + "'\n" + " type=" + pNode->getNodeTypeAsString() + "\n" + " line=" + std::to_string(pNode->getNumberOfLine()) + ")\n" + " To element (must be array) \n" + this->getForLogFormat()); + } + m_vObjects.push_back(pNode); // TODO clone object return true; } @@ -613,7 +618,7 @@ std::string WsjcppYamlNode::toString(std::string sIntent) { // --------------------------------------------------------------------- -std::string WsjcppYamlNode::getItemTypeAsString() { +std::string WsjcppYamlNode::getNodeTypeAsString() { if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) { return "undefined"; } else if (m_nItemType == WSJCPP_YAML_NODE_ARRAY) { @@ -666,6 +671,18 @@ int WsjcppYamlNode::getNodeIntent() { // --------------------------------------------------------------------- +int WsjcppYamlNode::getNumberOfLine() const { + return m_placeInFile.getNumberOfLine(); +} + +// --------------------------------------------------------------------- + +void WsjcppYamlNode::setNumberOfLine(int nNumberOfLine) { + m_placeInFile.setNumberOfLine(nNumberOfLine); +} + +// --------------------------------------------------------------------- + void WsjcppYamlNode::throw_error(const std::string &sError) { throw std::runtime_error(sError.c_str()); } @@ -1447,38 +1464,30 @@ void WsjcppYaml::process_hasName_emptyValue_arrayItem() { // --------------------------------------------------------------------- void WsjcppYaml::process_hasName_emptyValue_noArrayItem() { + // std::cout << "process_hasName_emptyValue_noArrayItem" << std::endl; if (m_parseLine.getIntent() == m_pParseCurrentParentNode->getNodeIntent()) { if (m_pParseCurrentParentNode->getParent() != nullptr) { m_pParseCurrentParentNode = m_pParseCurrentParentNode->getParent(); } } - // std::cout << "process_hasName_emptyValue_noArrayItem " << std::endl; WsjcppYamlNode *pNode = new WsjcppYamlNode( m_pParseCurrentParentNode, m_parsePlaceInFile, WSJCPP_YAML_NODE_UNDEFINED ); if (m_parseLine.getValueQuotes() != WSJCPP_YAML_QUOTES_NONE) { + // std::cout << "pNode->doValue() for '" << m_parseLine.getName() << "' value: '" << m_parseLine.getValue() << "' " << std::endl; pNode->doValue(); pNode->setValue(m_parseLine.getValue(), m_parseLine.getValueQuotes()); } - int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent; + // int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent; pNode->setName(m_parseLine.getName(), m_parseLine.getNameQuotes()); pNode->setComment(m_parseLine.getComment()); pNode->setNodeIntents(m_vStackDiffNodeIntents); - // std::cout << "current node [" << m_vStackDiffNodeIntents.back() << "]" << std::endl; - -/* if (nDiffIntent == 0 && m_pParseCurrentParentNode->isUndefined()) { - std::cout << "shit nDiffIntent = " << nDiffIntent << std::endl; - std::cout << "shit m_pParseCurrentParentNode->getName() = " << m_pParseCurrentParentNode->getName() << std::endl; - if (m_pParseCurrentParentNode->getParent() != nullptr) { - std::cout << "shit " - << " {" << m_pParseCurrentParentNode->getPlaceInFile().getLine() << "} " - << " {" << m_parsePlaceInFile.getLine() << "} " << std::endl; - } - } -*/ + m_pParseCurrentParentNode->setElement(m_parseLine.getName(), pNode); - m_pParseCurrentParentNode = pNode; + if (pNode->isUndefined()) { + m_pParseCurrentParentNode = pNode; + } } // --------------------------------------------------------------------- @@ -1488,11 +1497,18 @@ void WsjcppYaml::process_hasName_hasValue_arrayItem() { if (m_pParseCurrentParentNode->isUndefined()) { m_pParseCurrentParentNode->doArray(); } + // if (!m_pParseCurrentParentNode->isArray()) { + // std::cout << "m_pParseCurrentParentNode->getName(): " << m_pParseCurrentParentNode->getName() << std::endl; + // } + WsjcppYamlNode *pMapItem = new WsjcppYamlNode( m_pParseCurrentParentNode, m_parsePlaceInFile, WSJCPP_YAML_NODE_MAP ); + // std::cout << "m_parseLine.getName(): " << m_parseLine.getName() << std::endl; + m_pParseCurrentParentNode->appendElement(pMapItem); + // std::cout << "appended " << std::endl; m_pParseCurrentParentNode = pMapItem; pMapItem->setNodeIntents(m_vStackDiffNodeIntents); @@ -1513,6 +1529,7 @@ void WsjcppYaml::process_hasName_hasValue_arrayItem() { // --------------------------------------------------------------------- void WsjcppYaml::process_hasName_hasValue_noArrayItem() { + // std::cout << "process_hasName_hasValue_noArrayItem" << std::endl; WsjcppYamlNode *pNode = new WsjcppYamlNode( m_pParseCurrentParentNode, m_parsePlaceInFile, WSJCPP_YAML_NODE_VALUE @@ -1557,6 +1574,7 @@ void WsjcppYaml::process_emptyName_hasValue_noArrayItem() { // --------------------------------------------------------------------- void WsjcppYaml::process_emptyName_emptyValue_arrayItem() { + // std::cout << "process_emptyName_emptyValue_arrayItem" << std::endl; if (m_pParseCurrentParentNode->isUndefined()) { m_pParseCurrentParentNode->doArray(); } diff --git a/src/wsjcpp_yaml.h b/src/wsjcpp_yaml.h index d1723af..7723386 100644 --- a/src/wsjcpp_yaml.h +++ b/src/wsjcpp_yaml.h @@ -65,6 +65,11 @@ class WsjcppYamlNode { const WsjcppYamlPlaceInFile &placeInFile, WsjcppYamlNodeType nItemType ); + // WsjcppYamlNode( + // WsjcppYamlNode *pParent, + // WsjcppYamlNodeType nItemType + // const WsjcppYamlPlaceInFile &placeInFile, + // ); ~WsjcppYamlNode(); WsjcppYamlNode *getParent(); @@ -119,7 +124,7 @@ class WsjcppYamlNode { std::string getSerializedName(); std::string toString(std::string sIntent = ""); - std::string getItemTypeAsString(); + std::string getNodeTypeAsString(); std::string getForLogFormat(); int getNodeLastIntent(); @@ -127,6 +132,9 @@ class WsjcppYamlNode { void setNodeIntents(const std::vector & vNodeIntents); int getNodeIntent(); + int getNumberOfLine() const; + void setNumberOfLine(int nNumberOfLine); + private: void throw_error(const std::string &sError);