FSResourceProvider.cpp
Go to the documentation of this file.
1 #include "FSResourceProvider.hpp"
2 #include "../ResourcePath.hpp"
4 #include <fstream>
5 
6 namespace iv
7 {
8 
9 FSResourceProvider::FSResourceProvider( Instance * inst, size_t priority, const char * root_dir ) :
10  StreamResourceProvider( inst, priority ),
11  cm( inst, this, "FSResourceProvider" ),
12  root( root_dir )
13 {
14  this->cm.inherits( this->StreamResourceProvider::cm );
15 }
16 
17 void FSResourceProvider::with_stream( ResourcePath const & path, std::function< void( std::istream & ) > const & f ) const
18 {
19  LambdaLogTrace _log_trace(
20  [&]( std::ostream & out )
21  {
22  out << "File '" << this->toFsPath( path ) << "':" << std::endl;
23  }
24  );
25 
26  // get input filename
27  std::string rpath = this->toFsPath( path );
28 
29  // open stream
30  std::ifstream in( rpath.c_str(), std::ios::binary );
31  if( !in.good() )
32  {
33  this->cm.warning( SRC_INFO, "Can not properly open resource file '", rpath, "'." );
34  return;
35  }
36 
37  //
38  f( in );
39 
40  //
41  #warning "TODO - Maybe check if the the stream is valid."
42  /*if( in.fail() || in.bad() )
43  {
44  this->instance()->warning( SRC_INFO, Warning( "Reading stream failed: ",strerror(errno),"." ) );
45  return;
46  }*/
47 }
48 
49 void FSResourceProvider::with_metadata_stream( std::function< void( std::istream & ) > const & f ) const
50 {
51  this->with_stream( ResourcePath( "/metadata.json" ), f );
52 }
53 
54 std::string FSResourceProvider::toFsPath( ResourcePath path ) const
55 {
56  return path.to_real_path( this->root.c_str() );
57 }
58 
59 }