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.sourceforge.net/
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(std::string filename,
124  const std::string& basePath) {
125  if (!isAbsolute(filename)) {
126  filename = getConfigurationRelative(basePath, filename);
127  }
128  return filename;
129 }
130 
131 
132 // ---------------------------------------------------------------------------
133 // binary reading/writing functions
134 // ---------------------------------------------------------------------------
135 std::ostream&
136 FileHelpers::writeInt(std::ostream& strm, int value) {
137  strm.write((char*) &value, sizeof(int));
138  return strm;
139 }
140 
141 
142 std::ostream&
143 FileHelpers::writeUInt(std::ostream& strm, unsigned int value) {
144  strm.write((char*) &value, sizeof(unsigned int));
145  return strm;
146 }
147 
148 
149 std::ostream&
150 FileHelpers::writeFloat(std::ostream& strm, SUMOReal value) {
151  strm.write((char*) &value, sizeof(SUMOReal));
152  return strm;
153 }
154 
155 
156 std::ostream&
157 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
158  strm.write((char*) &value, sizeof(char));
159  return strm;
160 }
161 
162 
163 std::ostream&
164 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
165  size_t size = value.length();
166  const char* cstr = value.c_str();
167  writeUInt(strm, (unsigned int) size);
168  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
169  return strm;
170 }
171 
172 
173 std::ostream&
174 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
175  strm.write((char*) &value, sizeof(SUMOTime));
176  return strm;
177 }
178 
179 
180 /****************************************************************************/
181