FileHelpers.cpp

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

Generated on Wed May 5 00:06:28 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6