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-2018 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 // file access functions
45 // ---------------------------------------------------------------------------
46 bool
47 FileHelpers::isReadable(std::string path) {
48  if (path.length() == 0) {
49  return false;
50  }
51  while (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') {
52  path.erase(path.end() - 1);
53  }
54  if (path.length() == 0) {
55  return false;
56  }
57  return access(path.c_str(), R_OK) == 0;
58 }
59 
60 
61 // ---------------------------------------------------------------------------
62 // file path evaluating functions
63 // ---------------------------------------------------------------------------
64 std::string
65 FileHelpers::getFilePath(const std::string& path) {
66  const std::string::size_type beg = path.find_last_of("\\/");
67  if (beg == std::string::npos) {
68  return "";
69  }
70  return path.substr(0, beg + 1);
71 }
72 
73 
74 std::string
75 FileHelpers::getConfigurationRelative(const std::string& configPath,
76  const std::string& path) {
77  std::string retPath = getFilePath(configPath);
78  return retPath + path;
79 }
80 
81 
82 bool
83 FileHelpers::isSocket(const std::string& name) {
84  const std::string::size_type colonPos = name.find(":");
85  return (colonPos != std::string::npos) && (colonPos > 1);
86 }
87 
88 
89 bool
90 FileHelpers::isAbsolute(const std::string& path) {
91  if (isSocket(path)) {
92  return true;
93  }
94  // check UNIX - absolute paths
95  if (path.length() > 0 && path[0] == '/') {
96  return true;
97  }
98  // check Windows - absolute paths
99  if (path.length() > 0 && path[0] == '\\') {
100  return true;
101  }
102  if (path.length() > 1 && path[1] == ':') {
103  return true;
104  }
105  if (path == "nul" || path == "NUL") {
106  return true;
107  }
108  return false;
109 }
110 
111 
112 std::string
113 FileHelpers::checkForRelativity(const std::string& filename,
114  const std::string& basePath) {
115  if (filename == "stdout" || filename == "STDOUT" || filename == "-") {
116  return "stdout";
117  }
118  if (filename == "stderr" || filename == "STDERR") {
119  return "stderr";
120  }
121  if (filename == "nul" || filename == "NUL") {
122  return "/dev/null";
123  }
124  if (!isSocket(filename) && !isAbsolute(filename)) {
125  return getConfigurationRelative(basePath, filename);
126  }
127  return filename;
128 }
129 
130 
131 std::string
132 FileHelpers::prependToLastPathComponent(const std::string& prefix, const std::string& path) {
133  const std::string::size_type sep_index = path.find_last_of("\\/");
134  if (sep_index == std::string::npos) {
135  return prefix + path;
136  } else {
137  return path.substr(0, sep_index + 1) + prefix + path.substr(sep_index + 1);
138  }
139 }
140 
141 // ---------------------------------------------------------------------------
142 // binary reading/writing functions
143 // ---------------------------------------------------------------------------
144 std::ostream&
145 FileHelpers::writeInt(std::ostream& strm, int value) {
146  strm.write((char*) &value, sizeof(int));
147  return strm;
148 }
149 
150 
151 std::ostream&
152 FileHelpers::writeFloat(std::ostream& strm, double value) {
153  strm.write((char*) &value, sizeof(double));
154  return strm;
155 }
156 
157 
158 std::ostream&
159 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
160  strm.write((char*) &value, sizeof(char));
161  return strm;
162 }
163 
164 
165 std::ostream&
166 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
167  int size = (int)value.length();
168  const char* cstr = value.c_str();
169  writeInt(strm, size);
170  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
171  return strm;
172 }
173 
174 
175 std::ostream&
176 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
177  strm.write((char*) &value, sizeof(SUMOTime));
178  return strm;
179 }
180 
181 
182 /****************************************************************************/
183 
long long int SUMOTime
Definition: SUMOTime.h:36
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:75
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:47
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:83
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.
Definition: FileHelpers.cpp:90
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 getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:65
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.