Skip to content

Commit ed0b30b

Browse files
committed
Fixed read/write for WsjcppObjTreeNodeBuilding
1 parent fcb5d62 commit ed0b30b

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/examples/wsjcpp_obj_tree_node_building.cpp

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,43 @@ int WsjcppObjTreeNodeBuilding::getNumberOfFloors() {
7878
// ---------------------------------------------------------------------
7979

8080
bool WsjcppObjTreeNodeBuilding::writeDataPartToFile(std::ofstream &f, std::string &sError) {
81-
sError = "Not implemented";
82-
return false;
81+
82+
if (!this->writeUInt32(f, m_nNumberOfFloors, sError)) {
83+
return false;
84+
}
85+
86+
if (!this->writeString(f, m_value.getStreetName(), sError)) {
87+
return false;
88+
}
89+
90+
if (!this->writeString(f, m_value.getBuildingNumber(), sError)) {
91+
return false;
92+
}
93+
94+
return true;
8395
}
8496

8597
// ---------------------------------------------------------------------
8698

8799
bool WsjcppObjTreeNodeBuilding::readDataPartFromFile(std::ifstream &f, std::string &sError) {
88-
sError = "Not implemented";
89-
return false;
100+
uint32_t nFloors = 0;
101+
if (!this->readUInt32(f, nFloors, sError)) {
102+
return false;
103+
}
104+
m_nNumberOfFloors = nFloors;
105+
106+
std::string sStreet;
107+
if (!this->readString(f, sStreet, sError)) {
108+
return false;
109+
}
110+
111+
std::string sBuilding;
112+
if (!this->readString(f, sBuilding, sError)) {
113+
return false;
114+
}
115+
116+
m_value = Address(sStreet, sBuilding);
117+
return true;
90118
}
91119

92120
// ---------------------------------------------------------------------
@@ -97,3 +125,55 @@ std::string WsjcppObjTreeNodeBuilding::toString(const std::string &sIntent) {
97125
+ "\n" + sIntent + "number-of-floors: " + std::to_string(m_nNumberOfFloors);
98126
;
99127
}
128+
129+
// ---------------------------------------------------------------------
130+
131+
bool WsjcppObjTreeNodeBuilding::readUInt32(std::ifstream &f, uint32_t &nVal, std::string &sError) {
132+
uint32_t nStringLen = 0;
133+
char arrInteger[4];
134+
f.read(arrInteger, 4);
135+
if (!f) {
136+
sError = "WsjcppObjTreeNodeBuilding. Could not read string len. File broken. Can read " + std::to_string(f.gcount());
137+
return false;
138+
}
139+
nVal = *reinterpret_cast<uint32_t*>(arrInteger);
140+
return true;
141+
}
142+
143+
// ---------------------------------------------------------------------
144+
145+
bool WsjcppObjTreeNodeBuilding::writeUInt32(std::ofstream &f, uint32_t nVal, std::string &sError) {
146+
static_assert(sizeof(uint32_t) == 4, "Expected sizeof(uint32_t) == 4");
147+
const char *pData = reinterpret_cast<const char *>(&nVal);
148+
f.write(pData, 4);
149+
return true;
150+
}
151+
152+
// ---------------------------------------------------------------------
153+
154+
bool WsjcppObjTreeNodeBuilding::readString(std::ifstream &f, std::string &sVal, std::string &sError) {
155+
uint32_t nStringLen = 0;
156+
if (!this->readUInt32(f, nStringLen, sError)) {
157+
return false;
158+
}
159+
char *pStr = new char[nStringLen];
160+
f.read(pStr, nStringLen);
161+
if (!f) {
162+
delete[] pStr;
163+
sError = "WsjcppObjTreeNodeBuilding. Could not read string data. File broken. Can read " + std::to_string(f.gcount());
164+
return false;
165+
}
166+
sVal = std::string(pStr, nStringLen);
167+
delete[] pStr;
168+
return true;
169+
}
170+
171+
// ---------------------------------------------------------------------
172+
173+
bool WsjcppObjTreeNodeBuilding::writeString(std::ofstream &f, std::string sVal, std::string &sError) {
174+
if (!writeUInt32(f, sVal.size(), sError)) {
175+
return false;
176+
}
177+
f.write(sVal.c_str(), sVal.size());
178+
return true;
179+
}

src/examples/wsjcpp_obj_tree_node_building.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ class WsjcppObjTreeNodeBuilding : public WsjcppObjTreeNode {
3939
private:
4040
Address m_value;
4141
int m_nNumberOfFloors;
42+
43+
bool readUInt32(std::ifstream &f, uint32_t &nVal, std::string &sError);
44+
bool writeUInt32(std::ofstream &f, uint32_t nVal, std::string &sError);
45+
46+
bool readString(std::ifstream &f, std::string &nVal, std::string &sError);
47+
bool writeString(std::ofstream &f, std::string nVal, std::string &sError);
48+
4249
};
4350

4451
WSJCPP_OBJ_TREE_CHAIN_DECLARE_INLINE(Address, WsjcppObjTreeNodeBuilding)

0 commit comments

Comments
 (0)