SUMO - Simulation of Urban MObility
FileHelpers.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Functions for an easier usage of files
20 /****************************************************************************/
21 #ifndef FileHelpers_h
22 #define FileHelpers_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <fstream>
36 #include <string>
37 #include <vector>
38 #include "SUMOTime.h"
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
48 class FileHelpers {
49 public:
51 
52 
58  static bool isReadable(std::string path);
60 
61 
62 
64 
65 
71  static std::string getFilePath(const std::string& path);
72 
73 
86  static std::string getConfigurationRelative(const std::string& configPath,
87  const std::string& path);
88 
89 
98  static bool isSocket(const std::string& name);
99 
100 
111  static bool isAbsolute(const std::string& path);
112 
113 
125  static std::string checkForRelativity(const std::string& filename,
126  const std::string& basePath);
127 
129  static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
130 
132 
133 
134 
136 
137 
144  static std::ostream& writeInt(std::ostream& strm, int value);
145 
146 
155  static std::ostream& writeFloat(std::ostream& strm, double value);
156 
157 
164  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
165 
166 
177  static std::ostream& writeString(std::ostream& strm, const std::string& value);
178 
179 
189  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
190 
191 
198  template <typename E>
199  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
200 
201 
208  template <typename E>
209  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
211 
212 
213 };
214 
215 
216 template <typename E>
217 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
218  FileHelpers::writeInt(os, (int)edges.size());
219  std::vector<int> follow;
220  int maxFollow = 0;
221  E prev = edges.front();
222  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
223  int idx = 0;
224  for (; idx < prev->getNumSuccessors(); ++idx) {
225  if (idx > 15) {
226  break;
227  }
228  if (prev->getSuccessors()[idx] == (*i)) {
229  follow.push_back(idx);
230  if (idx > maxFollow) {
231  maxFollow = idx;
232  }
233  break;
234  }
235  }
236  if (idx > 15 || idx == prev->getNumSuccessors()) {
237  follow.clear();
238  break;
239  }
240  prev = *i;
241  }
242  if (follow.empty()) {
243  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
244  FileHelpers::writeInt(os, (*i)->getNumericalID());
245  }
246  } else {
247  const int bits = maxFollow > 3 ? 4 : 2;
248  const int numFields = 8 * sizeof(int) / bits;
249  FileHelpers::writeInt(os, -bits);
250  FileHelpers::writeInt(os, edges.front()->getNumericalID());
251  int data = 0;
252  int field = 0;
253  for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
254  data |= *i;
255  field++;
256  if (field == numFields) {
257  FileHelpers::writeInt(os, data);
258  data = 0;
259  field = 0;
260  } else {
261  data <<= bits;
262  }
263  }
264  if (field > 0) {
265  FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
266  }
267  }
268  return os;
269 }
270 
271 
272 template <typename E>
273 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
274  int size;
275  in.read((char*) &size, sizeof(int));
276  edges.reserve(size);
277  int bitsOrEntry;
278  in.read((char*) &bitsOrEntry, sizeof(int));
279  if (bitsOrEntry < 0) {
280  const int bits = -bitsOrEntry;
281  const int numFields = 8 * sizeof(int) / bits;
282  const int mask = (1 << bits) - 1;
283  int edgeID;
284  in.read((char*) &edgeID, sizeof(int));
285  const E* prev = E::getAllEdges()[edgeID];
286  assert(prev != 0);
287  edges.push_back(prev);
288  size--;
289  int data = 0;
290  int field = numFields;
291  for (; size > 0; size--) {
292  if (field == numFields) {
293  in.read((char*) &data, sizeof(int));
294  field = 0;
295  }
296  int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
297  if (followIndex >= prev->getNumSuccessors()) {
298  throw ProcessError("Invalid follower index in route '" + rid + "'!");
299  }
300  prev = prev->getSuccessors()[followIndex];
301  edges.push_back(prev);
302  field++;
303  }
304  } else {
305  while (size > 0) {
306  const E* edge = E::getAllEdges()[bitsOrEntry];
307  if (edge == 0) {
308  throw ProcessError("An edge within the route '" + rid + "' is not known!");
309  }
310  edges.push_back(edge);
311  size--;
312  if (size > 0) {
313  in.read((char*) &bitsOrEntry, sizeof(int));
314  }
315  }
316  }
317 }
318 #endif
319 
320 /****************************************************************************/
321 
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:81
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:53
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:89
static void readEdgeVector(std::istream &in, std::vector< const E *> &edges, const std::string &rid)
Reads an edge vector binary.
Definition: FileHelpers.h:273
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:96
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:48
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:217
long long int SUMOTime
Definition: TraCIDefs.h:51
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:71
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.