34 #ifndef SOMAIO_IO_READER_D_H 35 #define SOMAIO_IO_READER_D_H 58 #define localMsg( message ) cartoCondMsg( 4, message, "READER" ) 104 template <
typename T>
110 template <
typename T>
194 template <
typename T>
202 template <
typename T>
214 typedef std::multimap<std::string,std::string>
multi_S;
215 typedef std::set<std::string>
set_S;
216 typedef std::pair<std::multimap<std::string, std::string>::const_iterator,
217 std::multimap<std::string, std::string>::const_iterator>
pair_cit_S;
222 int passbegin,
int passend )
231 throw std::runtime_error(
"Reader with no source of data" );
237 std::string filename = FileUtil::uriFilename( uri );
239 if( urioptions.
get() ) {
242 _options->copyProperties( urioptions );
249 ( passbegin < 2 ? 1 : passbegin - 1 ), passend - 1 );
256 if(
_options->getProperty(
"partial_reading" )->getScalar() )
265 if( !
_options->getProperty(
"format", format )
269 localMsg(
"using format " + format +
" for file: " + uri );
273 std::set<FormatReader<T> *> triedf;
275 std::unique_ptr<FormatReader<T> > readerc;
276 set_S::iterator notyet = tried.end();
277 typename std::set<FormatReader<T> *>::iterator notyetf = triedf.end();
287 std::set<std::string>::const_iterator fit, fie = read_formats.end();
290 for(fit = read_formats.begin(); fit!=fie; ++fit)
295 if( passbegin <= 1 && passend >=1 && !format.empty() )
297 localMsg(
"1. try to find reader of " + format +
" for file: " + uri );
303 localMsg(
"1. try reader " + format );
304 readerc.reset( reader->
clone() );
310 catch( std::exception & e ) {
311 localMsg(
"1. " + format +
" failed" );
314 tried.insert( format );
315 triedf.insert( reader );
322 multi_S::const_iterator ie, ee;
325 if( passbegin <= 2 && passend >=2 )
327 localMsg(
"2. try to find reader using extension " + ext +
" for file: " + uri );
328 iext = extensions.equal_range( ext );
329 for( ie=iext.first, ee = iext.second; ie!=ee; ++ie ) {
330 if( tried.find( ie->second ) == notyet )
332 localMsg(
"2. try to find reader of " + ie->second +
" for file: " + uri );
335 if( reader && triedf.find( reader ) == notyetf ) {
337 localMsg(
"2. try reader " + ie->second );
338 readerc.reset( reader->
clone() );
341 localMsg(
"2. " + ie->second +
" OK" );
344 catch( std::exception & e ) {
345 localMsg(
"2. " + ie->second +
" failed" );
348 tried.insert( ie->second );
349 triedf.insert( reader );
356 if( passbegin <= 3 && passend >= 3 && !ext.empty())
358 localMsg(
"3. try to find reader using no extension for file: " + uri );
359 iext = extensions.equal_range(
"" );
360 for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
361 if( tried.find( ie->second ) == notyet ) {
362 localMsg(
"3. try to find reader of " + ie->second +
" for file: " + uri );
365 if( reader && triedf.find( reader ) == notyetf )
368 localMsg(
"3. try reader " + ie->second );
369 readerc.reset( reader->
clone() );
372 localMsg(
"3. " + ie->second +
" OK" );
375 catch( std::exception & e ) {
376 localMsg(
"3. " + ie->second +
" failed" );
379 tried.insert( ie->second );
380 triedf.insert( reader );
387 if( passbegin <= 4 && passend >= 4 )
389 localMsg(
"4. try every reader for file: " + uri );
390 iext.first = extensions.begin();
391 iext.second = extensions.end();
392 for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
393 if( tried.find( ie->second ) == notyet ) {
394 localMsg(
"4. try to find reader of " + ie->second +
" for file: " + uri );
397 if( reader && triedf.find( reader ) == notyetf )
400 localMsg(
"4. try reader " + ie->second );
401 readerc.reset( reader->
clone() );
404 localMsg(
"4. " + ie->second +
" OK" );
407 catch( std::exception & e ) {
408 localMsg(
"4. " + ie->second +
" failed" );
411 tried.insert( ie->second );
412 triedf.insert( reader );
420 filename +
" : no matching format" );
434 throw std::runtime_error(
"Reader with no source of data" );
440 std::string filename = FileUtil::uriFilename( uri );
442 if( urioptions.
get() ) {
445 _options->copyProperties( urioptions );
459 if( !
_options->getProperty(
"format", format )
463 localMsg(
"using format " + format +
" for file: " + uri );
467 std::set<FormatReader<T> *> triedf;
469 std::unique_ptr<FormatReader<T> > readerc;
470 set_S::iterator notyet = tried.end();
471 typename std::set<FormatReader<T> *>::iterator notyetf = triedf.end();
482 std::set<std::string>::const_iterator fit, fie = read_formats.end();
485 for(fit = read_formats.begin(); fit!=fie; ++fit)
490 if( passbegin <= 1 && passend >= 1 && !format.empty() )
492 localMsg(
"1. try to find reader of " + format +
" for file: " + uri );
499 localMsg(
"1. try reader " + format );
500 readerc.reset( reader->
clone() );
509 catch( std::exception & e )
511 localMsg(
"1. " + format +
" failed" );
514 tried.insert( format );
515 triedf.insert( reader );
524 multi_S::const_iterator ie, ee;
527 if( passbegin <= 2 && passend >= 2 )
529 localMsg(
"2. try to find reader using extension " + ext +
" for file: " + uri );
530 iext = extensions.equal_range( ext );
531 for( ie=iext.first, ee=iext.second; ie!=ee; ++ie )
533 if( tried.find( ie->second ) == notyet )
535 localMsg(
"2. try to find reader of " + ie->second +
" for file: " + uri );
538 if( reader && triedf.find( reader ) == notyetf )
542 localMsg(
"2. try reader " + ie->second );
543 readerc.reset( reader->
clone() );
548 localMsg(
"2. " + ie->second +
" OK" );
552 catch( std::exception & e )
554 localMsg(
"2. " + ie->second +
" failed" );
557 tried.insert( ie->second );
558 triedf.insert( reader );
565 if( passbegin <= 3 && passend >= 3 && !ext.empty() )
567 localMsg(
"3. try to find reader using no extension for file: " + uri );
568 iext = extensions.equal_range(
"" );
569 for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
570 if( tried.find( ie->second ) == notyet ) {
571 localMsg(
"3. try to find reader of " + ie->second +
" for file: " + uri );
574 if( reader && triedf.find( reader ) == notyetf )
577 localMsg(
"3. try reader " + ie->second );
578 readerc.reset( reader->
clone() );
582 localMsg(
"3. " + ie->second +
" OK" );
585 }
catch( std::exception & e ) {
586 localMsg(
"3. " + ie->second +
" failed" );
589 tried.insert( ie->second );
590 triedf.insert( reader );
597 if( passbegin <= 4 && passend >= 4 )
599 localMsg(
"4. try every reader for file: " + uri );
600 iext.first = extensions.begin();
601 iext.second = extensions.end();
602 for( ie=iext.first, ee=iext.second; ie!=ee; ++ie ) {
603 if( tried.find( ie->second ) == notyet ) {
604 localMsg(
"4. try to find reader of " + ie->second +
" for file: " + uri );
607 if( reader && triedf.find( reader ) == notyetf )
610 localMsg(
"4. try reader " + ie->second );
611 readerc.reset( reader->
clone() );
615 localMsg(
"4. " + ie->second +
" OK" );
618 }
catch( std::exception & e ) {
619 localMsg(
"4. " + ie->second +
" failed" );
622 tried.insert( ie->second );
623 triedf.insert( reader );
631 filename +
" : no matching format" );
static const carto::rc_ptr< DataSource > none()
An empty ref-counter that is more convenient than calling a constructor of rc_ptr<DataSource> (useful...
static std::string extension(const std::string &)
static void keepExceptionPriority(std::exception &e, int &prio, int &type, std::string &message, int raiseprio=0)
AllocatorContext _alloccontext
std::multimap< std::string, std::string > multi_S
bool empty() const
Returns true only if no keyword inserted.
const carto::rc_ptr< DataSource > dataSource() const
carto::rc_ptr< DataSourceInfo > _datasourceinfo
Abstraction layer for various data sources (file, buffer, socket...).
carto::Object options() const
DataSource on a std::istream (read-only stream)
void setAllocatorContext(const AllocatorContext &ac)
DataSourceInfo check(DataSourceInfo dsi, carto::Object options=carto::none(), int passbegin=1, int passend=3)
Finds the right format checker.
Generic information retreiver / checker for all data sources and file formats.
void setOptions(carto::Object options)
void attach(carto::rc_ptr< DataSource > ds)
void setDataSourceInfo(carto::rc_ptr< DataSourceInfo > dsi)
AllocatorContext becomes owner od datasourceinfo.
unsigned long long offset_t
Offsets are 64 bits if supported.
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > pair_cit_S
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
GenericObject * get() const
static void launchExcept(int code, const std::string &msg, const std::string &defmsg="")
Informative object used by IO system.
std::set< std::string > set_S
std::string toString(const T &object)
const carto::Object & header() const
#define localMsg(message)
const DataSourceList & list() const