FileHelpers Class Reference

#include <FileHelpers.h>


Detailed Description

Functions for an easier usage of files and paths.

Definition at line 44 of file FileHelpers.h.


Static Public Member Functions

static std::ostream & writeTime (std::ostream &strm, SUMOTime value)
 Writes a time description binary.
file path evaluating functions
static std::string checkForRelativity (std::string filename, const std::string &basePath)
 Returns the path from a configuration so that it is accessable from the current working directory.
static std::string getConfigurationRelative (const std::string &configPath, const std::string &path)
 Returns the second path as a relative path to the first file.
static std::string getFilePath (const std::string &path)
 Removes the file information from the given path.
static bool isAbsolute (const std::string &path)
 Returns the information whether the given path is absolute.
static bool isSocket (const std::string &name)
 Returns the information whether the given name represents a socket.
file access functions
static bool exists (std::string path)
 Checks whether the given file exists.
binary writing functions
static std::ostream & writeByte (std::ostream &strm, unsigned char value)
 Writes a byte binary.
static std::ostream & writeFloat (std::ostream &strm, SUMOReal value)
 Writes a float binary.
static std::ostream & writeInt (std::ostream &strm, int value)
 Writes an integer binary.
static std::ostream & writeString (std::ostream &strm, const std::string &value)
 Writes a string binary.
static std::ostream & writeUInt (std::ostream &strm, unsigned int value)
 Writes an unsigned integer binary.

Member Function Documentation

std::string FileHelpers::checkForRelativity ( std::string  filename,
const std::string &  basePath 
) [static]

Returns the path from a configuration so that it is accessable from the current working directory.

If the path is absolute, it is returned. Otherwise, the file's position is computed regarding the configuration path (see getConfigurationRelative).

See also:
isAbsolute

getConfigurationRelative

