Skip to content

Commit 8926906

Browse files
committed
Fix #35 Add valFloat / valDouble
1 parent e312846 commit 8926906

File tree

10 files changed

+184
-22
lines changed

10 files changed

+184
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@ 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.7] - 2022-01-12 (2022 Jan 12)
9-
10-
### Fixed
8+
## [v0.1.8] - 2022-01-12 (2022 Jan 12)
119

12-
- Make compatible code with `msvc2015x86`
10+
- Fixed #35 Add valFloat / valDouble
1311

14-
### Added
12+
## [v0.1.7] - 2022-01-12 (2022 Jan 12)
1513

14+
- Make compatible code with `msvc2015x86`
1615
- Added script `msvc2015x86_build_simple.bat`
17-
18-
### Changed
19-
2016
- All logging will be use a `IWsjcppYamlLog`. Like a `yaml.setLogger(pLogger);`
2117

2218
## [v0.1.6] - 2022-01-08 (2022 Jan 8)
2319

24-
### Fixed
25-
2620
- Fixed #29 Problem with lost empty line
2721
- Fixed #27 renamed 'intent' to 'indent'

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.2
1+
# Automaticly generated by wsjcpp@v0.2.4
22
cmake_minimum_required(VERSION 3.0)
33

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

src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class WsjcppUnitTestBase {
1717
virtual bool doAfterTest() = 0;
1818
protected:
1919
std::string TAG;
20-
20+
2121
bool compareD(const std::string &sMark, double nValue, double nExpected);
2222
template<typename T1, typename T2> bool compare(const std::string &sMark, T1 tGotValue, T2 tExpectedValue) {
2323
if (tGotValue != tExpectedValue) {
@@ -46,4 +46,4 @@ class WsjcppUnitTests {
4646
static classname * pRegistryWsjcppUnitTest ## classname = new classname(); \
4747

4848

49-
#endif // WSJCPP_UNIT_TESTS_H
49+
#endif // WSJCPP_UNIT_TESTS_H

src/wsjcpp_yaml.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Official Source Code: https://github.com/wsjcpp/wsjcpp-yaml
3939

4040
std::string WSJCPP_INT_TO_STR(int number) {
4141
#if defined(__CODEGEARC__) && !defined(_WIN64)
42+
// TODO
4243
char buffer[] = {
4344
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
4445
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
@@ -57,6 +58,58 @@ std::string WSJCPP_INT_TO_STR(int number) {
5758
#endif
5859
}
5960

61+
std::string WSJCPP_FLOAT_TO_STR(float number) {
62+
std::string ret;
63+
#if defined(__CODEGEARC__) && !defined(_WIN64)
64+
// TODO
65+
char buffer[] = {
66+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
67+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
68+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
69+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0
70+
};
71+
#if __CODEGEARC__ == 0x0770
72+
// 12.2
73+
_itoa(number, buffer, 10);
74+
#else
75+
itoa(number, buffer, 10);
76+
#endif
77+
ret = std::string(buffer);
78+
#else
79+
ret = std::to_string(number);
80+
#endif
81+
ret.erase(std::find_if(ret.rbegin(), ret.rend(), [](unsigned char ch) {
82+
return ch != '0';
83+
}).base(), ret.end());
84+
return ret;
85+
}
86+
87+
std::string WSJCPP_DOUBLE_TO_STR(double number) {
88+
std::string ret;
89+
#if defined(__CODEGEARC__) && !defined(_WIN64)
90+
// TODO
91+
char buffer[] = {
92+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
93+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
94+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,
95+
0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0
96+
};
97+
#if __CODEGEARC__ == 0x0770
98+
// 12.2
99+
_itoa(number, buffer, 10);
100+
#else
101+
itoa(number, buffer, 10);
102+
#endif
103+
ret = std::string(buffer);
104+
#else
105+
ret = std::to_string(number);
106+
#endif
107+
ret.erase(std::find_if(ret.rbegin(), ret.rend(), [](unsigned char ch) {
108+
return ch != '0';
109+
}).base(), ret.end());
110+
return ret;
111+
}
112+
60113
// ---------------------------------------------------------------------
61114
// WsjcppYamlPlaceInFile
62115

@@ -1234,6 +1287,48 @@ int WsjcppYamlCursor::valInt() const {
12341287
return 0;
12351288
}
12361289

1290+
float WsjcppYamlCursor::valFloat() const {
1291+
if (m_pCurrentNode != WSJCPP_NULL) {
1292+
std::string sValue = m_pCurrentNode->getValue();
1293+
float nValue = std::stof(sValue);
1294+
std::string sExpectedValue = WSJCPP_FLOAT_TO_STR(nValue);
1295+
if (sExpectedValue != sValue) {
1296+
std::string error_msg = TAG + ": valInt, Element must be float but have a string" + m_pCurrentNode->getForLogFormat() + "', but expected value is '" + sExpectedValue + "'";
1297+
throw std::runtime_error(error_msg);
1298+
}
1299+
return nValue;
1300+
}
1301+
return 0.0f;
1302+
}
1303+
1304+
WsjcppYamlCursor &WsjcppYamlCursor::val(float nValue) {
1305+
if (m_pCurrentNode != WSJCPP_NULL) {
1306+
m_pCurrentNode->setValue(WSJCPP_FLOAT_TO_STR(nValue));
1307+
}
1308+
return *this;
1309+
}
1310+
1311+
double WsjcppYamlCursor::valDouble() const {
1312+
if (m_pCurrentNode != WSJCPP_NULL) {
1313+
std::string sValue = m_pCurrentNode->getValue();
1314+
double nValue = std::stod(sValue);
1315+
std::string sExpectedValue = WSJCPP_DOUBLE_TO_STR(nValue);
1316+
if (sExpectedValue != sValue) {
1317+
std::string error_msg = TAG + ": valInt, Element must be float but have a string '" + m_pCurrentNode->getForLogFormat() + "', but expected value is '" + sExpectedValue + "'";
1318+
throw std::runtime_error(error_msg);
1319+
}
1320+
return nValue;
1321+
}
1322+
return 0.0f;
1323+
}
1324+
1325+
WsjcppYamlCursor &WsjcppYamlCursor::val(double nValue) {
1326+
if (m_pCurrentNode != WSJCPP_NULL) {
1327+
m_pCurrentNode->setValue(WSJCPP_DOUBLE_TO_STR(nValue));
1328+
}
1329+
return *this;
1330+
}
1331+
12371332
WsjcppYamlCursor &WsjcppYamlCursor::val(int nValue) {
12381333
if (m_pCurrentNode != WSJCPP_NULL) {
12391334
m_pCurrentNode->setValue(WSJCPP_INT_TO_STR(nValue));

src/wsjcpp_yaml.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class WsjcppYamlCursor {
269269
// WsjcppYamlCursor &set(const std::string &sName, bool bValue);
270270
// WsjcppYamlCursor &remove(const std::string &sKey);
271271

272-
// comment
272+
// comment
273273
std::string comment();
274274
WsjcppYamlCursor &comment(const std::string& sComment);
275275

@@ -279,6 +279,10 @@ class WsjcppYamlCursor {
279279
WsjcppYamlCursor &val(const char *sValue);
280280
int valInt() const;
281281
WsjcppYamlCursor &val(int nValue);
282+
float valFloat() const;
283+
WsjcppYamlCursor &val(float nValue);
284+
double valDouble() const;
285+
WsjcppYamlCursor &val(double nValue);
282286
bool valBool() const;
283287
WsjcppYamlCursor &val(bool bValue);
284288

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.2
1+
# Automaticly generated by wsjcpp@v0.2.4
22
cmake_minimum_required(VERSION 3.0)
33

44
project(unit-tests C CXX)
@@ -52,6 +52,7 @@ list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_t
5252
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_cleanup.cpp")
5353
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_append_elements.cpp")
5454
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_keep_format.cpp")
55+
list (APPEND WSJCPP_SOURCES "${CMAKE_SOURCE_DIR}/../unit-tests.wsjcpp/src/unit_test_float_double.cpp")
5556

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
float: 1.0001
2+
double: 1.0002
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestFloatDouble
8+
9+
class UnitTestFloatDouble : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestFloatDouble();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestFloatDouble)
18+
19+
UnitTestFloatDouble::UnitTestFloatDouble()
20+
: WsjcppUnitTestBase("UnitTestFloatDouble") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestFloatDouble::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestFloatDouble::executeTest() {
33+
WsjcppYaml yaml;
34+
std::string sFilepath = "./data-tests/read-file/float-double/example.yml";
35+
std::string sError;
36+
if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) {
37+
WsjcppLog::err(TAG, sError);
38+
return;
39+
}
40+
41+
compare("has float field ", yaml.getRoot()->hasElement("float"), true);
42+
compare("has float field is value", yaml.getRoot()->getElement("float")->isValue(), true);
43+
compare("has float value is 1.0001 (str)", yaml.getRoot()->getElement("float")->getValue(), "1.0001");
44+
compare("has float value is 1.0001 (float)", yaml["float"].valFloat(), 1.0001f);
45+
46+
compare("has double field ", yaml.getRoot()->hasElement("double"), true);
47+
compare("has double field is value", yaml.getRoot()->getElement("double")->isValue(), true);
48+
compare("has double value is 1.0002 (str)", yaml.getRoot()->getElement("double")->getValue(), "1.0002");
49+
compare("has double value is 1.0002 (double)", yaml["double"].valFloat(), 1.0002f);
50+
51+
yaml["double"].val(1.0003f);
52+
compare("has double value is 1.0003 (str)", yaml.getRoot()->getElement("double")->getValue(), "1.0003");
53+
compareD("has double value is 1.0003 (double 1)", yaml["double"].valFloat(), 1.0003f);
54+
compareD("has double value is 1.0003 (double 2)", yaml["double"].valDouble(), 1.0003f);
55+
}
56+
57+
// ---------------------------------------------------------------------
58+
59+
bool UnitTestFloatDouble::doAfterTest() {
60+
// do somethig after test
61+
return true;
62+
}
63+
64+

unit-tests.wsjcpp/src/unit_test_read_yaml.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void UnitTestReadYaml::executeTest() {
8383
compare("has services.result", pServices->hasElement("result"), true);
8484
compare("services.result is map", pServices->getElement("result")->isMap(), true);
8585
compare("services.result keys size 5", pServices->getElement("result")->getKeys().size(), 5);
86-
86+
8787
WsjcppYamlNode *pResult = pServices->getElement("result");
8888

8989
compare("has services.result.build", pResult->hasElement("build"), true);
@@ -115,9 +115,9 @@ void UnitTestReadYaml::executeTest() {
115115
compare("has services.worker is map", pServices->getElement("worker")->isMap(), true);
116116
compare("services.worker keys size 3", pServices->getElement("worker")->getKeys().size(), 3);
117117
WsjcppYamlNode *pWorker = pServices->getElement("worker");
118-
118+
119119
compare("has services.worker.build", pWorker->hasElement("build"), true);
120-
120+
121121
compare("has services.worker.depends_on", pWorker->hasElement("depends_on"), true);
122122
compare("has services.worker.networks", pWorker->hasElement("networks"), true);
123123

@@ -184,10 +184,10 @@ void UnitTestReadYaml::executeTest() {
184184

185185
compare("has services.db.environment.POSTGRES_PASSWORD", pDbEnvironment->hasElement("POSTGRES_PASSWORD"), true);
186186
compare("services.db.environment.POSTGRES_PASSWORD", pDbEnvironment->getElement("POSTGRES_PASSWORD")->getValue(), "postgres");
187-
187+
188188
compare("has services.db.volumes", pServicesDb->hasElement("volumes"), true);
189189
compare("services.db.volumes is array", pServicesDb->getElement("volumes")->isArray(), true);
190-
190+
191191
WsjcppYamlNode *pDbVolumes = pServicesDb->getElement("volumes");
192192
compare("services.db.volumes size 1", pDbVolumes->getLength(), 1);
193193
compare("services.db.volumes val 0", pDbVolumes->getElement(0)->getValue(), "db-data:/var/lib/postgresql/data");
@@ -209,12 +209,12 @@ void UnitTestReadYaml::executeTest() {
209209

210210
compare("has volumes.db-data", pVolumes->hasElement("db-data"), true);
211211
compare("has volumes.db-data is undefined", pVolumes->getElement("db-data")->isUndefined(), true);
212-
compare("has volumes keys size 1", pVolumes->getKeys().size(), 1);
212+
compare("has volumes keys size 1", pVolumes->getKeys().size(), 1);
213213
}
214214

215215
// networks
216216
{
217-
compare("has networks", yaml.getRoot()->hasElement("networks"), true);
217+
compare("has networks", yaml.getRoot()->hasElement("networks"), true);
218218
compare("has networks is map", yaml.getRoot()->getElement("networks")->isMap(), true);
219219

220220
WsjcppYamlNode *pNeworks = yaml.getRoot()->getElement("networks");

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ unit-tests:
7676
description: ""
7777
- name: "KeepFormat"
7878
description: ""
79+
- name: "FloatDouble"
80+
description: ""
7981

0 commit comments

Comments
 (0)