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