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-2022 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 <config.h>
24#include <cassert>
25#include <fstream>
26#include <string>
27#include <vector>
28#include "SUMOTime.h"
29
30
31// ===========================================================================
32// class definitions
33// ===========================================================================
39public:
42
48 static bool isReadable(std::string path);
49
55 static bool isDirectory(std::string path);
57
60
66 static std::string getFilePath(const std::string& path);
67
74 static std::string addExtension(const std::string& path, const std::string& extension);
75
88 static std::string getConfigurationRelative(const std::string& configPath, const std::string& path);
89
98 static bool isSocket(const std::string& name);
99
110 static bool isAbsolute(const std::string& path);
111
123 static std::string checkForRelativity(const std::string& filename, const std::string& basePath);
124
129 static std::string getCurrentDir();
130
139 static std::vector<std::string> splitDirs(const std::string& filename);
140
149 static std::string fixRelative(const std::string& filename, const std::string& basePath, const bool force, std::string curDir = "");
150
152 static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
153
155
158
165 static std::ostream& writeInt(std::ostream& strm, int value);
166
175 static std::ostream& writeFloat(std::ostream& strm, double value);
176
183 static std::ostream& writeByte(std::ostream& strm, unsigned char value);
184
195 static std::ostream& writeString(std::ostream& strm, const std::string& value);
196
206 static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
207
214 template <typename E>
215 static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
216
223 template <typename E>
224 static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
226};
227
228
229template <typename E>
230std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
231 FileHelpers::writeInt(os, (int)edges.size());
232 std::vector<int> follow;
233 int maxFollow = 0;
234 E prev = edges.front();
235 for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
236 int idx = 0;
237 for (; idx < prev->getNumSuccessors(); ++idx) {
238 if (idx > 15) {
239 break;
240 }
241 if (prev->getSuccessors()[idx] == (*i)) {
242 follow.push_back(idx);
243 if (idx > maxFollow) {
244 maxFollow = idx;
245 }
246 break;
247 }
248 }
249 if (idx > 15 || idx == prev->getNumSuccessors()) {
250 follow.clear();
251 break;
252 }
253 prev = *i;
254 }
255 if (follow.empty()) {
256 for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
257 FileHelpers::writeInt(os, (*i)->getNumericalID());
258 }
259 } else {
260 const int bits = maxFollow > 3 ? 4 : 2;
261 const int numFields = 8 * sizeof(int) / bits;
262 FileHelpers::writeInt(os, -bits);
263 FileHelpers::writeInt(os, edges.front()->getNumericalID());
264 int data = 0;
265 int field = 0;
266 for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
267 data |= *i;
268 field++;
269 if (field == numFields) {
270 FileHelpers::writeInt(os, data);
271 data = 0;
272 field = 0;
273 } else {
274 data <<= bits;
275 }
276 }
277 if (field > 0) {
278 FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
279 }
280 }
281 return os;
282}
283
284
285template <typename E>
286void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
287 int size;
288 in.read((char*) &size, sizeof(int));
289 edges.reserve(size);
290 int bitsOrEntry;
291 in.read((char*) &bitsOrEntry, sizeof(int));
292 if (bitsOrEntry < 0) {
293 const int bits = -bitsOrEntry;
294 const int numFields = 8 * sizeof(int) / bits;
295 const int mask = (1 << bits) - 1;
296 int edgeID;
297 in.read((char*) &edgeID, sizeof(int));
298 const E* prev = E::getAllEdges()[edgeID];
299 assert(prev != 0);
300 edges.push_back(prev);
301 size--;
302 int data = 0;
303 int field = numFields;
304 for (; size > 0; size--) {
305 if (field == numFields) {
306 in.read((char*) &data, sizeof(int));
307 field = 0;
308 }
309 int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
310 if (followIndex >= prev->getNumSuccessors()) {
311 throw ProcessError("Invalid follower index in route '" + rid + "'!");
312 }
313 prev = prev->getSuccessors()[followIndex];
314 edges.push_back(prev);
315 field++;
316 }
317 } else {
318 while (size > 0) {
319 const E* edge = E::getAllEdges()[bitsOrEntry];
320 if (edge == 0) {
321 throw ProcessError("An edge within the route '" + rid + "' is not known!");
322 }
323 edges.push_back(edge);
324 size--;
325 if (size > 0) {
326 in.read((char*) &bitsOrEntry, sizeof(int));
327 }
328 }
329 }
330}
long long int SUMOTime
Definition: GUI.h:36
Functions for an easier usage of files and paths.
Definition: FileHelpers.h:38
static std::string fixRelative(const std::string &filename, const std::string &basePath, const bool force, std::string curDir="")
Fixes the relative path for the given filename in relation to the basePath (usually a config file).
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:286
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:93
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:51
static std::vector< std::string > splitDirs(const std::string &filename)
Splits the given file path into directory components.
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:83
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:230
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:65
static std::string getCurrentDir()
Get the current working directory.
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