SUMO - Simulation of Urban MObility
FileHelpers.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Functions for an easier usage of files
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef FileHelpers_h
23 #define FileHelpers_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <fstream>
37 #include <string>
38 #include <vector>
39 #include "SUMOTime.h"
40 
41 
42 // ===========================================================================
43 // class definitions
44 // ===========================================================================
49 class FileHelpers {
50 public:
52 
53 
59  static bool isReadable(std::string path);
61 
62 
63 
65 
66 
72  static std::string getFilePath(const std::string& path);
73 
74 
87  static std::string getConfigurationRelative(const std::string& configPath,
88  const std::string& path);
89 
90 
99  static bool isSocket(const std::string& name);
100 
101 
112  static bool isAbsolute(const std::string& path);
113 
114 
126  static std::string checkForRelativity(const std::string& filename,
127  const std::string& basePath);
128 
130  static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
131 
133 
134 
135 
137 
138 
145  static std::ostream& writeInt(std::ostream& strm, int value);
146 
147 
154  static std::ostream& writeUInt(std::ostream& strm, unsigned int value);
155 
156 
165  static std::ostream& writeFloat(std::ostream& strm, SUMOReal value);
166 
167 
174  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
175 
176 
187  static std::ostream& writeString(std::ostream& strm, const std::string& value);
188 
189 
199  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
200 
201 
208  template <typename E>
209  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
210 
211 
218  template <typename E>
219  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
221 
222 
223 };
224 
225 
226 template <typename E>
227 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
228  FileHelpers::writeUInt(os, (unsigned int)edges.size());
229  std::vector<unsigned int> follow;
230  unsigned int maxFollow = 0;
231  E prev = edges.front();
232  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
233  unsigned int idx = 0;
234  for (; idx < prev->getNumSuccessors(); ++idx) {
235  if (idx > 15) {
236  break;
237  }
238  if (prev->getSuccessors()[idx] == (*i)) {
239  follow.push_back(idx);
240  if (idx > maxFollow) {
241  maxFollow = idx;
242  }
243  break;
244  }
245  }
246  if (idx > 15 || idx == prev->getNumSuccessors()) {
247  follow.clear();
248  break;
249  }
250  prev = *i;
251  }
252  if (follow.empty()) {
253  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
254  FileHelpers::writeInt(os, (*i)->getNumericalID());
255  }
256  } else {
257  const int bits = maxFollow > 3 ? 4 : 2;
258  const unsigned int numFields = 8 * sizeof(unsigned int) / bits;
259  FileHelpers::writeInt(os, -bits);
260  FileHelpers::writeUInt(os, edges.front()->getNumericalID());
261  unsigned int data = 0;
262  unsigned int field = 0;
263  for (std::vector<unsigned int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
264  data |= *i;
265  field++;
266  if (field == numFields) {
267  FileHelpers::writeUInt(os, data);
268  data = 0;
269  field = 0;
270  } else {
271  data <<= bits;
272  }
273  }
274  if (field > 0) {
275  FileHelpers::writeUInt(os, data << ((numFields - field - 1) * bits));
276  }
277  }
278  return os;
279 }
280 
281 
282 template <typename E>
283 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
284  int size;
285  in.read((char*) &size, sizeof(int));
286  edges.reserve(size);
287  int bitsOrEntry;
288  in.read((char*) &bitsOrEntry, sizeof(int));
289  if (bitsOrEntry < 0) {
290  const unsigned int bits = -bitsOrEntry;
291  const unsigned int numFields = 8 * sizeof(unsigned int) / bits;
292  const unsigned int mask = (1 << bits) - 1;
293  unsigned int edgeID;
294  in.read((char*) &edgeID, sizeof(int));
295  const E* prev = E::getAllEdges()[edgeID];
296  assert(prev != 0);
297  edges.push_back(prev);
298  size--;
299  unsigned int data = 0;
300  unsigned int field = numFields;
301  for (; size > 0; size--) {
302  if (field == numFields) {
303  in.read((char*) &data, sizeof(int));
304  field = 0;
305  }
306  unsigned int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
307  if (followIndex >= prev->getNumSuccessors()) {
308  throw ProcessError("Invalid follower index in route '" + rid + "'!");
309  }
310  prev = prev->getSuccessors()[followIndex];
311  edges.push_back(prev);
312  field++;
313  }
314  } else {
315  while (size > 0) {
316  const E* edge = E::getAllEdges()[bitsOrEntry];
317  if (edge == 0) {
318  throw ProcessError("An edge within the route '" + rid + "' is not known!");
319  }
320  edges.push_back(edge);
321  size--;
322  if (size > 0) {
323  in.read((char*) &bitsOrEntry, sizeof(int));
324  }
325  }
326  }
327 }
328 #endif
329 
330 /****************************************************************************/
331 
long long int SUMOTime
Definition: SUMOTime.h:43
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:86
static std::string prependToLastPathComponent(const std::string &prefix, const std::string &path)
prepend the given prefix to the last path component of the given file path
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:94
static std::ostream & writeFloat(std::ostream &strm, SUMOReal value)
Writes a float binary.
static std::ostream & writeUInt(std::ostream &strm, unsigned int value)
Writes an unsigned integer binary.
static std::ostream & writeTime(std::ostream &strm, SUMOTime value)
Writes a time description binary.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:49
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
static std::string checkForRelativity(const std::string &filename, const std::string &basePath)
Returns the path from a configuration so that it is accessable from the current working directory...
static void readEdgeVector(std::istream &in, std::vector< const E * > &edges, const std::string &rid)
Reads an edge vector binary.
Definition: FileHelpers.h:283
#define SUMOReal
Definition: config.h:213
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:227
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:76
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.