33 #ifndef __INCLUDE_MIST_CSV__
34 #define __INCLUDE_MIST_CSV__
37 #ifndef __INCLUDE_MIST_CONF_H__
38 #include "../config/mist_conf.h"
41 #ifndef __INCLUDE_MIST_MATRIX__
42 #include "../matrix.h"
50 #if defined( _COMPRESSED_CSV_SUPPORT_ ) && _COMPRESSED_CSV_SUPPORT_ != 0
59 namespace __csv_controller__
62 struct csv_data_converter
66 static value_type convert_to(
const unsigned char *s,
const unsigned char *e )
68 return( atoi( std::string( s, e ).c_str( ) ) );
71 static std::string convert_from(
const value_type &val )
73 static char buff[ 256 ];
74 sprintf( buff,
"%d", val );
80 struct csv_data_converter< float >
82 typedef float value_type;
84 static value_type convert_to(
const unsigned char *s,
const unsigned char *e )
86 return( (
float )atof( std::string( s, e ).c_str( ) ) );
89 static std::string convert_from(
const value_type &val )
91 static char buff[ 256 ];
92 sprintf( buff,
"%f", val );
98 struct csv_data_converter< double >
100 typedef double value_type;
102 static value_type convert_to(
const unsigned char *s,
const unsigned char *e )
104 return( atof( std::string( s, e ).c_str( ) ) );
107 static std::string convert_from(
const value_type &val )
109 static char buff[ 256 ];
110 sprintf( buff,
"%f", val );
116 struct csv_data_converter< std::string >
118 typedef std::string value_type;
120 static value_type convert_to(
const unsigned char *s,
const unsigned char *e )
122 return( std::string( s, e ).c_str( ) );
125 static std::string convert_from(
const value_type &val )
131 template <
class Array >
132 struct csv_controller
134 typedef typename Array::value_type element_type;
135 typedef typename element_type::value_type value_type;
136 typedef typename Array::size_type size_type;
137 typedef csv_data_converter< value_type > converter;
139 static const unsigned char *get_line(
const unsigned char *s,
const unsigned char *e,
bool &is_empty_line )
141 const unsigned char *sp = s;
142 const unsigned char *ep = s;
147 if( s + 1 != e && s[ 1 ] ==
'\n' )
157 else if( s[ 0 ] ==
'\n' )
168 if( s < e && sp == ep )
170 return( get_line( s, e, is_empty_line ) );
174 is_empty_line = sp == ep;
175 return( s > e ? e : s );
179 static const unsigned char *get_value(
const unsigned char *s,
const unsigned char *e, value_type &val,
const std::string &separator )
186 if( s + 1 != e && s[ 1 ] ==
'\n' )
195 else if( s[ 0 ] ==
'\n' )
199 else if( s[ 0 ] ==
' ' || s[ 0 ] ==
'\t' )
209 const unsigned char *sp = s;
210 const unsigned char *ep = sp;
217 if( s + 1 != e && s[ 1 ] ==
'\n' )
227 else if( s[ 0 ] ==
'\n' )
234 bool isFound =
false;
235 for( size_type i = 0 ; i < separator.size( ) ; i++ )
237 if( s[ 0 ] == separator[ i ] )
257 val = converter::convert_to( sp, ep );
260 return( s > e ? e : s );
263 static bool convert_from_csv_data( Array &csv,
const unsigned char *buff, size_type len,
const std::string &separator )
265 const unsigned char *p = buff;
266 const unsigned char *e = buff + len;
270 bool is_empty_line =
false;
271 const unsigned char *np = get_line( p, e, is_empty_line );
279 element_type element;
283 p = get_value( p, np, val, separator );
284 element.push_back( val );
289 if( csv.size( ) == 0 )
291 csv.push_back( element );
293 else if( csv[ csv.size( ) - 1 ].size( ) == element.size( ) )
295 csv.push_back( element );
306 static bool read( Array &csv,
const std::string &filename,
const std::string &separator )
308 #if defined( _COMPRESSED_CSV_SUPPORT_ ) && _COMPRESSED_CSV_SUPPORT_ != 0
310 if( ( fp = gzopen( filename.c_str( ),
"rb" ) ) == NULL )
316 if( ( fp = fopen( filename.c_str( ),
"rb" ) ) == NULL )
322 size_type numBytes = 4096;
323 unsigned char *buff =
new unsigned char[ numBytes ];
324 unsigned char *sp = buff;
325 ptrdiff_t read_size = 0;
328 #if defined( _COMPRESSED_CSV_SUPPORT_ ) && _COMPRESSED_CSV_SUPPORT_ != 0
329 while( gzeof( fp ) == 0 )
331 while( feof( fp ) == 0 )
334 ptrdiff_t restBytes = ( buff + numBytes ) - sp;
335 #if defined( _COMPRESSED_CSV_SUPPORT_ ) && _COMPRESSED_CSV_SUPPORT_ != 0
336 read_size = gzread( fp, (
void * )sp, static_cast< unsigned int >(
sizeof(
unsigned char ) * restBytes ) );
338 read_size = fread( (
void * )sp,
sizeof(
unsigned char ), static_cast< unsigned int >( restBytes ), fp );
341 unsigned char *eep = sp + read_size;
342 if( eep < buff + numBytes )
344 ret = convert_from_csv_data( csv, buff, sp + read_size - buff, separator );
348 unsigned char *e = sp;
349 unsigned char *ep = eep + 1;
350 for( ; e < eep ; e++ )
354 if( e < e && e[ 1 ] ==
'\n' )
365 else if( e[ 0 ] ==
'\n' )
379 unsigned char *tmp =
new unsigned char[ numBytes * 2 ];
380 memcpy( tmp, buff,
sizeof(
unsigned char ) * numBytes );
383 sp = buff + numBytes;
388 if( !convert_from_csv_data( csv, buff, ep - buff, separator ) )
394 unsigned char *s1 = buff;
395 unsigned char *s2 = ep;
401 sp = buff + ( eep - ep );
405 #if defined( _COMPRESSED_CSV_SUPPORT_ ) && _COMPRESSED_CSV_SUPPORT_ != 0
442 template <
class Array >
443 bool read_csv( Array &csv,
const std::string &filename,
const std::string &separator =
", " )
447 return( __csv_controller__::csv_controller< Array >::read( csv, filename, separator ) );
463 template <
class Array >
464 bool read_csv( Array &csv,
const std::wstring &filename,
const std::wstring &separator =
", " )
482 template <
class T,
class Allocator >
486 typedef std::vector< std::vector< T > > csv_data_type;
488 csv_data_type csv_data;
489 if(
read_csv( csv_data, filename, separator ) && csv_data.size( ) > 0 && csv_data[ 0 ].size( ) > 0 )
491 csv.
resize( csv_data.size( ), csv_data[ 0 ].size( ) );
493 for( size_type r = 0 ; r < csv_data.size( ) ; r++ )
495 typename csv_data_type::value_type &data = csv_data[ r ];
497 for( size_type c = 0 ; c < data.size( ) ; c++ )
499 csv( r, c ) = data[ c ];
521 template <
class T,
class Allocator >
535 #endif // __INCLUDE_MIST_CSV__