file_manager.cc
Go to the documentation of this file.
1 //#define DEBUG
2 
3 #include "file_manager.hh"
4 #include "stage.hh" // to get PRINT_DEBUG
5 #include "config.h" // to get INSTALL_PREFIX
6 
7 #include <sstream>
8 #include <fstream>
9 
10 std::string searchDirs( const std::vector<std::string>& dirs, const std::string& filename ) {
11  for ( unsigned int i=0; i<dirs.size(); i++ ) {
12  std::string path = dirs[i] + '/' + filename;
13  PRINT_DEBUG1("FileManager: trying %s\n", path.c_str());
14  if ( Stg::FileManager::readable( path ) ) {
15  return path;
16  }
17  }
18 
19  PRINT_DEBUG1("FileManager: %s not found.\n", filename.c_str() );
20  return "";
21 }
22 
23 namespace Stg
24 {
25  FileManager::FileManager() : WorldsRoot( "." )
26  { }
27 
28  std::string FileManager::stagePath() {
29  static char* stgPath = getenv("STAGEPATH");
30  if ( stgPath == NULL )
31  return "";
32  else
33  return std::string( stgPath );
34  }
35 
36  std::string FileManager::findFile( const std::string& filename )
37  {
38  PRINT_DEBUG1("FileManager: trying %s\n", filename.c_str());
39  if ( readable( filename ) )
40  return filename;
41 
42  static std::vector<std::string> paths;
43  static bool ranOnce = false;
44 
45  // initialize the path list, if necessary
46  if ( !ranOnce ) {
47  std::string SharePath = INSTALL_PREFIX "/share/stage";
48  paths.push_back( SharePath );
49 
50  std::string stgPath = stagePath();
51 
52  std::istringstream is( stgPath );
53  std::string path;
54  while ( getline( is, path, ':' ) ) {
55  paths.push_back( path );
56  PRINT_DEBUG1("FileManager - INIT: added path %s\n", path.c_str() );
57  }
58 
59  ranOnce = true;
60 
61  PRINT_DEBUG1("FileManager - INIT: %d paths in search paths\n", paths.size() );
62  }
63 
64  // search the path list
65  return searchDirs( paths, filename );
66  }
67 
68  bool FileManager::readable( const std::string& path ) {
69  std::ifstream iFile;
70  iFile.open( path.c_str() );
71  if ( iFile.is_open() ) {
72  iFile.close();
73  return true;
74  }
75  else {
76  return false;
77  }
78  }
79 
80  std::string FileManager::stripFilename( const std::string& path ) {
81  std::string pathChars( "\\/" );
82  std::string::size_type loc = path.find_last_of( pathChars );
83  if ( loc == std::string::npos )
84  return path;
85  else
86  return path.substr( 0, loc );
87  }
88 
89 }; // namespace Stg
90 
static bool readable(const std::string &path)
Determine whether a file can be opened for reading.
Definition: file_manager.cc:68
The Stage library uses its own namespace.
Definition: canvas.hh:8
std::string searchDirs(const std::vector< std::string > &dirs, const std::string &filename)
Definition: file_manager.cc:10
static std::string stagePath()
Return the STAGEPATH environment variable.
Definition: file_manager.cc:28
std::string stripFilename(const std::string &path)
Definition: file_manager.cc:80
static std::string findFile(const std::string &filename)
Definition: file_manager.cc:36
#define PRINT_DEBUG1(m, a)
Definition: stage.hh:667


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 Mon Jun 10 2019 15:06:09