34 #ifndef SOMAIO_IO_READER_D_H 
   35 #define SOMAIO_IO_READER_D_H 
   58 #define localMsg( message ) cartoCondMsg( 4, message, "READER" )
 
  106   template <
typename T>
 
  112   template <
typename T>
 
  115     return _alloccontext;
 
  145     if ( !_datasourceinfo->list().empty( 
"default" ) ) {
 
  146       _datasourceinfo->list().dataSource() = ds;
 
  148       _datasourceinfo->list().addDataSource( 
"default", ds );
 
  155     if ( !_datasourceinfo->list().empty( 
"default" ) ) {
 
  156       _datasourceinfo->list().dataSource()
 
  159       _datasourceinfo->list().addDataSource( 
"default",
 
  167     if ( !_datasourceinfo->list().empty( 
"default" ) ) {
 
  168       _datasourceinfo->list().dataSource()
 
  171       _datasourceinfo->list().addDataSource( 
"default",
 
  179     if ( !_datasourceinfo->list().empty( 
"default" ) ) {
 
  180       return _datasourceinfo->list().dataSource();
 
  189     if ( !_datasourceinfo->list().empty() ) {
 
  190       return _datasourceinfo->list().dataSource();
 
  196   template <
typename T>
 
  199     if( _datasourceinfo->list().dataSource() )
 
  200       _datasourceinfo->list().dataSource()->flush();
 
  204   template <
typename T>
 
  207     if( _datasourceinfo->list().dataSource() )
 
  208       _datasourceinfo->list().dataSource()->close();
 
  216   typedef std::multimap<std::string,std::string> 
multi_S;
 
  217   typedef std::set<std::string> 
set_S;
 
  218   typedef std::pair<std::multimap<std::string, std::string>::const_iterator,
 
  219       std::multimap<std::string, std::string>::const_iterator> 
pair_cit_S;
 
  224                         int passbegin, 
int passend )
 
  227       + _datasourceinfo->url() + 
" reading existing object" );
 
  230       _datasourceinfo->header() = header;
 
  233       throw std::runtime_error( 
"Reader with no source of data" );
 
  234     if( !_options.get() )
 
  238     std::string uri = _datasourceinfo->list().dataSource()->url();
 
  239     std::string filename = FileUtil::uriFilename( uri );
 
  241     if( urioptions.
get() ) {
 
  242       _datasourceinfo->list().dataSource()
 
  244       _options->copyProperties( urioptions );
 
  250       *_datasourceinfo, _options,
 
  251       ( passbegin < 2 ? 1 : passbegin - 1 ), passend - 1 );
 
  256     *_datasourceinfo = dsi;
 
  258       if( _options->getProperty( 
"partial_reading" )->getScalar() )
 
  264     _alloccontext.setDataSourceInfo( _datasourceinfo );
 
  267     if( !_options->getProperty( 
"format", format )
 
  268         && !_datasourceinfo->header()->getProperty( 
"format", format ) )
 
  269       _datasourceinfo->header()->getProperty( 
"file_type", format );
 
  271     localMsg( 
"using format " + format + 
" for file: " + uri );
 
  275     std::set<FormatReader<T> *>                    triedf;
 
  277     std::unique_ptr<FormatReader<T> >              readerc;
 
  278     set_S::iterator                                notyet = tried.end();
 
  279     typename std::set<FormatReader<T> *>::iterator notyetf = triedf.end();
 
  289     std::set<std::string>::const_iterator fit, fie = read_formats.end();
 
  292     for(fit = read_formats.begin(); fit!=fie; ++fit)
 
  297     if( passbegin <= 1 && passend >=1 && !format.empty() )
 
  299       localMsg( 
"1. try to find reader of " + format + 
" for file: " + uri );
 
  305           localMsg( 
"1. try reader " + format );
 
  306           readerc.reset( reader->
clone() );
 
  307           readerc->setupAndRead( obj, _datasourceinfo,
 
  308                                 _alloccontext, _options );
 
  312         catch( std::exception & e ) {
 
  313           localMsg( 
"1. " + format + 
" failed" );
 
  316         tried.insert( format );
 
  317         triedf.insert( reader );
 
  324     multi_S::const_iterator  ie, ee;
 
  327     if( passbegin <= 2 && passend >=2 )
 
  329       localMsg( 
"2. try to find reader using extension " + ext + 
" for file: " + uri );
 
  330       iext = extensions.equal_range( ext );
 
  331       for( ie=iext.first, ee = iext.second; ie!=ee; ++ie ) {
 
  332         if( tried.find( ie->second ) == notyet )
 
  334           localMsg( 
"2. try to find reader of " + ie->second + 
" for file: " + uri );
 
  337           if( reader && triedf.find( reader ) == notyetf ) {
 
  339               localMsg( 
"2. try reader " + ie->second );
 
  340               readerc.reset( reader->
clone() );
 
  341               readerc->setupAndRead( obj, _datasourceinfo,
 
  342                                     _alloccontext, _options );
 
  343               localMsg( 
"2. " + ie->second + 
" OK" );
 
  346             catch( std::exception & e ) {
 
  347               localMsg( 
"2. " + ie->second + 
" failed" );
 
  350             tried.insert( ie->second );
 
  351             triedf.insert( reader );
 
  358     if( passbegin <= 3 && passend >= 3 && !ext.empty())
 
  360       localMsg( 
"3. try to find reader using no extension for file: " + uri );
 
  361       iext = extensions.equal_range( 
"" );
 
  362       for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
 
  363         if( tried.find( ie->second ) == notyet ) {
 
  364           localMsg( 
"3. try to find reader of " + ie->second + 
" for file: " + uri );
 
  367           if( reader && triedf.find( reader ) == notyetf )
 
  370               localMsg( 
"3. try reader " + ie->second );
 
  371               readerc.reset( reader->
clone() );
 
  372               readerc->setupAndRead( obj, _datasourceinfo,
 
  373                                     _alloccontext, _options );
 
  374               localMsg( 
"3. " + ie->second + 
" OK" );
 
  377             catch( std::exception & e ) {
 
  378               localMsg( 
"3. " + ie->second + 
" failed" );
 
  381             tried.insert( ie->second );
 
  382             triedf.insert( reader );
 
  389     if( passbegin <= 4 && passend >= 4 )
 
  391       localMsg( 
"4. try every reader for file: " + uri );
 
  392       iext.first = extensions.begin();
 
  393       iext.second = extensions.end();
 
  394       for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
 
  395         if( tried.find( ie->second ) == notyet ) {
 
  396           localMsg( 
"4. try to find reader of " + ie->second + 
" for file: " + uri );
 
  399           if( reader && triedf.find( reader ) == notyetf )
 
  402               localMsg( 
"4. try reader " + ie->second );
 
  403               readerc.reset( reader->
clone() );
 
  404               readerc->setupAndRead( obj, _datasourceinfo,
 
  405                                     _alloccontext, _options );
 
  406               localMsg( 
"4. " + ie->second + 
" OK" );
 
  409             catch( std::exception & e ) {
 
  410               localMsg( 
"4. " + ie->second + 
" failed" );
 
  413             tried.insert( ie->second );
 
  414             triedf.insert( reader );
 
  422                                    filename + 
" : no matching format" );
 
  430       + _datasourceinfo->url() );
 
  433       _datasourceinfo->header() = header;
 
  436       throw std::runtime_error( 
"Reader with no source of data" );
 
  437     if( !_options.get() )
 
  441     std::string uri = _datasourceinfo->list().dataSource()->url();
 
  442     std::string filename = FileUtil::uriFilename( uri );
 
  444     if( urioptions.
get() ) {
 
  445       _datasourceinfo->list().dataSource()
 
  447       _options->copyProperties( urioptions );
 
  457     *_datasourceinfo = dsi;
 
  458     _alloccontext.setDataSourceInfo( _datasourceinfo );
 
  461     if( !_options->getProperty( 
"format", format )
 
  462         && !_datasourceinfo->header()->getProperty( 
"format", format ) )
 
  463       _datasourceinfo->
header()->getProperty( 
"file_type", format );
 
  465     localMsg( 
"using format " + format + 
" for file: " + uri );
 
  469     std::set<FormatReader<T> *>                    triedf;
 
  471     std::unique_ptr<FormatReader<T> >              readerc;
 
  472     set_S::iterator                                notyet = tried.end();
 
  473     typename std::set<FormatReader<T> *>::iterator notyetf = triedf.end();
 
  484     std::set<std::string>::const_iterator fit, fie = read_formats.end();
 
  487     for(fit = read_formats.begin(); fit!=fie; ++fit)
 
  492     if( passbegin <= 1 && passend >= 1 && !format.empty() )
 
  494       localMsg( 
"1. try to find reader of " + format + 
" for file: " + uri );
 
  501           localMsg( 
"1. try reader " + format );
 
  502           readerc.reset( reader->
clone() );
 
  503           obj = readerc->createAndRead( _datasourceinfo,
 
  504                                         _alloccontext, _options );
 
  511         catch( std::exception & e )
 
  513           localMsg( 
"1. " + format + 
" failed" );
 
  516         tried.insert( format );
 
  517         triedf.insert( reader );
 
  526     multi_S::const_iterator  ie, ee;
 
  529     if( passbegin <= 2 && passend >= 2 )
 
  531       localMsg( 
"2. try to find reader using extension " + ext + 
" for file: " + uri );
 
  532       iext = extensions.equal_range( ext );
 
  533       for( ie=iext.first, ee=iext.second; ie!=ee; ++ie )
 
  535         if( tried.find( ie->second ) == notyet )
 
  537           localMsg( 
"2. try to find reader of " + ie->second + 
" for file: " + uri );
 
  540           if( reader && triedf.find( reader ) == notyetf )
 
  544               localMsg( 
"2. try reader " + ie->second );
 
  545               readerc.reset( reader->
clone() );
 
  546               obj = readerc->createAndRead( _datasourceinfo,
 
  547                                             _alloccontext, _options );
 
  550                 localMsg( 
"2. " + ie->second + 
" OK" );
 
  554             catch( std::exception & e )
 
  556               localMsg( 
"2. " + ie->second + 
" failed" );
 
  559             tried.insert( ie->second );
 
  560             triedf.insert( reader );
 
  567     if( passbegin <= 3 && passend >= 3 && !ext.empty() )
 
  569       localMsg( 
"3. try to find reader using no extension for file: " + uri );
 
  570       iext = extensions.equal_range( 
"" );
 
  571       for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
 
  572         if( tried.find( ie->second ) == notyet ) {
 
  573           localMsg( 
"3. try to find reader of " + ie->second + 
" for file: " + uri );
 
  576           if( reader && triedf.find( reader ) == notyetf )
 
  579               localMsg( 
"3. try reader " + ie->second );
 
  580               readerc.reset( reader->
clone() );
 
  581               obj = readerc->createAndRead( _datasourceinfo,
 
  582                                            _alloccontext, _options );
 
  584                 localMsg( 
"3. " + ie->second + 
" OK" );
 
  587             } 
catch( std::exception & e ) {
 
  588               localMsg( 
"3.  " + ie->second + 
" failed" );
 
  591             tried.insert( ie->second );
 
  592             triedf.insert( reader );
 
  599     if( passbegin <= 4 && passend >= 4 )
 
  601       localMsg( 
"4. try every reader for file: " + uri );
 
  602       iext.first = extensions.begin();
 
  603       iext.second = extensions.end();
 
  604       for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
 
  605         if( tried.find( ie->second ) == notyet ) {
 
  606           localMsg( 
"4. try to find reader of " + ie->second + 
" for file: " + uri );
 
  609           if( reader && triedf.find( reader ) == notyetf )
 
  612               localMsg( 
"4. try reader " + ie->second );
 
  613               readerc.reset( reader->
clone() );
 
  614               obj = readerc->createAndRead( _datasourceinfo,
 
  615                                           _alloccontext, _options );
 
  617                 localMsg( 
"4. " + ie->second + 
" OK" );
 
  620             } 
catch( std::exception & e ) {
 
  621               localMsg( 
"4. " + ie->second + 
" failed" );
 
  624             tried.insert( ie->second );
 
  625             triedf.insert( reader );
 
  633                                    filename + 
" : no matching format" );
 
static std::string extension(const std::string &)
static void keepExceptionPriority(std::exception &e, int &prio, int &type, std::string &message, int raiseprio=0)
static void launchExcept(int code, const std::string &msg, const std::string &defmsg="")
GenericObject * get() const
void setMemoryMapping(const bool &boo=true)
Generic information retreiver / checker for all data sources and file formats.
DataSourceInfo check(DataSourceInfo dsi, carto::Object options=carto::none(), int passbegin=1, int passend=3)
Finds the right format checker.
Informative object used by IO system.
const DataSourceList & list() const
const DataSourceCapabilities & capabilities() const
const carto::Object & header() const
bool empty() const
Returns true only if no keyword inserted.
Abstraction layer for various data sources (file, buffer, socket...).
static const carto::rc_ptr< DataSource > none()
An empty ref-counter that is more convenient than calling a constructor of rc_ptr<DataSource> (useful...
DataSource on a std::istream (read-only stream)
void setAllocatorContext(const AllocatorContext &ac)
const carto::rc_ptr< DataSource > dataSource() const
carto::Object options() const
virtual bool read(T &obj, carto::Object header=carto::none(), int passbegin=1, int passend=4)
Finds the correct format and reads the object.
const AllocatorContext & allocatorContext() const
void setOptions(carto::Object options)
void attach(carto::rc_ptr< DataSource > ds)
std::string toString(const T &object)
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > pair_cit_S
std::set< std::string > set_S
std::multimap< std::string, std::string > multi_S
unsigned long long offset_t
Offsets are 64 bits if supported.
#define localMsg(message)