Eclipse SUMO - Simulation of Urban MObility
FileHelpers.cpp
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 /****************************************************************************/
19 // Functions for an easier usage of files
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <string>
24 #ifdef WIN32
25 // this is how fox does it in xincs.h
26 #include <io.h>
27 #define access _access
28 #define R_OK 4 /* Test for read permission. */
29 #else
30 #include <unistd.h>
31 #endif
32 #include <fstream>
33 #include <sys/stat.h>
34 #include "FileHelpers.h"
35 #include "StringTokenizer.h"
36 #include "StringUtils.h"
37 #include "MsgHandler.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 
44 // ---------------------------------------------------------------------------
45 // file access functions
46 // ---------------------------------------------------------------------------
47 
48 bool
49 FileHelpers::isReadable(std::string path) {
50  if (path.length() == 0) {
51  return false;
52  }
53  while (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') {
54  path.erase(path.end() - 1);
55  }
56  if (path.length() == 0) {
57  return false;
58  }
59  return access(StringUtils::transcodeToLocal(path).c_str(), R_OK) == 0;
60 }
61 
62 bool
63 FileHelpers::isDirectory(std::string path) {
64 #ifdef _MSC_VER
65  struct _stat64 fileInfo;
66  if (_stat64(StringUtils::transcodeToLocal(path).c_str(), &fileInfo) != 0) {
67 #else
68  struct stat fileInfo;
69  if (stat(StringUtils::transcodeToLocal(path).c_str(), &fileInfo) != 0) {
70 #endif
71  throw ProcessError("Cannot get file attributes for file '" + path + "'!");
72  }
73  return (fileInfo.st_mode & S_IFMT) == S_IFDIR;
74 }
75 
76 // ---------------------------------------------------------------------------
77 // file path evaluating functions
78 // ---------------------------------------------------------------------------
79 
80 std::string
81 FileHelpers::getFilePath(const std::string& path) {
82  const std::string::size_type beg = path.find_last_of("\\/");
83  if (beg == std::string::npos) {
84  return "";
85  }
86  return path.substr(0, beg + 1);
87 }
88 
89 
90 std::string
91 FileHelpers::addExtension(const std::string& path, const std::string& extension) {
92  if (path.empty()) {
93  return "";
94  } else if (extension.empty()) {
95  return path;
96  } else if (path == extension) {
97  return "";
98  } else if (path.size() < extension.size()) {
99  return path + extension;
100  } else {
101  // declare two reverse iterator for every string
102  std::string::const_reverse_iterator it_path = path.rbegin();
103  std::string::const_reverse_iterator it_extension = extension.rbegin();
104  // iterate over extension and compare both characters
105  while (it_extension != extension.rend()) {
106  // if both characters are different, then return path + extension
107  if (*it_path != *it_extension) {
108  return path + extension;
109  }
110  it_path++;
111  it_extension++;
112  }
113  // if comparison was successful, then the path has already the extension
114  return path;
115  }
116 }
117 
118 
119 std::string
120 FileHelpers::getConfigurationRelative(const std::string& configPath, const std::string& path) {
121  std::string retPath = getFilePath(configPath);
122  return retPath + path;
123 }
124 
125 
126 bool
127 FileHelpers::isSocket(const std::string& name) {
128  const std::string::size_type colonPos = name.find(":");
129  return (colonPos != std::string::npos) && (colonPos > 1);
130 }
131 
132 
133 bool
134 FileHelpers::isAbsolute(const std::string& path) {
135  if (isSocket(path)) {
136  return true;
137  }
138  // check UNIX - absolute paths
139  if (path.length() > 0 && path[0] == '/') {
140  return true;
141  }
142  // check Windows - absolute paths
143  if (path.length() > 0 && path[0] == '\\') {
144  return true;
145  }
146  if (path.length() > 1 && path[1] == ':') {
147  return true;
148  }
149  if (path == "nul" || path == "NUL") {
150  return true;
151  }
152  return false;
153 }
154 
155 
156 std::string
157 FileHelpers::checkForRelativity(const std::string& filename, const std::string& basePath) {
158  if (filename == "stdout" || filename == "STDOUT" || filename == "-") {
159  return "stdout";
160  }
161  if (filename == "stderr" || filename == "STDERR") {
162  return "stderr";
163  }
164  if (filename == "nul" || filename == "NUL") {
165  return "/dev/null";
166  }
167  if (!isSocket(filename) && !isAbsolute(filename)) {
168  return getConfigurationRelative(basePath, filename);
169  }
170  return filename;
171 }
172 
173 
174 std::string
175 FileHelpers::prependToLastPathComponent(const std::string& prefix, const std::string& path) {
176  const std::string::size_type sep_index = path.find_last_of("\\/");
177  if (sep_index == std::string::npos) {
178  return prefix + path;
179  } else {
180  return path.substr(0, sep_index + 1) + prefix + path.substr(sep_index + 1);
181  }
182 }
183 
184 // ---------------------------------------------------------------------------
185 // binary reading/writing functions
186 // ---------------------------------------------------------------------------
187 
188 std::ostream&
189 FileHelpers::writeInt(std::ostream& strm, int value) {
190  strm.write((char*) &value, sizeof(int));
191  return strm;
192 }
193 
194 
195 std::ostream&
196 FileHelpers::writeFloat(std::ostream& strm, double value) {
197  strm.write((char*) &value, sizeof(double));
198  return strm;
199 }
200 
201 
202 std::ostream&
203 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
204  strm.write((char*) &value, sizeof(char));
205  return strm;
206 }
207 
208 
209 std::ostream&
210 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
211  int size = (int)value.length();
212  const char* cstr = value.c_str();
213  writeInt(strm, size);
214  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
215  return strm;
216 }
217 
218 
219 std::ostream&
220 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
221  strm.write((char*) &value, sizeof(SUMOTime));
222  return strm;
223 }
224 
225 
226 /****************************************************************************/
long long int SUMOTime
Definition: SUMOTime.h:32
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 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:91
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:49
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:81
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 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:63
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
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage