inlib  1.2.0
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
inlib::rcsv::ntuple Class Reference
Inheritance diagram for inlib::rcsv::ntuple:
Inheritance graph
[legend]
Collaboration diagram for inlib::rcsv::ntuple:
Collaboration graph
[legend]

List of all members.

Classes

class  column

Public Member Functions

virtual void start ()
virtual bool next ()
virtual read::icolfind_icol (const std::string &a_name)
virtual const std::vector
< read::icol * > & 
columns () const
 ntuple (std::istream &a_reader)
virtual ~ntuple ()
void set_hippo (bool a_hippo)
std::istream & istrm ()
bool initialize (std::ostream &a_out, char a_sep=0, const std::string &a_suffix="x", bool a_verbose=false)
void dump_columns (std::ostream &a_out) const

Static Public Member Functions

static bool find_sep (std::ostream &a_out, std::istream &a_reader, bool a_hippo, bool a_verbose, char &a_sep)
static const std::string & s_cid (cid a_id)

Protected Member Functions

 ntuple (const ntuple &a_from)
ntupleoperator= (const ntuple &a_from)
template<class T >
column< T > * create_column (const std::string &a_name)
bool _read_line ()

Static Protected Member Functions

static bool _read (std::istream &a_reader, char, std::streampos, double &a_v)
static bool _read (std::istream &a_reader, char a_sep, std::streampos a_sz, time_t &a_v)
static bool _read (std::istream &a_reader, char a_sep, std::streampos a_sz, std::string &a_v)
static bool read_line (std::istream &a_reader, std::streampos a_sz, std::string &a_s)
static bool skip_line (std::istream &a_reader, std::streampos a_sz)
static bool skip_comment (std::istream &a_reader, std::streampos a_sz)

Protected Attributes

std::istream & m_reader
char m_sep
std::vector< read::icol * > m_cols
std::streampos m_sz
bool m_hippo

Detailed Description

Definition at line 37 of file rcsv_ntuple.


Constructor & Destructor Documentation

inlib::rcsv::ntuple::ntuple ( std::istream &  a_reader) [inline]

Definition at line 163 of file rcsv_ntuple.

  :m_reader(a_reader)
  ,m_sep(0)
  ,m_sz(0)
  ,m_hippo(false)
  {
#ifdef INLIB_MEM
    mem::increment(s_class().c_str());
#endif
  }
virtual inlib::rcsv::ntuple::~ntuple ( ) [inline, virtual]

Definition at line 173 of file rcsv_ntuple.

                    {
    inlib::clear<read::icol>(m_cols);
#ifdef INLIB_MEM
    mem::decrement(s_class().c_str());
#endif
  }
inlib::rcsv::ntuple::ntuple ( const ntuple a_from) [inline, protected]

Definition at line 180 of file rcsv_ntuple.

  :read::intuple(a_from)
  ,m_reader(a_from.m_reader)
  ,m_sep(a_from.m_sep)
  ,m_sz(a_from.m_sz)
  ,m_hippo(a_from.m_hippo)
  {
#ifdef INLIB_MEM
    mem::increment(s_class().c_str());
#endif
  }

Member Function Documentation

static bool inlib::rcsv::ntuple::_read ( std::istream &  a_reader,
char  ,
std::streampos  ,
double &  a_v 
) [inline, static, protected]

Definition at line 77 of file rcsv_ntuple.

                                        {
    a_reader >> a_v;
    if(a_reader.tellg()==std::streampos(-1)) {a_v = 0;return false;}
    //std::cout << "debug : _read(double) " << a_v << std::endl;
    return true;
  }
static bool inlib::rcsv::ntuple::_read ( std::istream &  a_reader,
char  a_sep,
std::streampos  a_sz,
time_t &  a_v 
) [inline, static, protected]

