#ifndef cvsread_hpp__ #define cvsread_hpp__ #include #include #include "lreader.hpp" template< typename charT > class csv_reader { typedef std::basic_string string_t; typedef std::basic_istream stream_t; typedef string_t::size_type size_type; string_t line_; size_type pos_; stream_line_reader slreader_; bool eof_; public: csv_reader( stream_t& in ) : slreader_( in ) { eof_ = false; } int find_first_columun( string_t *str ) { if (eof_) { *str = ""; return 0; } int br = slreader_.read_a_line( &line_ ); if (br == 0) eof_ = true; pos_ = line_.find((charT)','); if (pos_ == string_t::npos) *str = line_; else { *str = line_.substr( 0, pos_ ); ++pos_; } return 1; } int find_next_columun( string_t *str ) { if (eof_ || pos_ == string_t::npos) { *str = ""; return 0; } size_type newpos = line_.find((charT)',', pos_ ); if (newpos == string_t::npos) { *str = line_.substr( pos_ ); pos_ = newpos; // ‚±‚±‚Å pos_ == string_t::npos } else { *str = line_.substr( pos_, newpos - pos_ ); pos_ = newpos + 1; } return 1; } bool eof() const { return eof_; } csv_reader& operator >> ( string_t& str ) { find_first_columun( &str ); return *this; } csv_reader& operator , ( string_t& str ) { find_next_columun( &str ); return *this; } }; #endif // cvsread_hpp__