SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00008 // Functions for an easier usage of files 00009 /****************************************************************************/ 00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00012 /****************************************************************************/ 00013 // 00014 // This file is part of SUMO. 00015 // SUMO is free software: you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation, either version 3 of the License, or 00018 // (at your option) any later version. 00019 // 00020 /****************************************************************************/ 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <string> 00033 #include <cstring> 00034 #include <fstream> 00035 #include <sys/stat.h> 00036 #include "FileHelpers.h" 00037 #include "StringTokenizer.h" 00038 #include "MsgHandler.h" 00039 00040 #ifdef CHECK_MEMORY_LEAKS 00041 #include <foreign/nvwa/debug_new.h> 00042 #endif // CHECK_MEMORY_LEAKS 00043 00044 00045 // =========================================================================== 00046 // method definitions 00047 // =========================================================================== 00048 // --------------------------------------------------------------------------- 00049 // file access functions 00050 // --------------------------------------------------------------------------- 00051 bool 00052 FileHelpers::exists(std::string path) { 00053 if (path.length() == 0) { 00054 return false; 00055 } 00056 while (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\') { 00057 path.erase(path.end() - 1); 00058 } 00059 if (path.length() == 0) { 00060 return false; 00061 } 00062 struct stat st; 00063 bool ret = (stat(path.c_str(), &st) == 0); 00064 return ret; 00065 } 00066 00067 00068 // --------------------------------------------------------------------------- 00069 // file path evaluating functions 00070 // --------------------------------------------------------------------------- 00071 std::string 00072 FileHelpers::getFilePath(const std::string& path) { 00073 size_t beg = path.find_last_of("\\/"); 00074 if (beg == std::string::npos || beg == 0) { 00075 return ""; 00076 } 00077 return path.substr(0, beg + 1); 00078 } 00079 00080 00081 std::string 00082 FileHelpers::getConfigurationRelative(const std::string& configPath, 00083 const std::string& path) { 00084 std::string retPath = getFilePath(configPath); 00085 return retPath + path; 00086 } 00087 00088 00089 bool 00090 FileHelpers::isSocket(const std::string& name) { 00091 size_t colonPos = name.find(":"); 00092 return (colonPos != std::string::npos) && (colonPos > 1); 00093 } 00094 00095 00096 bool 00097 FileHelpers::isAbsolute(const std::string& path) { 00098 if (isSocket(path)) { 00099 return true; 00100 } 00101 // check UNIX - absolute paths 00102 if (path.length() > 0 && path[0] == '/') { 00103 return true; 00104 } 00105 // check Windows - absolute paths 00106 if (path.length() > 0 && path[0] == '\\') { 00107 return true; 00108 } 00109 if (path.length() > 1 && path[1] == ':') { 00110 return true; 00111 } 00112 if (path == "nul" || path == "NUL") { 00113 return true; 00114 } 00115 return false; 00116 } 00117 00118 00119 std::string 00120 FileHelpers::checkForRelativity(std::string filename, 00121 const std::string& basePath) { 00122 if (!isAbsolute(filename)) { 00123 filename = getConfigurationRelative(basePath, filename); 00124 } 00125 return filename; 00126 } 00127 00128 00129 // --------------------------------------------------------------------------- 00130 // binary reading/writing functions 00131 // --------------------------------------------------------------------------- 00132 std::ostream& 00133 FileHelpers::writeInt(std::ostream& strm, int value) { 00134 strm.write((char*) &value, sizeof(int)); 00135 return strm; 00136 } 00137 00138 00139 std::ostream& 00140 FileHelpers::writeUInt(std::ostream& strm, unsigned int value) { 00141 strm.write((char*) &value, sizeof(unsigned int)); 00142 return strm; 00143 } 00144 00145 00146 std::ostream& 00147 FileHelpers::writeFloat(std::ostream& strm, SUMOReal value) { 00148 strm.write((char*) &value, sizeof(SUMOReal)); 00149 return strm; 00150 } 00151 00152 00153 std::ostream& 00154 FileHelpers::writeByte(std::ostream& strm, unsigned char value) { 00155 strm.write((char*) &value, sizeof(char)); 00156 return strm; 00157 } 00158 00159 00160 std::ostream& 00161 FileHelpers::writeString(std::ostream& strm, const std::string& value) { 00162 size_t size = value.length(); 00163 const char* cstr = value.c_str(); 00164 writeUInt(strm, (unsigned int) size); 00165 strm.write((char*) cstr, (std::streamsize)(sizeof(char)*size)); 00166 return strm; 00167 } 00168 00169 00170 std::ostream& 00171 FileHelpers::writeTime(std::ostream& strm, SUMOTime value) { 00172 strm.write((char*) &value, sizeof(SUMOTime)); 00173 return strm; 00174 } 00175 00176 00177 /****************************************************************************/ 00178