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 /****************************************************************************/
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 
27 #include <cassert>
28 #include <fstream>
29 #include <string>
30 #include <vector>
31 #include "SUMOTime.h"
32 
33 
34 // ===========================================================================
35 // class definitions
36 // ===========================================================================
41 class FileHelpers {
42 public:
45 
51  static bool isReadable(std::string path);
53 
56 
62  static std::string getFilePath(const std::string& path);
63 
70  static std::string addExtension(const std::string& path, const std::string& extension);
71 
84  static std::string getConfigurationRelative(const std::string& configPath, const std::string& path);
85 
94  static bool isSocket(const std::string& name);
95 
106  static bool isAbsolute(const std::string& path);
107 
119  static std::string checkForRelativity(const std::string& filename, const std::string& basePath);
120 
122  static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
123 
125 
128 
135  static std::ostream& writeInt(std::ostream& strm, int value);
136 
145  static std::ostream& writeFloat(std::ostream& strm, double value);
146 
153  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
154 
165  static std::ostream& writeString(std::ostream& strm, const std::string& value);
166 
176  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
177 
184  template <typename E>
185  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
186 
193  template <typename E>
194  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
196 };
197 
198 
199 template <typename E>
200 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
201  FileHelpers::writeInt(os, (int)edges.size());
202  std::vector<int> follow;
203  int maxFollow = 0;
204  E prev = edges.front();
205  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
206  int idx = 0;
207  for (; idx < prev->getNumSuccessors(); ++idx) {
208  if (idx > 15) {
209  break;
210  }
211  if (prev->getSuccessors()[idx] == (*i)) {
212  follow.push_back(idx);
213  if (idx > maxFollow) {
214  maxFollow = idx;
215  }
216  break;
217  }
218  }
219  if (idx > 15 || idx == prev->getNumSuccessors()) {
220  follow.clear();
221  break;
222  }
223  prev = *i;
224  }
225  if (follow.empty()) {
226  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
227  FileHelpers::writeInt(os, (*i)->getNumericalID());
228  }
229  } else {
230  const int bits = maxFollow > 3 ? 4 : 2;
231  const int numFields = 8 * sizeof(int) / bits;
232  FileHelpers::writeInt(os, -bits);
233  FileHelpers::writeInt(os, edges.front()->getNumericalID());
234  int data = 0;
235  int field = 0;
236  for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
237  data |= *i;
238  field++;
239  if (field == numFields) {
240  FileHelpers::writeInt(os, data);
241  data = 0;
242  field = 0;
243  } else {
244  data <<= bits;
245  }
246  }
247  if (field > 0) {
248  FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
249  }
250  }
251  return os;
252 }
253 
254 
255 template <typename E>
256 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
257  int size;
258  in.read((char*) &size, sizeof(int));
259  edges.reserve(size);
260  int bitsOrEntry;
261  in.read((char*) &bitsOrEntry, sizeof(int));
262  if (bitsOrEntry < 0) {
263  const int bits = -bitsOrEntry;
264  const int numFields = 8 * sizeof(int) / bits;
265  const int mask = (1 << bits) - 1;
266  int edgeID;
267  in.read((char*) &edgeID, sizeof(int));
268  const E* prev = E::getAllEdges()[edgeID];
269  assert(prev != 0);
270  edges.push_back(prev);
271  size--;
272  int data = 0;
273  int field = numFields;
274  for (; size > 0; size--) {
275  if (field == numFields) {
276  in.read((char*) &data, sizeof(int));
277  field = 0;
278  }
279  int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
280  if (followIndex >= prev->getNumSuccessors()) {
281  throw ProcessError("Invalid follower index in route '" + rid + "'!");
282  }
283  prev = prev->getSuccessors()[followIndex];
284  edges.push_back(prev);
285  field++;
286  }
287  } else {
288  while (size > 0) {
289  const E* edge = E::getAllEdges()[bitsOrEntry];
290  if (edge == 0) {
291  throw ProcessError("An edge within the route '" + rid + "' is not known!");
292  }
293  edges.push_back(edge);
294  size--;
295  if (size > 0) {
296  in.read((char*) &bitsOrEntry, sizeof(int));
297  }
298  }
299  }
300 }
301 #endif
302 
303 /****************************************************************************/
304 
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:77
FileHelpers::writeEdgeVector
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:200
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:106
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
FileHelpers::writeInt
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
Definition: FileHelpers.cpp:175
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:143
FileHelpers::writeFloat
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float binary.
Definition: FileHelpers.cpp:182
FileHelpers::writeByte
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
Definition: FileHelpers.cpp:189
FileHelpers::getFilePath
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:67
FileHelpers::writeTime
static std::ostream & writeTime(std::ostream &strm, SUMOTime value)
Writes a time description binary.
Definition: FileHelpers.cpp:206
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:161
FileHelpers::isSocket
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:113
FileHelpers::writeString
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.
Definition: FileHelpers.cpp:196
ProcessError
Definition: UtilExceptions.h:40
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:256
FileHelpers
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:41
FileHelpers::isAbsolute
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Definition: FileHelpers.cpp:120
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49