34#ifndef SOMAIO_IO_READER_D_H
35#define SOMAIO_IO_READER_D_H
49#include <cartobase/exception/ioexcept.h>
50#include <cartobase/object/object.h>
51#include <cartobase/object/property.h>
52#include <cartobase/stream/fileutil.h>
57#include <cartobase/config/verbose.h>
58#define localMsg( message ) cartoCondMsg( 4, message, "READER" )
106 template <
typename T>
112 template <
typename T>
196 template <
typename T>
204 template <
typename T>
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 )
233 throw std::runtime_error(
"Reader with no source of data" );
239 std::string filename = FileUtil::uriFilename( uri );
241 if( urioptions.
get() ) {
244 _options->copyProperties( urioptions );
251 ( passbegin < 2 ? 1 : passbegin - 1 ), passend - 1 );
258 if(
_options->getProperty(
"partial_reading" )->getScalar() )
267 if( !
_options->getProperty(
"format", 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() );
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() );
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() );
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() );
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" );
436 throw std::runtime_error(
"Reader with no source of data" );
442 std::string filename = FileUtil::uriFilename( uri );
444 if( urioptions.
get() ) {
447 _options->copyProperties( urioptions );
461 if( !
_options->getProperty(
"format", 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() );
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() );
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() );
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() );
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="")
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 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)
AllocatorContext _alloccontext
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)
carto::rc_ptr< DataSourceInfo > _datasourceinfo
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.