STK::ReadWriteCsv Class Reference
[project DManager (Data Management)]

the ReadWriteCsv class : allow to write and/or to read a csv file. More...

#include <STK_ReadWriteCsv.h>

List of all members.

Public Member Functions

 ReadWriteCsv (bool read_names=true)
 ReadWriteCsv (const std::string &file_name, const bool &read_names=true, const String &delimiter=CSV::DEFAULT_DELIMITER, const Integer &reserve=CSV::DEFAULT_RESERVE)
 ReadWriteCsv (const ReadWriteCsv &df)
 ~ReadWriteCsv (void)
void clear ()
const std::string & getLastError () const
Integer getNumberOfCols () const
Integer getNumberOfRows (const Integer &icol) const
Integer getFirstVe (const Integer &icol) const
Integer getLastVe (const Integer &icol) const
Integer getLargestNumberOfRows () const
Integer getLargestLastVe () const
Integer getLowerFirstVe () const
Integer getColIndex (const String &variable_name, const Integer &iStartingIndex=0) const
Integer getColIndex (const String &variable_name, const std::string &sourceFilename, const Integer &iStartingIndex=0) const
Integer getColName (const Integer &icol, String &rStr) const
Integer getData (const Integer &icol, const Integer &irow, String &lpStr) const
Integer getData (const String &variable_name, const Integer &irow, String &lpStr) const
Integer getData (const Integer &icol, Variable< String > &rVector) const
Integer getData (const String &variable_name, Variable< String > &rVector) const
void setDelimiters (const String &delimiters) const
void setWithNames (const bool &with_names=true) const
void setReserve (const Integer &reserve) const
bool writeFile (const std::string &file_name) const
bool readFile (const std::string &file_name)
bool setData (const Integer &icol, const Integer &irow, const String &value)
bool appendData (const Integer &icol, const String &value)
bool appendData (const Integer &icol, const Variable< String > &data)
bool eraseCols (const Integer &icol)
ReadWriteCsvoperator= (const ReadWriteCsv &df)
ReadWriteCsvoperator+= (const ReadWriteCsv &df)
ReadWriteCsv operator+ (const ReadWriteCsv &df) const
void writeSelection (ostream &os, const Integer &top, const Integer &bottom, const Integer &left, const Integer &right) const
void writeCsv (ostream &os) const
Stringoperator() (const Integer &icol, const Integer &irow)
const Stringoperator() (const Integer &icol, const Integer &irow) const
Integer first () const
Integer last () const
Variable< String > & at (Integer icol)
const Variable< String > & at (Integer icol) const
Variable< String > & operator[] (const Integer &icol)
const Variable< String > & operator[] (const Integer &icol) const
Variable< String > & front ()
const Variable< String > & front () const
Variable< String > & back ()
const Variable< String > & back () const
bool push_back (const Variable< String > &data=Variable< String >())
bool push_front (const Variable< String > &data=Variable< String >())

Static Public Member Functions

static ReadWriteCsv fromVariable (const Variable< String > &variable)

Protected Member Functions

Integer lookupVariableIndex (const String &name, const Integer &offset=0) const

Protected Attributes

std::string file_name_
 Name of the Current file read.
bool with_names_
 Read and Write names of the variables.
String delimiter_
 Delimiter(s).
Integer reserve_
 Size of the buffer.
std::string msg_error_
 Contain the last error message.
Array1D< std::string > source_file_names_
Array1D< Variable< String > > str_data_

Friends

istreamoperator>> (istream &is, ReadWriteCsv &df)
ostreamoperator<< (ostream &os, const ReadWriteCsv &df)


Detailed Description

It is possible to merge two csv files and to extract subregion of the file too.

Definition at line 97 of file STK_ReadWriteCsv.h.


Constructor & Destructor Documentation

STK::ReadWriteCsv::ReadWriteCsv ( bool  read_names = true  ) 

The default constructor. Instantiates an instance of ReadWriteCsv and initialize data members to default values.

Parameters:
read_names true if we want ot read the names of the variables at the first line of the file

Definition at line 91 of file STK_ReadWriteCsv.cpp.

References delimiter_, msg_error_, reserve_, and with_names_.

Referenced by operator+().

