libosmscout 1.1.1
Loading...
Searching...
No Matches
FileScanner.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_FILESCANNER_H
2#define OSMSCOUT_FILESCANNER_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2009 Tim Teulings
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <cstdio>
24#include <string>
25#include <tuple>
26#include <vector>
27
29#include <osmscout/lib/CoreFeatures.h>
30
31#include <osmscout/GeoCoord.h>
32#include <osmscout/ObjectRef.h>
33#include <osmscout/Point.h>
35
36#include <osmscout/util/Color.h>
40
41#if defined(_WIN32)
42 #include <windows.h>
43 #undef max
44 #undef min
45#endif
46
47namespace osmscout {
48
61 class OSMSCOUT_API FileScanner CLASS_FINAL
62 {
63 public:
71
72 private:
73 std::string filename;
74 std::FILE *file=nullptr;
75 mutable bool hasError=true;
76
77 // For mmap usage
78 char *mmap=nullptr;
79 FileOffset size=0;
80 FileOffset offset=0;
81
82 // For std::vector<GeoCoord> loading
83 uint8_t *byteBuffer=nullptr;
84 size_t byteBufferSize=0;
85
86 // For Windows mmap usage
87#if defined(__WIN32__) || defined(WIN32)
88 HANDLE mmfHandle=0;
89#endif
90
91 private:
92 void AssureByteBufferSize(size_t size);
93 void FreeBuffer();
94
107 char* ReadInternal(size_t bytes);
108
120 GeoCoord CreateCoord(const uint32_t &latDat,
121 const uint32_t &lonDat)
122 {
123#ifndef NDEBUG
124 if (latDat > maxRawCoordValue ||
125 lonDat > maxRawCoordValue){
126 hasError=true;
127 throw IOException(filename,"Cannot read coordinate","Coordinate is not normalised");
128 }
129#endif
130
131 return {latDat/latConversionFactor-90.0,
132 lonDat/lonConversionFactor-180.0};
133 }
134
146 void SetCoord(const uint32_t &latDat,
147 const uint32_t &lonDat,
148 Point &point)
149 {
150#ifndef NDEBUG
151 if (latDat > maxRawCoordValue ||
152 lonDat > maxRawCoordValue){
153 hasError=true;
154 throw IOException(filename,"Cannot read coordinate","Coordinate is not normalised");
155 }
156#endif
157
158 point.SetCoord(GeoCoord(latDat/latConversionFactor-90.0,
159 lonDat/lonConversionFactor-180.0));
160 }
161
172 bool ConvertBool(const char &value)
173 {
174#ifndef NDEBUG
175 if (value != 0 && value != 1){
176 hasError=true;
177 throw IOException(filename,"Cannot read bool","Bool value is not normalised");
178 }
179#endif
180 return value!=0;
181 }
182
183 public:
184 FileScanner() = default;
186
187 void Open(const std::string& filename,
188 Mode mode,
189 bool useMmap);
190 void Close();
192
193 bool IsOpen() const
194 {
195 return file!=nullptr;
196 }
197
198 bool IsEOF() const;
199
200 bool HasError() const
201 {
202 return file==nullptr || hasError;
203 }
204
205 std::string GetFilename() const;
206
207 void GotoBegin();
210
211 void Read(char* buffer, size_t bytes);
212
213 std::string ReadString();
214
215 bool ReadBool();
216
217 std::byte ReadByte();
218
219 int8_t ReadInt8();
220 int16_t ReadInt16();
221 int32_t ReadInt32();
222 int64_t ReadInt64();
223
224 uint8_t ReadUInt8();
225 uint16_t ReadUInt16();
226 uint32_t ReadUInt32();
227 uint64_t ReadUInt64();
228
229 uint16_t Read(size_t bytes);
230 uint32_t ReadUInt32(size_t bytes);
231 uint64_t ReadUInt64(size_t bytes);
232
234
236
239
243
247
249 std::tuple<GeoCoord,bool> ReadConditionalCoord();
250
259 void Read(std::vector<Point>& nodes,
260 std::vector<SegmentGeoBox> &segments,
261 GeoBox &bbox,
262 bool readIds);
263
265
266 TypeId ReadTypeId(uint8_t maxBytes);
267
268 std::vector<ObjectFileRef> ReadObjectFileRefs(size_t count);
269 };
270
275 {
276 private:
277 FileScanner& reader;
278 FileOffset lastFileOffset=0;
279
280 public:
281 explicit ObjectFileRefStreamReader(FileScanner& reader);
282
283 void Reset();
284
285 void Read(ObjectFileRef& ref);
286 };
287
288}
289
290#endif
#define OSMSCOUT_API
Definition CoreImportExport.h:45
Definition Area.h:88
uint32_t ReadUInt32(size_t bytes)
std::string GetFilename() const
bool HasError() const
Definition FileScanner.h:200
uint64_t ReadUInt64Number()
std::tuple< GeoCoord, bool > ReadConditionalCoord()
uint16_t ReadUInt16Number()
std::vector< Point > nodes
List of nodes.
Definition Way.h:57
FileOffset ReadFileOffset()
uint16_t Read(size_t bytes)
void Read(std::vector< Point > &nodes, std::vector< SegmentGeoBox > &segments, GeoBox &bbox, bool readIds)
FileOffset GetPos() const
bool IsOpen() const
Definition FileScanner.h:193
std::string ReadString()
std::vector< Segment > segments
Definition Route.h:68
uint32_t ReadUInt32Number()
Mode
Definition FileScanner.h:65
@ Normal
Definition FileScanner.h:69
@ LowMemRandom
Definition FileScanner.h:68
@ Sequential
Definition FileScanner.h:66
@ FastRandom
Definition FileScanner.h:67
ObjectFileRef ReadObjectFileRef()
std::vector< ObjectFileRef > ReadObjectFileRefs(size_t count)
FileOffset ReadFileOffset(size_t bytes)
void Open(const std::string &filename, Mode mode, bool useMmap)
TypeId ReadTypeId(uint8_t maxBytes)
Vertex2D * buffer
Definition Transformation.h:343
void SetPos(FileOffset pos)
void Read(char *buffer, size_t bytes)
GeoBox bbox
Precomputed (cache) bounding box.
Definition DataAgent.h:53
uint64_t ReadUInt64(size_t bytes)
Definition Exception.h:73
void Read(ObjectFileRef &ref)
ObjectFileRefStreamReader(FileScanner &reader)
uint16_t TypeId
Definition OSMScoutTypes.h:52
uint64_t FileOffset
Definition OSMScoutTypes.h:46
Definition Area.h:39
constexpr uint32_t maxRawCoordValue
Definition GeoCoord.h:58