file_manager.cc
Go to the documentation of this file.
00001 //#define DEBUG
00002 
00003 #include "file_manager.hh"
00004 #include "stage.hh" // to get PRINT_DEBUG
00005 #include "config.h" // to get INSTALL_PREFIX
00006 
00007 #include <sstream>
00008 #include <fstream>
00009 
00010 std::string searchDirs( const std::vector<std::string>& dirs, const std::string& filename ) {
00011         for ( unsigned int i=0; i<dirs.size(); i++ ) {
00012                 std::string path = dirs[i] + '/' + filename;
00013                 PRINT_DEBUG1("FileManager: trying %s\n", path.c_str());
00014                 if ( Stg::FileManager::readable( path ) ) {
00015                         return path;
00016                 }
00017         }
00018         
00019         PRINT_DEBUG1("FileManager: %s not found.\n", filename.c_str() );
00020         return "";
00021 }
00022 
00023 namespace Stg
00024 {
00025         FileManager::FileManager() : WorldsRoot( "." )
00026         { }
00027 
00028         std::string FileManager::stagePath() {
00029                 static char* stgPath = getenv("STAGEPATH");
00030                 if ( stgPath == NULL )
00031                         return "";
00032                 else
00033                         return std::string( stgPath );
00034         }
00035 
00036         std::string FileManager::findFile( const std::string& filename ) 
00037         {
00038                 PRINT_DEBUG1("FileManager: trying %s\n", filename.c_str());
00039                 if ( readable( filename ) )
00040                         return filename;
00041 
00042                 static std::vector<std::string> paths;
00043                 static bool ranOnce = false;
00044 
00045                 // initialize the path list, if necessary
00046                 if ( !ranOnce ) {
00047                         std::string SharePath = INSTALL_PREFIX "/share/stage";
00048                         paths.push_back( SharePath );
00049 
00050                         std::string stgPath = stagePath();
00051 
00052                         std::istringstream is( stgPath );
00053                         std::string path;
00054                         while ( getline( is, path, ':' ) ) {
00055                                 paths.push_back( path );
00056                                 PRINT_DEBUG1("FileManager - INIT: added path %s\n", path.c_str() );
00057                         }
00058 
00059                         ranOnce = true;
00060                         
00061                         PRINT_DEBUG1("FileManager - INIT: %d paths in search paths\n", paths.size() );
00062                 }
00063 
00064                 // search the path list
00065                 return searchDirs( paths, filename );
00066         }
00067 
00068         bool FileManager::readable( const std::string& path ) {
00069                 std::ifstream iFile;
00070                 iFile.open( path.c_str() );
00071                 if ( iFile.is_open() ) {
00072                         iFile.close();
00073                         return true;
00074                 }
00075                 else {
00076                         return false;
00077                 }
00078         }
00079 
00080         std::string FileManager::stripFilename( const std::string& path ) {
00081                 std::string pathChars( "\\/" );
00082                 std::string::size_type loc = path.find_last_of( pathChars );
00083                 if ( loc == std::string::npos )
00084                         return path;
00085                 else
00086                         return path.substr( 0, loc );
00087         }
00088 
00089 }; // namespace Stg
00090 


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Thu Aug 27 2015 15:20:57