SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2001-2014 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);
129 
130 
131 
133 
134 
141  static std::ostream& writeInt(std::ostream& strm, int value);
142 
143 
150  static std::ostream& writeUInt(std::ostream& strm, unsigned int value);
151 
152 
161  static std::ostream& writeFloat(std::ostream& strm, SUMOReal value);
162 
163 
170  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
171 
172 
183  static std::ostream& writeString(std::ostream& strm, const std::string& value);
184 
185 
195  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
196 
197 
204  template <typename E>
205  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
206 
207 
214  template <typename E>
215  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
217 
218 
219 };
220 
221 
222 template <typename E>
223 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
224  FileHelpers::writeUInt(os, (unsigned int)edges.size());
225  std::vector<unsigned int> follow;
226  unsigned int maxFollow = 0;
227  E prev = edges.front();
228  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
229  unsigned int idx = 0;
230  for (; idx < prev->getNoFollowing(); ++idx) {
231  if (idx > 15) {
232  break;
233  }
234  if (prev->getFollower(idx) == (*i)) {
235  follow.push_back(idx);
236  if (idx > maxFollow) {
237  maxFollow = idx;
238  }
239  break;
240  }
241  }
242  if (idx > 15 || idx == prev->getNoFollowing()) {
243  follow.clear();
244  break;
245  }
246  prev = *i;
247  }
248  if (follow.empty()) {
249  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
250  FileHelpers::writeInt(os, (*i)->getNumericalID());
251  }
252  } else {
253  const int bits = maxFollow > 3 ? 4 : 2;
254  const unsigned int numFields = 8 * sizeof(unsigned int) / bits;
255  FileHelpers::writeInt(os, -bits);
256  FileHelpers::writeUInt(os, edges.front()->getNumericalID());
257  unsigned int data = 0;
258  unsigned int field = 0;
259  for (std::vector<unsigned int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
260  data |= *i;
261  field++;
262  if (field == numFields) {
263  FileHelpers::writeUInt(os, data);
264  data = 0;
265  field = 0;
266  } else {
267  data <<= bits;
268  }
269  }
270  if (field > 0) {
271  FileHelpers::writeUInt(os, data << ((numFields - field - 1) * bits));
272  }
273  }
274  return os;
275 }
276 
277 
278 template <typename E>
279 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
280  int size;
281  in.read((char*) &size, sizeof(int));
282  edges.reserve(size);
283  int bitsOrEntry;
284  in.read((char*) &bitsOrEntry, sizeof(int));
285  if (bitsOrEntry < 0) {
286  const unsigned int bits = -bitsOrEntry;
287  const unsigned int numFields = 8 * sizeof(unsigned int) / bits;
288  const unsigned int mask = (1 << bits) - 1;
289  unsigned int edgeID;
290  in.read((char*) &edgeID, sizeof(int));
291  const E* prev = E::dictionary(edgeID);
292  assert(prev != 0);
293  edges.push_back(prev);
294  size--;
295  unsigned int data = 0;
296  unsigned int field = numFields;
297  for (; size > 0; size--) {
298  if (field == numFields) {
299  in.read((char*) &data, sizeof(int));
300  field = 0;
301  }
302  unsigned int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
303  if (followIndex >= prev->getNoFollowing()) {
304  throw ProcessError("Invalid follower index in route '" + rid + "'!");
305  }
306  prev = prev->getFollower(followIndex);
307  edges.push_back(prev);
308  field++;
309  }
310  } else {
311  while (size > 0) {
312  const E* edge = E::dictionary(bitsOrEntry);
313  if (edge == 0) {
314  throw ProcessError("An edge within the route '" + rid + "' is not known!");
315  }
316  edges.push_back(edge);
317  size--;
318  if (size > 0) {
319  in.read((char*) &bitsOrEntry, sizeof(int));
320  }
321  }
322  }
323 }
324 #endif
325 
326 /****************************************************************************/
327 
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 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:279
#define SUMOReal
Definition: config.h:215
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:223
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.