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-2026 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#include "Translation.h"
30
31
32// ===========================================================================
33// class definitions
34// ===========================================================================
40public:
43
49 static bool isReadable(std::string path);
50
56 static bool isDirectory(std::string path);
58
61
67 static std::string getFilePath(const std::string& path);
68
74 static std::string getFileFromPath(std::string path, const bool removeExtension);
75
82 static std::string addExtension(const std::string& path, const std::string& extension);
83
96 static std::string getConfigurationRelative(const std::string& configPath, const std::string& path);
97
106 static bool isSocket(const std::string& name);
107
118 static bool isAbsolute(const std::string& path);
119
131 static std::string checkForRelativity(const std::string& filename, const std::string& basePath);
132
137 static std::string getCurrentDir();
138
147 static std::vector<std::string> splitDirs(const std::string& filename);
148
157 static std::string fixRelative(const std::string& filename, const std::string& basePath, const bool force, std::string curDir = "");
158
160 static std::string prependToLastPathComponent(const std::string& prefix, const std::string& path);
161
163 static std::string appendBeforeExtension(const std::string& path, const std::string& suffix, bool checkSep = true);
165
168
175 static std::ostream& writeInt(std::ostream& strm, int value);
176
185 static std::ostream& writeFloat(std::ostream& strm, double value);
186
193 static std::ostream& writeByte(std::ostream& strm, unsigned char value);
194
205 static std::ostream& writeString(std::ostream& strm, const std::string& value);
206
216 static std::ostream& writeTime(std::ostream& strm, SUMOTime value);
217
224 template <typename E>
225 static std::ostream& writeEdgeVector(std::ostream& os, const std::vector<E>& edges);
226
233 template <typename E>
234 static void readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid);
236};
237
238
239template <typename E>
240std::ostream& FileHelpers::writeEdgeVector(std::ostream& os, const std::vector<E>& edges) {
241 FileHelpers::writeInt(os, (int)edges.size());
242 std::vector<int> follow;
243 int maxFollow = 0;
244 E prev = edges.front();
245 for (typename std::vector<E>::const_iterator i = edges.begin() + 1; i != edges.end(); ++i) {
246 int idx = 0;
247 for (; idx < prev->getNumSuccessors(); ++idx) {
248 if (idx > 15) {
249 break;
250 }
251 if (prev->getSuccessors()[idx] == (*i)) {
252 follow.push_back(idx);
253 if (idx > maxFollow) {
254 maxFollow = idx;
255 }
256 break;
257 }
258 }
259 if (idx > 15 || idx == prev->getNumSuccessors()) {
260 follow.clear();
261 break;
262 }
263 prev = *i;
264 }
265 if (follow.empty()) {
266 for (typename std::vector<E>::const_iterator i = edges.begin(); i != edges.end(); ++i) {
267 FileHelpers::writeInt(os, (*i)->getNumericalID());
268 }
269 } else {
270 const int bits = maxFollow > 3 ? 4 : 2;
271 const int numFields = 8 * sizeof(int) / bits;
272 FileHelpers::writeInt(os, -bits);
273 FileHelpers::writeInt(os, edges.front()->getNumericalID());
274 int data = 0;
275 int field = 0;
276 for (std::vector<int>::const_iterator i = follow.begin(); i != follow.end(); ++i) {
277 data |= *i;
278 field++;
279 if (field == numFields) {
280 FileHelpers::writeInt(os, data);
281 data = 0;
282 field = 0;
283 } else {
284 data <<= bits;
285 }
286 }
287 if (field > 0) {
288 FileHelpers::writeInt(os, data << ((numFields - field - 1) * bits));
289 }
290 }
291 return os;
292}
293
294
295template <typename E>
296void FileHelpers::readEdgeVector(std::istream& in, std::vector<const E*>& edges, const std::string& rid) {
297 int size;
298 in.read((char*) &size, sizeof(int));
299 edges.reserve(size);
300 int bitsOrEntry;
301 in.read((char*) &bitsOrEntry, sizeof(int));
302 if (bitsOrEntry < 0) {
303 const int bits = -bitsOrEntry;
304 const int numFields = 8 * sizeof(int) / bits;
305 const int mask = (1 << bits) - 1;
306 int edgeID;
307 in.read((char*) &edgeID, sizeof(int));
308 const E* prev = E::getAllEdges()[edgeID];
309 assert(prev != 0);
310 edges.push_back(prev);
311 size--;
312 int data = 0;
313 int field = numFields;
314 for (; size > 0; size--) {
315 if (field == numFields) {
316 in.read((char*) &data, sizeof(int));
317 field = 0;
318 }
319 int followIndex = (data >> ((numFields - field - 1) * bits)) & mask;
320 if (followIndex >= prev->getNumSuccessors()) {
321 throw ProcessError(TLF("Invalid follower index in route '%'!", rid));
322 }
323 prev = prev->getSuccessors()[followIndex];
324 edges.push_back(prev);
325 field++;
326 }
327 } else {
328 while (size > 0) {
329 const E* edge = E::getAllEdges()[bitsOrEntry];
330 if (edge == 0) {
331 throw ProcessError(TLF("An edge within the route '%' is not known!", rid));
332 }
333 edges.push_back(edge);
334 size--;
335 if (size > 0) {
336 in.read((char*) &bitsOrEntry, sizeof(int));
337 }
338 }
339 }
340}
long long int SUMOTime
Definition GUI.h:36
#define TLF(string,...)
Definition MsgHandler.h:306
Functions for an easier usage of files and paths.
Definition FileHelpers.h:39
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 accessible 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::string appendBeforeExtension(const std::string &path, const std::string &suffix, bool checkSep=true)
append the given suffix after the file name but before the extension
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