00092 {
00093   delimiter_ = DEFAULT_DELIMITER;
00094   reserve_   = DEFAULT_RESERVE;
00095   with_names_ = read_names;
00096   msg_error_  = "";
00097 }

STK::ReadWriteCsv::ReadWriteCsv ( const std::string &  file_name,
const bool &  read_names = true,
const String delimiter = CSV::DEFAULT_DELIMITER,
const Integer reserve = CSV::DEFAULT_RESERVE 
)

Misc. constructor. Instantiates an instance of ReadWriteCsv and reads the specified file with the specified read flags.

Parameters:
file_name name of the file to read/write
read_names true if we want ot read the names of the variables at the first line of the file
delimiter a String of delimiter
reserve the place to reserve for the data

Definition at line 101 of file STK_ReadWriteCsv.cpp.

References msg_error_, and readFile().

00106                           : file_name_(file_name)
00107                           , with_names_(read_names)
00108                           , delimiter_(delimiter)
00109                           , reserve_(reserve)
00110 {
00111   // initialize the error message
00112   msg_error_  = "";
00113   // read file
00114   this->readFile(file_name);
00115 }

STK::ReadWriteCsv::ReadWriteCsv ( const ReadWriteCsv df  ) 

Copy constructor. Instantiates an instance of ReadWriteCsv with the contents of another ReadWriteCsv.

Parameters:
df the ReadWriteCsv to copy

Definition at line 119 of file STK_ReadWriteCsv.cpp.

00120 {
00121   *this = df;
00122 }

STK::ReadWriteCsv::~ReadWriteCsv ( void   ) 

Dtor : Calls clear() before the ReadWriteCsv is destroyed.

Definition at line 222 of file STK_ReadWriteCsv.cpp.

References clear().

00223 {
00224   clear();
00225 }


Member Function Documentation

Integer STK::ReadWriteCsv::lookupVariableIndex ( const String name,
const Integer offset = 0 
) const [protected]

Protected member function for internal bookeeping.

Parameters:
name name of the variable to find
offset offset to apply

Definition at line 513 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), and str_data_.

Referenced by getColIndex(), and getData().

00515 {
00516   Integer it = str_data_.first() + offset;
00517   while (it <= str_data_.last())
00518   { if (str_data_[it].getName() == variable_name) break;
00519     it++;
00520   }
00521 
00522   if(it == str_data_.last()+1) // variable Name was not found
00523     return -1;
00524 
00525   Integer retVal = str_data_.first();
00526   while(it != str_data_.first())
00527   {
00528     retVal++;
00529     it--;
00530   }
00531   return retVal;
00532 }

ReadWriteCsv STK::ReadWriteCsv::fromVariable ( const Variable< String > &  variable  )  [static]

Returns a ReadWriteCsv with one variable created with the specified Variable.

Parameters:
variable The Variable which create the ReadWriteCsv

Definition at line 126 of file STK_ReadWriteCsv.cpp.

References file_name_, STK::ITContainer1D< TYPE, TContainer1D >::push_back(), source_file_names_, and str_data_.

00127 {
00128   ReadWriteCsv retDF;
00129 
00130   retDF.source_file_names_.push_back(retDF.file_name_);
00131   retDF.str_data_.push_back(variable);
00132 
00133   return retDF;
00134 }

void STK::ReadWriteCsv::clear (  ) 

Clears the data contained in a ReadWriteCsv and reclaims any allocated memory.

Definition at line 380 of file STK_ReadWriteCsv.cpp.

References STK::Array1D< TYPE >::clear(), msg_error_, source_file_names_, and str_data_.

Referenced by ~ReadWriteCsv().

00381 {
00382   msg_error_ = "";
00383   source_file_names_.clear();
00384   str_data_.clear();
00385 }

const std::string& STK::ReadWriteCsv::getLastError (  )  const [inline]

Returns the last error encountered by the class.

Definition at line 172 of file STK_ReadWriteCsv.h.

References msg_error_.

00173     { return msg_error_; }

Integer STK::ReadWriteCsv::getNumberOfCols (  )  const [inline]

Returns the number of variables currently in the ReadWriteCsv.

Definition at line 176 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::size(), and str_data_.

00177     { return str_data_.size(); }

Integer STK::ReadWriteCsv::getNumberOfRows ( const Integer icol  )  const [inline]

Returns the number of samples currently in the Variable icol.

Parameters:
icol index of the col

Definition at line 182 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), STK::Variable< TYPE >::size(), and str_data_.

00183     { return str_data_.at(icol).size();}

Integer STK::ReadWriteCsv::getFirstVe ( const Integer icol  )  const [inline]

Returns the first index of samples currently in the variable.

Parameters:
icol index of the col

Definition at line 188 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), STK::Variable< TYPE >::first(), and str_data_.

00189     { return str_data_.at(icol).first();}

Integer STK::ReadWriteCsv::getLastVe ( const Integer icol  )  const [inline]

Returns the last index of samples currently in the variable.

Parameters:
icol index of the col

Definition at line 194 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), STK::Variable< TYPE >::last(), and str_data_.

00195     { return str_data_.at(icol).last();}

Integer STK::ReadWriteCsv::getLargestNumberOfRows (  )  const

Returns the largest number of samples.

Definition at line 345 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::max(), and str_data_.

00346 {
00347   Integer retVal = 0;
00348 
00349   for (Integer i=str_data_.first(); i<=str_data_.last(); i++)
00350     retVal = max(retVal, str_data_[i].size());
00351 
00352   return retVal;
00353 }

Integer STK::ReadWriteCsv::getLargestLastVe (  )  const

Returns the largest number of end index of samples.

Definition at line 357 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), last(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::max(), STK::min(), and str_data_.

Referenced by STK::operator<<(), writeCsv(), and writeFile().

00358 {
00359   Integer retVal = Arithmetic<Integer> ::min();
00360 
00361   for (Integer i=str_data_.first(); i<=str_data_.last(); i++)
00362     retVal = max(retVal, str_data_[i].last());
00363 
00364   return retVal;
00365 }

Integer STK::ReadWriteCsv::getLowerFirstVe (  )  const

Returns the lower number of end index of samples.

Definition at line 369 of file STK_ReadWriteCsv.cpp.

References first(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::max(), STK::min(), and str_data_.

Referenced by STK::operator<<(), writeCsv(), and writeFile().

00370 {
00371   Integer retVal = Arithmetic<Integer> ::max();
00372 
00373   for (Integer i=str_data_.first(); i<=str_data_.last(); i++)
00374     retVal = min(retVal, str_data_[i].first());
00375 
00376   return retVal;
00377 }

Integer STK::ReadWriteCsv::getColIndex ( const String variable_name,
const Integer iStartingIndex = 0 
) const

Looks up the index of a variable given its name, starting at the specified index. Returns the index if successful, -1 if an error is encountered.

Parameters:
variable_name Name of the variable to search
iStartingIndex first index

Definition at line 475 of file STK_ReadWriteCsv.cpp.

References lookupVariableIndex().

00478 {
00479   return lookupVariableIndex(variable_name, iStartingIndex);
00480 }

Integer STK::ReadWriteCsv::getColIndex ( const String variable_name,
const std::string &  sourceFilename,
const Integer iStartingIndex = 0 
) const

Looks up the index of a variable given its name and original source file, starting at the specified index. Returns the index if successful, -1 if an error is encountered.

Parameters:
variable_name Name of the variable to search
sourceFilename Name of the file to search
iStartingIndex first index

Definition at line 484 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), lookupVariableIndex(), msg_error_, and source_file_names_.

00488 {
00489   Integer it = source_file_names_.first();
00490   while (it <= source_file_names_.last())
00491   { if (source_file_names_[it] == sourceFilename) break;
00492     it++;
00493   }
00494 
00495   if(it == source_file_names_.last()+1) // sourceFilename was not found
00496   {
00497     msg_error_ = ERRORCODES[6];
00498     return -1;
00499   }
00500 
00501   Integer offset = source_file_names_.first();
00502   while(it != source_file_names_.first())
00503   {
00504     offset++;
00505     it--;
00506   }
00507   
00508   return lookupVariableIndex(variable_name, iStartingIndex+offset);
00509 }

Integer STK::ReadWriteCsv::getColName ( const Integer icol,
String rStr 
) const

Assigns the variable name at the specified index to rStr. Returns the new length of rStr if successful, -1 if an error is encountered.

Parameters:
icol index of the col
rStr name of the variable to assign

Definition at line 332 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), STK::IVariable::getName(), msg_error_, and str_data_.

