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-2018 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 /****************************************************************************/
17 // Functions for an easier usage of files
18 /****************************************************************************/
19 #ifndef FileHelpers_h
20 #define FileHelpers_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <cassert>
29 #include <fstream>
30 #include <string>
31 #include <vector>
32 #include "SUMOTime.h"
33 
34 
35 // ===========================================================================
36 // class definitions
37 // ===========================================================================
42 class FileHelpers {
43 public:
45 
46 
52  static bool isReadable(std::string path);
54 
55 
56 
58 
59 
65  static std::string getFilePath(const std::string& path);
66 
67 
80  static std::string getConfigurationRelative(const std::string& configPath,
81  const std::string& path);
82 
83 
92  static bool isSocket(const std::string& name);
93 
94 
105  static bool isAbsolute(const std::string& path);
106 
107 
119  static std::string checkForRelativity(const std::string& filename,
120  const std::string& basePath);
121 
123  static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
124 
126 
127 
128 
130 
131 
138  static std::ostream& writeInt(std::ostream& strm, int value);
139 
140 
149  static std::ostream& writeFloat(std::ostream& strm, double value);
150 
151 
158  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
159 
160 
171  static std::ostream& writeString(std::ostream& strm, const std::string& value);
172 
173 
183  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
184 
185 
192  template <typename E>
193  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
194 
195 
202  template <typename E>
203  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
205 
206 
207 };
208 
209 
210 template <typename E>
211 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
212  FileHelpers::writeInt(os, (int)edges.size());
213  std::vector<int> follow;
214  int maxFollow = 0;
215  E prev = edges.front();
216  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
217  int idx = 0;
218  for (; idx < prev->getNumSuccessors(); ++idx) {
219  if (idx > 15) {
220  break;
221  }
222  if (prev->getSuccessors()[idx] == (*i)) {
223  follow.push_back(idx);
224  if (idx > maxFollow) {
225  maxFollow = idx;
226  }
227  break;
228  }
229  }
230  if (idx > 15 || idx == prev->getNumSuccessors()) {
231  follow.clear();
232  break;
233  }
234  prev = *i;
235  }
236  if (follow.empty()) {
237  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
238  FileHelpers::writeInt(os, (*i)->getNumericalID());
239  }
240  } else {
241  const int bits = maxFollow > 3 ? 4 : 2;
242  const int numFields = 8 * sizeof(int) / bits;
243  FileHelpers::writeInt(os, -bits);
244  FileHelpers::writeInt(os, edges.front()->getNumericalID());
245  int data = 0;
246  int field = 0;
247  for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
248  data |= *i;
249  field++;
250  if (field == numFields) {
251  FileHelpers::writeInt(os, data);
252  data = 0;
253  field = 0;
254  } else {
255  data <<= bits;
256  }
257  }
258  if (field > 0) {
259  FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
260  }
261  }
262  return os;
263 }
264 
265 
266 template <typename E>
267 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
268  int size;
269  in.read((char*) &size, sizeof(int));
270  edges.reserve(size);
271  int bitsOrEntry;
272  in.read((char*) &bitsOrEntry, sizeof(int));
273  if (bitsOrEntry < 0) {
274  const int bits = -bitsOrEntry;
275  const int numFields = 8 * sizeof(int) / bits;
276  const int mask = (1 << bits) - 1;
277  int edgeID;
278  in.read((char*) &edgeID, sizeof(int));
279  const E* prev = E::getAllEdges()[edgeID];
280  assert(prev != 0);
281  edges.push_back(prev);
282  size--;
283  int data = 0;
284  int field = numFields;
285  for (; size > 0; size--) {
286  if (field == numFields) {
287  in.read((char*) &data, sizeof(int));
288  field = 0;
289  }
290  int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
291  if (followIndex >= prev->getNumSuccessors()) {
292  throw ProcessError("Invalid follower index in route '" + rid + "'!");
293  }
294  prev = prev->getSuccessors()[followIndex];
295  edges.push_back(prev);
296  field++;
297  }
298  } else {
299  while (size > 0) {
300  const E* edge = E::getAllEdges()[bitsOrEntry];
301  if (edge == 0) {
302  throw ProcessError("An edge within the route '" + rid + "' is not known!");
303  }
304  edges.push_back(edge);
305  size--;
306  if (size > 0) {
307  in.read((char*) &bitsOrEntry, sizeof(int));
308  }
309  }
310  }
311 }
312 #endif
313 
314 /****************************************************************************/
315 
long long int SUMOTime
Definition: SUMOTime.h:36
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:75
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:47
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:83
static void readEdgeVector(std::istream &in, std::vector< const E *> &edges, const std::string &rid)
Reads an edge vector binary.
Definition: FileHelpers.h:267
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:90
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:42
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:211
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:65
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.