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-2012 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 #include <cstring>
34 #include <fstream>
35 #include <sys/stat.h>
36 #include "FileHelpers.h"
37 #include "StringTokenizer.h"
38 #include "MsgHandler.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 // ---------------------------------------------------------------------------
49 // file access functions
50 // ---------------------------------------------------------------------------
51 bool
52 FileHelpers::exists(std::string path) {
53  if (path.length() == 0) {
54  return false;
55  }
56  while (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') {
57  path.erase(path.end() - 1);
58  }
59  if (path.length() == 0) {
60  return false;
61  }
62  struct stat st;
63  bool ret = (stat(path.c_str(), &st) == 0);
64  return ret;
65 }
66 
67 
68 // ---------------------------------------------------------------------------
69 // file path evaluating functions
70 // ---------------------------------------------------------------------------
71 std::string
72 FileHelpers::getFilePath(const std::string& path) {
73  size_t 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  size_t 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(std::string filename,
121  const std::string& basePath) {
122  if (!isAbsolute(filename)) {
123  filename = getConfigurationRelative(basePath, filename);
124  }
125  return filename;
126 }
127 
128 
129 // ---------------------------------------------------------------------------
130 // binary reading/writing functions
131 // ---------------------------------------------------------------------------
132 std::ostream&
133 FileHelpers::writeInt(std::ostream& strm, int value) {
134  strm.write((char*) &value, sizeof(int));
135  return strm;
136 }
137 
138 
139 std::ostream&
140 FileHelpers::writeUInt(std::ostream& strm, unsigned int value) {
141  strm.write((char*) &value, sizeof(unsigned int));
142  return strm;
143 }
144 
145 
146 std::ostream&
147 FileHelpers::writeFloat(std::ostream& strm, SUMOReal value) {
148  strm.write((char*) &value, sizeof(SUMOReal));
149  return strm;
150 }
151 
152 
153 std::ostream&
154 FileHelpers::writeByte(std::ostream& strm, unsigned char value) {
155  strm.write((char*) &value, sizeof(char));
156  return strm;
157 }
158 
159 
160 std::ostream&
161 FileHelpers::writeString(std::ostream& strm, const std::string& value) {
162  size_t size = value.length();
163  const char* cstr = value.c_str();
164  writeUInt(strm, (unsigned int) size);
165  strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size));
166  return strm;
167 }
168 
169 
170 std::ostream&
171 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) {
172  strm.write((char*) &value, sizeof(SUMOTime));
173  return strm;
174 }
175 
176 
177 /****************************************************************************/
178