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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Functions for an easier usage of files
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #ifdef _MSC_VER
33 // this is how fox does it in xincs.h
34 #include <io.h>
35 #define access _access
36 #define R_OK 4 /* Test for read permission. */
37 #else
38 #include <unistd.h>
39 #endif
40 #include <fstream>
41 #include "FileHelpers.h"
42 #include "StringTokenizer.h"
43 #include "MsgHandler.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 // ---------------------------------------------------------------------------
50 // file access functions
51 // ---------------------------------------------------------------------------
52 bool
53 FileHelpers::isReadable(std::string path) {
54  if (path.length() == 0) {
55  return false;
56  }
57  while (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') {
58  path.erase(path.end() - 1);
59  }
60  if (path.length() == 0) {
61  return false;
62  }
63  return access(path.c_str(), R_OK) == 0;
64 }
65 
66 
67 // ---------------------------------------------------------------------------
68 // file path evaluating functions
69 // ---------------------------------------------------------------------------
70 std::string
71 FileHelpers::getFilePath(const std::string& path) {
72  const std::string::size_type beg = path.find_last_of("\\/");
73  if (beg == std::string::npos || beg == 0) {
74  return "";
75  }
76  return path.substr(0, beg + 1);
77 }
78 
79 
80 std::string
81 FileHelpers::getConfigurationRelative(const std::string& configPath,
82  const std::string& path) {
83  std::string retPath = getFilePath(configPath);
84  return retPath + path;
85 }
86 
87 
88 bool
89 FileHelpers::isSocket(const std::string& name) {
90  const std::string::size_type colonPos = name.find(":");
91  return (colonPos != std::string::npos) && (colonPos > 1);
92 }
93 
94 
95 bool
96 FileHelpers::isAbsolute(const std::string& path) {
97  if (isSocket(path)) {
98  return true;
99  }
100  // check UNIX - absolute paths
101  if (path.length() > 0 && path[0] == '/') {
102  return true;
103  }
104  // check Windows - absolute paths
105  if (path.length() > 0 && path[0] == '\\') {
106  return true;
107  }
108  if (path.length() > 1 && path[1] == ':') {
109  return true;
110  }
111  if (path == "nul" || path == "NUL") {
112  return true;
113  }
114  return false;
115 }
116 
117 
118 std::string
119 FileHelpers::checkForRelativity(const std::string& filename,
120  const std::string& basePath) {
121  if (filename == "stdout" || filename == "STDOUT" || filename == "-") {
122  return "stdout";
123  }
124  if (filename == "stderr" || filename == "STDERR") {
125  return "stderr";
126  }
127  if (filename == "nul" || filename == "NUL") {
128  return "/dev/null";
129  }
130  if (!isSocket(filename) && !isAbsolute(filename)) {
131  return getConfigurationRelative(basePath, filename);
132  }
133  return filename;
134 }
135 
136 
137 std::string
138 FileHelpers::prependToLastPathComponent(const std::string& prefix, const std::string& path) {
139  const std::string::size_type sep_index = path.find_last_of("\\/");
140  if (sep_index == std::string::npos) {
141  return prefix + path;
142  } else {
143  return path.substr(0, sep_index + 1) + prefix + path.substr(sep_index + 1);
144  }
145 }
146 
147 // ---------------------------------------------------------------------------
148 // binary reading/writing functions
149 // ---------------------------------------------------------------------------
150 std::ostream&
151 FileHelpers::writeInt(std::ostream& strm, int value) {
152  strm.write((char*) &value, sizeof(int));
153  return strm;
154 }
155 
156 
157 std::ostream&
158 FileHelpers::writeFloat(std::ostream& strm, double value) {
159  strm.write((char*) &value, sizeof(double));
160  return strm;
161 }
162 
163 
164 std::ostream&
165 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
166  strm.write((char*) &value, sizeof(char));
167  return strm;
168 }
169 
170 
171 std::ostream&
172 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
173  int size = (int)value.length();
174  const char* cstr = value.c_str();
175  writeInt(strm, size);
176  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
177  return strm;
178 }
179 
180 
181 std::ostream&
182 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
183  strm.write((char*) &value, sizeof(SUMOTime));
184  return strm;
185 }
186 
187 
188 /****************************************************************************/
189 
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:81
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:53
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:89
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:96
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...
long long int SUMOTime
Definition: TraCIDefs.h:51
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:71
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.