2020#include < cstddef>
2121#include < istream>
2222#include < sstream>
23+ #include < utility>
2324#include < vector>
2425
2526#include " geo/ByteOrderDataInStream.h"
@@ -75,19 +76,17 @@ unsigned char ASCIIHexToUChar(char val) {
7576 }
7677}
7778
78- GeoParseStatus WkbParse::parse_wkb (std::istream& is, GeoShape** shape) {
79+ GeoParseStatus WkbParse::parse_wkb (std::istream& is, std::unique_ptr< GeoShape>& shape) {
7980 WkbParseContext ctx;
8081
81- ctx = *( WkbParse::read_hex (is, & ctx) );
82+ WkbParse::read_hex (is, ctx);
8283 if (ctx.parse_status == GEO_PARSE_OK) {
83- *shape = ctx.shape ;
84- } else {
85- ctx.parse_status = GEO_PARSE_WKT_SYNTAX_ERROR;
84+ shape = std::move (ctx.shape );
8685 }
8786 return ctx.parse_status ;
8887}
8988
90- WkbParseContext* WkbParse::read_hex (std::istream& is, WkbParseContext* ctx) {
89+ void WkbParse::read_hex (std::istream& is, WkbParseContext& ctx) {
9190 // setup input/output stream
9291 std::stringstream os (std::ios_base::binary | std::ios_base::in | std::ios_base::out);
9392
@@ -99,8 +98,8 @@ WkbParseContext* WkbParse::read_hex(std::istream& is, WkbParseContext* ctx) {
9998
10099 const int input_low = is.get ();
101100 if (input_low == std::char_traits<char >::eof ()) {
102- ctx-> parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
103- return ctx ;
101+ ctx. parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
102+ return ;
104103 }
105104
106105 const char high = static_cast <char >(input_high);
@@ -109,80 +108,79 @@ WkbParseContext* WkbParse::read_hex(std::istream& is, WkbParseContext* ctx) {
109108 const unsigned char result_high = ASCIIHexToUChar (high);
110109 const unsigned char result_low = ASCIIHexToUChar (low);
111110
112- const unsigned char value = static_cast <unsigned char >((result_high << 4 ) + result_low);
111+ const auto value = static_cast <unsigned char >((result_high << 4 ) + result_low);
113112
114113 // write the value to the output stream
115114 os << value;
116115 }
117- return WkbParse::read (os, ctx);
116+ WkbParse::read (os, ctx);
118117}
119118
120- WkbParseContext* WkbParse::read (std::istream& is, WkbParseContext* ctx) {
119+ void WkbParse::read (std::istream& is, WkbParseContext& ctx) {
121120 is.seekg (0 , std::ios::end);
122121 auto size = is.tellg ();
123122 is.seekg (0 , std::ios::beg);
124123
125124 // Check if size is valid
126125 if (size <= 0 ) {
127- ctx-> parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
128- return ctx ;
126+ ctx. parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
127+ return ;
129128 }
130129
131130 std::vector<unsigned char > buf (static_cast <size_t >(size));
132131 if (!is.read (reinterpret_cast <char *>(buf.data ()), static_cast <std::streamsize>(size))) {
133- ctx-> parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
134- return ctx ;
132+ ctx. parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
133+ return ;
135134 }
136135
137136 // Ensure we have at least one byte for byte order
138137 if (buf.empty ()) {
139- ctx-> parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
140- return ctx ;
138+ ctx. parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
139+ return ;
141140 }
142141
143142 // First read the byte order using machine endian
144143 auto byteOrder = buf[0 ];
145144
146145 // Create ByteOrderDataInStream with the correct byte order
147146 if (byteOrder == byteOrder::wkbNDR) {
148- ctx-> dis = ByteOrderDataInStream (buf.data (), buf.size ());
149- ctx-> dis .setOrder (ByteOrderValues::ENDIAN_LITTLE);
147+ ctx. dis = ByteOrderDataInStream (buf.data (), buf.size ());
148+ ctx. dis .setOrder (ByteOrderValues::ENDIAN_LITTLE);
150149 } else if (byteOrder == byteOrder::wkbXDR) {
151- ctx-> dis = ByteOrderDataInStream (buf.data (), buf.size ());
152- ctx-> dis .setOrder (ByteOrderValues::ENDIAN_BIG);
150+ ctx. dis = ByteOrderDataInStream (buf.data (), buf.size ());
151+ ctx. dis .setOrder (ByteOrderValues::ENDIAN_BIG);
153152 } else {
154- ctx-> parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
155- return ctx ;
153+ ctx. parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
154+ return ;
156155 }
157156
158157 std::unique_ptr<GeoShape> shape = readGeometry (ctx);
159158 if (!shape) {
160- ctx-> parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
161- return ctx ;
159+ ctx. parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
160+ return ;
162161 }
163162
164- ctx->shape = shape.release ();
165- return ctx;
163+ ctx.shape = std::move (shape);
166164}
167165
168- std::unique_ptr<GeoShape> WkbParse::readGeometry (WkbParseContext* ctx) {
166+ std::unique_ptr<GeoShape> WkbParse::readGeometry (WkbParseContext& ctx) {
169167 try {
170168 // Ensure we have enough data to read
171- if (ctx-> dis .size () < 5 ) { // At least 1 byte for order and 4 bytes for type
169+ if (ctx. dis .size () < 5 ) { // At least 1 byte for order and 4 bytes for type
172170 return nullptr ;
173171 }
174172
175173 // Skip the byte order as we've already handled it
176- ctx-> dis .readByte ();
174+ ctx. dis .readByte ();
177175
178- uint32_t typeInt = ctx-> dis .readUnsigned ();
176+ uint32_t typeInt = ctx. dis .readUnsigned ();
179177
180178 // Check if geometry has SRID
181179 bool has_srid = (typeInt & WKB_SRID_FLAG) != 0 ;
182180
183181 // Read SRID if present
184182 if (has_srid) {
185- ctx-> dis .readUnsigned (); // Read and store SRID if needed
183+ ctx. dis .readUnsigned (); // Read and store SRID if needed
186184 }
187185
188186 // Get the base geometry type
@@ -211,7 +209,7 @@ std::unique_ptr<GeoShape> WkbParse::readGeometry(WkbParseContext* ctx) {
211209 }
212210}
213211
214- std::unique_ptr<GeoPoint> WkbParse::readPoint (WkbParseContext* ctx) {
212+ std::unique_ptr<GeoPoint> WkbParse::readPoint (WkbParseContext& ctx) {
215213 GeoCoordinateList coords = WkbParse::readCoordinateList (1 , ctx);
216214 if (coords.list .empty ()) {
217215 return nullptr ;
@@ -225,8 +223,8 @@ std::unique_ptr<GeoPoint> WkbParse::readPoint(WkbParseContext* ctx) {
225223 return point;
226224}
227225
228- std::unique_ptr<GeoLine> WkbParse::readLine (WkbParseContext* ctx) {
229- uint32_t size = ctx-> dis .readUnsigned ();
226+ std::unique_ptr<GeoLine> WkbParse::readLine (WkbParseContext& ctx) {
227+ uint32_t size = ctx. dis .readUnsigned ();
230228 if (minMemSize (wkbLine, size, ctx) != GEO_PARSE_OK) {
231229 return nullptr ;
232230 }
@@ -244,15 +242,15 @@ std::unique_ptr<GeoLine> WkbParse::readLine(WkbParseContext* ctx) {
244242 return line;
245243}
246244
247- std::unique_ptr<GeoPolygon> WkbParse::readPolygon (WkbParseContext* ctx) {
248- uint32_t num_loops = ctx-> dis .readUnsigned ();
245+ std::unique_ptr<GeoPolygon> WkbParse::readPolygon (WkbParseContext& ctx) {
246+ uint32_t num_loops = ctx. dis .readUnsigned ();
249247 if (minMemSize (wkbPolygon, num_loops, ctx) != GEO_PARSE_OK) {
250248 return nullptr ;
251249 }
252250
253251 GeoCoordinateListList coordss;
254252 for (uint32_t i = 0 ; i < num_loops; ++i) {
255- uint32_t size = ctx-> dis .readUnsigned ();
253+ uint32_t size = ctx. dis .readUnsigned ();
256254 if (size < 3 ) { // A polygon loop must have at least 3 points
257255 return nullptr ;
258256 }
@@ -262,7 +260,7 @@ std::unique_ptr<GeoPolygon> WkbParse::readPolygon(WkbParseContext* ctx) {
262260 if (coords->list .empty ()) {
263261 return nullptr ;
264262 }
265- coordss.add (coords. release ( ));
263+ coordss.add (std::move (coords ));
266264 }
267265
268266 std::unique_ptr<GeoPolygon> polygon = GeoPolygon::create_unique ();
@@ -273,22 +271,22 @@ std::unique_ptr<GeoPolygon> WkbParse::readPolygon(WkbParseContext* ctx) {
273271 return polygon;
274272}
275273
276- GeoCoordinateList WkbParse::readCoordinateList (unsigned size, WkbParseContext* ctx) {
274+ GeoCoordinateList WkbParse::readCoordinateList (unsigned size, WkbParseContext& ctx) {
277275 GeoCoordinateList coords;
278276 for (uint32_t i = 0 ; i < size; i++) {
279277 if (!readCoordinate (ctx)) {
280278 return GeoCoordinateList ();
281279 }
282280 unsigned int j = 0 ;
283281 GeoCoordinate coord;
284- coord.x = ctx-> ordValues [j++];
285- coord.y = ctx-> ordValues [j++];
282+ coord.x = ctx. ordValues [j++];
283+ coord.y = ctx. ordValues [j++];
286284 coords.add (coord);
287285 }
288286 return coords;
289287}
290288
291- GeoParseStatus WkbParse::minMemSize (int wkbType, uint64_t size, WkbParseContext* ctx) {
289+ GeoParseStatus WkbParse::minMemSize (int wkbType, uint64_t size, WkbParseContext& ctx) {
292290 uint64_t minSize = 0 ;
293291 constexpr uint64_t minCoordSize = 2 * sizeof (double );
294292 // constexpr uint64_t minPtSize = (1+4) + minCoordSize;
@@ -305,14 +303,14 @@ GeoParseStatus WkbParse::minMemSize(int wkbType, uint64_t size, WkbParseContext*
305303 minSize = size * minLoopSize;
306304 break ;
307305 }
308- if (ctx-> dis .size () < minSize) {
306+ if (ctx. dis .size () < minSize) {
309307 return GEO_PARSE_WKB_SYNTAX_ERROR;
310308 }
311309 return GEO_PARSE_OK;
312310}
313- bool WkbParse::readCoordinate (WkbParseContext* ctx) {
314- for (std::size_t i = 0 ; i < ctx-> inputDimension ; ++i) {
315- ctx-> ordValues [i] = ctx-> dis .readDouble ();
311+ bool WkbParse::readCoordinate (WkbParseContext& ctx) {
312+ for (std::size_t i = 0 ; i < ctx. inputDimension ; ++i) {
313+ ctx. ordValues [i] = ctx. dis .readDouble ();
316314 }
317315
318316 return true ;
0 commit comments