Skip to content

Commit 14b07cc

Browse files
committed
Added operator= std::string and added examples
1 parent 6b1ed8a commit 14b07cc

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ list (APPEND WSJCPP_INCLUDE_DIRS "src")
1919
list (APPEND WSJCPP_SOURCES "src/wsjcpp_yaml.h")
2020
list (APPEND WSJCPP_SOURCES "src/wsjcpp_yaml.cpp")
2121
list (APPEND WSJCPP_SOURCES "src/main.cpp")
22+
list (APPEND WSJCPP_SOURCES "src/examples/example_iterate_array.cpp")
23+
list (APPEND WSJCPP_SOURCES "src/examples/example_operators.cpp")
2224

2325
include_directories(${WSJCPP_INCLUDE_DIRS})
2426

src/examples.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <iostream>
2+
3+
void example_iterate_array();
4+
void example_operator_1();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "examples.h"
2+
3+
#include "wsjcpp_yaml.h"
4+
5+
void example_iterate_array() {
6+
7+
std::string sTest =
8+
"'2021-10-08':\n"
9+
" - code: '300209'\n"
10+
" focusstr: ''\n"
11+
" - code: '300047'\n"
12+
" focusstr: ''\n"
13+
" - code: '300970'\n"
14+
" focusstr: ''\n"
15+
" - code: '300288'\n"
16+
" focusstr: ''\n"
17+
"'2021-10-11':\n"
18+
" - code: '300162'\n"
19+
" focusstr: ''\n"
20+
" - code: '300209'\n"
21+
" focusstr: ''\n"
22+
" - code: '300972'\n"
23+
" focusstr: ''\n"
24+
" - code: '300159'\n"
25+
" focusstr: ''\n"
26+
;
27+
28+
WsjcppYaml yaml2;
29+
std::string sError;
30+
if (!yaml2.loadFromString("test", sTest, sError)) {
31+
std::cerr << sError << std::endl;
32+
return;
33+
}
34+
WsjcppYamlCursor cur = yaml2.getCursor();
35+
std::vector<std::string> vKeys = cur.keys();
36+
for (int i = 0; i < vKeys.size(); i++) {
37+
std::string sKey = vKeys[i];
38+
int nCount = cur[sKey].size(); // musts array
39+
for (int n = 0; n < nCount; n++) {
40+
WsjcppYamlCursor el = cur[sKey][n];
41+
std::cout
42+
<< "code = " << el["code"].valInt()
43+
<< "; focusstr = " << el["focusstr"].valStr()
44+
<< ";"
45+
<< std::endl;
46+
}
47+
}
48+
}

src/examples/example_operators.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "examples.h"
2+
3+
#include "wsjcpp_yaml.h"
4+
5+
void example_operator_1() {
6+
7+
std::string sTest =
8+
"test: 'string'\n"
9+
;
10+
11+
WsjcppYaml yaml;
12+
std::string sError;
13+
if (!yaml.loadFromString("test", sTest, sError)) {
14+
std::cerr << sError << std::endl;
15+
return;
16+
}
17+
WsjcppYamlCursor cur = yaml.getCursor();
18+
std::cout << "before : " << cur["test"].valStr() << std::endl;
19+
cur["test"] = "some new string";
20+
std::cout << "after : " << cur["test"].valStr() << std::endl;
21+
22+
}

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <string.h>
22
#include <iostream>
33
#include <algorithm>
4+
#include "examples.h"
45
#include "wsjcpp_yaml.h"
6+
57
// #include "wsjcpp_core.h"
68

79
class MyLogger : public IWsjcppYamlLog {
@@ -53,7 +55,5 @@ int main(int argc, char* argv[]) {
5355
return -1;
5456
}
5557
std::cout << "Done." << std::endl;
56-
57-
5858
return 0;
5959
}

src/wsjcpp_yaml.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ class WsjcppYamlCursor {
259259

260260
WsjcppYamlCursor operator[](int idx) const;
261261
WsjcppYamlCursor operator[](const std::string &sName) const;
262+
WsjcppYamlCursor& operator=(const std::string &sVal) noexcept {
263+
return this->val(sVal);
264+
}
262265

263266
private:
264267
std::string TAG;

0 commit comments

Comments
 (0)