Definition at line 85 of file rcsv_ntuple.

                                        {
    std::string s;
    char c;
    while(true){
      if(a_reader.tellg()>=a_sz) break;
      a_reader.get(c);
      if((c==a_sep)||(c==CR())||(c==LF())) {
        a_reader.putback(c);
        break;
      }
      s += c;
    }
    if(!s2time(s,a_v)) return false;
    return true;
  }
static bool inlib::rcsv::ntuple::_read ( std::istream &  a_reader,
char  a_sep,
std::streampos  a_sz,
std::string &  a_v 
) [inline, static, protected]

Definition at line 102 of file rcsv_ntuple.

                                           {
    a_v.clear();
    char c;
    while(true){
      if(a_reader.tellg()>=a_sz) break;
      a_reader.get(c);
      if((c==a_sep)||(c==CR())||(c==LF())) {
        a_reader.putback(c);
        break;
      }
      a_v += c;
    }
    return true;
  }
bool inlib::rcsv::ntuple::_read_line ( ) [inline, protected]

Definition at line 507 of file rcsv_ntuple.

                    {
    // have to loop on all columns !
    typedef read::icol icol_t;
    typedef ntuple::column<double> cold_t;
    typedef ntuple::column<time_t> colt_t;
    typedef ntuple::column<std::string> cols_t;
    unsigned int index = 0;
    unsigned int num = m_cols.size();
    std::vector<icol_t*>::const_iterator it;
    for(it=m_cols.begin();it!=m_cols.end();++it,index++) {
      if(cold_t* cold = inlib::id_cast<icol_t,cold_t>(*(*it))) {
        double v;
        if(!_read(m_reader,m_sep,m_sz,v)) return false;
        cold->set_value(v);
      } else if(colt_t* colt = inlib::id_cast<icol_t,colt_t>(*(*it))) {
        time_t v;
        if(!_read(m_reader,m_sep,m_sz,v)) return false;
        colt->set_value(v);
      } else if(cols_t* cols = inlib::id_cast<icol_t,cols_t>(*(*it))) {
        std::string v;
        if(!_read(m_reader,m_sep,m_sz,v)) return false;
        cols->set_value(v);
      } else {
        //std::cout << "column cast failed." << std::endl;
        return false; 
      }
      if(index==(num-1)) { //read up to LF()
        char c;
        while(true){
          if(m_reader.tellg()>=m_sz) break;
          m_reader.get(c);
          if(c==LF()) break;
        }
      } else { //read sep :
        char sep;
        m_reader.get(sep);
      }
    }
    return true;
  }
virtual const std::vector<read::icol*>& inlib::rcsv::ntuple::columns ( ) const [inline, virtual]

Implements inlib::read::intuple.

Definition at line 75 of file rcsv_ntuple.

{return m_cols;}
template<class T >
column<T>* inlib::rcsv::ntuple::create_column ( const std::string &  a_name) [inline, protected]

Definition at line 460 of file rcsv_ntuple.

                                                   {
    if(find_named<read::icol>(m_cols,a_name)) return 0;
    column<T>* col = new column<T>(a_name);
    if(!col) return 0;
    m_cols.push_back(col);
    return col;
  }
void inlib::rcsv::ntuple::dump_columns ( std::ostream &  a_out) const [inline]

Definition at line 444 of file rcsv_ntuple.

                                             {
    if((m_sep>=32)&&(m_sep<=126)) { //printable
      a_out << "separator is '" << m_sep << "'" << std::endl;
    } else {
      a_out << "separator is " << (unsigned int)m_sep << std::endl;
    }
    std::vector<read::icol*>::const_iterator it;
    for(it=m_cols.begin();it!=m_cols.end();++it) {
      a_out << (*it)->name()
            << " " << s_cid((*it)->id_cls())
            << std::endl;
    }
  }
virtual read::icol* inlib::rcsv::ntuple::find_icol ( const std::string &  a_name) [inline, virtual]

Implements inlib::read::intuple.

Definition at line 71 of file rcsv_ntuple.

                                                      {
    return find_named<read::icol>(m_cols,a_name);
  }
