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-2017 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 
156  static std::ostream& writeFloat(std::ostream& strm, double value);
157 
158 
165  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
166 
167 
178  static std::ostream& writeString(std::ostream& strm, const std::string& value);
179 
180 
190  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
191 
192 
199  template <typename E>
200  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
201 
202 
209  template <typename E>
210  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
212 
213 
214 };
215 
216 
217 template <typename E>
218 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
219  FileHelpers::writeInt(os, (int)edges.size());
220  std::vector<int> follow;
221  int maxFollow = 0;
222  E prev = edges.front();
223  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
224  int idx = 0;
225  for (; idx < prev->getNumSuccessors(); ++idx) {
226  if (idx > 15) {
227  break;
228  }
229  if (prev->getSuccessors()[idx] == (*i)) {
230  follow.push_back(idx);
231  if (idx > maxFollow) {
232  maxFollow = idx;
233  }
234  break;
235  }
236  }
237  if (idx > 15 || idx == prev->getNumSuccessors()) {
238  follow.clear();
239  break;
240  }
241  prev = *i;
242  }
243  if (follow.empty()) {
244  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
245  FileHelpers::writeInt(os, (*i)->getNumericalID());
246  }
247  } else {
248  const int bits = maxFollow > 3 ? 4 : 2;
249  const int numFields = 8 * sizeof(int) / bits;
250  FileHelpers::writeInt(os, -bits);
251  FileHelpers::writeInt(os, edges.front()->getNumericalID());
252  int data = 0;
253  int field = 0;
254  for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
255  data |= *i;
256  field++;
257  if (field == numFields) {
258  FileHelpers::writeInt(os, data);
259  data = 0;
260  field = 0;
261  } else {
262  data <<= bits;
263  }
264  }
265  if (field > 0) {
266  FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
267  }
268  }
269  return os;
270 }
271 
272 
273 template <typename E>
274 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
275  int size;
276  in.read((char*) &size, sizeof(int));
277  edges.reserve(size);
278  int bitsOrEntry;
279  in.read((char*) &bitsOrEntry, sizeof(int));
280  if (bitsOrEntry < 0) {
281  const int bits = -bitsOrEntry;
282  const int numFields = 8 * sizeof(int) / bits;
283  const int mask = (1 << bits) - 1;
284  int edgeID;
285  in.read((char*) &edgeID, sizeof(int));
286  const E* prev = E::getAllEdges()[edgeID];
287  assert(prev != 0);
288  edges.push_back(prev);
289  size--;
290  int data = 0;
291  int field = numFields;
292  for (; size > 0; size--) {
293  if (field == numFields) {
294  in.read((char*) &data, sizeof(int));
295  field = 0;
296  }
297  int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
298  if (followIndex >= prev->getNumSuccessors()) {
299  throw ProcessError("Invalid follower index in route '" + rid + "'!");
300  }
301  prev = prev->getSuccessors()[followIndex];
302  edges.push_back(prev);
303  field++;
304  }
305  } else {
306  while (size > 0) {
307  const E* edge = E::getAllEdges()[bitsOrEntry];
308  if (edge == 0) {
309  throw ProcessError("An edge within the route '" + rid + "' is not known!");
310  }
311  edges.push_back(edge);
312  size--;
313  if (size > 0) {
314  in.read((char*) &bitsOrEntry, sizeof(int));
315  }
316  }
317  }
318 }
319 #endif
320 
321 /****************************************************************************/
322 
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:82
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:54
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:90
static void readEdgeVector(std::istream &in, std::vector< const E *> &edges, const std::string &rid)
Reads an edge vector binary.
Definition: FileHelpers.h:274
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float 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.
Definition: FileHelpers.cpp:97
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 std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:218
long long int SUMOTime
Definition: TraCIDefs.h:52
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:72
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.