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