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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Functions for an easier usage of files
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #ifdef _MSC_VER
26 // this is how fox does it in xincs.h
27 #include <io.h>
28 #define access _access
29 #define R_OK 4 /* Test for read permission. */
30 #else
31 #include <unistd.h>
32 #endif
33 #include <fstream>
34 #include <sys/stat.h>
35 #include "FileHelpers.h"
36 #include "StringTokenizer.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(path.c_str(), R_OK) == 0;
60 }
61 
62 bool
63 FileHelpers::isDirectory(std::string path) {
64  struct stat fileInfo;
65  if (stat(path.c_str(), &fileInfo) != 0) {
66  throw ProcessError("Cannot get file attributes for file '" + path + "'!");
67  }
68  return (fileInfo.st_mode & S_IFMT) == S_IFDIR;
69 }
70 
71 // ---------------------------------------------------------------------------
72 // file path evaluating functions
73 // ---------------------------------------------------------------------------
74 
75 std::string
76 FileHelpers::getFilePath(const std::string& path) {
77  const std::string::size_type beg = path.find_last_of("\\/");
78  if (beg == std::string::npos) {
79  return "";
80  }
81  return path.substr(0, beg + 1);
82 }
83 
84 
85 std::string
86 FileHelpers::addExtension(const std::string& path, const std::string& extension) {
87  if (path.empty()) {
88  return "";
89  } else if (extension.empty()) {
90  return path;
91  } else if (path == extension) {
92  return "";
93  } else if (path.size() < extension.size()) {
94  return path + extension;
95  } else {
96  // declare two reverse iterator for every string
97  std::string::const_reverse_iterator it_path = path.rbegin();
98  std::string::const_reverse_iterator it_extension = extension.rbegin();
99  // iterate over extension and compare both characters
100  while (it_extension != extension.rend()) {
101  // if both characters are different, then return path + extension
102  if (*it_path != *it_extension) {
103  return path + extension;
104  }
105  it_path++;
106  it_extension++;
107  }
108  // if comparison was successful, then the path has already the extension
109  return path;
110  }
111 }
112 
113 
114 std::string
115 FileHelpers::getConfigurationRelative(const std::string& configPath, const std::string& path) {
116  std::string retPath = getFilePath(configPath);
117  return retPath + path;
118 }
119 
120 
121 bool
122 FileHelpers::isSocket(const std::string& name) {
123  const std::string::size_type colonPos = name.find(":");
124  return (colonPos != std::string::npos) && (colonPos > 1);
125 }
126 
127 
128 bool
129 FileHelpers::isAbsolute(const std::string& path) {
130  if (isSocket(path)) {
131  return true;
132  }
133  // check UNIX - absolute paths
134  if (path.length() > 0 && path[0] == '/') {
135  return true;
136  }
137  // check Windows - absolute paths
138  if (path.length() > 0 && path[0] == '\\') {
139  return true;
140  }
141  if (path.length() > 1 && path[1] == ':') {
142  return true;
143  }
144  if (path == "nul" || path == "NUL") {
145  return true;
146  }
147  return false;
148 }
149 
150 
151 std::string
152 FileHelpers::checkForRelativity(const std::string& filename, const std::string& basePath) {
153  if (filename == "stdout" || filename == "STDOUT" || filename == "-") {
154  return "stdout";
155  }
156  if (filename == "stderr" || filename == "STDERR") {
157  return "stderr";
158  }
159  if (filename == "nul" || filename == "NUL") {
160  return "/dev/null";
161  }
162  if (!isSocket(filename) && !isAbsolute(filename)) {
163  return getConfigurationRelative(basePath, filename);
164  }
165  return filename;
166 }
167 
168 
169 std::string
170 FileHelpers::prependToLastPathComponent(const std::string& prefix, const std::string& path) {
171  const std::string::size_type sep_index = path.find_last_of("\\/");
172  if (sep_index == std::string::npos) {
173  return prefix + path;
174  } else {
175  return path.substr(0, sep_index + 1) + prefix + path.substr(sep_index + 1);
176  }
177 }
178 
179 // ---------------------------------------------------------------------------
180 // binary reading/writing functions
181 // ---------------------------------------------------------------------------
182 
183 std::ostream&
184 FileHelpers::writeInt(std::ostream& strm, int value) {
185  strm.write((char*) &value, sizeof(int));
186  return strm;
187 }
188 
189 
190 std::ostream&
191 FileHelpers::writeFloat(std::ostream& strm, double value) {
192  strm.write((char*) &value, sizeof(double));
193  return strm;
194 }
195 
196 
197 std::ostream&
198 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
199  strm.write((char*) &value, sizeof(char));
200  return strm;
201 }
202 
203 
204 std::ostream&
205 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
206  int size = (int)value.length();
207  const char* cstr = value.c_str();
208  writeInt(strm, size);
209  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
210  return strm;
211 }
212 
213 
214 std::ostream&
215 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
216  strm.write((char*) &value, sizeof(SUMOTime));
217  return strm;
218 }
219 
220 /****************************************************************************/
221 
FileHelpers::addExtension
static std::string addExtension(const std::string &path, const std::string &extension)
Add an extension to the given file path.
Definition: FileHelpers.cpp:86
MsgHandler.h
FileHelpers::getConfigurationRelative
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:115
FileHelpers.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
FileHelpers::writeInt
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
Definition: FileHelpers.cpp:184
FileHelpers::checkForRelativity
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.
Definition: FileHelpers.cpp:152
FileHelpers::writeFloat
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float binary.
Definition: FileHelpers.cpp:191
FileHelpers::writeByte
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
Definition: FileHelpers.cpp:198
FileHelpers::getFilePath
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:76
FileHelpers::writeTime
static std::ostream & writeTime(std::ostream &strm, SUMOTime value)
Writes a time description binary.
Definition: FileHelpers.cpp:215
FileHelpers::prependToLastPathComponent
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
Definition: FileHelpers.cpp:170
FileHelpers::isSocket
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:122
FileHelpers::writeString
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.
Definition: FileHelpers.cpp:205
ProcessError
Definition: UtilExceptions.h:39
FileHelpers::isAbsolute
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Definition: FileHelpers.cpp:129
FileHelpers::isDirectory
static bool isDirectory(std::string path)
Checks whether the given file is a directory.
Definition: FileHelpers.cpp:63
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
config.h
StringTokenizer.h