STK++ 1.0
STK_ReadWriteCsv.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  stkpp::DManager
00027  * Purpose:  Declaration of the class ReaWriteCsv.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00038 #ifndef READWRITECSV_H
00039 #define READWRITECSV_H
00040 
00041 #include "STK_Variable.h"
00042 #include "../../Arrays/include/STK_Array1D.h"
00043 
00044 namespace STK
00045 {
00046 
00050 namespace Csv
00051 {
00054   static const Integer   DEFAULT_RESERVE    = Integer(0x0FFFF);
00055 
00059   static const Char* DEFAULT_DELIMITER  =     _T(",");
00060 
00065   enum readflags
00066   {
00067     RF_APPEND_DATA    = 0x00000004,
00068     RF_REPLACE_DATA   = 0x00000008
00069   };
00070 }
00071 
00072 
00080 class ReadWriteCsv
00081 {
00082   public:
00088     ReadWriteCsv(bool read_names = true);
00089 
00098     ReadWriteCsv( std::string const& file_name
00099                 , bool read_names = true
00100                 , String const& delimiter = Csv::DEFAULT_DELIMITER
00101                 , Integer const&  reserve   = Csv::DEFAULT_RESERVE
00102                 );
00103 
00108     ReadWriteCsv(ReadWriteCsv const& df);
00109 
00112     ~ReadWriteCsv();
00113 
00117     void clear();
00118 
00122     inline Integer first() const
00123     { return str_data_.first(); }
00124 
00128     inline Integer last() const
00129     { return str_data_.last(); }
00130 
00134     inline Integer size() const
00135     { return str_data_.size(); }
00136 
00141      inline Integer firstVe( Integer const& icol) const
00142      { return str_data_.at(icol).first();}
00143 
00148      inline Integer lastVe( Integer const& icol) const
00149      { return str_data_.at(icol).last();}
00150 
00155     inline Integer sizeVe( Integer const& icol) const
00156     { return str_data_.at(icol).size();}
00157 
00159     Integer firstVe() const;
00160 
00162     Integer lastVe() const;
00163 
00167     inline String const& error() const
00168     { return msg_error_; }
00169 
00173     inline Variable<String>& at(Integer icol)
00174     { return str_data_.at(icol); }
00175 
00179     inline const Variable<String>& at(Integer icol) const
00180     { return str_data_.at(icol); }
00181 
00185     inline Variable<String>& operator[](Integer const& icol)
00186     { return str_data_.at(icol); }
00187 
00191     inline const Variable<String>& operator[](Integer const& icol) const
00192     { return str_data_.at(icol); }
00193 
00195     inline Variable<String>& front()
00196     { return str_data_.at(first());}
00197 
00199     inline const Variable<String>& front() const
00200     { return str_data_.at(first());}
00201 
00203     inline Variable<String>& back()
00204     { return str_data_.at(last());}
00205 
00207     inline const Variable<String>& back() const
00208     { return str_data_.at(last());}
00209 
00213     bool read();
00214 
00219     bool push_back(const Variable<String>& data = Variable<String>());
00220 
00225     bool push_front(const Variable<String>& data = Variable<String>());
00226 
00233     Integer colIndex( const String  &variable_name
00234                     , Integer const& iStartingIndex = 0) const;
00235 
00243     Integer colIndex( String const& variable_name
00244                     , std::string const& sourceFilename
00245                     , Integer const& iStartingIndex = 0) const;
00246 
00253     Integer name( Integer const& icol, String& rStr) const;
00254 
00260     bool setName( Integer const& icol, String const& name);
00261 
00268     Integer data( Integer const& icol, Integer const& irow, String &lpStr) const;
00269 
00276     Integer data( String const& variable_name, Integer const& irow, String &lpStr) const;
00277 
00283     Integer data( Integer const& icol, Variable<String>& rVector) const;
00284 
00290     Integer data( String const& variable_name, Variable<String>& rVector) const;
00291 
00296     inline void setDelimiters( String const& delimiters) const
00297     { delimiter_ = delimiters; }
00298 
00304     inline void setWithNames( bool with_names = true) const
00305     { with_names_ = with_names; }
00306 
00310     inline void setReserve(Integer const& reserve) const
00311     { reserve_ = reserve; }
00312 
00319     bool setData( Integer const& icol, Integer const& irow, String const& value);
00320 
00326     bool appendData( Integer const& icol, String const& value);
00327 
00333     bool appendData( Integer const& icol, const Variable<String>& data);
00334 
00339     bool eraseColumn(Integer const& icol);
00340 
00345     bool read(std::string const& file_name);
00346 
00350     ReadWriteCsv& operator=( ReadWriteCsv const& df);
00351 
00355     ReadWriteCsv& operator+=( ReadWriteCsv const& df);
00356 
00360     ReadWriteCsv operator+( ReadWriteCsv const& df) const;
00361 
00367     bool write( std::string const& file_name) const;
00368 
00372     void write( ostream& os) const;
00373 
00382     void writeSelection( ostream& os
00383                        , Integer const& top
00384                        , Integer const& bottom
00385                        , Integer const& left
00386                        , Integer const& right
00387                        ) const;
00388 
00394     inline String& operator()( Integer const& icol, Integer const& irow)
00395     { return str_data_.at(icol).at(irow);}
00396 
00402     inline String const& operator()( Integer const& icol
00403                                    , Integer const& irow) const
00404     { return str_data_.at(icol).at(irow);}
00405 
00406   protected:
00408     mutable std::string  file_name_;
00410     mutable bool with_names_;
00412     mutable String  delimiter_;
00414     mutable Integer reserve_;
00416     mutable String msg_error_;
00417 
00419     Array1D<std::string> source_file_names_;
00421     Array1D< Variable<String> > str_data_;
00422 
00428     Integer lookupVariableIndex( String const& name
00429                                , Integer const& offset=0) const;
00430 
00432     Integer largestNumberOfRows() const;
00433 
00434     // friend operators
00435     friend istream& operator>>( istream& is, ReadWriteCsv& df);
00436     friend ostream& operator<<( ostream& os, ReadWriteCsv const& df);
00437 };
00438 
00444 istream& operator>>( istream& is, ReadWriteCsv& df);
00445 
00451 ostream& operator<<( ostream& os, ReadWriteCsv const& df);
00452 
00453 } // Namespace STK
00454 
00455 #endif // READWRITECSV_H