Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src.wsjcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
81 changes: 53 additions & 28 deletions src/wsjcpp_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "'");
}
}

Expand All @@ -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() + "'");
}
}

Expand All @@ -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() + "'");
}
}

Expand All @@ -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() + "'");
}
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -1191,6 +1208,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));
Expand Down Expand Up @@ -1240,9 +1263,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;
Expand All @@ -1256,7 +1280,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;
}
Expand Down Expand Up @@ -1440,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;
}
}

// ---------------------------------------------------------------------
Expand All @@ -1481,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);

Expand All @@ -1506,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
Expand Down Expand Up @@ -1550,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();
}
Expand Down
16 changes: 13 additions & 3 deletions src/wsjcpp_yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class WsjcppYamlNode {
const WsjcppYamlPlaceInFile &placeInFile,
WsjcppYamlNodeType nItemType
);
// WsjcppYamlNode(
// WsjcppYamlNode *pParent,
// WsjcppYamlNodeType nItemType
// const WsjcppYamlPlaceInFile &placeInFile,
// );
~WsjcppYamlNode();
WsjcppYamlNode *getParent();

Expand Down Expand Up @@ -119,14 +124,17 @@ class WsjcppYamlNode {

std::string getSerializedName();
std::string toString(std::string sIntent = "");
std::string getItemTypeAsString();
std::string getNodeTypeAsString();

std::string getForLogFormat();
int getNodeLastIntent();
std::string getStringNodeLastIntent();
void setNodeIntents(const std::vector<int> & vNodeIntents);
int getNodeIntent();

int getNumberOfLine() const;
void setNumberOfLine(int nNumberOfLine);

private:
void throw_error(const std::string &sError);

Expand Down Expand Up @@ -235,6 +243,8 @@ class WsjcppYamlCursor {
bool valBool();
WsjcppYamlCursor &val(bool bValue);

// node
WsjcppYamlNode *node();

WsjcppYamlCursor operator[](int idx) const;
WsjcppYamlCursor operator[](const std::string &sName) const;
Expand All @@ -253,9 +263,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;
Expand Down
4 changes: 3 additions & 1 deletion unit-tests.wsjcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down
91 changes: 91 additions & 0 deletions unit-tests.wsjcpp/data-tests/read-wsjcpp-hold-yml/wsjcpp.hold.yml
Original file line number Diff line number Diff line change
@@ -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: ""
Loading