static bool inlib::rcsv::ntuple::find_sep ( std::ostream &  a_out,
std::istream &  a_reader,
bool  a_hippo,
bool  a_verbose,
char &  a_sep 
) [inline, static]

Definition at line 201 of file rcsv_ntuple.

                                          {
    // analyse first data line to find the char separator.

    a_reader.clear();
    a_reader.seekg(0,std::ios::end);
    std::streampos sz = a_reader.tellg();
    a_reader.seekg(0,std::ios::beg);
    if(!sz) {
      a_out << "inlib::rcsv::ntuple::find_sep :"
            << " stream is empty."
            << std::endl;
      a_sep = 0;
      return false;
    } //file empty.
    if(a_verbose) a_out << "file size " << sz << std::endl;

    if(a_hippo) { //skip first two lines :
      if(!skip_line(a_reader,sz)) {a_sep = 0;return false;}
      if(!skip_line(a_reader,sz)) {a_sep = 0;return false;}
    } else {
      while(skip_comment(a_reader,sz)){}
    }
    if(a_reader.tellg()>=sz) {a_sep=0;return false;} //no data line.

    // get first data line :
    std::string sfirst;
   {char c;
    while(true) {
      if(a_reader.tellg()>=sz) break;
      a_reader.get(c);
      if((c==CR())||(c==LF())) break;
      sfirst += c;
    }}
    if(sfirst.empty()) {
      a_out << "inlib::rcsv::ntuple::find_set :"
            << " first datat line is empty."
            << std::endl;
      a_sep = 0;
      return false;
    }
    if(a_verbose) a_out << "first data line \"" << sfirst << "\"" << std::endl;

    //guess sep from first data line :
    std::istringstream strm(sfirst.c_str());
    double d;
    strm >> d;
    std::streampos pos = strm.tellg();
    if(pos==std::streampos(-1)) {
      a_out << "inlib::rcsv::ntuple::find_sep :"
            << " first line does not start with a number."
            << std::endl;
      a_sep = 0;
      return false;
    } //not a number.
    if(a_verbose) a_out << "first number " << d
                        << " ending at pos " << pos << std::endl;
    if(pos>=(std::streampos)sfirst.size()) {
      a_out << "inlib::rcsv::ntuple::find_sep :"
            << " no separator found in first line."
            << " pos " << pos
            << " sfirst.size() " << sfirst.size()
            << std::endl;
      a_sep = 0;
      return false;
    } //no sep.

    strm.get(a_sep);

    return true;
  }
bool inlib::rcsv::ntuple::initialize ( std::ostream &  a_out,
char  a_sep = 0,
const std::string &  a_suffix = "x",
bool  a_verbose = false 
) [inline]

Reimplemented in inlib::rcsv::fntuple.

