Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Functions for an easier usage of files
17 /****************************************************************************/
18 #ifndef FileHelpers_h
19 #define FileHelpers_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 
26 #include <cassert>
27 #include <fstream>
28 #include <string>
29 #include <vector>
30 #include "SUMOTime.h"
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
40 class FileHelpers {
41 public:
44 
50  static bool isReadable(std::string path);
51 
57  static bool isDirectory(std::string path);
59 
62 
68  static std::string getFilePath(const std::string& path);
69 
76  static std::string addExtension(const std::string& path, const std::string& extension);
77 
90  static std::string getConfigurationRelative(const std::string& configPath, const std::string& path);
91 
100  static bool isSocket(const std::string& name);
101 
112  static bool isAbsolute(const std::string& path);
113 
125  static std::string checkForRelativity(const std::string& filename, const std::string& basePath);
126 
128  static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
129 
131 
134 
141  static std::ostream& writeInt(std::ostream& strm, int value);
142 
151  static std::ostream& writeFloat(std::ostream& strm, double value);
152 
159  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
160 
171  static std::ostream& writeString(std::ostream& strm, const std::string& value);
172 
182  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
183 
190  template <typename E>
191  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
192 
199  template <typename E>
200  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
202 };
203 
204 
205 template <typename E>
206 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
207  FileHelpers::writeInt(os, (int)edges.size());
208  std::vector<int> follow;
209  int maxFollow = 0;
210  E prev = edges.front();
211  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
212  int idx = 0;
213  for (; idx < prev->getNumSuccessors(); ++idx) {
214  if (idx > 15) {
215  break;
216  }
217  if (prev->getSuccessors()[idx] == (*i)) {
218  follow.push_back(idx);
219  if (idx > maxFollow) {
220  maxFollow = idx;
221  }
222  break;
223  }
224  }
225  if (idx > 15 || idx == prev->getNumSuccessors()) {
226  follow.clear();
227  break;
228  }
229  prev = *i;
230  }
231  if (follow.empty()) {
232  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
233  FileHelpers::writeInt(os, (*i)->getNumericalID());
234  }
235  } else {
236  const int bits = maxFollow > 3 ? 4 : 2;
237  const int numFields = 8 * sizeof(int) / bits;
238  FileHelpers::writeInt(os, -bits);
239  FileHelpers::writeInt(os, edges.front()->getNumericalID());
240  int data = 0;
241  int field = 0;
242  for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
243  data |= *i;
244  field++;
245  if (field == numFields) {
246  FileHelpers::writeInt(os, data);
247  data = 0;
248  field = 0;
249  } else {
250  data <<= bits;
251  }
252  }
253  if (field > 0) {
254  FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
255  }
256  }
257  return os;
258 }
259 
260 
261 template <typename E>
262 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
263  int size;
264  in.read((char*) &size, sizeof(int));
265  edges.reserve(size);
266  int bitsOrEntry;
267  in.read((char*) &bitsOrEntry, sizeof(int));
268  if (bitsOrEntry < 0) {
269  const int bits = -bitsOrEntry;
270  const int numFields = 8 * sizeof(int) / bits;
271  const int mask = (1 << bits) - 1;
272  int edgeID;
273  in.read((char*) &edgeID, sizeof(int));
274  const E* prev = E::getAllEdges()[edgeID];
275  assert(prev != 0);
276  edges.push_back(prev);
277  size--;
278  int data = 0;
279  int field = numFields;
280  for (; size > 0; size--) {
281  if (field == numFields) {
282  in.read((char*) &data, sizeof(int));
283  field = 0;
284  }
285  int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
286  if (followIndex >= prev->getNumSuccessors()) {
287  throw ProcessError("Invalid follower index in route '" + rid + "'!");
288  }
289  prev = prev->getSuccessors()[followIndex];
290  edges.push_back(prev);
291  field++;
292  }
293  } else {
294  while (size > 0) {
295  const E* edge = E::getAllEdges()[bitsOrEntry];
296  if (edge == 0) {
297  throw ProcessError("An edge within the route '" + rid + "' is not known!");
298  }
299  edges.push_back(edge);
300  size--;
301  if (size > 0) {
302  in.read((char*) &bitsOrEntry, sizeof(int));
303  }
304  }
305  }
306 }
307 #endif
308 
309 /****************************************************************************/
310 
SUMOTime.h
FileHelpers::addExtension
static std::string addExtension(const std::string &path, const std::string &extension)
Add an extension to the given file path.
Definition: FileHelpers.cpp:86
FileHelpers::writeEdgeVector
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:206
FileHelpers::getConfigurationRelative
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:115
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
FileHelpers::writeInt
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
Definition: FileHelpers.cpp:184
FileHelpers::checkForRelativity
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.
Definition: FileHelpers.cpp:152
FileHelpers::writeFloat
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float binary.
Definition: FileHelpers.cpp:191
FileHelpers::writeByte
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
Definition: FileHelpers.cpp:198
FileHelpers::getFilePath
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:76
FileHelpers::writeTime
static std::ostream & writeTime(std::ostream &strm, SUMOTime value)
Writes a time description binary.
Definition: FileHelpers.cpp:215
FileHelpers::prependToLastPathComponent
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
Definition: FileHelpers.cpp:170
FileHelpers::isSocket
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:122
FileHelpers::writeString
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.
Definition: FileHelpers.cpp:205
ProcessError
Definition: UtilExceptions.h:39
FileHelpers::readEdgeVector
static void readEdgeVector(std::istream &in, std::vector< const E * > &edges, const std::string &rid)
Reads an edge vector binary.
Definition: FileHelpers.h:262
FileHelpers
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:40
FileHelpers::isAbsolute
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Definition: FileHelpers.cpp:129
FileHelpers::isDirectory
static bool isDirectory(std::string path)
Checks whether the given file is a directory.
Definition: FileHelpers.cpp:63
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49