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-2016 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  const std::string::size_type 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  const std::string::size_type 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 (filename == "nul" || filename == "NUL") {
133  return "/dev/null";
134  }
135  if (!isSocket(filename) && !isAbsolute(filename)) {
136  return getConfigurationRelative(basePath, filename);
137  }
138  return filename;
139 }
140 
141 
142 std::string
143 FileHelpers::prependToLastPathComponent(const std::string& prefix, const std::string& path) {
144  const std::string::size_type sep_index = path.find_last_of("\\/");
145  if (sep_index == std::string::npos) {
146  return prefix + path;
147  } else {
148  return path.substr(0, sep_index + 1) + prefix + path.substr(sep_index + 1);
149  }
150 }
151 
152 // ---------------------------------------------------------------------------
153 // binary reading/writing functions
154 // ---------------------------------------------------------------------------
155 std::ostream&
156 FileHelpers::writeInt(std::ostream& strm, int value) {
157  strm.write((char*) &value, sizeof(int));
158  return strm;
159 }
160 
161 
162 std::ostream&
163 FileHelpers::writeUInt(std::ostream& strm, int value) {
164  strm.write((char*) &value, sizeof(int));
165  return strm;
166 }
167 
168 
169 std::ostream&
170 FileHelpers::writeFloat(std::ostream& strm, SUMOReal value) {
171  strm.write((char*) &value, sizeof(SUMOReal));
172  return strm;
173 }
174 
175 
176 std::ostream&
177 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
178  strm.write((char*) &value, sizeof(char));
179  return strm;
180 }
181 
182 
183 std::ostream&
184 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
185  int size = (int)value.length();
186  const char* cstr = value.c_str();
187  writeUInt(strm, (int) size);
188  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
189  return strm;
190 }
191 
192 
193 std::ostream&
194 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
195  strm.write((char*) &value, sizeof(SUMOTime));
196  return strm;
197 }
198 
199 
200 /****************************************************************************/
201 
long long int SUMOTime
Definition: SUMOTime.h:43
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 & 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::ostream & writeUInt(std::ostream &strm, int value)
Writes an integer binary.
#define SUMOReal
Definition: config.h:214
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.