Skip to content

Commit 961eb96

Browse files
committed
Fixed #5 added WSJCPP-OBJ-TREE-FILE
1 parent 0fc17b6 commit 961eb96

File tree

10 files changed

+38
-1
lines changed

10 files changed

+38
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ tmp/*
33
wsjcpp-obj-tree
44
.logs/*
55
.vscode/*
6+
some.obj-tree
7+
some2.obj-tree
8+
unit-tests.wsjcpp/data/tmp/*
69

710
# Prerequisites
811
*.d

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ addons:
1717
# Build steps
1818
script:
1919
- ./build_simple.sh
20+
- ./wsjcpp-obj-tree --example -o some.wsjcpp-obj-tree
21+
- ./wsjcpp-obj-tree -i some.wsjcpp-obj-tree -o some2.wsjcpp-obj-tree
22+
- rm some.wsjcpp-obj-tree && rm some2.wsjcpp-obj-tree
2023
- cd unit-tests.wsjcpp
2124
- ./build_simple.sh
2225
- ./unit-tests

src/examples/wsjcpp_obj_tree_node_building.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ const char *WsjcppObjTreeNodeBuilding::getData() {
9191

9292
// ---------------------------------------------------------------------
9393

94+
bool WsjcppObjTreeNodeBuilding::readDataPartFromFile(std::ifstream &f, std::string &sError) {
95+
96+
}
97+
98+
// ---------------------------------------------------------------------
99+
94100
std::string WsjcppObjTreeNodeBuilding::toString(const std::string &sIntent) {
95101
return
96102
"Building: st. " + m_value.getStreetName() + ", " + m_value.getBuildingNumber()

src/examples/wsjcpp_obj_tree_node_building.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class WsjcppObjTreeNodeBuilding : public WsjcppObjTreeNode {
3434
// WsjcppObjTreeNode
3535
virtual int getDataSize() override;
3636
virtual const char *getData() override;
37+
virtual bool readDataPartFromFile(std::ifstream &f, std::string &sError) override;
3738
virtual std::string toString(const std::string &sIntent = "") override;
3839

3940
private:

src/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ int main(int argc, const char* argv[]) {
6161

6262
std::cout << tree.toString();
6363

64+
std::string sError;
65+
tree.writeTreeToFile("some.obj-tree", sError);
66+
tree.readTreeFromFile("some.obj-tree", sError);
67+
68+
std::cout << tree.toString();
69+
WsjcppCore::removeFile("some.obj-tree");
70+
6471
return 0;
6572
}
6673

src/wsjcpp_obj_tree.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ bool WsjcppObjTree::readTreeFromFile(const std::string &sFilename, std::string &
9999
std::ifstream f;
100100
f.open(sFilename.c_str(), std::ios::in | std::ios::binary);
101101

102+
char sExpectedFileHeader[20];
103+
f.read(sExpectedFileHeader, 20);
104+
if (!f) {
105+
sError = "readTreeFromFile. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
106+
return false;
107+
}
108+
109+
if (std::string(sExpectedFileHeader,20) != "WSJCPP-OBJ-TREE-FILE") {
110+
sError = "readTreeFromFile. Expected first 20 bytes of file like this WSJCPP-OBJ-TREE-FILE";
111+
return false;
112+
}
113+
102114
uint32_t nTreeSize = 0;
103115
if (!this->readUInt32(f, nTreeSize, sError)) {
104116
return false;
@@ -167,6 +179,8 @@ bool WsjcppObjTree::writeTreeToFile(const std::string &sFilename, std::string &s
167179
std::cout << "FAILED could not create file to write " << sFilename << std::endl;
168180
return false;
169181
}
182+
static const std::string sFileHeader = "WSJCPP-OBJ-TREE-FILE";
183+
f.write(sFileHeader.c_str(), sFileHeader.length());
170184

171185
// m_nLastId
172186
int nTreeSize = m_vNodes.size();
Binary file not shown.
-296 Bytes
Binary file not shown.

unit-tests.wsjcpp/src/unit_test_read_tree.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ bool UnitTestReadTree::run() {
3737
std::string sError;
3838
bool bWrote = comp.readTreeFromFile(sFilename, sError);
3939
compareB(bTestSuccess, "read from file", bWrote, true);
40+
if (!bWrote) {
41+
WsjcppLog::err(TAG, sError);
42+
}
4043

4144
WsjcppLog::info(TAG, "\n" + comp.toString());
4245

unit-tests.wsjcpp/src/unit_test_write_tree.cpp

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

76-
compareN(bTestSuccess, "write to file", nBufferSize, 296);
76+
compareN(bTestSuccess, "write to file", nBufferSize, 316);
7777

7878
return bTestSuccess;
7979
}

0 commit comments

Comments
 (0)