Skip to content

Commit 01515e2

Browse files
committed
Fixed #8 redesign write to file
1 parent 0723ffd commit 01515e2

File tree

8 files changed

+47
-99
lines changed

8 files changed

+47
-99
lines changed

scripts.wsjcpp/generate.WsjcppObjTreeNode

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class " class_name " : public WsjcppObjTreeNode {
6666
void setValue(const " init_class_name " &nValue);
6767

6868
// WsjcppObjTreeNode
69-
virtual int getDataSize() override;
70-
virtual const char *getData() override;
69+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) override;
70+
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
7171
virtual std::string toString(const std::string &sIntent = \"\") override;
7272
private:
7373
" init_class_name " m_value;
@@ -120,16 +120,16 @@ void " class_name "::setValue(const " init_class_name " &nValue) {
120120

121121
// ---------------------------------------------------------------------
122122

123-
int " class_name "::getDataSize() {
124-
WsjcppLog::throw_err(\"" class_name "\", \"::getDataSize() Not implemented\");
125-
return sizeof(" init_class_name ");
123+
bool " class_name "::writeDataPartToFile(std::ofstream &f, std::string &sError) {
124+
sError = \"" class_name " - writeDataPartToFile not implemented\";
125+
return false;
126126
}
127127

128128
// ---------------------------------------------------------------------
129129

130-
const char *" class_name "::getData() {
131-
WsjcppLog::throw_err(\"" class_name "\", \"::getData() Not implemented\");
132-
return reinterpret_cast<const char *>(&m_value);
130+
bool " class_name "::readDataPartFromFile(std::ifstream &f, std::string &sError) {
131+
sError = \"" class_name " - readDataPartFromFile not implemented\";
132+
return false;
133133
}
134134

135135
// ---------------------------------------------------------------------

src/examples/wsjcpp_obj_tree_node_building.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,16 @@ int WsjcppObjTreeNodeBuilding::getNumberOfFloors() {
7777

7878
// ---------------------------------------------------------------------
7979

80-
int WsjcppObjTreeNodeBuilding::getDataSize() {
81-
WsjcppLog::throw_err("WsjcppObjTreeNodeBuilding", "::getDataSize() Not implemented");
82-
return sizeof(Address);
83-
}
84-
85-
// ---------------------------------------------------------------------
86-
87-
const char *WsjcppObjTreeNodeBuilding::getData() {
88-
WsjcppLog::throw_err("WsjcppObjTreeNodeBuilding", "::getData() Not implemented");
89-
return reinterpret_cast<const char *>(&m_value);
80+
bool WsjcppObjTreeNodeBuilding::writeDataPartToFile(std::ofstream &f, std::string &sError) {
81+
sError = "Not implemented";
82+
return false;
9083
}
9184

9285
// ---------------------------------------------------------------------
9386

9487
bool WsjcppObjTreeNodeBuilding::readDataPartFromFile(std::ifstream &f, std::string &sError) {
95-
88+
sError = "Not implemented";
89+
return false;
9690
}
9791

9892
// ---------------------------------------------------------------------

src/examples/wsjcpp_obj_tree_node_building.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ class WsjcppObjTreeNodeBuilding : public WsjcppObjTreeNode {
3232
int getNumberOfFloors();
3333

3434
// WsjcppObjTreeNode
35-
virtual int getDataSize() override;
36-
virtual const char *getData() override;
35+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) override;
3736
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
3837
virtual std::string toString(const std::string &sIntent = "") override;
3938

src/wsjcpp_obj_tree.cpp

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,9 @@ bool WsjcppObjTree::writeTreeToFile(const std::string &sFilename, std::string &s
225225
// write id
226226
this->writeUInt32(f, pNode->getId());
227227

228-
// write data size
229-
int nDataSize = pNode->getDataSize();
230-
this->writeUInt32(f, nDataSize);
231-
232-
// write data
233-
f.write(pNode->getData(), nDataSize);
228+
if (!pNode->writeDataPartToFile(f, sError)) {
229+
return false;
230+
}
234231
}
235232
f.close();
236233
return true;
@@ -440,15 +437,13 @@ void WsjcppObjTreeNodeString::setValue(const std::string &sValue) {
440437

441438
// ---------------------------------------------------------------------
442439

443-
int WsjcppObjTreeNodeString::getDataSize() {
444-
return m_sValue.size();
445-
}
446-
447-
// ---------------------------------------------------------------------
448-
449-
const char *WsjcppObjTreeNodeString::getData() {
450-
return m_sValue.c_str();
451-
}
440+
bool WsjcppObjTreeNodeString::writeDataPartToFile(std::ofstream &f, std::string &sError) {
441+
uint32_t nStringLen = m_sValue.size();
442+
const char *pData = reinterpret_cast<const char *>(&nStringLen);
443+
f.write(pData, 4); // Write size of string
444+
f.write(m_sValue.c_str(), nStringLen);
445+
return true;
446+
};
452447

453448
// ---------------------------------------------------------------------
454449

@@ -500,29 +495,18 @@ void WsjcppObjTreeNodeInteger::setValue(int32_t nValue) {
500495

501496
// ---------------------------------------------------------------------
502497

503-
int WsjcppObjTreeNodeInteger::getDataSize() {
504-
return sizeof(uint32_t);
505-
}
506-
507-
// ---------------------------------------------------------------------
508-
509-
const char *WsjcppObjTreeNodeInteger::getData() {
510-
const char *p = reinterpret_cast<const char *>(&m_nValue);
511-
return p;
512-
}
498+
bool WsjcppObjTreeNodeInteger::writeDataPartToFile(std::ofstream &f, std::string &sError) {
499+
static_assert(sizeof(uint32_t) == 4, "Expected sizeof(uint32_t) == 4");
500+
const char *pData = reinterpret_cast<const char *>(&m_nValue);
501+
f.write(pData, 4);
502+
return true;
503+
};
513504

514505
// ---------------------------------------------------------------------
515506

516507
bool WsjcppObjTreeNodeInteger::readDataPartFromFile(std::ifstream &f, std::string &sError) {
517-
// size
518-
// TODO remove - because this not need
519-
char arrBytes[4];
520-
f.read(arrBytes, 4);
521-
if (!f) {
522-
sError = "WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
523-
return false;
524-
}
525508
// value
509+
char arrBytes[4];
526510
f.read(arrBytes, 4);
527511
if (!f) {
528512
sError = "WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
@@ -561,30 +545,19 @@ void WsjcppObjTreeNodeFloat::setValue(float nValue) {
561545

562546
// ---------------------------------------------------------------------
563547

564-
int WsjcppObjTreeNodeFloat::getDataSize() {
548+
bool WsjcppObjTreeNodeFloat::writeDataPartToFile(std::ofstream &f, std::string &sError) {
565549
static_assert(sizeof(float) == 4, "Expected sizeof(float) == 4");
566-
return sizeof(float);
567-
}
568-
569-
// ---------------------------------------------------------------------
570-
571-
const char *WsjcppObjTreeNodeFloat::getData() {
572-
return reinterpret_cast<const char *>(&m_nValue);
573-
}
550+
const char *pData = reinterpret_cast<const char *>(&m_nValue);
551+
f.write(pData, 4);
552+
return true;
553+
};
574554

575555
// ---------------------------------------------------------------------
576556

577557
bool WsjcppObjTreeNodeFloat::readDataPartFromFile(std::ifstream &f, std::string &sError) {
578558
static_assert(sizeof(float) == 4, "Expected sizeof(float) == 4");
579-
// size
580-
// TODO remove - because this not need
581-
char arrBytes[4];
582-
f.read(arrBytes, 4);
583-
if (!f) {
584-
sError = "WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
585-
return false;
586-
}
587559
// value
560+
char arrBytes[4];
588561
f.read(arrBytes, 4);
589562
if (!f) {
590563
sError = "WsjcppObjTreeNodeFloat. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
@@ -622,29 +595,17 @@ void WsjcppObjTreeNodeDouble::setValue(float nValue) {
622595

623596
// ---------------------------------------------------------------------
624597

625-
int WsjcppObjTreeNodeDouble::getDataSize() {
598+
bool WsjcppObjTreeNodeDouble::writeDataPartToFile(std::ofstream &f, std::string &sError) {
626599
static_assert(sizeof(double) == 8, "Expected sizeof(double) == 8");
627-
return sizeof(double);
628-
}
629-
630-
// ---------------------------------------------------------------------
631-
632-
const char *WsjcppObjTreeNodeDouble::getData() {
633-
return reinterpret_cast<const char *>(&m_nValue);
600+
const char *pData = reinterpret_cast<const char *>(&m_nValue);
601+
f.write(pData, 8);
602+
return true;
634603
}
635604

636605
// ---------------------------------------------------------------------
637606

638607
bool WsjcppObjTreeNodeDouble::readDataPartFromFile(std::ifstream &f, std::string &sError) {
639608
static_assert(sizeof(double) == 8, "Expected sizeof(double) == 8");
640-
// size
641-
// TODO remove - because this not need
642-
char arrBytes4[4];
643-
f.read(arrBytes4, 4);
644-
if (!f) {
645-
sError = "WsjcppObjTreeNodeInteger. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
646-
return false;
647-
}
648609
// value
649610
char arrBytes[8];
650611
f.read(arrBytes, 8);

src/wsjcpp_obj_tree.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class WsjcppObjTreeNode {
2424
uint16_t getType();
2525

2626
// virtual zero methods will be deny create basic class
27-
virtual int getDataSize() = 0;
28-
virtual const char *getData() = 0;
27+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) = 0;
2928
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) = 0;
3029

3130
virtual std::string toString(const std::string &sIntent = "") = 0;
@@ -177,8 +176,7 @@ class WsjcppObjTreeNodeString : public WsjcppObjTreeNode {
177176
void setValue(const std::string &sValue);
178177

179178
// WsjcppObjTreeNode
180-
virtual int getDataSize() override;
181-
virtual const char *getData() override;
179+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) override;
182180
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
183181
virtual std::string toString(const std::string &sIntent = "") override;
184182

@@ -198,8 +196,7 @@ class WsjcppObjTreeNodeInteger : public WsjcppObjTreeNode {
198196
void setValue(int32_t nValue);
199197

200198
// WsjcppObjTreeNode
201-
virtual int getDataSize() override;
202-
virtual const char *getData() override;
199+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) override;
203200
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
204201
virtual std::string toString(const std::string &sIntent = "") override;
205202

@@ -218,8 +215,7 @@ class WsjcppObjTreeNodeFloat : public WsjcppObjTreeNode {
218215
void setValue(float nValue);
219216

220217
// WsjcppObjTreeNode
221-
virtual int getDataSize() override;
222-
virtual const char *getData() override;
218+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) override;
223219
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
224220
virtual std::string toString(const std::string &sIntent = "") override;
225221

@@ -239,8 +235,7 @@ class WsjcppObjTreeNodeDouble : public WsjcppObjTreeNode {
239235
void setValue(float nValue);
240236

241237
// WsjcppObjTreeNode
242-
virtual int getDataSize() override;
243-
virtual const char *getData() override;
238+
virtual bool writeDataPartToFile(std::ofstream &f, std::string &sError) override;
244239
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
245240
virtual std::string toString(const std::string &sIntent = "") override;
246241

Binary file not shown.

unit-tests.wsjcpp/src/unit_test_find_nodes.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ bool UnitTestFindNodes::run() {
7070
compareB(bTestSuccess, "'frequency' parent of the child", pNode2->getParent() == pNode, true);
7171
compareN(bTestSuccess, "'frequency' child type", pNode2->getType(), WSJCPP_OBJ_TREE_NODE_DOUBLE);
7272
WsjcppObjTreeNodeDouble *pDouble = (WsjcppObjTreeNodeDouble *)pNode2;
73-
compareN(bTestSuccess, "'frequency' child data size", pDouble->getDataSize(), 8);
7473
compareS(bTestSuccess, "'frequency' child value", std::to_string(pDouble->getValue()), std::to_string(3.2));
7574
}
7675
}

unit-tests.wsjcpp/src/unit_test_write_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool UnitTestWriteTree::run() {
7474
int nBufferSize = 0;
7575
WsjcppCore::readFileToBuffer(sFilename, &pBuffer, nBufferSize);
7676

77-
compareN(bTestSuccess, "write to file", nBufferSize, 320);
77+
compareN(bTestSuccess, "write to file", nBufferSize, 296);
7878

7979
return bTestSuccess;
8080
}

0 commit comments

Comments
 (0)