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 /****************************************************************************/
8 // Functions for an easier usage of files
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef FileHelpers_h
22 #define FileHelpers_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <fstream>
36 #include <string>
37 #include <vector>
38 #include "SUMOTime.h"
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
48 class FileHelpers {
49 public:
51 
52 
58  static bool exists(std::string path);
60 
61 
62 
64 
65 
71  static std::string getFilePath(const std::string& path);
72 
73 
86  static std::string getConfigurationRelative(const std::string& configPath,
87  const std::string& path);
88 
89 
98  static bool isSocket(const std::string& name);
99 
100 
111  static bool isAbsolute(const std::string& path);
112 
113 
125  static std::string checkForRelativity(const std::string& filename,
126  const std::string& basePath);
128 
129 
130 
132 
133 
140  static std::ostream& writeInt(std::ostream& strm, int value);
141 
142 
149  static std::ostream& writeUInt(std::ostream& strm, unsigned int value);
150 
151 
160  static std::ostream& writeFloat(std::ostream& strm, SUMOReal value);
161 
162 
169  static std::ostream& writeByte(std::ostream& strm, unsigned char value);
170 
171 
182  static std::ostream& writeString(std::ostream& strm, const std::string& value);
183 
184 
194  static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
195 
196 
203  template <typename E>
204  static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
205 
206 
213  template <typename E>
214  static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
216 
217 
218 };
219 
220 
221 template <typename E>
222 std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
223  FileHelpers::writeUInt(os, (unsigned int)edges.size());
224  std::vector<unsigned int> follow;
225  unsigned int maxFollow = 0;
226  E prev = edges.front();
227  for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
228  unsigned int idx = 0;
229  for (; idx < prev->getNoFollowing(); ++idx) {
230  if (idx > 15) {
231  break;
232  }
233  if (prev->getFollower(idx) == (*i)) {
234  follow.push_back(idx);
235  if (idx > maxFollow) {
236  maxFollow = idx;
237  }
238  break;
239  }
240  }
241  if (idx > 15 || idx == prev->getNoFollowing()) {
242  follow.clear();
243  break;
244  }
245  prev = *i;
246  }
247  if (follow.empty()) {
248  for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
249  FileHelpers::writeInt(os, (*i)->getNumericalID());
250  }
251  } else {
252  const int bits = maxFollow > 3 ? 4 : 2;
253  const unsigned int numFields = 8 * sizeof(unsigned int) / bits;
254  FileHelpers::writeInt(os, -bits);
255  FileHelpers::writeUInt(os, edges.front()->getNumericalID());
256  unsigned int data = 0;
257  unsigned int field = 0;
258  for (std::vector<unsigned int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
259  data |= *i;
260  field++;
261  if (field == numFields) {
262  FileHelpers::writeUInt(os, data);
263  data = 0;
264  field = 0;
265  } else {
266  data <<= bits;
267  }
268  }
269  if (field > 0) {
270  FileHelpers::writeUInt(os, data << ((numFields - field - 1) * bits));
271  }
272  }
273  return os;
274 }
275 
276 
277 template <typename E>
278 void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
279  int size;
280  in.read((char*) &size, sizeof(int));
281  edges.reserve(size);
282  int bitsOrEntry;
283  in.read((char*) &bitsOrEntry, sizeof(int));
284  if (bitsOrEntry < 0) {
285  const unsigned int bits = -bitsOrEntry;
286  const unsigned int numFields = 8 * sizeof(unsigned int) / bits;
287  const unsigned int mask = (1 << bits) - 1;
288  unsigned int edgeID;
289  in.read((char*) &edgeID, sizeof(int));
290  const E* prev = E::dictionary(edgeID);
291  assert(prev != 0);
292  edges.push_back(prev);
293  size--;
294  unsigned int data = 0;
295  unsigned int field = numFields;
296  for (; size > 0; size--) {
297  if (field == numFields) {
298  in.read((char*) &data, sizeof(int));
299  field = 0;
300  }
301  unsigned int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
302  if (followIndex >= prev->getNoFollowing()) {
303  throw ProcessError("Invalid follower index in route '" + rid + "'!");
304  }
305  prev = prev->getFollower(followIndex);
306  edges.push_back(prev);
307  field++;
308  }
309  } else {
310  while (size > 0) {
311  const E* edge = E::dictionary(bitsOrEntry);
312  if (edge == 0) {
313  throw ProcessError("An edge within the route '" + rid + "' is not known!");
314  }
315  edges.push_back(edge);
316  size--;
317  if (size > 0) {
318  in.read((char*) &bitsOrEntry, sizeof(int));
319  }
320  }
321  }
322 }
323 #endif
324 
325 /****************************************************************************/
326