@@ -78,15 +78,43 @@ int WsjcppObjTreeNodeBuilding::getNumberOfFloors() {
78
78
// ---------------------------------------------------------------------
79
79
80
80
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 ;
83
95
}
84
96
85
97
// ---------------------------------------------------------------------
86
98
87
99
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 ;
90
118
}
91
119
92
120
// ---------------------------------------------------------------------
@@ -97,3 +125,55 @@ std::string WsjcppObjTreeNodeBuilding::toString(const std::string &sIntent) {
97
125
+ " \n " + sIntent + " number-of-floors: " + std::to_string (m_nNumberOfFloors);
98
126
;
99
127
}
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
+ }
0 commit comments