Definition at line 276 of file rcsv_ntuple.

                                                 {
    inlib::clear<read::icol>(m_cols);
    m_sep = 0;
    m_sz = 0;

    if(a_suffix.empty()) {
      a_out << "inlib::rcsv::ntuple::initialize :"
            << " expect a column suffix."
            << std::endl;
      return false;
    }

    m_reader.clear();
    m_reader.seekg(0,std::ios::end);
    m_sz = m_reader.tellg();
    m_reader.seekg(0,std::ios::beg);
    if(!m_sz) {
      a_out << "inlib::rcsv::ntuple::initialize :"
            << " stream is empty."
            << std::endl;
      return false; //file empty.
    }
    if(a_verbose) a_out << "file size " << m_sz << std::endl;

    std::vector<std::string> labels;
    if(m_hippo) { //skip first two lines :
      std::string title;
      if(!read_line(m_reader,m_sz,title)) {a_sep = 0;return false;}
      std::string s;
      if(!read_line(m_reader,m_sz,s)) {a_sep = 0;return false;}
      inlib::words(s,"\t",false,labels);
    } else {
      while(skip_comment(m_reader,m_sz)){}
    }
    if(m_reader.tellg()>=m_sz) {m_sz=0;return false;}

    // get first data line :
    std::string sfirst;
  {{char c;
    while(true) {
      if(m_reader.tellg()>=m_sz) break;
      m_reader.get(c);
      if((c==CR())||(c==LF())) break;
      sfirst += c;
    }}
    if(sfirst.empty()) {
      a_out << "inlib::rcsv::ntuple::initialize :"
            << " first datat line is empty."
            << std::endl;
      m_sz = 0;
      return false;
    }}
    if(a_verbose) a_out << "first data line \"" << sfirst << "\"" << std::endl;

    if(a_sep) {
      m_sep = a_sep;
    } else {
      //guess sep from first data line :
      std::istringstream strm(sfirst.c_str());
      double d;
      strm >> d;
      std::streampos pos = strm.tellg();
      if(pos==std::streampos(-1)) {
        a_out << "inlib::rcsv::ntuple::initialize :"
              << " first line does not start with a number."
              << std::endl;
        m_sz = 0;
        return false;
      }
      if(a_verbose) a_out << "first number " << d
                          << " ending at pos " << pos << std::endl;
      if(pos>=(std::streampos)sfirst.size()) {
        a_out << "inlib::rcsv::ntuple::initialize :"
              << " no separator found in first line."
              << std::endl;
        m_sz = 0;
        return false;
      }
      strm.get(m_sep);
    }
    if(a_verbose) a_out << "sep " << (int)m_sep << std::endl;

    // in case sep is ' ', there is an ambiguity with some leading
    // space in front of first number.
    if(m_sep==' ') inlib::strip(sfirst,leading,' ');

    std::vector<std::string> words;
   {std::string sep;
    sep += m_sep;
    inlib::words(sfirst,sep,true,words);}

    // look if words are numbers :
    if(a_verbose) a_out << "words " << words.size() << std::endl;
    unsigned int index = 0;
    std::vector<std::string>::iterator it;
    for(it=words.begin();it!=words.end();++it,index++) {
      if(a_verbose) a_out << "word " << sout(*it) << "" << std::endl;
      if((*it).empty()) {
        // do not accept :
        //   <num><sep><num><sep><sep><num>...
        // but accept a trailing <sep> (glast.tnt) :
        //   <num><sep><num>....<sep><num><sep>
        if(index==(words.size()-1)) {
          break;
        } else {
          a_out << "inlib::rcsv::ntuple::initialize :"
                << " empty word."
                << std::endl;
          m_sep = 0;
          m_sz = 0;
          return false;
        }      
      }      
      std::string name(a_suffix+to<unsigned int>(m_cols.size()));
      if(m_hippo) {
        if(index>=labels.size()) {
          a_out << "inlib::rcsv::ntuple::initialize :"
                << " warning : not enough labels."
                << std::endl;
        } else {
          name = labels[index];
        }
      }
      double d;
      if(to<double>(*it,d)) {
        if(a_verbose) a_out << "number " << d << std::endl;
        create_column<double>(name);
      } else {
        time_t time;
        if(s2time(*it,time)) {
          create_column<time_t>(name);
        } else {
          create_column<std::string>(name);
        }
      }
    }
    unsigned int num = m_cols.size();
    if(!num) {
      a_out << "inlib::rcsv::ntuple::initialize :"
            << " zero columns."
            << std::endl;
      m_sep = 0;
      m_sz = 0;
      return false;
    }

    return true;
  }
std::istream& inlib::rcsv::ntuple::istrm ( ) [inline]

Definition at line 199 of file rcsv_ntuple.

{return m_reader;}
virtual bool inlib::rcsv::ntuple::next ( ) [inline, virtual]

Implements inlib::read::intuple.

