Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
FileHelpers.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 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
73 static std::string getFileFromPath(std::string path, const bool removeExtension);
74
81 static std::string addExtension(const std::string& path, const std::string& extension);
82
95 static std::string getConfigurationRelative(const std::string& configPath, const std::string& path);
96
105 static bool isSocket(const std::string& name);
106
117 static bool isAbsolute(const std::string& path);
118
130 static std::string checkForRelativity(const std::string& filename, const std::string& basePath);
131
136 static std::string getCurrentDir();
137
146 static std::vector<std::string> splitDirs(const std::string& filename);
147
156 static std::string fixRelative(const std::string& filename, const std::string& basePath, const bool force, std::string curDir = "");
157
159 static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
160
162
165
172 static std::ostream& writeInt(std::ostream& strm, int value);
173
182 static std::ostream& writeFloat(std::ostream& strm, double value);
183
190 static std::ostream& writeByte(std::ostream& strm, unsigned char value);
191
202 static std::ostream& writeString(std::ostream& strm, const std::string& value);
203
213 static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
214
221 template <typename E>
222 static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
223
230 template <typename E>
231 static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
233};
234
235
236template <typename E>
237std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
238 FileHelpers::writeInt(os, (int)edges.size());
239 std::vector<int> follow;
240 int maxFollow = 0;
241 E prev = edges.front();
242 for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
243 int idx = 0;
244 for (; idx < prev->getNumSuccessors(); ++idx) {
245 if (idx > 15) {
246 break;
247 }
248 if (prev->getSuccessors()[idx] == (*i)) {
249 follow.push_back(idx);
250 if (idx > maxFollow) {
251 maxFollow = idx;
252 }
253 break;
254 }
255 }
256 if (idx > 15 || idx == prev->getNumSuccessors()) {
257 follow.clear();
258 break;
259 }
260 prev = *i;
261 }
262 if (follow.empty()) {
263 for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
264 FileHelpers::writeInt(os, (*i)->getNumericalID());
265 }
266 } else {
267 const int bits = maxFollow > 3 ? 4 : 2;
268 const int numFields = 8 * sizeof(int) / bits;
269 FileHelpers::writeInt(os, -bits);
270 FileHelpers::writeInt(os, edges.front()->getNumericalID());
271 int data = 0;
272 int field = 0;
273 for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
274 data |= *i;
275 field++;
276 if (field == numFields) {
277 FileHelpers::writeInt(os, data);
278 data = 0;
279 field = 0;
280 } else {
281 data <<= bits;
282 }
283 }
284 if (field > 0) {
285 FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
286 }
287 }
288 return os;
289}
290
291
292template <typename E>
293void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
294 int size;
295 in.read((char*) &size, sizeof(int));
296 edges.reserve(size);
297 int bitsOrEntry;
298 in.read((char*) &bitsOrEntry, sizeof(int));
299 if (bitsOrEntry < 0) {
300 const int bits = -bitsOrEntry;
301 const int numFields = 8 * sizeof(int) / bits;
302 const int mask = (1 << bits) - 1;
303 int edgeID;
304 in.read((char*) &edgeID, sizeof(int));
305 const E* prev = E::getAllEdges()[edgeID];
306 assert(prev != 0);
307 edges.push_back(prev);
308 size--;
309 int data = 0;
310 int field = numFields;
311 for (; size > 0; size--) {
312 if (field == numFields) {
313 in.read((char*) &data, sizeof(int));
314 field = 0;
315 }
316 int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
317 if (followIndex >= prev->getNumSuccessors()) {
318 throw ProcessError(TLF("Invalid follower index in route '%'!", rid));
319 }
320 prev = prev->getSuccessors()[followIndex];
321 edges.push_back(prev);
322 field++;
323 }
324 } else {
325 while (size > 0) {
326 const E* edge = E::getAllEdges()[bitsOrEntry];
327 if (edge == 0) {
328 throw ProcessError(TLF("An edge within the route '%' is not known!", rid));
329 }
330 edges.push_back(edge);
331 size--;
332 if (size > 0) {
333 in.read((char*) &bitsOrEntry, sizeof(int));
334 }
335 }
336 }
337}
long long int SUMOTime
Definition GUI.h:36
#define TLF(string,...)
Definition MsgHandler.h:317
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.
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.
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.
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.
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.
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
static std::string getFileFromPath(std::string path, const bool removeExtension)
Removes the path information from the given path.
static bool isDirectory(std::string path)
Checks whether the given file is a directory.
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