Parameters:
[in] filename The path to the file to be examined
[in] basePath The path the configuration file (including the config's file name)
Returns:
The file's position

Definition at line 118 of file FileHelpers.cpp.

References getConfigurationRelative(), and isAbsolute().

Referenced by OutputDevice::getDevice().

00119                                                            {
00120     if (!isAbsolute(filename)) {
00121         filename = getConfigurationRelative(basePath, filename);
00122     }
00123     return filename;
00124 }

bool FileHelpers::exists ( std::string  path  )  [static]

Checks whether the given file exists.

Parameters:
[in] path The path to the file that shall be examined
Returns:
Whether the named file exists

Definition at line 50 of file FileHelpers.cpp.

Referenced by NLBuilder::buildRouteLoaderControl(), OptionsCont::isUsableFileList(), OptionsIO::loadConfiguration(), loadDistricts(), PCNetProjectionLoader::loadIfSet(), PCLoaderXML::loadIfSet(), PCLoaderVisum::loadIfSet(), PCLoaderOSM::loadIfSet(), ROLoader::loadNet(), NIImporter_SUMO::loadNetwork(), NIImporter_RobocupRescue::loadNetwork(), NIImporter_OpenStreetMap::loadNetwork(), NIImporter_OpenDrive::loadNetwork(), NIImporter_ArcView::loadNetwork(), PCLoaderDlrNavteq::loadPOIFiles(), PCLoaderDlrNavteq::loadPolyFiles(), NILoader::loadXMLType(), readDetectorFlows(), and readDetectors().

00050                                   {
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 }

std::string FileHelpers::getConfigurationRelative ( const std::string &  configPath,
const std::string &  path 
) [static]

Returns the second path as a relative path to the first file.

Given the position of the configuration file, and the information where a second file is relative to the configuration file's position, we want to known where this second file can be found. This method gets the path to the configuration file (including the configuration file name) and the path to get the relative position of and returns this relative position.

Parameters:
[in] configPath The path the configuration file (including the config's file name)
[in] path The path to the references file (relativ to configuration path)
Returns:
The file's position (relative to curent working directory)

Definition at line 80 of file FileHelpers.cpp.

References getFilePath().

Referenced by checkForRelativity(), NLTriggerBuilder::getFileName(), GUISettingsHandler::myStartElement(), OptionsCont::relocateFiles(), and GenericSAXHandler::startElement().

00081                                                              {
00082     std::string retPath = getFilePath(configPath);
00083     return retPath + path;
00084 }

std::string FileHelpers::getFilePath ( const std::string &  path  )  [static]

Removes the file information from the given path.

Parameters:
[in] path The path to the file to return the folder it is located in
Returns:
The directory of the named file

Definition at line 70 of file FileHelpers.cpp.

Referenced by getConfigurationRelative(), RODFDetectorCon::writeEmitters(), and RODFDetectorCon::writeSpeedTrigger().

00070                                               {
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 }

bool FileHelpers::isAbsolute ( const std::string &  path  )  [static]

Returns the information whether the given path is absolute.

A path is meant to be absolute, if

  • it is a socket
  • it starts with a "/" (Linux)
  • it has a ':' at the second position (Windows)
Parameters:
[in] path The path to examine
Returns:
Whether the path is absolute

Definition at line 95 of file FileHelpers.cpp.

References isSocket().

Referenced by checkForRelativity(), NLTriggerBuilder::getFileName(), GUISettingsHandler::myStartElement(), OptionsCont::relocateFiles(), and GenericSAXHandler::startElement().

00095                                              {
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 }

bool FileHelpers::isSocket ( const std::string &  name  )  [static]

Returns the information whether the given name represents a socket.

A file name is meant to describe a socket address if a colon is found at a position larger than one.

Parameters:
[in] name The name of a file
Returns:
Whether the name names a socket

Definition at line 88 of file FileHelpers.cpp.

Referenced by OutputDevice::getDevice(), and isAbsolute().

00088                                            {
00089     size_t colonPos = name.find(":");
00090     return (colonPos != std::string::npos) && (colonPos > 1);
00091 }

std::ostream & FileHelpers::writeByte ( std::ostream &  strm,
unsigned char  value 
) [static]

Writes a byte binary.

Parameters:
[in,out] strm The stream to write into
[in] value The byte to write
Returns:
Reference to the stream

Definition at line 152 of file FileHelpers.cpp.

00152                                                             {
00153     strm.write((char*) &value, sizeof(char));
00154     return strm;
00155 }

std::ostream & FileHelpers::writeFloat ( std::ostream &  strm,
SUMOReal  value 
) [static]

Writes a float binary.

This method behaves differently depending on the definition of SUMOReal at compile time.

Parameters:
[in,out] strm The stream to write into
[in] value The float to write
Returns:
Reference to the stream

Definition at line 145 of file FileHelpers.cpp.

References SUMOReal.

Referenced by MSVehicleType::saveState(), and MSVehicle::saveState().

00145                                                         {
00146     strm.write((char*) &value, sizeof(SUMOReal));
00147     return strm;
00148 }

std::ostream & FileHelpers::writeInt ( std::ostream &  strm,
int  value 
) [static]

Writes an integer binary.

Parameters:
[in,out] strm The stream to write into
[in] value The integer to write
Returns:
Reference to the stream

Definition at line 131 of file FileHelpers.cpp.

Referenced by MSVehicleType::saveState(), and MSVehicle::saveState().

00131                                                  {
00132     strm.write((char*) &value, sizeof(int));
00133     return strm;
00134 }

std::ostream & FileHelpers::writeString ( std::ostream &  strm,
const std::string &  value 
) [static]

Writes a string binary.

Writes the length of the string, first, using writeInt. Writes then the string's characters.

See also:
writeInt
Parameters:
[in,out] strm The stream to write into
[in] value The string to write
Returns:
Reference to the stream

Definition at line 159 of file FileHelpers.cpp.

References size, and writeUInt().

Referenced by MSVehicleType::saveState(), and MSVehicle::saveState().

00159                                                                  {
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 }

std::ostream & FileHelpers::writeTime ( std::ostream &  strm,
SUMOTime  value 
) [static]

Writes a time description binary.

This method behaves differently depending on the definition of SUMOTime at compile time, which in turn depends on the enabling of subsecond timesteps.

Parameters:
[in,out] strm The stream to write into
[in] value The time to write
Returns:
Reference to the stream

Definition at line 169 of file FileHelpers.cpp.

Referenced by MSVehicle::saveState().

00169                                                        {
00170     strm.write((char*) &value, sizeof(SUMOTime));
00171     return strm;
00172 }

std::ostream & FileHelpers::writeUInt ( std::ostream &  strm,
unsigned int  value 
) [static]

Writes an unsigned integer binary.

Parameters:
[in,out] strm The stream to write into
[in] value The unsigned integer to write
Returns:
Reference to the stream

Definition at line 138 of file FileHelpers.cpp.

Referenced by MSVehicle::saveState(), and writeString().

00138                                                            {
00139     strm.write((char*) &value, sizeof(unsigned int));
00140     return strm;
00141 }


The documentation for this class was generated from the following files:

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