00334 {
00335   try
00336   {
00337     rStr = str_data_.at(icol).getName();
00338     return static_cast<Integer> (rStr.length());
00339   }
00340   catch(const std::exception& e) { msg_error_ = e.what(); }
00341   catch(...)  { msg_error_ = ERRORCODES[1]; }
00342   return -1;
00343 }

Integer STK::ReadWriteCsv::getData ( const Integer icol,
const Integer irow,
String lpStr 
) const

Assigns lpStr with the data at the target variable. Returns the new length of lpStr. Returns -1 if an error is encountered.

Parameters:
icol index of the col
irow index of the row
lpStr returned value

Definition at line 389 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), msg_error_, and str_data_.

Referenced by getData().

00392 {
00393   Integer retVal = 0;
00394 
00395   try
00396   {
00397     lpStr  = str_data_.at(icol).at(irow);
00398     retVal = static_cast<Integer> (lpStr.length());
00399   }
00400   catch(const std::exception& e) 
00401   {  
00402     msg_error_ = e.what(); 
00403     retVal = -1;
00404   }
00405   catch(...) // other exceptions
00406   { 
00407     msg_error_ = ERRORCODES[1]; 
00408     retVal = -1;
00409   }
00410   return retVal;
00411 }

Integer STK::ReadWriteCsv::getData ( const String variable_name,
const Integer irow,
String lpStr 
) const

Assigns lpStr with the data at the target variable. Returns the new length of lpStr. Returns -1 if an error is encountered.

Parameters:
variable_name name of the variable
irow index of the row
lpStr returned value

Definition at line 415 of file STK_ReadWriteCsv.cpp.

References getData(), lookupVariableIndex(), and msg_error_.

00418 {
00419   Integer retVal = 0;
00420   Integer iVar   = lookupVariableIndex(variable_name); // find col
00421   
00422   if(iVar != -1)
00423     retVal = getData(iVar, irow, rStr);
00424   else
00425   {
00426     msg_error_ = ERRORCODES[5]; 
00427     retVal = -1;
00428   }
00429   return retVal;
00430 }

Integer STK::ReadWriteCsv::getData ( const Integer icol,
Variable< String > &  rVector 
) const

Assigns rVector with the data at the target variable. Returns the new size of rVector. Returns -1 if an error is encountered.

Parameters:
icol index of the col
rVector vector of the returned data

Definition at line 435 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), msg_error_, STK::Variable< TYPE >::size(), and str_data_.

00437 {
00438   Integer retVal = 0;
00439 
00440   try
00441   {
00442     rVector = str_data_.at(icol);
00443     retVal  = rVector.size();
00444   }
00445   catch(const std::exception& e) 
00446   {  
00447     msg_error_ = e.what();
00448     retVal = -1;
00449   }
00450   catch(...) 
00451   { 
00452     msg_error_ = ERRORCODES[2]; 
00453     retVal = -1;
00454   }
00455   return retVal;
00456 }

Integer STK::ReadWriteCsv::getData ( const String variable_name,
Variable< String > &  rVector 
) const

Assigns rVector with the data at the target variable. Returns the new size of rVector. Returns -1 if an error is encountered.

Parameters:
variable_name name of the variable
rVector vector of the returned data

Definition at line 460 of file STK_ReadWriteCsv.cpp.

References getData(), lookupVariableIndex(), and msg_error_.

00462 {
00463   Integer index = lookupVariableIndex(variable_name);
00464   
00465   if(index != -1)
00466     return getData(index, rVector);
00467   else
00468     msg_error_ = ERRORCODES[5]; 
00469   return -1;
00470 }

void STK::ReadWriteCsv::setDelimiters ( const String delimiters  )  const [inline]

Sets the delimiters to use for parsing data (delimiters_ is mutable).

Parameters:
delimiters delimiters to use

Definition at line 279 of file STK_ReadWriteCsv.h.

References delimiter_.

Referenced by STK::out2D(), and STK::DataFrame::writeDataFrame().

