libosmscout  1.1.1
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 
30 #include <osmscout/CoreFeatures.h>
31 
32 #include <osmscout/GeoCoord.h>
33 #include <osmscout/ObjectRef.h>
34 #include <osmscout/Point.h>
35 #include <osmscout/OSMScoutTypes.h>
36 
37 #include <osmscout/util/Color.h>
39 #include <osmscout/util/GeoBox.h>
40 #include <osmscout/util/Geometry.h>
41 
42 #if defined(_WIN32)
43  #include <windows.h>
44  #undef max
45  #undef min
46 #endif
47 
48 namespace osmscout {
49 
62  class OSMSCOUT_API FileScanner CLASS_FINAL
63  {
64  public:
65  enum Mode
66  {
70  Normal
71  };
72 
73  private:
74  std::string filename;
75  std::FILE *file;
76  mutable bool hasError;
77 
78  // For mmap usage
79  char *mmap;
80  FileOffset size;
81  FileOffset offset;
82 
83  // For std::vector<GeoCoord> loading
84  uint8_t *byteBuffer;
85  size_t byteBufferSize;
86 
87  // For Windows mmap usage
88 #if defined(__WIN32__) || defined(WIN32)
89  HANDLE mmfHandle;
90 #endif
91 
92  private:
93  void AssureByteBufferSize(size_t size);
94  void FreeBuffer();
95 
108  char* ReadInternal(size_t bytes);
109 
121  GeoCoord CreateCoord(const uint32_t &latDat,
122  const uint32_t &lonDat)
123  {
124 #ifndef NDEBUG
125  if (latDat > maxRawCoordValue ||
126  lonDat > maxRawCoordValue){
127  hasError=true;
128  throw IOException(filename,"Cannot read coordinate","Coordinate is not normalised");
129  }
130 #endif
131 
132  return {latDat/latConversionFactor-90.0,
133  lonDat/lonConversionFactor-180.0};
134  }
135 
147  void SetCoord(const uint32_t &latDat,
148  const uint32_t &lonDat,
149  Point &point)
150  {
151 #ifndef NDEBUG
152  if (latDat > maxRawCoordValue ||
153  lonDat > maxRawCoordValue){
154  hasError=true;
155  throw IOException(filename,"Cannot read coordinate","Coordinate is not normalised");
156  }
157 #endif
158 
159  point.SetCoord(GeoCoord(latDat/latConversionFactor-90.0,
160  lonDat/lonConversionFactor-180.0));
161  }
162 
173  bool ConvertBool(const char &value)
174  {
175 #ifndef NDEBUG
176  if (value != 0 && value != 1){
177  hasError=true;
178  throw IOException(filename,"Cannot read bool","Bool value is not normalised");
179  }
180 #endif
181  return value!=0;
182  }
183 
184  public:
185  FileScanner();
186  ~FileScanner();
187 
188  void Open(const std::string& filename,
189  Mode mode,
190  bool useMmap);
191  void Close();
192  void CloseFailsafe();
193 
194  bool IsOpen() const
195  {
196  return file!=nullptr;
197  }
198 
199  bool IsEOF() const;
200 
201  bool HasError() const
202  {
203  return file==nullptr || hasError;
204  }
205 
206  std::string GetFilename() const;
207 
208  void GotoBegin();
209  void SetPos(FileOffset pos);
210  FileOffset GetPos() const;
211 
212  void Read(char* buffer, size_t bytes);
213 
214  std::string ReadString();
215 
216  bool ReadBool();
217 
218  int8_t ReadInt8();
219  int16_t ReadInt16();
220  int32_t ReadInt32();
221  int64_t ReadInt64();
222 
223  uint8_t ReadUInt8();
224  uint16_t ReadUInt16();
225  uint32_t ReadUInt32();
226  uint64_t ReadUInt64();
227 
228  uint16_t Read(size_t bytes);
229  uint32_t ReadUInt32(size_t bytes);
230  uint64_t ReadUInt64(size_t bytes);
231 
232  ObjectFileRef ReadObjectFileRef();
233 
234  Color ReadColor();
235 
236  FileOffset ReadFileOffset();
237  FileOffset ReadFileOffset(size_t bytes);
238 
239  int16_t ReadInt16Number();
240  int32_t ReadInt32Number();
241  int64_t ReadInt64Number();
242 
243  uint16_t ReadUInt16Number();
244  uint32_t ReadUInt32Number();
245  uint64_t ReadUInt64Number();
246 
247  GeoCoord ReadCoord();
248  std::tuple<GeoCoord,bool> ReadConditionalCoord();
249 
258  void Read(std::vector<Point>& nodes,
259  std::vector<SegmentGeoBox> &segments,
260  GeoBox &bbox,
261  bool readIds);
262 
263  GeoBox ReadBox();
264 
265  TypeId ReadTypeId(uint8_t maxBytes);
266 
267  std::vector<ObjectFileRef> ReadObjectFileRefs(size_t count);
268  };
269 
274  {
275  private:
276  FileScanner& reader;
277  FileOffset lastFileOffset;
278 
279  public:
280  explicit ObjectFileRefStreamReader(FileScanner& reader);
281 
282  void Reset();
283 
284  void Read(ObjectFileRef& ref);
285  };
286 
287 }
288 
289 #endif
constexpr uint32_t maxRawCoordValue
Definition: GeoCoord.h:56
Mode
Definition: FileScanner.h:65
Definition: FileScanner.h:67
uint16_t TypeId
Definition: OSMScoutTypes.h:53
Definition: Exception.h:72
Definition: Area.h:38
#define CLASS_FINAL
Definition: Compiler.h:26
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
OSMSCOUT_API const double latConversionFactor
bool IsOpen() const
Definition: FileScanner.h:194
OSMSCOUT_API const double lonConversionFactor
Definition: FileScanner.h:69
uint64_t FileOffset
Definition: OSMScoutTypes.h:47
Definition: FileScanner.h:68
Definition: FileScanner.h:273
bool HasError() const
Definition: FileScanner.h:201