Skip to content

Commit 539679a

Browse files
committed
Fixed #38, #24
1 parent d6d42de commit 539679a

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to wsjcpp-yaml project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [v0.1.10] - 2025-12-?? (2025 Dec ??)
9+
10+
- Fixed #38: TODO escaping names
11+
- Fixed #24: Auto detect quotes on setValue setName
12+
813
## [v0.1.9] - 2025-12-18 (2025 Dec 18)
914

1015
- Migrated unit-tests.wsjcpp to ctest

src/tests/test_auto_quotes.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2019-2025 wsjcpp
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
24+
Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
25+
*/
26+
27+
#include <iostream>
28+
#include <wsjcpp_yaml.h>
29+
30+
int main() {
31+
// https://github.com/wsjcpp/wsjcpp-yaml/issues/24
32+
33+
std::string sInput =
34+
"# commet1:\n"
35+
"val1: 1\n"
36+
;
37+
38+
WsjcppYaml yaml;
39+
std::string sError;
40+
if (!yaml.loadFromString("input.yml", sInput, sError)) {
41+
std::cerr << sError << std::endl;
42+
return -1;
43+
}
44+
45+
yaml["val1"].val("some\nsome");
46+
yaml.getRoot()->setElementValue("v\ng", "opa\ndone");
47+
48+
std::string sOutput = "";
49+
if (!yaml.saveToString(sOutput, sError)) {
50+
std::cerr << sError << std::endl;
51+
return -1;
52+
}
53+
54+
std::string sExpected =
55+
"# commet1:\n"
56+
"val1: \"some\\nsome\"\n"
57+
"\"v\\ng\": \"opa\\ndone\"\n"
58+
;
59+
60+
if (sOutput != sExpected) {
61+
std::cerr << "sOutput != sExpected" << std::endl;
62+
std::cerr << "Got: \n----\n" << sOutput << "\n------\nExpected\n-----\n" << sExpected << "\n------\n" << std::endl;
63+
return -1;
64+
}
65+
return 0;
66+
}

src/wsjcpp_yaml.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ std::string WsjcppYamlNode::getComment() {
218218

219219
void WsjcppYamlNode::setName(const std::string &sName, WsjcppYamlQuotes nNameQuotes) {
220220
m_sName = sName;
221+
if (m_sName.find('\n') != std::string::npos) {
222+
// automaticly set quotes double for escaping
223+
nNameQuotes = WSJCPP_YAML_QUOTES_DOUBLE;
224+
}
221225
m_nNameQuotes = nNameQuotes;
222226
}
223227

@@ -524,6 +528,10 @@ void WsjcppYamlNode::setValue(const std::string &sValue, WsjcppYamlQuotes nQuote
524528
if (m_nItemType != WSJCPP_YAML_NODE_VALUE) {
525529
throw std::runtime_error(TAG + ": setValue, Element must be value for " + this->getForLogFormat());
526530
}
531+
if (sValue.find('\n') != std::string::npos) {
532+
// automaticly set quotes double
533+
nQuotes = WSJCPP_YAML_QUOTES_DOUBLE;
534+
}
527535
m_nValueQuotes = nQuotes;
528536
m_sValue = sValue;
529537
}
@@ -534,9 +542,8 @@ WsjcppYamlQuotes WsjcppYamlNode::getValueQuotes() {
534542

535543
std::string WsjcppYamlNode::getSerializedName() {
536544
std::string sRet = "";
537-
// TODO escape quotes
538545
if (this->getNameQuotes() == WSJCPP_YAML_QUOTES_DOUBLE) {
539-
sRet += "\"" + this->getName() + "\"";
546+
sRet += "\"" + escapingString(this->getName()) + "\"";
540547
} else if (this->getNameQuotes() == WSJCPP_YAML_QUOTES_SINGLE) {
541548
sRet += "\'" + this->getName() + "\'";
542549
} else {
@@ -549,7 +556,7 @@ std::string WsjcppYamlNode::toString(std::string sIndent) {
549556
std::string sRet = "";
550557
if (this->isValue()) {
551558
if (m_nValueQuotes == WSJCPP_YAML_QUOTES_DOUBLE) {
552-
sRet = "\"" + m_sValue + "\"";
559+
sRet = "\"" + escapingString(m_sValue) + "\"";
553560
} else if (m_nValueQuotes == WSJCPP_YAML_QUOTES_SINGLE) {
554561
sRet = "\'" + m_sValue + "\'";
555562
} else {
@@ -722,6 +729,19 @@ void WsjcppYamlNode::removeLastCharNewLine(std::string &sLine) {
722729
}
723730
}
724731

732+
std::string WsjcppYamlNode::escapingString(const std::string &sVal) {
733+
size_t nLen = sVal.length();
734+
std::string res;
735+
for (int i = 0; i < nLen; i++) {
736+
if (sVal[i] == '\n') {
737+
res += "\\n";
738+
} else {
739+
res += sVal[i];
740+
}
741+
}
742+
return res;
743+
}
744+
725745
bool WsjcppYamlNode::hasContent(const std::string &sVal) {
726746
std::string sVal_ = sVal;
727747
sVal_ = WsjcppYaml::trim(sVal_);

src/wsjcpp_yaml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class WsjcppYamlNode {
169169
private:
170170
void throw_error(const std::string &sError);
171171
void removeLastCharNewLine(std::string &sLine);
172+
std::string escapingString(const std::string &sVal);
172173
bool hasContent(const std::string &sVal);
173174
bool hasObjects();
174175
std::string TAG;

0 commit comments

Comments
 (0)