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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Functions for an easier usage of files
21 /****************************************************************************/
22 #pragma once
23 #include <cassert>
24 #include <fstream>
25 #include <string>
26 #include <vector>
27 #include "SUMOTime.h"
28 
29 
30 // ===========================================================================
31 // class definitions
32 // ===========================================================================
37 class FileHelpers {
38 public:
41 
47  static bool isReadable(std::string path);
48 
54  static bool isDirectory(std::string path);
56 
59 
65  static std::string getFilePath(const std::string& path);
66 
73  static std::string addExtension(const std::string& path, const std::string& extension);
74 
87  static std::string getConfigurationRelative(const std::string& configPath, const std::string& path);
88 
97  static bool isSocket(const std::string& name);
98 
109  static bool isAbsolute(const std::string& path);
110 
122  static std::string checkForRelativity(const std::string& filename, const std::string& basePath);
123 
125  static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
126 
128 
131 
138  static std::ostream& writeInt(std::ostream& strm, int value);
139 
148  static std::ostream& writeFloat(std::ostream& strm, double value);
149 
156  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
157 
168  static std::ostream& writeString(std::ostream& strm, const std::string& value);
169 
179  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
180 
187  template <typename E>
188  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
189 
196  template <typename E>
197  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
199 };
200 
201 
202 template <typename E>
203 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
204  FileHelpers::writeInt(os, (int)edges.size());
205  std::vector<int> follow;
206  int maxFollow = 0;
207  E prev = edges.front();
208  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
209  int idx = 0;
210  for (; idx < prev->getNumSuccessors(); ++idx) {
211  if (idx > 15) {
212  break;
213  }
214  if (prev->getSuccessors()[idx] == (*i)) {
215  follow.push_back(idx);
216  if (idx > maxFollow) {
217  maxFollow = idx;
218  }
219  break;
220  }
221  }
222  if (idx > 15 || idx == prev->getNumSuccessors()) {
223  follow.clear();
224  break;
225  }
226  prev = *i;
227  }
228  if (follow.empty()) {
229  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
230  FileHelpers::writeInt(os, (*i)->getNumericalID());
231  }
232  } else {
233  const int bits = maxFollow > 3 ? 4 : 2;
234  const int numFields = 8 * sizeof(int) / bits;
235  FileHelpers::writeInt(os, -bits);
236  FileHelpers::writeInt(os, edges.front()->getNumericalID());
237  int data = 0;
238  int field = 0;
239  for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
240  data |= *i;
241  field++;
242  if (field == numFields) {
243  FileHelpers::writeInt(os, data);
244  data = 0;
245  field = 0;
246  } else {
247  data <<= bits;
248  }
249  }
250  if (field > 0) {
251  FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
252  }
253  }
254  return os;
255 }
256 
257 
258 template <typename E>
259 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
260  int size;
261  in.read((char*) &size, sizeof(int));
262  edges.reserve(size);
263  int bitsOrEntry;
264  in.read((char*) &bitsOrEntry, sizeof(int));
265  if (bitsOrEntry < 0) {
266  const int bits = -bitsOrEntry;
267  const int numFields = 8 * sizeof(int) / bits;
268  const int mask = (1 << bits) - 1;
269  int edgeID;
270  in.read((char*) &edgeID, sizeof(int));
271  const E* prev = E::getAllEdges()[edgeID];
272  assert(prev != 0);
273  edges.push_back(prev);
274  size--;
275  int data = 0;
276  int field = numFields;
277  for (; size > 0; size--) {
278  if (field == numFields) {
279  in.read((char*) &data, sizeof(int));
280  field = 0;
281  }
282  int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
283  if (followIndex >= prev->getNumSuccessors()) {
284  throw ProcessError("Invalid follower index in route '" + rid + "'!");
285  }
286  prev = prev->getSuccessors()[followIndex];
287  edges.push_back(prev);
288  field++;
289  }
290  } else {
291  while (size > 0) {
292  const E* edge = E::getAllEdges()[bitsOrEntry];
293  if (edge == 0) {
294  throw ProcessError("An edge within the route '" + rid + "' is not known!");
295  }
296  edges.push_back(edge);
297  size--;
298  if (size > 0) {
299  in.read((char*) &bitsOrEntry, sizeof(int));
300  }
301  }
302  }
303 }
long long int SUMOTime
Definition: SUMOTime.h:31
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:37
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float binary.
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
static void readEdgeVector(std::istream &in, std::vector< const E * > &edges, const std::string &rid)
Reads an edge vector binary.
Definition: FileHelpers.h:259
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::string addExtension(const std::string &path, const std::string &extension)
Add an extension to the given file path.
Definition: FileHelpers.cpp:85
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:48
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:75
static std::ostream & writeTime(std::ostream &strm, SUMOTime value)
Writes a time description binary.
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
static std::ostream & writeEdgeVector(std::ostream &os, const std::vector< E > &edges)
Writes an edge vector binary.
Definition: FileHelpers.h:203
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
static bool isDirectory(std::string path)
Checks whether the given file is a directory.
Definition: FileHelpers.cpp:62
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
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