00280     { delimiter_ = delimiters; }

void STK::ReadWriteCsv::setWithNames ( const bool &  with_names = true  )  const [inline]

Sets the with_names_ value for reading/writting variables names (with_names_ is mutable).

Parameters:
with_names true if we want to read the names of the variables

Definition at line 287 of file STK_ReadWriteCsv.h.

References with_names_.

Referenced by STK::out2D().

00288     { with_names_ = with_names; }

void STK::ReadWriteCsv::setReserve ( const Integer reserve  )  const [inline]

Sets the reserve value for data storage (reserve_ is mutable).

Parameters:
reserve number of place to reserve

Definition at line 293 of file STK_ReadWriteCsv.h.

References reserve_.

00294     { reserve_ = reserve; }

bool STK::ReadWriteCsv::writeFile ( const std::string &  file_name  )  const

Attempts to write the ReadWriteCsv to the location specified by file_name using the delimiter specified by delimiter_. Returns true if successful, false if an error is encountered.

Parameters:
file_name name of the file to write

Definition at line 311 of file STK_ReadWriteCsv.cpp.

References file_name_, STK::ITContainer1D< TYPE, TContainer1D >::first(), getLargestLastVe(), getLowerFirstVe(), STK::ITContainer1D< TYPE, TContainer1D >::last(), msg_error_, str_data_, and writeSelection().

00312 {
00313   file_name_  = file_name;
00314   try
00315   {
00316     ofstream os(file_name.c_str());
00317     writeSelection( os
00318                   , getLowerFirstVe()
00319                   , getLargestLastVe()
00320                   , str_data_.first()
00321                   , str_data_.last()
00322                   );
00323     os.close();
00324     return true;
00325   }
00326   catch(const std::exception& e) { msg_error_ = e.what(); }
00327   catch(...) { msg_error_ = ERRORCODES[0]; }
00328   return false;
00329 }

bool STK::ReadWriteCsv::readFile ( const std::string &  file_name  ) 

Reads the specified file with the specified read flags. Returns true if successful, false if an error is encountered.

Parameters:
file_name name of the file to read

Definition at line 229 of file STK_ReadWriteCsv.cpp.

References file_name_, and msg_error_.

Referenced by ReadWriteCsv().

00230 {
00231   try
00232   {
00233     // update file_name
00234     file_name_ = file_name;
00235     // input file stream
00236     ifstream inFile;
00237     // open file
00238     inFile.open(file_name.c_str());
00239     // check error
00240     if (inFile.rdstate() & std::ios::failbit)
00241     {
00242       inFile.close();
00243       msg_error_ = ERRORCODES[4]; 
00244       msg_error_ += "\nDetails: ";
00245       msg_error_ += file_name;
00246       return false;
00247     }
00248     // read file
00249     inFile >> *this;
00250     // close file
00251     inFile.close();
00252     return true;
00253   }
00254   catch(const std::exception& e)
00255   { msg_error_ = e.what();}
00256   catch(...)
00257   { msg_error_ = ERRORCODES[0];}
00258 
00259   return false;
00260 }

bool STK::ReadWriteCsv::setData ( const Integer icol,
const Integer irow,
const String value 
)

Attempts to set the specified element to value. Returns true if successful, false if an error is encountered.

Parameters:
icol index of the col
irow index of thhe row
value value to set

Definition at line 263 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), msg_error_, and str_data_.

00266 {
00267   try
00268   {
00269     str_data_.at(icol).at(irow) = value;
00270     return true;
00271   }
00272   catch(const std::exception& e) { msg_error_ = e.what(); }
00273   catch(...) { msg_error_ = ERRORCODES[0]; }
00274   return false;
00275 }

bool STK::ReadWriteCsv::appendData ( const Integer icol,
const String value 
)

Attempts to append a data to the variable specified by icol. Returns true if successful, false if an error is encountered.

Parameters:
icol index of the col
value value to set

Definition at line 277 of file STK_ReadWriteCsv.cpp.

References msg_error_, STK::ITContainer1D< TYPE, TContainer1D >::push_back(), and str_data_.

