Skip to content

Commit f0c0364

Browse files
committed
Redesign line keep place in file
1 parent c2b2636 commit f0c0364

File tree

4 files changed

+195
-62
lines changed

4 files changed

+195
-62
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 139 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,81 @@
22
#include "wsjcpp_yaml.h"
33
#include <wsjcpp_core.h>
44

5+
// ---------------------------------------------------------------------
6+
// WSJCppYAMLPlaceInFile
7+
8+
WSJCppYAMLPlaceInFile::WSJCppYAMLPlaceInFile() {
9+
m_sFilename = "";
10+
m_nNumberOfLine = 0;
11+
m_sLine = "";
12+
}
13+
14+
// ---------------------------------------------------------------------
15+
16+
WSJCppYAMLPlaceInFile::WSJCppYAMLPlaceInFile(const std::string &sFilename, int nNumberOfLine, const std::string &sLine) {
17+
m_sFilename = sFilename;
18+
m_nNumberOfLine = nNumberOfLine;
19+
m_sLine = sLine;
20+
}
21+
22+
// ---------------------------------------------------------------------
23+
24+
std::string WSJCppYAMLPlaceInFile::getFilename() const {
25+
return m_sFilename;
26+
}
27+
28+
// ---------------------------------------------------------------------
29+
30+
void WSJCppYAMLPlaceInFile::setFilename(const std::string &sFilename) {
31+
m_sFilename = sFilename;
32+
}
33+
34+
// ---------------------------------------------------------------------
35+
36+
int WSJCppYAMLPlaceInFile::getNumberOfLine() const {
37+
return m_nNumberOfLine;
38+
}
39+
40+
// ---------------------------------------------------------------------
41+
42+
void WSJCppYAMLPlaceInFile::setNumberOfLine(int nNumberOfLine) {
43+
m_nNumberOfLine = nNumberOfLine;
44+
}
45+
46+
// ---------------------------------------------------------------------
47+
48+
std::string WSJCppYAMLPlaceInFile::getLine() const {
49+
return m_sLine;
50+
}
51+
52+
// ---------------------------------------------------------------------
53+
54+
void WSJCppYAMLPlaceInFile::setLine(const std::string &sLine) {
55+
m_sLine = sLine;
56+
}
57+
58+
// ---------------------------------------------------------------------
59+
60+
std::string WSJCppYAMLPlaceInFile::getForLogFormat() {
61+
return "(" + m_sFilename + ":" + std::to_string(m_nNumberOfLine) + "): " + m_sLine;
62+
}
63+
564
// ---------------------------------------------------------------------
665
// WSJCppYAMLItem
766

867
WSJCppYAMLItem::WSJCppYAMLItem(
968
WSJCppYAMLItem *pParent,
10-
int nOriginalNumberOfLine,
11-
const std::string &sOriginalLine,
69+
const WSJCppYAMLPlaceInFile &placeInFile,
1270
WSJCppYAMLItemType nItemType
1371
) {
1472
m_pParent = pParent;
15-
m_nOriginalNumberOfLine = nOriginalNumberOfLine;
16-
m_sOriginalLine = sOriginalLine;
73+
m_placeInFile.setFilename(placeInFile.getFilename());
74+
m_placeInFile.setLine(placeInFile.getLine());
75+
m_placeInFile.setNumberOfLine(placeInFile.getNumberOfLine());
1776
m_nItemType = nItemType;
1877
m_bValueHasDoubleQuotes = false;
1978
m_bNameHasDoubleQuotes = false;
20-
TAG = "WSJCppYAMLItem(line:" + std::to_string(m_nOriginalNumberOfLine) + ":'" + sOriginalLine + "')";
79+
TAG = "WSJCppYAMLNode";
2180
}
2281

2382
// ---------------------------------------------------------------------
@@ -38,12 +97,16 @@ WSJCppYAMLItem *WSJCppYAMLItem::getParent() {
3897

3998
// ---------------------------------------------------------------------
4099

41-
std::string WSJCppYAMLItem::getOriginalLine() {
42-
return m_sOriginalLine;
100+
WSJCppYAMLPlaceInFile WSJCppYAMLItem::getPlaceInFile() {
101+
return m_placeInFile;
43102
}
44103

45-
int WSJCppYAMLItem::getOriginalNumberOfLine() {
46-
return m_nOriginalNumberOfLine;
104+
// ---------------------------------------------------------------------
105+
106+
void WSJCppYAMLItem::setPlaceInFile(const WSJCppYAMLPlaceInFile &placeInFile) {
107+
m_placeInFile.setFilename(placeInFile.getFilename());
108+
m_placeInFile.setLine(placeInFile.getLine());
109+
m_placeInFile.setNumberOfLine(placeInFile.getNumberOfLine());
47110
}
48111

49112
// ---------------------------------------------------------------------
@@ -173,7 +236,7 @@ bool WSJCppYAMLItem::setElement(const std::string &sName, WSJCppYAMLItem *pItem)
173236
}
174237

175238
if (m_nItemType != WSJCPP_YAML_ITEM_MAP) {
176-
WSJCppLog::throw_err(TAG, "setElement, Element must be 'map' for line(" + std::to_string(pItem->getOriginalNumberOfLine()) + "): '" + pItem->getOriginalLine() + "'");
239+
WSJCppLog::throw_err(TAG, "setElement, Element must be 'map' for line(" + std::to_string(pItem->getPlaceInFile().getNumberOfLine()) + "): '" + pItem->getPlaceInFile().getLine() + "'");
177240
}
178241

179242
if (this->hasElement(sName)) { // TODO remove previous element
@@ -195,6 +258,23 @@ bool WSJCppYAMLItem::removeElement(const std::string &sName) {
195258

196259
// ---------------------------------------------------------------------
197260

261+
std::vector<std::string> WSJCppYAMLItem::getKeys() {
262+
if (m_nItemType != WSJCPP_YAML_ITEM_MAP) {
263+
WSJCppLog::throw_err(TAG, "getKeys: Element must be map");
264+
}
265+
std::vector<std::string> vKeys;
266+
for (int i = 0; i < m_vObjects.size(); i++) {
267+
WSJCppYAMLItem *pItem = m_vObjects[i];
268+
if (pItem->isValue() || pItem->isMap() || pItem->isArray()) {
269+
std::string sName = pItem->getName();
270+
vKeys.push_back(sName);
271+
}
272+
}
273+
return vKeys;
274+
}
275+
276+
// ---------------------------------------------------------------------
277+
198278
bool WSJCppYAMLItem::isArray() {
199279
return m_nItemType == WSJCPP_YAML_ITEM_ARRAY;
200280
}
@@ -232,7 +312,7 @@ WSJCppYAMLItem *WSJCppYAMLItem::getElement(int i) {
232312
}
233313
}
234314
if (pItem == nullptr) {
235-
WSJCppLog::throw_err(TAG, "getElement(" + std::to_string(i) + "), Out of range in array for '" + this->getOriginalLine() + "'");
315+
WSJCppLog::throw_err(TAG, "getElement(" + std::to_string(i) + "), Out of range in array for '" + this->getPlaceInFile().getLine() + "'");
236316
}
237317
return pItem;
238318
}
@@ -245,7 +325,7 @@ bool WSJCppYAMLItem::appendElement(WSJCppYAMLItem *pItem) {
245325
return true;
246326
}
247327
if (m_nItemType != WSJCPP_YAML_ITEM_ARRAY) {
248-
WSJCppLog::throw_err(TAG, "appendElement, Element must be array for line(" + std::to_string(pItem->getOriginalNumberOfLine()) + "): '" + pItem->getOriginalLine() + "'");
328+
WSJCppLog::throw_err(TAG, "appendElement, Element must be array for " + this->getForLogFormat());
249329
}
250330
m_vObjects.push_back(pItem); // TODO clone object
251331
return true;
@@ -261,7 +341,7 @@ bool WSJCppYAMLItem::isValue() {
261341

262342
std::string WSJCppYAMLItem::getValue() {
263343
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
264-
WSJCppLog::throw_err(TAG, "getValue, Element must be value for line");
344+
WSJCppLog::throw_err(TAG, "getValue, Element must be value for " + this->getForLogFormat());
265345
}
266346
return m_sValue;
267347
}
@@ -270,7 +350,7 @@ std::string WSJCppYAMLItem::getValue() {
270350

271351
void WSJCppYAMLItem::setValue(const std::string &sValue, bool bHasQuotes) {
272352
if (m_nItemType != WSJCPP_YAML_ITEM_VALUE) {
273-
WSJCppLog::throw_err(TAG, "setValue, Element must be value for line(" + std::to_string(this->getOriginalNumberOfLine()) + "): '" + this->getOriginalLine() + "'");
353+
WSJCppLog::throw_err(TAG, "setValue, Element must be value for " + this->getForLogFormat());
274354
}
275355
m_bValueHasDoubleQuotes = bHasQuotes;
276356
m_sValue = sValue;
@@ -368,6 +448,12 @@ std::string WSJCppYAMLItem::getItemTypeAsString() {
368448

369449
// ---------------------------------------------------------------------
370450

451+
std::string WSJCppYAMLItem::getForLogFormat() {
452+
return m_placeInFile.getForLogFormat();
453+
}
454+
455+
// ---------------------------------------------------------------------
456+
371457
WSJCppYAMLParsebleLine::WSJCppYAMLParsebleLine(int nLine) {
372458
TAG = "WSJCppYAMLParsebleLine(line:" + std::to_string(nLine) + ")";
373459
m_nLine = nLine;
@@ -571,17 +657,18 @@ std::string WSJCppYAMLParsebleLine::removeStringDoubleQuotes(const std::string &
571657
// WSJCppYAMLParserStatus
572658

573659
void WSJCppYAMLParserStatus::logUnknownLine(const std::string &sPrefix) {
574-
WSJCppLog::warn(sPrefix, "Unknown line (" + std::to_string(nLine) + "): '" + sLine + "' \n"
660+
WSJCppLog::warn(sPrefix, "Unknown line (" + std::to_string(placeInFile.getNumberOfLine()) + "): '" + placeInFile.getLine() + "' \n"
575661
+ "Current Intent: " + std::to_string(nIntent) + "\n"
576-
+ "Current Item(line: " + std::to_string(pCurItem->getOriginalNumberOfLine()) + "): '" + pCurItem->getOriginalLine() + "'"
662+
+ "Current Item(line: " + std::to_string(pCurItem->getPlaceInFile().getNumberOfLine()) + "): '" + pCurItem->getPlaceInFile().getLine() + "'"
663+
+ "Current Item(filename: " + pCurItem->getPlaceInFile().getFilename() + "'"
577664
);
578665
}
579666

580667
// ---------------------------------------------------------------------
581668
// WSJCppYAML
582669

583670
WSJCppYAML::WSJCppYAML() {
584-
m_pRoot = new WSJCppYAMLItem(nullptr, -1, "", WSJCPP_YAML_ITEM_MAP);
671+
m_pRoot = new WSJCppYAMLItem(nullptr, WSJCppYAMLPlaceInFile(), WSJCPP_YAML_ITEM_MAP);
585672
}
586673

587674
// ---------------------------------------------------------------------
@@ -597,7 +684,7 @@ bool WSJCppYAML::loadFromFile(const std::string &sFileName) {
597684
if (!WSJCppCore::readTextFile(sFileName, sTextContent)) {
598685
return false;
599686
}
600-
return parse(sTextContent);
687+
return parse(sFileName, sTextContent);
601688
}
602689

603690
// ---------------------------------------------------------------------
@@ -619,7 +706,7 @@ bool WSJCppYAML::loadFromString(const std::string &sBuffer) {
619706
// ---------------------------------------------------------------------
620707

621708
bool WSJCppYAML::loadFromString(std::string &sBuffer) {
622-
return parse(sBuffer);
709+
return parse("", sBuffer);
623710
}
624711

625712
// ---------------------------------------------------------------------
@@ -658,18 +745,20 @@ std::vector<std::string> WSJCppYAML::splitToLines(const std::string &sBuffer) {
658745

659746
// ---------------------------------------------------------------------
660747

661-
bool WSJCppYAML::parse(const std::string &sBuffer) {
748+
bool WSJCppYAML::parse(const std::string &sFileName, const std::string &sBuffer) {
662749
std::vector<std::string> vLines = this->splitToLines(sBuffer);
663750
WSJCppYAMLParserStatus st;
664-
st.pCurItem = m_pRoot; // TODO create again new root element
751+
st.pCurItem = m_pRoot; // TODO recreate again new root element
752+
st.placeInFile.setFilename(sFileName);
665753
st.nIntent = 0;
754+
m_pRoot->setPlaceInFile(st.placeInFile);
666755

667756
for (int nLine = 0; nLine < vLines.size(); nLine++) {
668-
st.sLine = vLines[nLine];
757+
st.placeInFile.setLine(vLines[nLine]);
669758
// WSJCppLog::info(TAG, "Line(" + std::to_string(nLine) + ") '" + st.sLine + "'");
670-
st.nLine = nLine;
759+
st.placeInFile.setNumberOfLine(nLine);
671760
st.line = WSJCppYAMLParsebleLine(nLine);
672-
st.line.parseLine(st.sLine);
761+
st.line.parseLine(st.placeInFile.getLine());
673762

674763
bool isEmptyName = st.line.isEmptyName();
675764
bool isEmptyValue = st.line.isEmptyValue();
@@ -736,9 +825,7 @@ void WSJCppYAML::process_sameIntent_hasName_emptyValue_arrayItem(WSJCppYAMLParse
736825

737826
void WSJCppYAML::process_sameIntent_hasName_emptyValue_noArrayItem(WSJCppYAMLParserStatus &st) {
738827
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(
739-
st.pCurItem,
740-
st.nLine,
741-
st.sLine,
828+
st.pCurItem, st.placeInFile,
742829
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_UNDEFINED
743830
);
744831
if (st.line.hasValueDoubleQuotes()) {
@@ -758,12 +845,18 @@ void WSJCppYAML::process_sameIntent_hasName_hasValue_arrayItem(WSJCppYAMLParserS
758845
if (st.pCurItem->isUndefined()) {
759846
st.pCurItem->doArray();
760847
}
761-
WSJCppYAMLItem *pMapItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_MAP);
848+
WSJCppYAMLItem *pMapItem = new WSJCppYAMLItem(
849+
st.pCurItem, st.placeInFile,
850+
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_MAP
851+
);
762852
st.pCurItem->appendElement(pMapItem);
763853
st.pCurItem = pMapItem;
764854
st.nIntent = st.nIntent + 2;
765855

766-
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE);
856+
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(
857+
st.pCurItem, st.placeInFile,
858+
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE
859+
);
767860
pItem->setComment(st.line.getComment());
768861
pItem->setValue(st.line.getValue(), st.line.hasValueDoubleQuotes());
769862
pItem->setName(st.line.getName(), st.line.hasNameDoubleQuotes());
@@ -775,7 +868,10 @@ void WSJCppYAML::process_sameIntent_hasName_hasValue_arrayItem(WSJCppYAMLParserS
775868
// ---------------------------------------------------------------------
776869

777870
void WSJCppYAML::process_sameIntent_hasName_hasValue_noArrayItem(WSJCppYAMLParserStatus &st) {
778-
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE);
871+
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(
872+
st.pCurItem, st.placeInFile,
873+
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE
874+
);
779875
pItem->setComment(st.line.getComment());
780876
pItem->setValue(st.line.getValue(), st.line.hasValueDoubleQuotes());
781877
pItem->setName(st.line.getName(), st.line.hasNameDoubleQuotes());
@@ -790,7 +886,10 @@ void WSJCppYAML::process_sameIntent_emptyName_hasValue_arrayItem(WSJCppYAMLParse
790886
if (st.pCurItem->isUndefined()) {
791887
st.pCurItem->doArray();
792888
}
793-
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE);
889+
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(
890+
st.pCurItem, st.placeInFile,
891+
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE
892+
);
794893
pItem->setComment(st.line.getComment());
795894
pItem->setValue(st.line.getValue(), st.line.hasValueDoubleQuotes());
796895
st.pCurItem->appendElement(pItem);
@@ -810,7 +909,10 @@ void WSJCppYAML::process_sameIntent_emptyName_emptyValue_arrayItem(WSJCppYAMLPar
810909
if (st.pCurItem->isUndefined()) {
811910
st.pCurItem->doArray();
812911
}
813-
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE);
912+
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(
913+
st.pCurItem, st.placeInFile,
914+
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_VALUE
915+
);
814916
pItem->setComment(st.line.getComment());
815917
pItem->setValue(st.line.getValue(), st.line.hasValueDoubleQuotes());
816918
st.pCurItem->appendElement(pItem);
@@ -821,9 +923,12 @@ void WSJCppYAML::process_sameIntent_emptyName_emptyValue_arrayItem(WSJCppYAMLPar
821923
// ---------------------------------------------------------------------
822924

823925
void WSJCppYAML::process_sameIntent_emptyName_emptyValue_noArrayItem(WSJCppYAMLParserStatus &st) {
824-
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(st.pCurItem, st.nLine, st.sLine, WSJCppYAMLItemType::WSJCPP_YAML_ITEM_EMPTY);
926+
WSJCppYAMLItem *pItem = new WSJCppYAMLItem(
927+
st.pCurItem, st.placeInFile,
928+
WSJCppYAMLItemType::WSJCPP_YAML_ITEM_EMPTY
929+
);
825930
pItem->setComment(st.line.getComment());
826931
st.pCurItem->appendElement(pItem);
827932
}
828933

829-
// ---------------------------------------------------------------------
934+
// ---------------------------------------------------------------------

0 commit comments

Comments
 (0)