34#ifndef SOMAIO_UTILITIES_ASCIIDATASOURCETRAITS_H
35#define SOMAIO_UTILITIES_ASCIIDATASOURCETRAITS_H
40#include <cartobase/type/types.h>
41#include <cartobase/exception/ioexcept.h>
42#include <cartobase/stream/sstream.h>
43#include <cartobase/type/string_conversion.h>
44#include <cartobase/type/limits.h>
45#include <cartobase/type/voxelvalue.h>
46#include <cartobase/type/voxelrgb.h>
47#include <cartobase/type/voxelrgba.h>
48#include <cartobase/type/voxelhsv.h>
71 const std::string & chars =
" \t\n\r",
74 const std::string & chars =
" \t\n\r",
79 const std::string & chars =
" \t\n\r",
83 static bool skip( std::istream &,
const std::string & chars =
" \t\n\r",
89#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
95 make_compil_error( x,
"Instantiation on an unexpected type. "
96 "This function must be specialized." );
111 make_compil_error( x,
"Instantiation on an unexpected type. "
112 "This function must be specialized." );
126 template <
typename T>
134 template <
typename T>
170 const uint8_t & item )
185 const int8_t & item )
190#if !defined(__sun__) || !defined(_CHAR_IS_SIGNED)
218 const uint16_t & item )
233 const int16_t & item )
249 const uint32_t & item )
264 const int32_t & item )
279 const int64_t & item )
296 const uint64_t & item )
301#ifdef CARTO_LONG_IS_DISTINCT
321 unsigned long & item )
330 const unsigned long & item )
362 const double & item )
378 if( c ==
'\0' || !ds.
isOpen() || c ==
'\n' || c ==
'\r' || c ==
' '
381 item +=
static_cast<char>( c );
393 const std::string & item )
403 char *
const & item )
413 const char *
const & item )
436 if( c ==
'\0' || !ds.
isOpen() )
438 if( c <
'0' || c >
'9' )
440 if( !sign && i == expo && ( c ==
'-' || c ==
'+' ) )
442 else if( c ==
'x' && !hex && i == sign+1 && num[sign] ==
'0' )
444 else if( !hex && !expo && ( c ==
'e' || c ==
'E' ) && i > sign )
449 else if( !hex || c <
'A' || ( c >
'F' && c <
'a' ) || c >
'f' )
452 num +=
static_cast<char>( c );
467 std::ostringstream ss;
490 if( c ==
'\0' || !ds.
isOpen() )
494 if( c !=
"nf"[inf-1] && c !=
"NF"[inf-1] )
508 if( c !=
"an"[nan-1] && c !=
"AN"[nan-1] )
520 else if( c <
'0' || c >
'9' )
522 if( !sign && i == expo && ( c ==
'-' || c ==
'+' ) )
524 else if( !dot && c ==
'x' && !hex && i == sign+1
525 && num[sign] ==
'0' )
527 else if( !dot && !hex && c ==
'.' )
529 else if( !hex && !expo && ( c ==
'e' || c ==
'E' ) && i > sign )
534 else if( !hex && !dot && ( c ==
'n' || c ==
'N' ) )
536 else if( !hex && !dot && ( c ==
'i' || c ==
'I' ) )
538 else if( !hex || c <
'A' || ( c >
'F' && c <
'a' ) || c >
'f' )
541 num +=
static_cast<char>( c );
556 std::ostringstream ss;
578 template<
typename T>
inline
586#if !defined(__sun__) || !defined(_CHAR_IS_SIGNED)
588 operator << (
DataSource & ds,
const char & x )
596 operator << (
DataSource & ds,
const int8_t & x )
603 operator << (
DataSource & ds,
const uint8_t & x )
610 operator << (
DataSource & ds,
const int16_t & x )
617 operator << (
DataSource & ds,
const uint16_t & x )
624 operator << (
DataSource & ds,
const int32_t & x )
631 operator << (
DataSource & ds,
const uint32_t & x )
638 operator << (
DataSource & ds,
const int64_t & x )
645 operator << (
DataSource & ds,
const uint64_t & x )
651#ifdef CARTO_LONG_IS_DISTINCT
654 operator << (
DataSource & ds,
const long & x )
661 operator << (
DataSource & ds,
const unsigned long & x )
669 operator << (
DataSource & ds,
const float & x )
676 operator << (
DataSource & ds,
const double & x )
683 operator << (
DataSource & ds,
const std::string & x )
690 operator << (
DataSource & ds,
const char *
const & x )
698 template <
typename T,
unsigned int C>
706 template <
typename T,
unsigned int C>
inline
716 if( c !=
' ' && c !=
'\t' && c !=
'\n' && c !=
'(' )
723 for(
unsigned int i=1; i<C; ++i )
725 while( ( c ==
' ' || c ==
'\t' ) && ds.
isOpen() )
741 while( ( c ==
' ' || c ==
'\t' ) && ds.
isOpen() )
743 if( c == 0 || c ==
')' ) {
753 template <
typename T,
unsigned int C>
inline
759 for(
unsigned int i=0; i<C-1; ++i )
760 ds <<
static_cast<int>( item[i] ) <<
',';
761 ds << static_cast<int>( item[C-1] ) <<
')';
850 item = std::complex<float>( x[0], x[1] );
856 DataSource & ds,
const std::complex<float> & item )
866 operator << (
DataSource & ds,
const std::complex<float> & x )
876 DataSource & ds, std::complex<double> & item )
881 item = std::complex<double>( x[0], x[1] );
887 DataSource & ds,
const std::complex<double> & item )
897 operator << (
DataSource & ds,
const std::complex<double> & x )
static bool read(DataSource &ds, AimsVector< T, D > &item)
static bool write(DataSource &ds, const AimsVector< T, D > &item)
static bool write(DataSource &ds, const carto::VoxelValue< T, C > &item)
static bool read(DataSource &ds, carto::VoxelValue< T, C > &item)
static bool read(DataSource &ds, T &item)
static bool write(DataSource &ds, const T &item)
Abstraction layer for various data sources (file, buffer, socket...).
virtual long writeBlock(const char *data, unsigned long len)=0
virtual bool ungetch(int ch)=0
virtual bool isOpen() const =0
static std::string readUntil(DataSource &, const std::string &chars=" \t\n\r", bool ascii=true)
static std::string readWhile(DataSource &, const std::string &chars=" \t\n\r", bool ascii=true)
static bool skip(DataSource &, const std::string &chars=" \t\n\r", bool ascii=true)
static bool skip(std::istream &, const std::string &chars=" \t\n\r", bool ascii=true)
static bool getline(DataSource &ds, std::string &)
static bool skipUntil(DataSource &, const std::string &chars=" \t\n\r", bool ascii=true)
static bool read(DataSource &, T &)
static bool write(DataSource &, const T &)
static bool read(DataSource &, T &)
static bool write(DataSource &, const T &)
void stringTo(const std::string &value, T &result)
static const int digits10