00278 {
00279   try
00280   {
00281     if (Arithmetic<String>::isNA(value))
00282       str_data_[icol].push_back(Arithmetic<String>::NA());
00283     else
00284       str_data_[icol].push_back(value);
00285     return true;
00286   }
00287   catch(const std::exception& e) { msg_error_ = e.what(); }
00288   catch(...) { msg_error_ = ERRORCODES[0]; }
00289   return false;
00290 }

bool STK::ReadWriteCsv::appendData ( const Integer icol,
const Variable< String > &  data 
)

Attempts to append values from data to the variable specified by icol. Returns true if successful, false if an error is encountered.

Parameters:
icol index of the col
data values to set

bool STK::ReadWriteCsv::eraseCols ( const Integer icol  ) 

Deletes the variable whose index is icol from a ReadWriteCsv. Returns true if successful, false if an error is encountered.

Parameters:
icol index of the col to erase

Definition at line 294 of file STK_ReadWriteCsv.cpp.

References STK::Array1D< TYPE >::eraseElts(), msg_error_, source_file_names_, and str_data_.

00295 {
00296   try
00297   {
00298     // delete the variable from source_file_names_
00299     source_file_names_.eraseElts(icol),
00300 
00301     // delete the variable from str_data_
00302     str_data_.eraseElts(icol);
00303 
00304     return true;
00305   }
00306   catch(const std::exception& e) { msg_error_ = e.what(); }
00307   catch(...) { msg_error_ = ERRORCODES[0]; }
00308   return false;
00309 }

ReadWriteCsv & STK::ReadWriteCsv::operator= ( const ReadWriteCsv df  ) 

Assigns a ReadWriteCsv equal to another ReadWriteCsv.

Parameters:
df the ReadWriteCsv to copy

Definition at line 137 of file STK_ReadWriteCsv.cpp.

References delimiter_, file_name_, msg_error_, reserve_, source_file_names_, str_data_, and with_names_.

00138 {
00139   delimiter_         = df.delimiter_;
00140   reserve_           = df.reserve_;
00141   with_names_        = df.with_names_;
00142   file_name_         = df.file_name_;
00143   msg_error_         = df.msg_error_;
00144   source_file_names_ = df.source_file_names_;
00145   str_data_          = df.str_data_;
00146 
00147   return *this;
00148 }

ReadWriteCsv & STK::ReadWriteCsv::operator+= ( const ReadWriteCsv df  ) 

Appends a ReadWriteCsv to another ReadWriteCsv.

Parameters:
df the ReadWriteCsv to append

Definition at line 151 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::ITContainer1D< TYPE, TContainer1D >::push_back(), source_file_names_, and str_data_.

00152 {
00153   for ( Integer i=df.source_file_names_.first()
00154       ; i<=df.source_file_names_.last()
00155       ; i++)
00156     source_file_names_.push_back(df.source_file_names_[i]);
00157 
00158   for ( Integer i=df.str_data_.first()
00159       ; i<=df.str_data_.last()
00160       ; i++)
00161     str_data_.push_back(df.str_data_[i]);
00162 
00163   return *this;
00164 }

ReadWriteCsv STK::ReadWriteCsv::operator+ ( const ReadWriteCsv df  )  const

Combines ReadWriteCsv(s)

Parameters:
df the ReadWriteCsv to add

Definition at line 167 of file STK_ReadWriteCsv.cpp.

References ReadWriteCsv().

00168 { // copy this, add df and return the result
00169   return ReadWriteCsv((*this)) += df;
00170 }

void STK::ReadWriteCsv::writeSelection ( ostream os,
const Integer top,
const Integer bottom,
const Integer left,
const Integer right 
) const

Write to output stream a selection based on the coordinates passed (Think of it as highlighting cells in Excel).

Parameters:
os the output stream
top the top index
bottom th bottom index
left the left index
right the right index

Definition at line 173 of file STK_ReadWriteCsv.cpp.

References _T, STK::ITContainer1D< TYPE, TContainer1D >::at(), delimiter_, STK::IVariable::getName(), STK::max(), str_data_, and with_names_.

Referenced by STK::operator<<(), writeCsv(), STK::DataFrame::writeDataFrame(), and writeFile().

