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