Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to wsjcpp-yaml project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [v0.1.10] - 2025-12-?? (2025 Dec ??)

- Downgrade min c++ standart to C++11
- Fixed #38: TODO escaping names
- Fixed #24: Auto detect quotes on setValue setName

## [v0.1.9] - 2025-12-18 (2025 Dec 18)

- Migrated unit-tests.wsjcpp to ctest
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ project(wsjcpp-yaml)

# include(${CMAKE_CURRENT_SOURCE_DIR}/src.wsjcpp/CMakeLists.txt)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 11)
set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-yaml_SOURCE_DIR})

# Sources
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# wsjcpp-yaml

[![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-yaml.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-yaml) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-yaml.svg)](https://github.com/wsjcpp/wsjcpp-yaml) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-yaml.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-yaml/network/members)
[![Github Stars](https://img.shields.io/github/stars/wsjcpp/wsjcpp-yaml.svg?label=github%20%E2%98%85)](https://github.com/wsjcpp/wsjcpp-yaml) [![Github Stars](https://img.shields.io/github/contributors/wsjcpp/wsjcpp-yaml.svg)](https://github.com/wsjcpp/wsjcpp-yaml) [![Github Forks](https://img.shields.io/github/forks/wsjcpp/wsjcpp-yaml.svg?label=github%20forks)](https://github.com/wsjcpp/wsjcpp-yaml/network/members) [![wsjcpp-yaml C++](https://img.shields.io/badge/C++-11-green.svg)](https://github.com/wsjcpp/wsjcpp-yaml) [![wsjcpp-yaml C++](https://img.shields.io/badge/C++-17-green.svg)](https://github.com/wsjcpp/wsjcpp-yaml)

C++ YAML parser/reader and writer of *.yaml/*.yml files with keeping user formatting

Expand Down
66 changes: 66 additions & 0 deletions src/tests/test_auto_quotes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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
*/

#include <iostream>
#include <wsjcpp_yaml.h>

int main() {
// https://github.com/wsjcpp/wsjcpp-yaml/issues/24

std::string sInput =
"# commet1:\n"
"val1: 1\n"
;

WsjcppYaml yaml;
std::string sError;
if (!yaml.loadFromString("input.yml", sInput, sError)) {
std::cerr << sError << std::endl;
return -1;
}

yaml["val1"].val("some\nsome");
yaml.getRoot()->setElementValue("v\ng", "opa\ndone");

std::string sOutput = "";
if (!yaml.saveToString(sOutput, sError)) {
std::cerr << sError << std::endl;
return -1;
}

std::string sExpected =
"# commet1:\n"
"val1: \"some\\nsome\"\n"
"\"v\\ng\": \"opa\\ndone\"\n"
;

if (sOutput != sExpected) {
std::cerr << "sOutput != sExpected" << std::endl;
std::cerr << "Got: \n----\n" << sOutput << "\n------\nExpected\n-----\n" << sExpected << "\n------\n" << std::endl;
return -1;
}
return 0;
}
7 changes: 4 additions & 3 deletions src/tests/test_double_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
#include <iostream>
#include <cmath>
#include <fstream>
#include <cfloat>
#include <wsjcpp_yaml.h>
#include "helpers.h"

Expand Down Expand Up @@ -63,7 +64,7 @@ int main() {
// std::numeric_limits<double>::epsilon() => 2.22045e-16
// std::numeric_limits<float>::epsilon() => 1.19209e-07
// abs(val_double - expected_double) => 3.31879e-08
if (abs(val_double - expected_double) > std::numeric_limits<float>::epsilon()) {
if (abs(val_double - expected_double) > FLT_EPSILON) {
std::cerr << "Parameter 'double' has value number '" << val_double << "', but expected '" << expected_double << "'" << std::endl;
ret = -1;
}
Expand All @@ -78,13 +79,13 @@ int main() {

expected_double = 1.0003f;
val_double = yaml["double"].valDouble();
if (abs(val_double - expected_double) > std::numeric_limits<float>::epsilon()) {
if (abs(val_double - expected_double) > FLT_EPSILON) {
std::cerr << "Parameter 'double' has value number '" << val_double << "', but expected '" << expected_double << "'" << std::endl;
ret = -1;
}
float val_double_like_float = yaml["double"].valFloat();
float expected_float = 1.0003f;
if (abs(val_double_like_float - expected_float) > std::numeric_limits<float>::epsilon()) {
if (abs(val_double_like_float - expected_float) > FLT_EPSILON) {
std::cerr << "Parameter 'double' has value number (like float) '" << val_double_like_float << "', but expected '" << expected_float << "'" << std::endl;
ret = -1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_float_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
#include <iostream>
#include <cmath>
#include <fstream>
#include <cfloat>
#include <wsjcpp_yaml.h>
#include "helpers.h"

Expand Down Expand Up @@ -62,7 +63,7 @@ int main() {

float val_float = yaml["float"].valFloat();
float expected_float = 1.0001f;
if (abs(val_float - expected_float) > std::numeric_limits<float>::epsilon()) {
if (abs(val_float - expected_float) > FLT_EPSILON) {
std::cerr << "Parameter 'float' has value '" << val_float << "', but expected '" << expected_float << "'" << std::endl;
ret = -1;
}
Expand Down
26 changes: 23 additions & 3 deletions src/wsjcpp_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ std::string WsjcppYamlNode::getComment() {

void WsjcppYamlNode::setName(const std::string &sName, WsjcppYamlQuotes nNameQuotes) {
m_sName = sName;
if (m_sName.find('\n') != std::string::npos) {
// automaticly set quotes double for escaping
nNameQuotes = WSJCPP_YAML_QUOTES_DOUBLE;
}
m_nNameQuotes = nNameQuotes;
}

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

std::string WsjcppYamlNode::getSerializedName() {
std::string sRet = "";
// TODO escape quotes
if (this->getNameQuotes() == WSJCPP_YAML_QUOTES_DOUBLE) {
sRet += "\"" + this->getName() + "\"";
sRet += "\"" + escapingString(this->getName()) + "\"";
} else if (this->getNameQuotes() == WSJCPP_YAML_QUOTES_SINGLE) {
sRet += "\'" + this->getName() + "\'";
} else {
Expand All @@ -549,7 +556,7 @@ std::string WsjcppYamlNode::toString(std::string sIndent) {
std::string sRet = "";
if (this->isValue()) {
if (m_nValueQuotes == WSJCPP_YAML_QUOTES_DOUBLE) {
sRet = "\"" + m_sValue + "\"";
sRet = "\"" + escapingString(m_sValue) + "\"";
} else if (m_nValueQuotes == WSJCPP_YAML_QUOTES_SINGLE) {
sRet = "\'" + m_sValue + "\'";
} else {
Expand Down Expand Up @@ -722,6 +729,19 @@ void WsjcppYamlNode::removeLastCharNewLine(std::string &sLine) {
}
}

std::string WsjcppYamlNode::escapingString(const std::string &sVal) {
size_t nLen = sVal.length();
std::string res;
for (int i = 0; i < nLen; i++) {
if (sVal[i] == '\n') {
res += "\\n";
} else {
res += sVal[i];
}
}
return res;
}

bool WsjcppYamlNode::hasContent(const std::string &sVal) {
std::string sVal_ = sVal;
sVal_ = WsjcppYaml::trim(sVal_);
Expand Down
1 change: 1 addition & 0 deletions src/wsjcpp_yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class WsjcppYamlNode {
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;
Expand Down
6 changes: 3 additions & 3 deletions wsjcpp.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
wsjcpp_version: v0.0.1
wsjcpp_version: v0.2.4
cmake_minimum_required: 3.0
cmake_cxx_standard: 17
cmake_cxx_standard: 11
name: wsjcpp-yaml
version: v0.1.9
version: v0.1.10
description: Read/Write yaml files
issues: https://github.com/wsjcpp/wsjcpp-yaml/issues

Expand Down