00178 {
00179   // create a vector for the format of the output
00180   Array1D<Integer>  format(Inx(left, right), 0);
00181   // for each var, find the largest size
00182   for(Integer iVar=left; iVar<=right; iVar++)
00183   {
00184     format.at(iVar)   = maxLength(str_data_.at(iVar));
00185     if (with_names_)
00186       format.at(iVar) = max( format.at(iVar)
00187                            , (Integer )str_data_.at(iVar).getName().size());
00188   }  
00189   // write if needed names variables
00190   if (with_names_)
00191     for(Integer iVar=left; iVar<=right; iVar++)
00192     {
00193       os << std::setw(format[iVar]) << std::right
00194          << ConstProxy<String>(str_data_.at(iVar).getName())
00195          << ((iVar==right) ? _T('\n') : delimiter_.at(0));
00196     }
00197   
00198   // write data
00199   for(Integer irow = top; irow<=bottom; irow++)
00200     for(Integer iVar = left; iVar<=right; iVar++)
00201     {
00202       try 
00203       {
00204         os << std::setw(format[iVar]) << std::right
00205            << ConstProxy<String>(str_data_.at(iVar).at(irow));
00206       }
00207       catch(...)
00208       {
00209         // if an error occur, we put NA value
00210         os << std::setw(format[iVar]) << std::right << STRING_NA;
00211       }
00212       os << ((iVar==right) ? _T('\n') : delimiter_.at(0));
00213     }
00214 }

void STK::ReadWriteCsv::writeCsv ( ostream os  )  const

Write to output stream the csv.

Parameters:
os the output stream

Definition at line 217 of file STK_ReadWriteCsv.cpp.

References first(), getLargestLastVe(), getLowerFirstVe(), last(), and writeSelection().

Referenced by STK::out2D().

00218 {
00219   writeSelection(os, getLowerFirstVe(), getLargestLastVe(), first(), last());   
00220 }

String& STK::ReadWriteCsv::operator() ( const Integer icol,
const Integer irow 
) [inline]

Returns a reference of the value specified by the given coordinates.

Parameters:
icol index of the col
irow index of thhe row

Definition at line 384 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), and str_data_.

00385     { return str_data_.at(icol).at(irow);}

const String& STK::ReadWriteCsv::operator() ( const Integer icol,
const Integer irow 
) const [inline]

Returns a const reference of the value specified by the given coordinates.

Parameters:
icol index of the col
irow index of the row

Definition at line 392 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), and str_data_.

00394     { return str_data_.at(icol).at(irow);}

Integer STK::ReadWriteCsv::first (  )  const [inline]

Returns the begining of the container (should be 1).

Definition at line 399 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::first(), and str_data_.

Referenced by front(), getLowerFirstVe(), and writeCsv().

00400     { return str_data_.first(); }

Integer STK::ReadWriteCsv::last (  )  const [inline]

Returns the end of the container (should be NumberOfCols).

Definition at line 404 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::last(), and str_data_.

Referenced by back(), getLargestLastVe(), and writeCsv().

00405     { return str_data_.last(); }

Variable<String>& STK::ReadWriteCsv::at ( Integer  icol  )  [inline]

return the element with the index icol.

Parameters:
icol index of the col

Definition at line 410 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), and str_data_.

00411     { return str_data_.at(icol); }

const Variable<String>& STK::ReadWriteCsv::at ( Integer  icol  )  const [inline]

return the element with the index icol (const).

Parameters:
icol index of the col

Definition at line 416 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), and str_data_.

00417     { return str_data_.at(icol); }

Variable<String>& STK::ReadWriteCsv::operator[] ( const Integer icol  )  [inline]

Returns a reference to the value at the specified location.

Parameters:
icol index of the col

Definition at line 422 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), and str_data_.

00423     { return str_data_.at(icol); }

const Variable<String>& STK::ReadWriteCsv::operator[] ( const Integer icol  )  const [inline]

Returns a const reference to the value at the specified location.

Parameters:
icol index of the col

Definition at line 428 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), and str_data_.

00429     { return str_data_.at(icol); }

Variable<String>& STK::ReadWriteCsv::front (  )  [inline]

return the first element.

Definition at line 432 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), first(), and str_data_.

00433     { return str_data_.at(first());}

const Variable<String>& STK::ReadWriteCsv::front (  )  const [inline]

