Skip to content

Commit 62a6f85

Browse files
committed
Added '#define WSJCPP_NULL nullptr' for make compatibale with differrent compilers
1 parent b132efb commit 62a6f85

File tree

1 file changed

+57
-55
lines changed

1 file changed

+57
-55
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ std::string WsjcppYamlPlaceInFile::getForLogFormat() {
7878
// ---------------------------------------------------------------------
7979
// WsjcppYamlNode
8080

81+
#define WSJCPP_NULL nullptr
82+
8183
WsjcppYamlNode::WsjcppYamlNode(
82-
WsjcppYamlNode *pParent,
84+
WsjcppYamlNode *pParent,
8385
IWsjcppYamlLog *pLog,
8486
const WsjcppYamlPlaceInFile &placeInFile,
8587
WsjcppYamlNodeType nItemType
@@ -93,14 +95,14 @@ WsjcppYamlNode::WsjcppYamlNode(
9395
m_nValueQuotes = WSJCPP_YAML_QUOTES_NONE;
9496
m_nNameQuotes = WSJCPP_YAML_QUOTES_NONE;
9597
// TODO get child indent
96-
if (m_pParent != nullptr && m_pParent->getParent() != nullptr) {
98+
if (m_pParent != WSJCPP_NULL && m_pParent->getParent() != WSJCPP_NULL) {
9799
m_nNodeDiffIndent = 2;
98100
m_sNodeDiffIndent = " ";
99101
} else {
100102
m_nNodeDiffIndent = 0;
101103
m_sNodeDiffIndent = "";
102104
}
103-
105+
104106
TAG = "WsjcppYamlNode";
105107
}
106108

@@ -242,28 +244,28 @@ WsjcppYamlNode *WsjcppYamlNode::getElement(const std::string &sName) {
242244
if (m_nItemType != WSJCPP_YAML_NODE_MAP) {
243245
throw std::runtime_error(TAG + ": getElement: Element must be map");
244246
}
245-
247+
246248
for (int i = 0; i < m_vObjects.size(); i++) {
247249
std::string sObjectName = m_vObjects[i]->getName();
248250
if (m_vObjects[i]->getName() == sName) {
249251
return m_vObjects[i];
250252
}
251253
}
252-
throw_error(TAG + "Element '" + sName + "' not found for " + this->getForLogFormat());
253-
return nullptr;
254+
throw_error(TAG + "Element '" + sName + "' not found for " + this->getForLogFormat());
255+
return WSJCPP_NULL;
254256
}
255257

256258
// ---------------------------------------------------------------------
257259

258260
bool WsjcppYamlNode::setElement(const std::string &sName, WsjcppYamlNode *pItem) {
259261
if (m_nItemType == WSJCPP_YAML_NODE_UNDEFINED) {
260-
m_nItemType = WSJCPP_YAML_NODE_MAP; // change item type to map on first element
262+
m_nItemType = WSJCPP_YAML_NODE_MAP; // change item type to map on first element
261263
}
262264

263265
if (m_nItemType != WSJCPP_YAML_NODE_MAP) {
264266
throw std::runtime_error(TAG + ": setElement, Element must be 'map' for " + pItem->getPlaceInFile().getForLogFormat());
265267
}
266-
268+
267269
if (this->hasElement(sName)) { // TODO remove previous element
268270
throw std::runtime_error(TAG + ": setElement: Current map '" + this->getName() + "' "
269271
"(" + m_placeInFile.getFilename() + ":" + std::to_string(m_placeInFile.getNumberOfLine()) + ") "
@@ -410,17 +412,17 @@ WsjcppYamlNode *WsjcppYamlNode::getElement(int i) {
410412
throw std::runtime_error(TAG + ": getElement, Element must be array");
411413
}
412414
int nCounter = -1;
413-
WsjcppYamlNode *pItem = nullptr;
415+
WsjcppYamlNode *pItem = WSJCPP_NULL;
414416
for (int n = 0; n < m_vObjects.size(); n++) {
415417
if (!m_vObjects[n]->isEmpty()) {
416418
nCounter++;
417419
if (nCounter == i) {
418420
pItem = m_vObjects[n];
419-
break;
421+
break;
420422
}
421423
}
422424
}
423-
if (pItem == nullptr) {
425+
if (pItem == WSJCPP_NULL) {
424426
throw std::runtime_error(TAG + ": getElement(" + std::to_string(i) + "), Out of range in array for '" + this->getPlaceInFile().getLine() + "'");
425427
}
426428
return pItem;
@@ -464,17 +466,17 @@ bool WsjcppYamlNode::removeElement(int i) {
464466
throw std::runtime_error(TAG + ": appendElement, Element must be array for " + this->getForLogFormat());
465467
}
466468
int nCounter = -1;
467-
WsjcppYamlNode *pItem = nullptr;
469+
WsjcppYamlNode *pItem = WSJCPP_NULL;
468470
for (int n = 0; n < m_vObjects.size(); n++) {
469471
if (!m_vObjects[n]->isEmpty()) {
470472
nCounter++;
471473
if (nCounter == i) {
472474
pItem = m_vObjects[n];
473-
break;
475+
break;
474476
}
475477
}
476478
}
477-
if (pItem == nullptr) {
479+
if (pItem == WSJCPP_NULL) {
478480
throw std::runtime_error(TAG + ": getElement(" + std::to_string(i) + "), Out of range in array for '" + this->getPlaceInFile().getLine() + "'");
479481
}
480482
std::vector<WsjcppYamlNode *>::iterator it;
@@ -646,8 +648,8 @@ std::string WsjcppYamlNode::toString(std::string sIndent) {
646648
m_pLog->warn(TAG, "????");
647649
sRet = ""; // undefined element must be empty
648650
}
649-
650-
if (m_pParent == nullptr) {
651+
652+
if (m_pParent == WSJCPP_NULL) {
651653
removeLastCharNewLine(sRet);
652654
}
653655
return sRet;
@@ -1085,8 +1087,8 @@ WsjcppYamlCursor::WsjcppYamlCursor(WsjcppYamlNode *pCurrentNode) {
10851087

10861088
// ---------------------------------------------------------------------
10871089

1088-
WsjcppYamlCursor::WsjcppYamlCursor()
1089-
: WsjcppYamlCursor(nullptr) {
1090+
WsjcppYamlCursor::WsjcppYamlCursor()
1091+
: WsjcppYamlCursor(WSJCPP_NULL) {
10901092
// nothing
10911093
}
10921094

@@ -1099,49 +1101,49 @@ WsjcppYamlCursor::~WsjcppYamlCursor() {
10991101
// ---------------------------------------------------------------------
11001102

11011103
bool WsjcppYamlCursor::isNull() const {
1102-
return m_pCurrentNode == nullptr;
1104+
return m_pCurrentNode == WSJCPP_NULL;
11031105
}
11041106

11051107
// ---------------------------------------------------------------------
11061108

11071109
bool WsjcppYamlCursor::isUndefined() const {
1108-
return m_pCurrentNode != nullptr && m_pCurrentNode->isUndefined();
1110+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isUndefined();
11091111
}
11101112

11111113
// ---------------------------------------------------------------------
11121114

11131115
bool WsjcppYamlCursor::isValue() const {
1114-
return m_pCurrentNode != nullptr && m_pCurrentNode->isValue();
1116+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isValue();
11151117
}
11161118

11171119
// ---------------------------------------------------------------------
11181120

11191121
bool WsjcppYamlCursor::isArray() const {
1120-
return m_pCurrentNode != nullptr && m_pCurrentNode->isArray();
1122+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isArray();
11211123
}
11221124

11231125
// ---------------------------------------------------------------------
11241126

11251127
size_t WsjcppYamlCursor::size() const {
1126-
return m_pCurrentNode != nullptr && m_pCurrentNode->isArray() ? m_pCurrentNode->getLength() : -1;
1128+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isArray() ? m_pCurrentNode->getLength() : -1;
11271129
}
11281130

11291131
// ---------------------------------------------------------------------
11301132

11311133
bool WsjcppYamlCursor::isMap() const {
1132-
return m_pCurrentNode != nullptr && m_pCurrentNode->isMap();
1134+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap();
11331135
}
11341136

11351137
// ---------------------------------------------------------------------
11361138

11371139
std::vector<std::string> WsjcppYamlCursor::keys() const {
1138-
return m_pCurrentNode != nullptr && m_pCurrentNode->isMap() ? m_pCurrentNode->getKeys() : std::vector<std::string>();
1140+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap() ? m_pCurrentNode->getKeys() : std::vector<std::string>();
11391141
}
11401142

11411143
// ---------------------------------------------------------------------
11421144

11431145
bool WsjcppYamlCursor::hasKey(const std::string &sKey) const {
1144-
return m_pCurrentNode != nullptr && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sKey);
1146+
return m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sKey);
11451147
}
11461148

11471149
// ---------------------------------------------------------------------
@@ -1171,13 +1173,13 @@ bool WsjcppYamlCursor::hasKey(const std::string &sKey) const {
11711173
// ---------------------------------------------------------------------
11721174

11731175
std::string WsjcppYamlCursor::comment() {
1174-
return m_pCurrentNode != nullptr ? m_pCurrentNode->getComment() : "";
1176+
return m_pCurrentNode != WSJCPP_NULL ? m_pCurrentNode->getComment() : "";
11751177
}
11761178

11771179
// ---------------------------------------------------------------------
11781180

11791181
WsjcppYamlCursor &WsjcppYamlCursor::comment(const std::string& sComment) {
1180-
if (m_pCurrentNode != nullptr) {
1182+
if (m_pCurrentNode != WSJCPP_NULL) {
11811183
m_pCurrentNode->setComment(sComment);
11821184
}
11831185
return *this;
@@ -1186,11 +1188,11 @@ WsjcppYamlCursor &WsjcppYamlCursor::comment(const std::string& sComment) {
11861188
// ---------------------------------------------------------------------
11871189

11881190
std::string WsjcppYamlCursor::valStr() const {
1189-
return m_pCurrentNode != nullptr ? m_pCurrentNode->getValue() : "";
1191+
return m_pCurrentNode != WSJCPP_NULL ? m_pCurrentNode->getValue() : "";
11901192
}
11911193

11921194
WsjcppYamlCursor &WsjcppYamlCursor::val(const std::string &sValue) {
1193-
if (m_pCurrentNode != nullptr) {
1195+
if (m_pCurrentNode != WSJCPP_NULL) {
11941196
m_pCurrentNode->setValue(sValue); // TODO reserch need or not add quotes
11951197
}
11961198
return *this;
@@ -1202,10 +1204,10 @@ WsjcppYamlCursor &WsjcppYamlCursor::val(const char *sValue) {
12021204
}
12031205

12041206
int WsjcppYamlCursor::valInt() const {
1205-
if (m_pCurrentNode != nullptr) {
1207+
if (m_pCurrentNode != WSJCPP_NULL) {
12061208
std::string sValue = m_pCurrentNode->getValue();
12071209
sValue = WsjcppYaml::toLower(sValue);
1208-
int nValue = std::strtol(sValue.c_str(), nullptr, 10);
1210+
int nValue = std::strtol(sValue.c_str(), WSJCPP_NULL, 10);
12091211
if (std::to_string(nValue) != sValue) {
12101212
throw std::runtime_error(TAG + ": valInt, Element must be int but have a string" + m_pCurrentNode->getForLogFormat());
12111213
}
@@ -1215,14 +1217,14 @@ int WsjcppYamlCursor::valInt() const {
12151217
}
12161218

12171219
WsjcppYamlCursor &WsjcppYamlCursor::val(int nValue) {
1218-
if (m_pCurrentNode != nullptr) {
1220+
if (m_pCurrentNode != WSJCPP_NULL) {
12191221
m_pCurrentNode->setValue(std::to_string(nValue));
12201222
}
12211223
return *this;
12221224
}
12231225

12241226
bool WsjcppYamlCursor::valBool() const {
1225-
if (m_pCurrentNode != nullptr) {
1227+
if (m_pCurrentNode != WSJCPP_NULL) {
12261228
std::string sValue = m_pCurrentNode->getValue();
12271229
sValue = WsjcppYaml::toLower(sValue);
12281230
if (sValue == "yes" || sValue == "true") {
@@ -1238,7 +1240,7 @@ bool WsjcppYamlCursor::valBool() const {
12381240
}
12391241

12401242
WsjcppYamlCursor &WsjcppYamlCursor::val(bool bValue) {
1241-
if (m_pCurrentNode != nullptr) {
1243+
if (m_pCurrentNode != WSJCPP_NULL) {
12421244
m_pCurrentNode->setValue((bValue ? "yes" : "no"));
12431245
}
12441246
return *this;
@@ -1249,22 +1251,22 @@ WsjcppYamlNode *WsjcppYamlCursor::node() {
12491251
}
12501252

12511253
WsjcppYamlCursor WsjcppYamlCursor::operator[](int idx) const {
1252-
if (m_pCurrentNode != nullptr && m_pCurrentNode->isArray() && idx < m_pCurrentNode->getLength() && idx >= 0) {
1254+
if (m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isArray() && idx < m_pCurrentNode->getLength() && idx >= 0) {
12531255
return WsjcppYamlCursor(m_pCurrentNode->getElement(idx));
12541256
}
12551257
return WsjcppYamlCursor();
12561258
}
12571259

12581260
WsjcppYamlCursor WsjcppYamlCursor::operator[](const std::string &sName) const {
1259-
if (m_pCurrentNode != nullptr && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sName)) {
1261+
if (m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sName)) {
12601262
return WsjcppYamlCursor(m_pCurrentNode->getElement(sName));
12611263
}
12621264
return WsjcppYamlCursor();
12631265
}
12641266

12651267
WsjcppYamlCursor WsjcppYamlCursor::operator[](const char *pName) const {
12661268
std::string sName(pName);
1267-
if (m_pCurrentNode != nullptr && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sName)) {
1269+
if (m_pCurrentNode != WSJCPP_NULL && m_pCurrentNode->isMap() && m_pCurrentNode->hasElement(sName)) {
12681270
return WsjcppYamlCursor(m_pCurrentNode->getElement(sName));
12691271
}
12701272
return WsjcppYamlCursor();
@@ -1290,9 +1292,9 @@ WsjcppYamlCursor& WsjcppYamlCursor::operator=(const bool &bVal) {
12901292
// WsjcppYaml
12911293

12921294
WsjcppYaml::WsjcppYaml() {
1293-
m_pRoot = new WsjcppYamlNode(nullptr, this, WsjcppYamlPlaceInFile(), WSJCPP_YAML_NODE_MAP);
1295+
m_pRoot = new WsjcppYamlNode(WSJCPP_NULL, this, WsjcppYamlPlaceInFile(), WSJCPP_YAML_NODE_MAP);
12941296
TAG = "WsjcppYaml";
1295-
m_pLog = nullptr;
1297+
m_pLog = WSJCPP_NULL;
12961298
}
12971299

12981300
WsjcppYaml::~WsjcppYaml() {
@@ -1305,7 +1307,7 @@ void WsjcppYaml::setLogger(IWsjcppYamlLog *pLog) {
13051307

13061308
void WsjcppYaml::clear() {
13071309
delete m_pRoot;
1308-
m_pRoot = nullptr;
1310+
m_pRoot = WSJCPP_NULL;
13091311
}
13101312

13111313
bool WsjcppYaml::loadFromFile(const std::string &sFileName, std::string &sError) {
@@ -1323,7 +1325,7 @@ bool WsjcppYaml::saveToFile(const std::string &sFileName, std::string &sError) {
13231325
std::string sBuffer = m_pRoot->toString() + "\n"; // last empty line must be always
13241326
if (!WsjcppYaml::writeFile(sFileName, sBuffer)) {
13251327
sError = "Could not save to file";
1326-
return false;
1328+
return false;
13271329
}
13281330
return true;
13291331
}
@@ -1473,8 +1475,8 @@ std::vector<std::string> WsjcppYaml::splitToLines(const std::string &sBuffer) {
14731475

14741476
bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer, std::string &sError) {
14751477
this->clear();
1476-
if (m_pRoot == nullptr) {
1477-
m_pRoot = new WsjcppYamlNode(nullptr, this, WsjcppYamlPlaceInFile(), WSJCPP_YAML_NODE_MAP);
1478+
if (m_pRoot == WSJCPP_NULL) {
1479+
m_pRoot = new WsjcppYamlNode(WSJCPP_NULL, this, WsjcppYamlPlaceInFile(), WSJCPP_YAML_NODE_MAP);
14781480
}
14791481

14801482
std::vector<std::string> vLines = this->splitToLines(sBuffer);
@@ -1492,7 +1494,7 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
14921494
if (!m_parseLine.parseLine(m_parsePlaceInFile.getLine(), sError)) {
14931495
return false;
14941496
}
1495-
1497+
14961498
bool isEmptyName = m_parseLine.isEmptyName();
14971499
bool isEmptyValue = m_parseLine.isEmptyValue();
14981500
bool isArrayItem = m_parseLine.isArrayItem();
@@ -1505,7 +1507,7 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
15051507
}
15061508

15071509
if (m_parseLine.isEmptyLine()) {
1508-
if (m_pParseCurrentParentNode != nullptr) {
1510+
if (m_pParseCurrentParentNode != WSJCPP_NULL) {
15091511
// WsjcppLog::warn(TAG, "here empty line and parent exists " + m_parsePlaceInFile.getForLogFormat());
15101512
if (m_pParseCurrentParentNode->isArray() || m_pParseCurrentParentNode->isMap() || m_pParseCurrentParentNode->isUndefined()) {
15111513
// WsjcppLog::warn(TAG, "array, map or undefined");
@@ -1517,7 +1519,7 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
15171519
);
15181520
pNode->setNodeIndents(m_vStackDiffNodeIndents);
15191521
m_pParseCurrentParentNode->appendElement(pNode);
1520-
} else if (m_pParseCurrentParentNode->getParent() != nullptr && (m_pParseCurrentParentNode->getParent()->isArray() || m_pParseCurrentParentNode->getParent()->isMap())) {
1522+
} else if (m_pParseCurrentParentNode->getParent() != WSJCPP_NULL && (m_pParseCurrentParentNode->getParent()->isArray() || m_pParseCurrentParentNode->getParent()->isMap())) {
15211523
// WsjcppLog::warn(TAG, "parent exists and parent map or array");
15221524
WsjcppYamlNode *pNode = new WsjcppYamlNode(
15231525
m_pParseCurrentParentNode->getParent(),
@@ -1544,15 +1546,15 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
15441546
m_vStackDiffNodeIndents.clear();
15451547
m_vStackDiffNodeIndents.push_back(0);
15461548
}
1547-
1549+
15481550
// switch to parent
15491551
while (nDiffIndent < 0 && m_nParseCurrentIndent != m_parseLine.getIndent()) {
1550-
if (m_pParseCurrentParentNode == nullptr) {
1551-
sError = "Current node is nullptr, line: " + std::to_string(nLine);
1552+
if (m_pParseCurrentParentNode == WSJCPP_NULL) {
1553+
sError = "Current node is null, line: " + std::to_string(nLine);
15521554
return false;
15531555
}
1554-
if (m_pParseCurrentParentNode->getParent() == nullptr) {
1555-
sError = "Parent of current node is nullptr, line: " + std::to_string(nLine);
1556+
if (m_pParseCurrentParentNode->getParent() == WSJCPP_NULL) {
1557+
sError = "Parent of current node is null, line: " + std::to_string(nLine);
15561558
return false;
15571559
}
15581560
m_nParseCurrentIndent = m_nParseCurrentIndent - m_vStackDiffNodeIndents.back();
@@ -1561,7 +1563,7 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
15611563
if (m_nParseCurrentIndent < m_parseLine.getIndent()) {
15621564
sError = "Wrong indent, expected "
15631565
"'" + std::to_string(m_parseLine.getIndent()) + "',"
1564-
" but got '" +
1566+
" but got '" +
15651567
" in line: (" + sFileName + ":" + std::to_string(nLine) + ")";
15661568
return false;
15671569
}
@@ -1614,14 +1616,14 @@ void WsjcppYaml::process_hasName_emptyValue_arrayItem() {
16141616
void WsjcppYaml::process_hasName_emptyValue_noArrayItem() {
16151617
// std::cout << "process_hasName_emptyValue_noArrayItem" << std::endl;
16161618
if (m_parseLine.getIndent() == m_pParseCurrentParentNode->getNodeIndent()) {
1617-
if (m_pParseCurrentParentNode->getParent() != nullptr) {
1619+
if (m_pParseCurrentParentNode->getParent() != WSJCPP_NULL) {
16181620
m_pParseCurrentParentNode = m_pParseCurrentParentNode->getParent();
16191621
}
16201622
}
16211623
WsjcppYamlNode *pNode = new WsjcppYamlNode(
16221624
m_pParseCurrentParentNode,
16231625
this,
1624-
m_parsePlaceInFile,
1626+
m_parsePlaceInFile,
16251627
WSJCPP_YAML_NODE_UNDEFINED
16261628
);
16271629
if (m_parseLine.getValueQuotes() != WSJCPP_YAML_QUOTES_NONE) {

0 commit comments

Comments
 (0)