SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2015 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 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 // ---------------------------------------------------------------------------
55 // file access functions
56 // ---------------------------------------------------------------------------
57 bool
58 FileHelpers::isReadable(std::string path) {
59  if (path.length() == 0) {
60  return false;
61  }
62  while (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') {
63  path.erase(path.end() - 1);
64  }
65  if (path.length() == 0) {
66  return false;
67  }
68  return access(path.c_str(), R_OK) == 0;
69 }
70 
71 
72 // ---------------------------------------------------------------------------
73 // file path evaluating functions
74 // ---------------------------------------------------------------------------
75 std::string
76 FileHelpers::getFilePath(const std::string& path) {
77  size_t beg = path.find_last_of("\\/");
78  if (beg == std::string::npos || beg == 0) {
79  return "";
80  }
81  return path.substr(0, beg + 1);
82 }
83 
84 
85 std::string
86 FileHelpers::getConfigurationRelative(const std::string& configPath,
87  const std::string& path) {
88  std::string retPath = getFilePath(configPath);
89  return retPath + path;
90 }
91 
92 
93 bool
94 FileHelpers::isSocket(const std::string& name) {
95  size_t colonPos = name.find(":");
96  return (colonPos != std::string::npos) && (colonPos > 1);
97 }
98 
99 
100 bool
101 FileHelpers::isAbsolute(const std::string& path) {
102  if (isSocket(path)) {
103  return true;
104  }
105  // check UNIX - absolute paths
106  if (path.length() > 0 && path[0] == '/') {
107  return true;
108  }
109  // check Windows - absolute paths
110  if (path.length() > 0 && path[0] == '\\') {
111  return true;
112  }
113  if (path.length() > 1 && path[1] == ':') {
114  return true;
115  }
116  if (path == "nul" || path == "NUL") {
117  return true;
118  }
119  return false;
120 }
121 
122 
123 std::string
124 FileHelpers::checkForRelativity(const std::string& filename,
125  const std::string& basePath) {
126  if (filename == "stdout" || filename == "STDOUT" || filename == "-") {
127  return "stdout";
128  }
129  if (filename == "stderr" || filename == "STDERR") {
130  return "stderr";
131  }
132  if (!isSocket(filename) && !isAbsolute(filename)) {
133  return getConfigurationRelative(basePath, filename);
134  }
135  return filename;
136 }
137 
138 
139 std::string
140 FileHelpers::prependToLastPathComponent(const std::string& prefix, const std::string& path) {
141  size_t sep_index = path.find_last_of("\\/");
142  if (sep_index == std::string::npos) {
143  return prefix + path;
144  } else {
145  return path.substr(0, sep_index + 1) + prefix + path.substr(sep_index + 1);
146  }
147 }
148 
149 // ---------------------------------------------------------------------------
150 // binary reading/writing functions
151 // ---------------------------------------------------------------------------
152 std::ostream&
153 FileHelpers::writeInt(std::ostream& strm, int value) {
154  strm.write((char*) &value, sizeof(int));
155  return strm;
156 }
157 
158 
159 std::ostream&
160 FileHelpers::writeUInt(std::ostream& strm, unsigned int value) {
161  strm.write((char*) &value, sizeof(unsigned int));
162  return strm;
163 }
164 
165 
166 std::ostream&
167 FileHelpers::writeFloat(std::ostream& strm, SUMOReal value) {
168  strm.write((char*) &value, sizeof(SUMOReal));
169  return strm;
170 }
171 
172 
173 std::ostream&
174 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
175  strm.write((char*) &value, sizeof(char));
176  return strm;
177 }
178 
179 
180 std::ostream&
181 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
182  size_t size = value.length();
183  const char* cstr = value.c_str();
184  writeUInt(strm, (unsigned int) size);
185  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
186  return strm;
187 }
188 
189 
190 std::ostream&
191 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
192  strm.write((char*) &value, sizeof(SUMOTime));
193  return strm;
194 }
195 
196 
197 /****************************************************************************/
198 
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:86
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:58
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:94
static std::ostream & writeFloat(std::ostream &strm, SUMOReal value)
Writes a float binary.
static std::ostream & writeUInt(std::ostream &strm, unsigned int value)
Writes an unsigned integer 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...
int SUMOTime
Definition: SUMOTime.h:43
#define SUMOReal
Definition: config.h:218
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
Definition: FileHelpers.cpp:76
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.