return the first element (const).

Definition at line 436 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), first(), and str_data_.

00437     { return str_data_.at(first());}

Variable<String>& STK::ReadWriteCsv::back (  )  [inline]

return the last element.

Definition at line 440 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), last(), and str_data_.

Referenced by STK::ExportToCsv::ExportToCsv().

00441     { return str_data_.at(last());}

const Variable<String>& STK::ReadWriteCsv::back (  )  const [inline]

return the last element (const).

Definition at line 444 of file STK_ReadWriteCsv.h.

References STK::ITContainer1D< TYPE, TContainer1D >::at(), last(), and str_data_.

00445     { return str_data_.at(last());}

bool STK::ReadWriteCsv::push_back ( const Variable< String > &  data = Variable<String>()  ) 

Attempts to add a column with the values contained in data. Returns true if successful, false if an error is encountered.

Parameters:
data the column to push back

Definition at line 537 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::back(), file_name_, msg_error_, STK::ITContainer1D< TYPE, TContainer1D >::push_back(), STK::IArray1DBase< TYPE, PTRELT, TArray1D >::reserve(), reserve_, source_file_names_, and str_data_.

Referenced by STK::ExportToCsv::ExportToCsv().

00538 {
00539   try
00540   {
00541     source_file_names_.push_back(file_name_);
00542     str_data_.push_back(data);
00543     str_data_.back().reserve(reserve_);
00544     return true;
00545   }
00546   catch(const std::exception& e) { msg_error_ = e.what(); }
00547   catch(...) { msg_error_ = ERRORCODES[0]; }
00548   return false;
00549 }

bool STK::ReadWriteCsv::push_front ( const Variable< String > &  data = Variable<String>()  ) 

Attempts to add a column with the values contained in data. Returns true if successful, false if an error is encountered.

Parameters:
data the column to push back

Definition at line 554 of file STK_ReadWriteCsv.cpp.

References file_name_, msg_error_, STK::ITContainer1D< TYPE, TContainer1D >::push_front(), source_file_names_, and str_data_.

00555 {
00556   try
00557   {
00558     source_file_names_.push_front(file_name_);
00559     str_data_.push_front(data);
00560     return true;
00561   }
00562   catch(const std::exception& e) { msg_error_ = e.what(); }
00563   catch(...) { msg_error_ = ERRORCODES[0]; }
00564   return false;
00565 }


Friends And Related Function Documentation

istream& operator>> ( istream is,
ReadWriteCsv df 
) [friend]

Read the data from the stream and returns the stream when done.

Parameters:
is input stream
df the ReadWriteCsv to read

ostream& operator<< ( ostream os,
const ReadWriteCsv df 
) [friend]

write the data into the stream and returns the stream when done.

Parameters:
os output stream
df the ReadWriteCsv to write


Member Data Documentation

std::string STK::ReadWriteCsv::file_name_ [mutable, protected]

Definition at line 107 of file STK_ReadWriteCsv.h.

Referenced by fromVariable(), operator=(), push_back(), push_front(), readFile(), and writeFile().

bool STK::ReadWriteCsv::with_names_ [mutable, protected]

Definition at line 109 of file STK_ReadWriteCsv.h.

Referenced by operator=(), ReadWriteCsv(), setWithNames(), and writeSelection().

String STK::ReadWriteCsv::delimiter_ [mutable, protected]

Definition at line 111 of file STK_ReadWriteCsv.h.

Referenced by operator=(), ReadWriteCsv(), setDelimiters(), and writeSelection().

Integer STK::ReadWriteCsv::reserve_ [mutable, protected]

Definition at line 113 of file STK_ReadWriteCsv.h.

Referenced by operator=(), push_back(), ReadWriteCsv(), and setReserve().

std::string STK::ReadWriteCsv::msg_error_ [mutable, protected]

Array for the source file_names.

Definition at line 118 of file STK_ReadWriteCsv.h.

Referenced by clear(), eraseCols(), fromVariable(), getColIndex(), operator+=(), operator=(), push_back(), and push_front().


The documentation for this class was generated from the following files:

Generated on Fri Sep 25 10:31:00 2009 for STK++ by  doxygen 1.5.8