-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsjcpp_yaml.h
More file actions
377 lines (308 loc) · 13.1 KB
/
wsjcpp_yaml.h
File metadata and controls
377 lines (308 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
MIT License
Copyright (c) 2019-2025 wsjcpp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
*/
#ifndef WSJCPP_YAML_H
#define WSJCPP_YAML_H
#include <iostream>
#include <vector>
#include <cstddef>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum WsjcppYamlNodeType {
WSJCPP_YAML_NODE_UNDEFINED = 0,
WSJCPP_YAML_NODE_EMPTY = 1,
WSJCPP_YAML_NODE_ARRAY = 2,
WSJCPP_YAML_NODE_MAP = 3,
WSJCPP_YAML_NODE_VALUE = 4
};
enum WsjcppYamlQuotes {
WSJCPP_YAML_QUOTES_NONE,
WSJCPP_YAML_QUOTES_DOUBLE,
WSJCPP_YAML_QUOTES_SINGLE
};
class WsjcppYamlPlaceInFile {
public:
WsjcppYamlPlaceInFile();
WsjcppYamlPlaceInFile(const std::string &sFilename, int nNumberOfLine, const std::string &sLine);
std::string getFilename() const;
void setFilename(const std::string &sFilename);
int getNumberOfLine() const;
void setNumberOfLine(int nNumberOfLine);
std::string getLine() const;
void setLine(const std::string &sLine);
std::string getForLogFormat();
private:
std::string m_sFilename;
int m_nNumberOfLine;
std::string m_sLine;
};
// ---------------------------------------------------------------------
// WsjcppYamlQuotes
class IWsjcppYamlLog {
public:
virtual void err(const std::string &TAG, const std::string &sMessage) = 0;
virtual void throw_err(const std::string &TAG, const std::string &sMessage) = 0;
virtual void warn(const std::string &TAG, const std::string &sMessage) = 0;
virtual void info(const std::string &TAG, const std::string &sMessage) = 0;
};
// ---------------------------------------------------------------------
/*!
\brief Class for keep data of yaml node
Basic class for yaml tree
*/
class WsjcppYamlNode {
public:
WsjcppYamlNode(
WsjcppYamlNode *pParent,
IWsjcppYamlLog *pLog,
const WsjcppYamlPlaceInFile &placeInFile,
WsjcppYamlNodeType nItemType
);
// WsjcppYamlNode(
// WsjcppYamlNode *pParent,
// WsjcppYamlNodeType nItemType
// const WsjcppYamlPlaceInFile &placeInFile,
// );
~WsjcppYamlNode();
WsjcppYamlNode *getParent();
WsjcppYamlPlaceInFile getPlaceInFile();
void setPlaceInFile(const WsjcppYamlPlaceInFile &placeInFile);
void setComment(const std::string &sComment);
std::string getComment();
void setName(const std::string &sName, WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE);
std::string getName();
WsjcppYamlQuotes getNameQuotes();
bool isEmpty();
void doEmpty();
bool isUndefined();
void doArray();
void doMap();
void doValue();
bool isMap();
bool hasElement(const std::string &sName);
WsjcppYamlNode *getElement(const std::string &sName);
bool setElement(const std::string &sName, WsjcppYamlNode *pItem);
bool removeElement(const std::string &sName);
std::vector<std::string> getKeys();
bool setElementValue(
const std::string &sName, const std::string &sValue,
WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE,
WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE
);
bool createElementMap(const std::string &sName, WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE);
WsjcppYamlNode *createElementMap();
bool createElementArray(const std::string &sName, WsjcppYamlQuotes nNameQuotes = WSJCPP_YAML_QUOTES_NONE);
bool isArray();
int getLength();
WsjcppYamlNode *getElement(int i);
bool appendElement(WsjcppYamlNode *pItem);
bool appendElementValue(const std::string &sValue, WsjcppYamlQuotes nValueQuotes = WSJCPP_YAML_QUOTES_NONE);
bool removeElement(int i);
bool isValue();
std::string getValue(); // contains only strings
void setValue(const std::string &sValue, WsjcppYamlQuotes nQuotes = WSJCPP_YAML_QUOTES_NONE);
WsjcppYamlQuotes getValueQuotes();
std::string getSerializedName();
std::string toString(std::string sIndent = "");
std::string getNodeTypeAsString();
std::string getForLogFormat();
int getNodeLastIndent();
std::string getStringNodeLastIndent();
void setNodeIndents(const std::vector<int> & vNodeIndents);
int getNodeIndent();
int getNumberOfLine() const;
void setNumberOfLine(int nNumberOfLine);
private:
void throw_error(const std::string &sError);
void removeLastCharNewLine(std::string &sLine);
std::string escapingString(const std::string &sVal);
bool hasContent(const std::string &sVal);
bool hasObjects();
std::string TAG;
IWsjcppYamlLog *m_pLog;
WsjcppYamlNode *m_pParent;
WsjcppYamlPlaceInFile m_placeInFile;
WsjcppYamlNodeType m_nItemType;
std::vector<WsjcppYamlNode *> m_vObjects;
std::string m_sValue; // if it is not array or map
WsjcppYamlQuotes m_nValueQuotes;
std::string m_sName;
WsjcppYamlQuotes m_nNameQuotes;
std::string m_sComment;
int m_nNodeDiffIndent;
std::string m_sNodeDiffIndent;
int m_nNodeIndent;
};
// ---------------------------------------------------------------------
class WsjcppYamlParsebleLine {
public:
WsjcppYamlParsebleLine(int nLine);
WsjcppYamlParsebleLine();
int getLineNumber();
std::string getPrefix();
int getIndent(); // prefix length
bool isArrayItem();
std::string getComment();
bool hasComment();
std::string getName();
WsjcppYamlQuotes getNameQuotes();
bool isEmptyName();
std::string getValue();
WsjcppYamlQuotes getValueQuotes();
bool isEmptyValue();
bool isEmptyLine();
bool parseLine(const std::string &sLine, std::string &sError);
private:
void initInstance(int nLine);
std::string TAG;
int m_nLineNumber;
std::string m_sPrefix;
bool m_bArrayItem;
std::string m_sComment;
std::string m_sTagName;
std::string m_sValue;
WsjcppYamlQuotes m_nNameQuotes;
WsjcppYamlQuotes m_nValueQuotes;
bool m_bHasComment;
bool m_bEmptyLine;
bool canTagName(const std::string &sVal);
std::string removeStringDoubleQuotes(const std::string &sValue);
std::string removeStringSingleQuotes(const std::string &sValue);
};
// ---------------------------------------------------------------------
class WsjcppYamlCursor {
public:
WsjcppYamlCursor(WsjcppYamlNode *pCurrentNode);
WsjcppYamlCursor();
~WsjcppYamlCursor();
// null or undefined
bool isNull() const;
// isUndefined
bool isUndefined() const;
// value
bool isValue() const;
// array
bool isArray() const;
size_t size() const;
// WsjcppYamlCursor &push(const std::string &sVal);
// WsjcppYamlCursor &push(int nVal);
// WsjcppYamlCursor &push(bool bVal);
// WsjcppYamlCursor &remove(int nIdx);
// map
bool isMap() const;
std::vector<std::string> keys() const;
bool hasKey(const std::string &sKey) const;
// WsjcppYamlCursor &set(const std::string &sName, const std::string &sValue);
// WsjcppYamlCursor &set(const std::string &sName, int nValue);
// WsjcppYamlCursor &set(const std::string &sName, bool bValue);
// WsjcppYamlCursor &remove(const std::string &sKey);
// comment
std::string comment();
WsjcppYamlCursor &comment(const std::string& sComment);
// val
std::string valStr() const;
WsjcppYamlCursor &val(const std::string &sValue);
WsjcppYamlCursor &val(const char *sValue);
int valInt() const;
WsjcppYamlCursor &val(int nValue);
float valFloat() const;
WsjcppYamlCursor &val(float nValue);
double valDouble() const;
WsjcppYamlCursor &val(double nValue);
bool valBool() const;
WsjcppYamlCursor &val(bool bValue);
// node
WsjcppYamlNode *node();
std::string getCurrentNodePath();
operator int() const { return int(valInt()); };
// explicit operator bool() const { return valBool(); }
operator std::string() const { return valStr(); };
WsjcppYamlCursor operator[](int idx) const;
WsjcppYamlCursor operator[](const std::string &sName) const;
WsjcppYamlCursor operator[](const char *sName) const;
WsjcppYamlCursor& operator=(const char *sVal);
WsjcppYamlCursor& operator=(const std::string &sVal);
WsjcppYamlCursor& operator=(const int &nVal);
WsjcppYamlCursor& operator=(const bool &bVal);
private:
void initInstance(WsjcppYamlNode *pCurrentNode);
std::string TAG;
WsjcppYamlNode *m_pCurrentNode;
};
class WsjcppYaml : public IWsjcppYamlLog {
public:
WsjcppYaml();
~WsjcppYaml();
void clear();
void setLogger(IWsjcppYamlLog *pLog);
bool loadFromFile(const std::string &sFileName, std::string &sError);
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, std::string &sError);
WsjcppYamlNode *getRoot();
WsjcppYamlCursor getCursor() const;
WsjcppYamlCursor operator[](int idx) const;
WsjcppYamlCursor operator[](const std::string &sName) const;
// copy functions from WsjcppCore
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent, std::string &sError);
static bool writeFile(const std::string &sFilename, const std::string &sContent);
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
static std::string toLower(const std::string &str);
// IWsjcppYamlLog
#if defined(__CODEGEARC__) && !defined(_WIN64)
virtual void err(const std::string &TAG, const std::string &sMessage);
virtual void throw_err(const std::string &TAG, const std::string &sMessage);
virtual void warn(const std::string &TAG, const std::string &sMessage);
virtual void info(const std::string &TAG, const std::string &sMessage);
#else
virtual void err(const std::string &TAG, const std::string &sMessage) override;
virtual void throw_err(const std::string &TAG, const std::string &sMessage) override;
virtual void warn(const std::string &TAG, const std::string &sMessage) override;
virtual void info(const std::string &TAG, const std::string &sMessage) override;
#endif
private:
std::string TAG;
IWsjcppYamlLog *m_pLog;
// TODO replace to WsjcppCore::split()
std::vector<std::string> splitToLines(const std::string &sBuffer);
bool parse(const std::string &sFileName, const std::string &sBuffer, std::string &sError);
void process_hasName_emptyValue_arrayItem();
void process_hasName_emptyValue_noArrayItem();
void process_hasName_hasValue_arrayItem();
void process_hasName_hasValue_noArrayItem();
void process_emptyName_hasValue_arrayItem();
void process_emptyName_hasValue_noArrayItem();
void process_emptyName_emptyValue_arrayItem();
void process_emptyName_emptyValue_noArrayItem();
std::vector<std::string> m_sLines;
WsjcppYamlNode *m_pRoot;
// prsing line status
void logUnknownParseLine();
WsjcppYamlNode *m_pParseCurrentParentNode;
int m_nParseCurrentIndent;
WsjcppYamlPlaceInFile m_parsePlaceInFile;
WsjcppYamlParsebleLine m_parseLine;
std::vector<int> m_vStackDiffNodeIndents;
};
#endif // WSJCPP_YAML_H