Skip to content

Commit c2b2636

Browse files
committed
Implemented
1 parent d706979 commit c2b2636

22 files changed

+679
-119
lines changed

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,38 @@ int main(int argc, char* argv[]) {
3737
WSJCppYAML yaml;
3838
if (yaml.loadFromString(
3939
"# yaml content\n"
40-
"yaml: nice format\n"
40+
"yaml1: nice format\n"
4141
"some-map: value1\n"
4242
"some-map2: value2\n"
4343
"some-array:\n"
4444
" - test1 \n"
4545
" - test2 \n"
46+
"some-am: # array + map element \n"
47+
" - p1: v1 \n"
48+
" p2: v2 \n"
49+
" - p1: v3 \n"
50+
" p2: v4 \n"
51+
"param2: 111\n"
4652
)) {
4753
WSJCppLog::throw_err(TAG, "Error parsing");
4854
return -1;
4955
}
5056

51-
std::cout << "yaml is " << yaml.getRoot()->getElement("yaml")->getValue() << std::endl;
52-
std::cout << "some-map is " << yaml.getRoot()->getElement("some-map")->getValue() << std::endl;
53-
std::cout << "some-map2 is " << yaml.getRoot()->getElement("some-map2")->getValue() << std::endl;
54-
std::cout << "some-array has " << std::to_string(yaml.getRoot()->getElement("some-array")->getLength()) << std::endl;
55-
std::cout << "some-array element 1 is " << yaml.getRoot()->getElement("some-array")->->getElement(0)->getValue() << std::endl;
56-
std::cout << "some-array element 2 is " << yaml.getRoot()->getElement("some-array")->->getElement(1)->getValue() << std::endl;
57+
std::cout << "yaml is " << yaml["yaml1"].getValue() << std::endl;
58+
std::cout << "some-map is " << yaml["some-map"].getValue() << std::endl;
59+
std::cout << "some-map2 is " << yaml["some-map2"].getValue() << std::endl;
60+
std::cout << "some-array has " << std::to_string(yaml["some-array"].getLength()) << std::endl;
61+
std::cout << "some-array element 0 is " << yaml["some-array"][0].getValue() << std::endl;
62+
std::cout << "some-array element 1 is " << yaml["some-array"][1].getValue() << std::endl;
63+
std::cout << "some-am has " << std::to_string(yaml["some-am"].getLength()) << std::endl;
64+
std::cout << "some-am is array: " << (yaml["some-am"].isArray() ? "yes" : "no") << std::endl;
65+
std::cout << "some-am has comment " << yaml["some-am"].getComment() << std::endl;
66+
std::cout << "some-am element 0 : p1 is " << yaml["some-am"][0]["p1"].getValue() << std::endl;
67+
std::cout << "some-am element 0 : p2 is " << yaml["some-am"][0]["p2"].getValue() << std::endl;
68+
std::cout << "some-am element 1 : p1 is " << yaml["some-am"][1]["p1"].getValue() << std::endl;
69+
std::cout << "some-am element 1 : p2 is " << yaml["some-am"][1]["p2"].getValue() << std::endl;
70+
5771
return 0;
5872
}
5973

60-
```
74+
```

src.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Automaticly generated by wsjcpp-yaml@0.0.1
1+
# Automaticly generated by wsjcpp-yaml@0.0.2
22

3-
add_definitions(-DWSJCPP_VERSION="0.0.1")
3+
add_definitions(-DWSJCPP_VERSION="0.0.2")
44
add_definitions(-DWSJCPP_NAME="wsjcpp-yaml")
55

66
# set(CMAKE_AUTOMOC ON)

src.wsjcpp/wsjcpp-core/wsjcpp_core.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <thread>
1616
#include <cstdint>
1717
#include <unistd.h>
18+
#include <streambuf>
1819

1920
// ---------------------------------------------------------------------
2021

@@ -282,7 +283,7 @@ bool WSJCppCore::makeDir(const std::string &sDirname) {
282283

283284
bool WSJCppCore::writeFile(const std::string &sFilename, const std::string &sContent) {
284285

285-
std::ofstream f(sFilename, std::ios::out);
286+
std::ofstream f(sFilename, std::ifstream::in);
286287
if (!f) {
287288
std::cout << "FAILED could not create file to wtite " << sFilename << std::endl;
288289
return false;
@@ -295,6 +296,24 @@ bool WSJCppCore::writeFile(const std::string &sFilename, const std::string &sCon
295296

296297
// ---------------------------------------------------------------------
297298

299+
bool WSJCppCore::readTextFile(const std::string &sFilename, std::string &sContent) {
300+
301+
std::ifstream f(sFilename);
302+
if (!f) {
303+
std::cout << "FAILED could not open file to read " << sFilename << std::endl;
304+
return false;
305+
}
306+
307+
sContent = std::string(
308+
(std::istreambuf_iterator<char>(f)),
309+
std::istreambuf_iterator<char>()
310+
);
311+
312+
return true;
313+
}
314+
315+
// ---------------------------------------------------------------------
316+
298317
bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize) {
299318
std::ofstream f(sFilename, std::ios::out | std::ios::binary);
300319
if (!f) {
@@ -307,6 +326,7 @@ bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, co
307326
}
308327

309328

329+
310330
// ---------------------------------------------------------------------
311331

312332
std::string& WSJCppCore::ltrim(std::string& str, const std::string& chars) {

src.wsjcpp/wsjcpp-core/wsjcpp_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class WSJCppCore {
3636
static std::vector<std::string> listOfFiles(const std::string &sDirname);
3737
static bool makeDir(const std::string &sDirname);
3838
static bool writeFile(const std::string &sFilename, const std::string &sContent);
39+
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
3940
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
41+
4042

4143
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4244
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");

src/main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ int main(int argc, char* argv[]) {
1414
if (!WSJCppCore::dirExists(appLogPath)) {
1515
WSJCppCore::makeDir(appLogPath);
1616
}
17-
18-
// TODO load and check yaml format
17+
WSJCppYAML yaml;
18+
if (!yaml.loadFromFile("./wsjcpp.yml")) {
19+
WSJCppLog::err(TAG, "Could not read data from file");
20+
return -1;
21+
}
22+
23+
if (!yaml.saveToFile("./wsjcpp.yml")) {
24+
WSJCppLog::err(TAG, "Could not save data to file");
25+
return -1;
26+
}
1927

2028
return 0;
21-
}
29+
}

0 commit comments

Comments
 (0)