Skip to content

Commit b80b175

Browse files
committed
Updated README and added yaml parser simple map
1 parent 65cd44d commit b80b175

File tree

5 files changed

+134
-6
lines changed

5 files changed

+134
-6
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,59 @@
33
[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-yaml.svg?branch=master)](https://api.travis-ci.com/wsjcpp/wsjcpp-yaml) [![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-yaml.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-yaml/stargazers) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-yaml.svg)](https://github.com/wsjcpp/wsjcpp-yaml/) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-yaml.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-yaml/network/members)
44

55
C++ Write/Reader yaml files
6+
7+
8+
## Integrate to your c++ project
9+
10+
include files:
11+
12+
- src/wsjcpp_yaml.cpp
13+
- src/wsjcpp_yaml.h
14+
- src.wsjcpp/wsjcpp-logger/fallen.h
15+
- src.wsjcpp/wsjcpp-logger/fallen.cpp
16+
17+
In you main file configure logger:
18+
19+
```cpp
20+
21+
#include <string.h>
22+
#include <iostream>
23+
#include "wsjcpp_yaml.h"
24+
#include "fallen.h"
25+
26+
27+
28+
int main(int argc, char* argv[]) {
29+
std::string TAG = "MAIN";
30+
std::string appLogPath = ".logs";
31+
Log::setLogDirectory(appLogPath);
32+
if (!Fallen::dirExists(appLogPath)) {
33+
Fallen::makeDir(appLogPath);
34+
}
35+
Log::info(TAG, "Hello!");
36+
37+
// now you can use WSJCppYAML
38+
WSJCppYAML yaml;
39+
if (yaml.loadFromString(
40+
"# yaml content\n"
41+
"yaml: nice format\n"
42+
"some-map: value1\n"
43+
"some-map2: value2\n"
44+
"some-array:\n"
45+
" - test1 \n"
46+
" - test2 \n"
47+
)) {
48+
Log::throw_err(TAG, "Error parsing");
49+
return -1;
50+
}
51+
52+
std::cout << "yaml is " << yaml.getRoot()->getElement("yaml")->getValue() << std::endl;
53+
std::cout << "some-map is " << yaml.getRoot()->getElement("some-map")->getValue() << std::endl;
54+
std::cout << "some-map2 is " << yaml.getRoot()->getElement("some-map2")->getValue() << std::endl;
55+
std::cout << "some-array has " << std::to_string(yaml.getRoot()->getElement("some-array")->getLength()) << std::endl;
56+
std::cout << "some-array element 1 is " << yaml.getRoot()->getElement("some-array")->->getElement(0)->getValue() << std::endl;
57+
std::cout << "some-array element 2 is " << yaml.getRoot()->getElement("some-array")->->getElement(1)->getValue() << std::endl;
58+
return 0;
59+
}
60+
61+
```

src/wsjcpp_yaml.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,15 @@ std::string WSJCppYAMLItem::toString() {
240240
if (this->isValue()) {
241241
sRet = m_sValue;
242242
if (m_sComment.length() > 0) {
243-
sRet += " #" + m_sComment;
243+
if (sRet.length() > 0) {
244+
sRet += " ";
245+
}
246+
sRet += "#" + m_sComment;
244247
}
245248
} else if (this->isEmpty()) {
246-
sRet = "#" + m_sComment;
249+
if (m_sComment.length() > 0) {
250+
sRet += "#" + m_sComment;
251+
}
247252
} else if (this->isArray()) {
248253
sRet += "\n";
249254
for (int i = 0; i < m_vObjects.size(); i++) {
@@ -511,7 +516,7 @@ bool WSJCppYAML::loadFromString(std::string &sBuffer) {
511516

512517
// ---------------------------------------------------------------------
513518

514-
bool WSJCppYAML::saveToString(std::string &sBuffer) {
519+
bool WSJCppYAML::saveToString(std::string &sBuffer) { // TODO move to fallen
515520
sBuffer = m_pRoot->toString();
516521
return true;
517522
}
@@ -553,7 +558,7 @@ bool WSJCppYAML::parse(const std::string &sBuffer) {
553558

554559
for (int nLine = 0; nLine < vLines.size(); nLine++) {
555560
st.sLine = vLines[nLine];
556-
Log::info(TAG, "Line(" + std::to_string(nLine) + ") '" + st.sLine + "'");
561+
// Log::info(TAG, "Line(" + std::to_string(nLine) + ") '" + st.sLine + "'");
557562
st.nLine = nLine;
558563
st.line = WSJCppYAMLParsebleLine(nLine);
559564
st.line.parseLine(st.sLine);

unit-tests/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ list (APPEND WSJCPP_SOURCES "./src/unit_tests.h")
2929
list (APPEND WSJCPP_SOURCES "./src/unit_tests.cpp")
3030
list (APPEND WSJCPP_SOURCES "./src/unit_test_line_parser.h")
3131
list (APPEND WSJCPP_SOURCES "./src/unit_test_line_parser.cpp")
32-
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.h")
33-
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.cpp")
32+
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser_simple_map.h")
33+
list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser_simple_map.cpp")
34+
# list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.h")
35+
# list (APPEND WSJCPP_SOURCES "./src/unit_test_yaml_parser.cpp")
36+
3437
list (APPEND WSJCPP_SOURCES "./src/main.cpp")
3538

3639
include_directories(${WSJCPP_INCLUDE_DIRS})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "unit_test_yaml_parser_simple_map.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <fallen.h>
5+
#include <wsjcpp_yaml.h>
6+
7+
REGISTRY_UNIT_TEST(UnitTestYamlParserSimpleMap)
8+
9+
UnitTestYamlParserSimpleMap::UnitTestYamlParserSimpleMap()
10+
: UnitTestBase("UnitTestYamlParserSimpleMap") {
11+
//
12+
}
13+
14+
// ---------------------------------------------------------------------
15+
16+
void UnitTestYamlParserSimpleMap::init() {
17+
// nothing
18+
}
19+
20+
// ---------------------------------------------------------------------
21+
22+
bool UnitTestYamlParserSimpleMap::run() {
23+
24+
std::string g_sTestYaml =
25+
"# Some comment 1\n"
26+
"param1: value1\n"
27+
"param2: value2 # some comment 2\n"
28+
"\n" // empty line
29+
;
30+
31+
bool bTestSuccess = true;
32+
33+
WSJCppYAML yaml;
34+
if (yaml.loadFromString(g_sTestYaml)) {
35+
Log::throw_err(TAG, "Error parsing");
36+
return -1;
37+
}
38+
39+
WSJCppYAMLItem *pItem = nullptr;
40+
compareS(bTestSuccess, "param1", yaml.getRoot()->getElement("param1")->getValue(), "value1");
41+
compareS(bTestSuccess, "param2", yaml.getRoot()->getElement("param2")->getValue(), "value2");
42+
43+
std::string sSaved = "";
44+
if (yaml.saveToString(sSaved)) {
45+
compareS(bTestSuccess, "yaml_save", sSaved, g_sTestYaml);
46+
} else {
47+
Log::err(TAG, "Could not save to string");
48+
bTestSuccess = false;
49+
}
50+
return bTestSuccess;
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_YAML_PARSER_SIMPLE_MAP_H
2+
#define UNIT_TEST_YAML_PARSER_SIMPLE_MAP_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestYamlParserSimpleMap : public UnitTestBase {
7+
public:
8+
UnitTestYamlParserSimpleMap();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_YAML_PARSER_SIMPLE_MAP_H

0 commit comments

Comments
 (0)