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