Definition at line 47 of file rcsv_ntuple.

                      { 
    if(!m_sep) return false; //not inited.
    if(m_reader.tellg()>=m_sz) return false;
    // first time we are at bol but else we are at eol.
    char c;
    m_reader.get(c);
    if(c==LF()){
      if(m_reader.tellg()>=m_sz) {
        //eof. Tell caller to stop looping on ntuple rows.
        return false;
      }
      //eol. Next char read is going to be at bol.
    } else {
      m_reader.putback(c);
      //bol
    }
    // ready for a new row :

    while(skip_comment(m_reader,m_sz)){}
    if(m_reader.tellg()>=m_sz) return false;

    return _read_line();
  }
ntuple& inlib::rcsv::ntuple::operator= ( const ntuple a_from) [inline, protected]

Definition at line 191 of file rcsv_ntuple.

                                         {
    m_sep = a_from.m_sep;
    m_hippo = a_from.m_hippo;
    return *this;
  }
static bool inlib::rcsv::ntuple::read_line ( std::istream &  a_reader,
std::streampos  a_sz,
std::string &  a_s 
) [inline, static, protected]

Definition at line 468 of file rcsv_ntuple.

                                              {
    a_s.clear();
    char c;
    while(true) {
      if(a_reader.tellg()>=a_sz) {a_s.clear();return false;}
      a_reader.get(c);
      if(c==CR()) continue;
      if(c==LF()) break; //eol.
      a_s += c;
    }
    return true;
  }
static const std::string& inlib::rcsv::ntuple::s_cid ( cid  a_id) [inline, static]

Definition at line 428 of file rcsv_ntuple.

                                          {
    if(a_id==_cid(double())) {
      static const std::string s_v("double");
      return s_v;      
    } else if(a_id==_cid(time_t())) {
      static const std::string s_v("time");
      return s_v;      
    } else if(a_id==_cid(std::string())) {
      static const std::string s_v("string");
      return s_v;      
    } else {
      static const std::string s_v("unknown");
      return s_v;      
    }
  }
void inlib::rcsv::ntuple::set_hippo ( bool  a_hippo) [inline]

Definition at line 197 of file rcsv_ntuple.

{m_hippo = a_hippo;}
static bool inlib::rcsv::ntuple::skip_comment ( std::istream &  a_reader,
std::streampos  a_sz 
) [inline, static, protected]

Definition at line 492 of file rcsv_ntuple.

                                                                  {
    //ret true = we had a commented line, false : a data line or nothing.
    if(a_reader.tellg()>=a_sz) return false;
    //we should be at bol :
    char c;
    a_reader.get(c);
    if(c=='#') {
      return skip_line(a_reader,a_sz);
      //eol. Next char should be bol.
    } else {
      a_reader.putback(c);
      return false;
    }
  }
static bool inlib::rcsv::ntuple::skip_line ( std::istream &  a_reader,
std::streampos  a_sz 
) [inline, static, protected]

Definition at line 482 of file rcsv_ntuple.

                                                               {
    char c;
    while(true) {
      if(a_reader.tellg()>=a_sz) return false;
      a_reader.get(c);
      if(c==LF()) break;
    }
    return true;
  }
virtual void inlib::rcsv::ntuple::start ( ) [inline, virtual]

Implements inlib::read::intuple.

Definition at line 39 of file rcsv_ntuple.

                       {
    m_reader.clear();
    m_reader.seekg(0,std::ios::beg);
    if(m_hippo) {
      skip_line(m_reader,m_sz);
      skip_line(m_reader,m_sz);
    }
  }

Member Data Documentation

std::vector<read::icol*> inlib::rcsv::ntuple::m_cols [protected]

Definition at line 550 of file rcsv_ntuple.

bool inlib::rcsv::ntuple::m_hippo [protected]

Definition at line 552 of file rcsv_ntuple.

std::istream& inlib::rcsv::ntuple::m_reader [protected]

Definition at line 548 of file rcsv_ntuple.

char inlib::rcsv::ntuple::m_sep [protected]

Definition at line 549 of file rcsv_ntuple.

std::streampos inlib::rcsv::ntuple::m_sz [protected]

Definition at line 551 of file rcsv_ntuple.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines