Skip to content

Commit f81f4d0

Browse files
committed
Fixed #29 Problem with lost empty line
1 parent 696c920 commit f81f4d0

11 files changed

+204
-48
lines changed

src.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Automaticly generated by wsjcpp@v0.2.0
1+
# Automaticly generated by wsjcpp@v0.2.2
22
cmake_minimum_required(VERSION 3.0)
33

44
add_definitions(-DWSJCPP_APP_VERSION="v0.1.6")

src/wsjcpp_yaml.cpp

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -556,62 +556,84 @@ std::string WsjcppYamlNode::toString(std::string sIndent) {
556556
for (int i = 0; i < m_vObjects.size(); i++) {
557557
WsjcppYamlNode *pNode = m_vObjects[i];
558558
if (pNode->isEmpty()) {
559-
std::string sVal = pNode->toString();
560-
sVal = WsjcppCore::trim(sVal);
561-
if (sVal.length() > 0) { // empty string have content
562-
sRet += sIndent + pNode->getStringNodeLastIndent();
563-
}
559+
// std::string sVal = pNode->toString(sIndent + pNode->getStringNodeLastIndent());
560+
std::string sVal = pNode->toString(sIndent);
564561
sRet += sVal;
565562
} else if (pNode->isMap()) {
566563
sRet += sIndent + pNode->getStringNodeLastIndent();
567564
std::string s = pNode->toString(sIndent + pNode->getStringNodeLastIndent());
568-
WsjcppCore::trim(s);
565+
WsjcppCore::ltrim(s);
569566
sRet += "- " + s;
570567
} else {
571568
sRet += sIndent + pNode->getStringNodeLastIndent();
572569
sRet += "- " + pNode->toString();
573570
}
574571
sRet += "\n";
575572
}
573+
removeLastCharNewLine(sRet);
576574
} else if (this->isMap()) {
577-
for (int i = 0; i < m_vObjects.size(); i++) {
578-
WsjcppYamlNode *pNode = m_vObjects[i];
579-
if (pNode->isEmpty() ) {
580-
// sRet += " * " + pNode->toString(sIndent);
581-
sRet += pNode->toString(sIndent);
582-
sRet += "\n";
583-
} else if (pNode->isUndefined()) {
584-
sRet += sIndent + pNode->getStringNodeLastIndent()
585-
+ pNode->getSerializedName() + ":\n" + pNode->toString();
586-
} else if (pNode->isArray() || pNode->isMap()) {
587-
sRet += sIndent + pNode->getStringNodeLastIndent()
588-
+ pNode->getSerializedName() + ":";
589-
if (pNode->getComment().length() > 0) {
590-
sRet += " # " + pNode->getComment();
591-
}
592-
sRet += "\n";
593-
sRet += pNode->toString(sIndent + pNode->getStringNodeLastIndent());
594-
} else {
595-
std::string sVal = pNode->toString();
596-
std::string sVal_ = sVal;
597-
sVal_ = WsjcppCore::trim(sVal_);
598-
if (sVal_.length() > 0) {
599-
sVal = " " + sVal;
575+
if (m_vObjects.size() > 0) {
576+
// sRet += "-s-" + std::to_string(m_vObjects.size()) + "--";
577+
for (int i = 0; i < m_vObjects.size(); i++) {
578+
WsjcppYamlNode *pNode = m_vObjects[i];
579+
if (pNode->isEmpty() ) {
580+
std::string s = pNode->toString(sIndent);
581+
// WsjcppLog::warn(TAG, "Empty");
582+
// WsjcppCore::trim(s);
583+
// sRet += s + " # rmpty";
584+
sRet += s;
585+
} else if (pNode->isUndefined()) {
586+
std::string s = pNode->toString();
587+
sRet += sIndent + pNode->getStringNodeLastIndent()
588+
+ pNode->getSerializedName() + ":";
589+
if (pNode->hasObjects()) {
590+
sRet += "\n" + pNode->toString();
591+
removeLastCharNewLine(sRet);
592+
}
593+
} else if (pNode->isArray() || pNode->isMap()) {
594+
sRet += sIndent + pNode->getStringNodeLastIndent()
595+
+ pNode->getSerializedName() + ":";
596+
if (pNode->getComment().length() > 0) {
597+
sRet += " # " + pNode->getComment();
598+
}
599+
std::string s = pNode->toString(sIndent + pNode->getStringNodeLastIndent());
600+
if (pNode->isMap()) {
601+
if (pNode->getKeys().size() > 0) {
602+
sRet += "\n";
603+
} else {
604+
// removeLastCharNewLine(s);
605+
}
606+
}
607+
if (pNode->isArray()) {
608+
if (pNode->getLength() > 0) {
609+
sRet += "\n";
610+
} else {
611+
// removeLastCharNewLine(s);
612+
}
613+
}
614+
sRet += s;
615+
} else {
616+
std::string sVal = pNode->toString();
617+
std::string sVal_ = sVal;
618+
sVal_ = WsjcppCore::trim(sVal_);
619+
if (sVal_.length() > 0) {
620+
sVal = " " + sVal;
621+
}
622+
WsjcppCore::rtrim(sVal);
623+
sRet += sIndent + pNode->getStringNodeLastIndent()
624+
+ pNode->getSerializedName() + ":" + sVal;
600625
}
601-
sRet += sIndent + pNode->getStringNodeLastIndent()
602-
+ pNode->getSerializedName() + ":" + sVal;
603626
sRet += "\n";
604627
}
628+
removeLastCharNewLine(sRet);
605629
}
606630
} else {
631+
WsjcppLog::warn(TAG, "????");
607632
sRet = ""; // undefined element must be empty
608633
}
609634

610635
if (m_pParent == nullptr) {
611-
int nLen = sRet.length();
612-
if (nLen > 0 && sRet[nLen - 1] == '\n') {
613-
sRet = sRet.substr(0, nLen - 1);
614-
}
636+
removeLastCharNewLine(sRet);
615637
}
616638
return sRet;
617639
}
@@ -675,18 +697,31 @@ int WsjcppYamlNode::getNumberOfLine() const {
675697
return m_placeInFile.getNumberOfLine();
676698
}
677699

678-
// ---------------------------------------------------------------------
679-
680700
void WsjcppYamlNode::setNumberOfLine(int nNumberOfLine) {
681701
m_placeInFile.setNumberOfLine(nNumberOfLine);
682702
}
683703

684-
// ---------------------------------------------------------------------
685-
686704
void WsjcppYamlNode::throw_error(const std::string &sError) {
687705
throw std::runtime_error(sError.c_str());
688706
}
689707

708+
void WsjcppYamlNode::removeLastCharNewLine(std::string &sLine) {
709+
int nLen = sLine.length();
710+
if (nLen > 0 && sLine[nLen - 1] == '\n') {
711+
sLine = sLine.substr(0, nLen - 1);
712+
}
713+
}
714+
715+
bool WsjcppYamlNode::hasContent(const std::string &sVal) {
716+
std::string sVal_ = sVal;
717+
sVal_ = WsjcppCore::trim(sVal_);
718+
return sVal_.length() > 0;
719+
}
720+
721+
bool WsjcppYamlNode::hasObjects() {
722+
return m_vObjects.size() > 0;
723+
}
724+
690725
// ---------------------------------------------------------------------
691726
// WsjcppYamlParsebleLine
692727

@@ -1264,7 +1299,7 @@ bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError)
12641299
// ---------------------------------------------------------------------
12651300

12661301
bool WsjcppYaml::saveToFile(const std::string &sFileName, std::string &sError) {
1267-
std::string sBuffer = m_pRoot->toString();
1302+
std::string sBuffer = m_pRoot->toString() + "\n"; // last empty line must be always
12681303
if (!WsjcppCore::writeFile(sFileName, sBuffer)) {
12691304
sError = "Could not save to file";
12701305
return false;
@@ -1281,7 +1316,8 @@ bool WsjcppYaml::loadFromString(const std::string &sBufferName, const std::strin
12811316
// ---------------------------------------------------------------------
12821317

12831318
bool WsjcppYaml::saveToString(std::string &sBuffer, std::string &sError) {
1284-
sBuffer = m_pRoot->toString();
1319+
// WsjcppLog::warn(TAG, "WsjcppYaml::saveToString");
1320+
sBuffer = m_pRoot->toString() + "\n"; // last empty line must be always
12851321
return true;
12861322
}
12871323

@@ -1367,14 +1403,17 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
13671403

13681404
if (m_parseLine.isEmptyLine()) {
13691405
if (m_pParseCurrentParentNode != nullptr) {
1406+
// WsjcppLog::warn(TAG, "here empty line and parent exists " + m_parsePlaceInFile.getForLogFormat());
13701407
if (m_pParseCurrentParentNode->isArray() || m_pParseCurrentParentNode->isMap() || m_pParseCurrentParentNode->isUndefined()) {
1408+
// WsjcppLog::warn(TAG, "array, map or undefined");
13711409
WsjcppYamlNode *pNode = new WsjcppYamlNode(
13721410
m_pParseCurrentParentNode, m_parsePlaceInFile,
13731411
WSJCPP_YAML_NODE_EMPTY
13741412
);
13751413
pNode->setNodeIndents(m_vStackDiffNodeIndents);
13761414
m_pParseCurrentParentNode->appendElement(pNode);
13771415
} else if (m_pParseCurrentParentNode->getParent() != nullptr && (m_pParseCurrentParentNode->getParent()->isArray() || m_pParseCurrentParentNode->getParent()->isMap())) {
1416+
// WsjcppLog::warn(TAG, "parent exists and parent map or array");
13781417
WsjcppYamlNode *pNode = new WsjcppYamlNode(
13791418
m_pParseCurrentParentNode->getParent(), m_parsePlaceInFile,
13801419
WSJCPP_YAML_NODE_EMPTY
@@ -1384,6 +1423,8 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
13841423
} else {
13851424
throw std::runtime_error(TAG + ": Empty element can be added only to map or to array");
13861425
}
1426+
} else {
1427+
WsjcppLog::warn(TAG, "here empty line" );
13871428
}
13881429
continue;
13891430
}

src/wsjcpp_yaml.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ class WsjcppYamlNode {
137137

138138
private:
139139
void throw_error(const std::string &sError);
140-
140+
void removeLastCharNewLine(std::string &sLine);
141+
bool hasContent(const std::string &sVal);
142+
bool hasObjects();
141143
std::string TAG;
142144
WsjcppYamlNode *m_pParent;
143145
WsjcppYamlPlaceInFile m_placeInFile;

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Automaticly generated by wsjcpp@v0.2.0
1+
# Automaticly generated by wsjcpp@v0.2.2
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
@@ -51,6 +51,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cursor.cpp")
5151
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_tag_names.cpp")
5252
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_cleanup.cpp")
5353
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_append_elements.cpp")
54+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_keep_format.cpp")
5455

5556
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5657

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestKeepFormat
8+
9+
class UnitTestKeepFormat : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestKeepFormat();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestKeepFormat)
18+
19+
UnitTestKeepFormat::UnitTestKeepFormat()
20+
: WsjcppUnitTestBase("UnitTestKeepFormat") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestKeepFormat::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestKeepFormat::executeTest() {
33+
// https://github.com/wsjcpp/wsjcpp-yaml/issues/29
34+
35+
std::string sInput =
36+
"# commet1:\n"
37+
"\n"
38+
"val1: 1\n"
39+
"\n"
40+
"unit-tests:\n"
41+
" cases:\n"
42+
" - name: \"NormalizeUnitTestName\"\n"
43+
" description: \"Test Normalize UnitTest Name\"\n"
44+
" - name: \"generateFilenameForUnitTest\"\n"
45+
" description: \"Test generate filename for UnitTest\"\n"
46+
" - name: \"InitPackage\"\n"
47+
" description: \"Test for init package\"\n"
48+
" enabled: \"no\"\n"
49+
"\n"
50+
" - name: \"ReadPackage\"\n"
51+
" description: \"Test for read package\"\n"
52+
"\n"
53+
"\n"
54+
"resources:\n"
55+
" - filepath: \"./build_simple.sh\"\n"
56+
" filesize: 77\n"
57+
" pack-as: \"binary\"\n"
58+
" modified: 1600401759\n"
59+
" sha1: \"\"\n"
60+
;
61+
62+
WsjcppYaml yaml;
63+
std::string sError;
64+
if (!compare("Error parsing", yaml.loadFromString("input.yml", sInput, sError), true)) {
65+
WsjcppLog::err(TAG, sError);
66+
return;
67+
}
68+
69+
std::string sOutput = "";
70+
if (!compare("Error parsing", yaml.saveToString(sOutput, sError), true)) {
71+
WsjcppLog::err(TAG, sError);
72+
return;
73+
}
74+
75+
if (!compare("Keep formating", sOutput, sInput)) {
76+
return;
77+
}
78+
79+
// try again
80+
if (!compare("Error parsing", yaml.loadFromString("output.yml", sOutput, sError), true)) {
81+
WsjcppLog::err(TAG, sError);
82+
return;
83+
}
84+
85+
sOutput = "";
86+
if (!compare("Error parsing", yaml.saveToString(sOutput, sError), true)) {
87+
WsjcppLog::err(TAG, sError);
88+
return;
89+
}
90+
91+
if (!compare("Keep formating", sOutput, sInput)) {
92+
return;
93+
}
94+
}
95+
96+
// ---------------------------------------------------------------------
97+
98+
bool UnitTestKeepFormat::doAfterTest() {
99+
// do somethig after test
100+
return true;
101+
}
102+
103+

unit-tests.wsjcpp/src/unit_test_read_write_file.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void UnitTestReadWriteFile::executeTest() {
5656
std::string sOutputFileContent;
5757
WsjcppCore::readTextFile(sFilepathOutput, sOutputFileContent);
5858

59+
sOriginalFileContent += "\n"; // TODO fix
5960
compare("compare conteent ", sOutputFileContent, sOriginalFileContent);
6061

6162
}

unit-tests.wsjcpp/src/unit_test_yaml_parser_array_included_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void UnitTestYamlParserArrayIncludedMap::executeTest() {
104104
" submap-param2: v02\n"
105105
" - submap-param1: v11\n"
106106
" submap-param2: v12\n"
107-
"param2: v2"
107+
"param2: v2\n"
108108
);
109109
}
110110
}

unit-tests.wsjcpp/src/unit_test_yaml_parser_comments.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void UnitTestYamlParserComments::executeTest() {
9999
"map1: # comment 9\n"
100100
" p1: val 1 # comment 10\n"
101101
" # comment 11\n"
102-
" p2: val 2 # comment 12"
102+
" p2: val 2 # comment 12\n"
103103
);
104104
}
105105

unit-tests.wsjcpp/src/unit_test_yaml_parser_quotes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void UnitTestYamlParserQuotes::executeTest() {
8888
"issues: https://github.com/wsjcpp/wsjcpp-yaml/issues\n"
8989
"empty: \"\"\n"
9090
"array:\n"
91-
" - \"https://github.com/wsjcpp/wsjcpp-core:v0.0.1\""
91+
" - \"https://github.com/wsjcpp/wsjcpp-core:v0.0.1\"\n"
9292
);
9393
}
9494

unit-tests.wsjcpp/src/unit_test_yaml_parser_simple_array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void UnitTestYamlParserSimpleArray::executeTest() {
103103
" - free@free\n"
104104
" - # empty\n"
105105
" - 1\n"
106-
"param2: val2 # value 2"
106+
"param2: val2 # value 2\n"
107107
);
108108
}
109109

0 commit comments

Comments
 (0)