STK++ 1.0

STK::ReadWriteCsv Class Reference

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)
 The default constructor.
 ReadWriteCsv (std::string const &file_name, bool read_names=true, String const &delimiter=Csv::DEFAULT_DELIMITER, Integer const &reserve=Csv::DEFAULT_RESERVE)
 Misc.
 ReadWriteCsv (ReadWriteCsv const &df)
 Copy constructor.
 ~ReadWriteCsv (void)
 destructor : Calls clear() before the ReadWriteCsv is destroyed.
void clear ()
 Clears the data contained in a ReadWriteCsv and reclaims any allocated memory.
Integer first () const
 Returns the beginning of the container (should be 1).
Integer last () const
 Returns the end of the container (should be NumberOfCols).
Integer size () const
 Returns the number of variables currently in the ReadWriteCsv.
Integer firstVe (Integer const &icol) const
 Returns the first index of the samples of the variable icol.
Integer lastVe (Integer const &icol) const
 Returns the last index of the samples of the variable icol.
Integer sizeVe (Integer const &icol) const
 Returns the number of samples currently in the Variable icol.
Integer firstVe () const
 Returns the lower number of first index of samples.
Integer lastVe () const
 Returns the largest number of end index of samples.
String const & error () const
 Returns the last error encountered by the class.
Variable< String > & at (Integer icol)
 return the element with the index icol.
const Variable< String > & at (Integer icol) const
 return the element with the index icol (const).
Variable< String > & operator[] (Integer const &icol)
 Returns a reference to the value at the specified location.
const Variable< String > & operator[] (Integer const &icol) const
 Returns a const reference to the value at the specified location.
Variable< String > & front ()
 return the first element.
const Variable< String > & front () const
 return the first element (const).
Variable< String > & back ()
 return the last element.
const Variable< String > & back () const
 return the last element (const).
bool write (std::string const &file_name) const
 Attempts to write the ReadWriteCsv to the location specified by file_name using the delimiters specified by delimiter_.
bool read ()
 Reads the default file with the specified read flags.
bool push_back (const Variable< String > &data=Variable< String >())
 Attempts to add a column with the values contained in data.
bool push_front (const Variable< String > &data=Variable< String >())
 Attempts to add a column with the values contained in data.
Integer colIndex (const String &variable_name, Integer const &iStartingIndex=0) const
 Looks up the index of a variable given its name, starting at the specified index.
Integer colIndex (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.
Integer name (Integer const &icol, String &rStr) const
 Assigns the variable name at the specified index to rStr.
bool setName (Integer const &icol, String const &name)
 Set the variable name name at the specified index.
Integer data (Integer const &icol, Integer const &irow, String &lpStr) const
 Assigns lpStr with the data at the target variable.
Integer data (String const &variable_name, Integer const &irow, String &lpStr) const
 Assigns lpStr with the data at the target variable.
Integer data (Integer const &icol, Variable< String > &rVector) const
 Assigns rVector with the data at the target variable.
Integer data (String const &variable_name, Variable< String > &rVector) const
 Assigns rVector with the data at the target variable.
void setDelimiters (String const &delimiters) const
 Sets the delimiters to use for parsing data (delimiters_ is mutable).
void setWithNames (bool with_names=true) const
 Sets the with_names_ value for reading/writting variables names (with_names_ is mutable).
void setReserve (Integer const &reserve) const
 Sets the reserve value for data storage (reserve_ is mutable).
bool setData (Integer const &icol, Integer const &irow, String const &value)
 Attempts to set the specified value to the element (icol, irow).
bool appendData (Integer const &icol, String const &value)
 Attempts to append a data to the variable specified by icol.
bool appendData (Integer const &icol, const Variable< String > &data)
 Attempts to append values from data to the variable specified by icol.
bool eraseColumn (Integer const &icol)
 Deletes the variable whose index is icol from a ReadWriteCsv.
bool read (std::string const &file_name)
 Reads the specified file with the specified read flags.
ReadWriteCsvoperator= (ReadWriteCsv const &df)
 Assigns a ReadWriteCsv equal to another ReadWriteCsv.
ReadWriteCsvoperator+= (ReadWriteCsv const &df)
 Appends a ReadWriteCsv to another ReadWriteCsv.
ReadWriteCsv operator+ (ReadWriteCsv const &df) const
 Combines ReadWriteCsv(s)
void write (ostream &os) const
 Write the csv to an output stream.
void writeSelection (ostream &os, Integer const &top, Integer const &bottom, Integer const &left, Integer const &right) const
 Write to output stream a selection based on the coordinates passed (Think of it as highlighting cells in Excel).
Stringoperator() (Integer const &icol, Integer const &irow)
 Returns a reference of the value specified by the given coordinates.
String const & operator() (Integer const &icol, Integer const &irow) const
 Returns a const reference of the value specified by the given coordinates.

Protected Member Functions

Integer lookupVariableIndex (String const &name, Integer const &offset=0) const
 Protected member function for internal bookeeping.
Integer largestNumberOfRows () const
 Returns the largest number of samples.

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.
String msg_error_
 Contain the last error message.
Array1D< std::string > source_file_names_
 Array for the source file_names.
Array1D< Variable< String > > str_data_
 Array of array for the data.

Friends

istreamoperator>> (istream &is, ReadWriteCsv &df)
 Read the data from the stream and returns the stream when done.
ostreamoperator<< (ostream &os, ReadWriteCsv const &df)
 write the data into the stream and returns the stream when done.

Detailed Description

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

It is possible to merge two csv files and to extract subregion of the file too. All data are stored in a String format.

Definition at line 79 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_namestrue if we want ot read the names of the variables at the first line of the file

Definition at line 124 of file STK_ReadWriteCsv.cpp.

Referenced by operator+().

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

Misc.

constructor. Instantiates an instance of ReadWriteCsv with the specified read flags.

Parameters:
file_namename of the file to read/write
read_namestrue if we want ot read the names of the variables at the first line of the file
delimitera String of delimiters
reservethe place to reserve for the data

Definition at line 134 of file STK_ReadWriteCsv.cpp.

                          : file_name_(file_name)
                          , with_names_(read_names)
                          , delimiter_(delimiter)
                          , reserve_(reserve)
                          , msg_error_("")
{ }
STK::ReadWriteCsv::ReadWriteCsv ( ReadWriteCsv const &  df)

Copy constructor.

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

Parameters:
dfthe ReadWriteCsv to copy

Definition at line 148 of file STK_ReadWriteCsv.cpp.

References df().

{ *this = df;}
STK::ReadWriteCsv::~ReadWriteCsv ( void  )

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

Definition at line 152 of file STK_ReadWriteCsv.cpp.

References clear().

{ clear();}

Member Function Documentation

void STK::ReadWriteCsv::clear ( )

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

Definition at line 423 of file STK_ReadWriteCsv.cpp.

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

Referenced by STK::operator>>(), and ~ReadWriteCsv().

{
  msg_error_ = "";
  source_file_names_.clear();
  str_data_.clear();
}
Integer STK::ReadWriteCsv::first ( ) const [inline]

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

Returns:
the index of the first variable.

Definition at line 121 of file STK_ReadWriteCsv.h.

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

Referenced by firstVe(), front(), lastVe(), STK::ImportFromCsv::runNumeric(), STK::ImportFromCsv::runOnlyNumeric(), STK::ImportFromCsv::runString(), STK::ExportToCsv::setColumnsNames(), and write().

    { return str_data_.first(); }
Integer STK::ReadWriteCsv::last ( ) const [inline]

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

Returns:
the index of the last variable

Definition at line 127 of file STK_ReadWriteCsv.h.

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

Referenced by back(), firstVe(), lastVe(), STK::ImportFromCsv::runNumeric(), STK::ImportFromCsv::runOnlyNumeric(), STK::ImportFromCsv::runString(), STK::ExportToCsv::setColumnsNames(), and write().

    { return str_data_.last(); }
Integer STK::ReadWriteCsv::size ( ) const [inline]

Returns the number of variables currently in the ReadWriteCsv.

Returns:
The current number of variables of the ReadWriteCsv

Definition at line 133 of file STK_ReadWriteCsv.h.

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

Referenced by largestNumberOfRows(), and STK::operator>>().

    { return str_data_.size(); }
Integer STK::ReadWriteCsv::firstVe ( Integer const &  icol) const [inline]

Returns the first index of the samples of the variable icol.

Parameters:
icolindex of the variable
Returns:
the first index in the column icol

Definition at line 140 of file STK_ReadWriteCsv.h.

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

Referenced by STK::operator<<().

     { return str_data_.at(icol).first();}
Integer STK::ReadWriteCsv::lastVe ( Integer const &  icol) const [inline]

Returns the last index of the samples of the variable icol.

Parameters:
icolindex of the column
Returns:
the last index in the column icol

Definition at line 147 of file STK_ReadWriteCsv.h.

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

Referenced by STK::operator<<().

     { return str_data_.at(icol).last();}
Integer STK::ReadWriteCsv::sizeVe ( Integer const &  icol) const [inline]

Returns the number of samples currently in the Variable icol.

Parameters:
icolindex of the column
Returns:
the number of element in the column icol

Definition at line 154 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol).size();}
Integer STK::ReadWriteCsv::firstVe ( ) const

Returns the lower number of first index of samples.

Definition at line 411 of file STK_ReadWriteCsv.cpp.

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

Referenced by write().

{
  Integer retVal = Arithmetic<Integer> ::max();

  const Integer first = str_data_.first(), last = str_data_.last();
  for (Integer i=first; i<=last; i++)
    retVal = min(retVal, firstVe(i));

  return retVal;
}
Integer STK::ReadWriteCsv::lastVe ( ) const

Returns the largest number of end index of samples.

Definition at line 398 of file STK_ReadWriteCsv.cpp.

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

Referenced by write().

{
  Integer retVal = Arithmetic<Integer> ::min();

  const Integer first = str_data_.first(), last = str_data_.last();
  for (Integer i=first; i<=last; i++)
    retVal = max(retVal, lastVe(i));

  return retVal;
}
String const& STK::ReadWriteCsv::error ( ) const [inline]

Returns the last error encountered by the class.

Returns:
the last error encountered

Definition at line 166 of file STK_ReadWriteCsv.h.

References msg_error_.

    { return msg_error_; }
Variable<String>& STK::ReadWriteCsv::at ( Integer  icol) [inline]

return the element with the index icol.

Parameters:
icolindex of the col

Definition at line 172 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol); }
const Variable<String>& STK::ReadWriteCsv::at ( Integer  icol) const [inline]

return the element with the index icol (const).

Parameters:
icolindex of the col

Definition at line 178 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol); }
Variable<String>& STK::ReadWriteCsv::operator[] ( Integer const &  icol) [inline]

Returns a reference to the value at the specified location.

Parameters:
icolindex of the col

Definition at line 184 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol); }
const Variable<String>& STK::ReadWriteCsv::operator[] ( Integer const &  icol) const [inline]

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

Parameters:
icolindex of the col

Definition at line 190 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol); }
Variable<String>& STK::ReadWriteCsv::front ( ) [inline]

return the first element.

Definition at line 194 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(first());}
const Variable<String>& STK::ReadWriteCsv::front ( ) const [inline]

return the first element (const).

Definition at line 198 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(first());}
Variable<String>& STK::ReadWriteCsv::back ( ) [inline]

return the last element.

Definition at line 202 of file STK_ReadWriteCsv.h.

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

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

    { return str_data_.at(last());}
const Variable<String>& STK::ReadWriteCsv::back ( ) const [inline]

return the last element (const).

Definition at line 206 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(last());}
bool STK::ReadWriteCsv::write ( std::string const &  file_name) const

Attempts to write the ReadWriteCsv to the location specified by file_name using the delimiters specified by delimiter_.

Parameters:
file_namename of the file to write
Returns:
true if successful, false if an error is encountered.

Definition at line 265 of file STK_ReadWriteCsv.cpp.

References STK::ERRORCODES, file_name_, STK::ITContainer1D< TYPE, TContainer1D >::first(), firstVe(), STK::ITContainer1D< TYPE, TContainer1D >::last(), lastVe(), msg_error_, str_data_, and writeSelection().

Referenced by STK::out2D().

{
  file_name_  = file_name;
  try
  {
    ofstream os(file_name.c_str());
    writeSelection( os
                  , firstVe()
                  , lastVe()
                  , str_data_.first()
                  , str_data_.last()
                  );
    os.close();
    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...) { msg_error_ = ERRORCODES[0]; }
  return false;
}
bool STK::ReadWriteCsv::read ( )

Reads the default file with the specified read flags.

Returns:
true if successful, false if an error is encountered.

Definition at line 221 of file STK_ReadWriteCsv.cpp.

References file_name_.

{
  return read(file_name_);
}
bool STK::ReadWriteCsv::push_back ( const Variable< String > &  data = Variable<String>())

Attempts to add a column with the values contained in data.

Parameters:
datathe column to push back
Returns:
true if successful, false if an error is encountered.

Definition at line 579 of file STK_ReadWriteCsv.cpp.

References STK::ITContainer1D< TYPE, TContainer1D >::back(), STK::ERRORCODES, 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(), and STK::operator>>().

{
  try
  {
    source_file_names_.push_back(file_name_);
    str_data_.push_back(data);
    str_data_.back().reserve(reserve_);
    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...) { msg_error_ = ERRORCODES[0]; }
  return false;
}
bool STK::ReadWriteCsv::push_front ( const Variable< String > &  data = Variable<String>())

Attempts to add a column with the values contained in data.

Parameters:
datathe column to push front
Returns:
true if successful, false if an error is encountered.

Definition at line 596 of file STK_ReadWriteCsv.cpp.

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

{
  try
  {
    source_file_names_.push_front(file_name_);
    str_data_.push_front(data);
    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...) { msg_error_ = ERRORCODES[0]; }
  return false;
}
Integer STK::ReadWriteCsv::colIndex ( const String variable_name,
Integer const &  iStartingIndex = 0 
) const

Looks up the index of a variable given its name, starting at the specified index.

Parameters:
variable_nameName of the variable to search
iStartingIndexfirst index
Returns:
the index if successful, -1 if an error is encountered.

Definition at line 517 of file STK_ReadWriteCsv.cpp.

References lookupVariableIndex().

{
  return lookupVariableIndex(variable_name, iStartingIndex);
}
Integer STK::ReadWriteCsv::colIndex ( 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.

Parameters:
variable_nameName of the variable to search
sourceFilenameName of the file to search
iStartingIndexfirst index
Returns:
the index if successful, -1 if an error is encountered.

Definition at line 526 of file STK_ReadWriteCsv.cpp.

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

{
  Integer it = source_file_names_.first();
  while (it <= source_file_names_.last())
  { if (source_file_names_[it] == sourceFilename) break;
    it++;
  }

  if(it == source_file_names_.last()+1) // sourceFilename was not found
  {
    msg_error_ = ERRORCODES[6];
    return -1;
  }

  Integer offset = source_file_names_.first();
  while(it != source_file_names_.first())
  {
    offset++;
    it--;
  }
  
  return lookupVariableIndex(variable_name, iStartingIndex+offset);
}
Integer STK::ReadWriteCsv::name ( Integer const &  icol,
String rStr 
) const

Assigns the variable name at the specified index to rStr.

Parameters:
icolindex of the variable
rStrresult name of the variable
Returns:
the new length of rStr if successful, -1 if an error is encountered.

Definition at line 357 of file STK_ReadWriteCsv.cpp.

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

Referenced by lookupVariableIndex().

{
  try
  {
    rStr = str_data_.at(icol).name();
    return static_cast<Integer> (rStr.length());
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...)  { msg_error_ = ERRORCODES[1]; }
  return -1;
}
bool STK::ReadWriteCsv::setName ( Integer const &  icol,
String const &  name 
)

Set the variable name name at the specified index.

Parameters:
icolindex of the variable
namename of the variable to set
Returns:
true if successful, false if an error is encountered.

Definition at line 374 of file STK_ReadWriteCsv.cpp.

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

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

{
  try
  {
    str_data_.at(icol).setName(name);
    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...)  { msg_error_ = ERRORCODES[1]; }
  return false;
}
Integer STK::ReadWriteCsv::data ( Integer const &  icol,
Integer const &  irow,
String lpStr 
) const

Assigns lpStr with the data at the target variable.

Parameters:
icolindex of the col
irowindex of the row
lpStrreturned value
Returns:
the new length of lpStr. Returns -1 if an error is encountered.

Definition at line 432 of file STK_ReadWriteCsv.cpp.

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

Referenced by data().

{
  Integer retVal = 0;

  try
  {
    lpStr  = str_data_.at(icol).at(irow);
    retVal = static_cast<Integer> (lpStr.length());
  }
  catch(const std::exception& e) 
  {  
    msg_error_ = e.what(); 
    retVal = -1;
  }
  catch(...) // other exceptions
  { 
    msg_error_ = ERRORCODES[1]; 
    retVal = -1;
  }
  return retVal;
}
Integer STK::ReadWriteCsv::data ( String const &  variable_name,
Integer const &  irow,
String lpStr 
) const

Assigns lpStr with the data at the target variable.

Parameters:
variable_namename of the variable
irowindex of the row
lpStrreturned value
Returns:
the new length of lpStr. Returns -1 if an error is encountered.

Definition at line 458 of file STK_ReadWriteCsv.cpp.

References data(), STK::ERRORCODES, lookupVariableIndex(), and msg_error_.

{
  Integer retVal = 0;
  Integer iVar   = lookupVariableIndex(variable_name); // find col
  
  if(iVar != -1)
    retVal = data(iVar, irow, rStr);
  else
  {
    msg_error_ = ERRORCODES[5]; 
    retVal = -1;
  }
  return retVal;
}
Integer STK::ReadWriteCsv::data ( Integer const &  icol,
Variable< String > &  rVector 
) const

Assigns rVector with the data at the target variable.

Parameters:
icolindex of the col
rVectorvector of the returned data
Returns:
the new length of lpStr. Returns -1 if an error is encountered.

Definition at line 478 of file STK_ReadWriteCsv.cpp.

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

{
  Integer retVal = 0;

  try
  {
    rVector = str_data_.at(icol);
    retVal  = rVector.size();
  }
  catch(const std::exception& e) 
  {  
    msg_error_ = e.what();
    retVal = -1;
  }
  catch(...) 
  { 
    msg_error_ = ERRORCODES[2]; 
    retVal = -1;
  }
  return retVal;
}
Integer STK::ReadWriteCsv::data ( String const &  variable_name,
Variable< String > &  rVector 
) const

Assigns rVector with the data at the target variable.

Parameters:
variable_namename of the variable
rVectorvector of the returned data
Returns:
the new length of lpStr. Returns -1 if an error is encountered.

Definition at line 502 of file STK_ReadWriteCsv.cpp.

References data(), STK::ERRORCODES, lookupVariableIndex(), and msg_error_.

{
  Integer index = lookupVariableIndex(variable_name);
  
  if(index != -1)
    return data(index, rVector);
  else
    msg_error_ = ERRORCODES[5]; 
  return -1;
}
void STK::ReadWriteCsv::setDelimiters ( String const &  delimiters) const [inline]

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

Parameters:
delimitersdelimiters to use

Definition at line 308 of file STK_ReadWriteCsv.h.

References delimiter_.

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

    { delimiter_ = delimiters; }
void STK::ReadWriteCsv::setWithNames ( bool  with_names = true) const [inline]

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

Parameters:
with_namestrue if we want to read the names of the variables

Definition at line 316 of file STK_ReadWriteCsv.h.

References with_names_.

    { with_names_ = with_names; }
void STK::ReadWriteCsv::setReserve ( Integer const &  reserve) const [inline]

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

Parameters:
reservenumber of place to reserve

Definition at line 322 of file STK_ReadWriteCsv.h.

References reserve_.

    { reserve_ = reserve; }
bool STK::ReadWriteCsv::setData ( Integer const &  icol,
Integer const &  irow,
String const &  value 
)

Attempts to set the specified value to the element (icol, irow).

Parameters:
icolindex of the col
irowindex of the row
valuethe value to set
Returns:
true if successful, false if an error is encountered.

Definition at line 309 of file STK_ReadWriteCsv.cpp.

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

{
  try
  {
    str_data_.at(icol).at(irow) = value;
    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...) { msg_error_ = ERRORCODES[0]; }
  return false;
}
bool STK::ReadWriteCsv::appendData ( Integer const &  icol,
String const &  value 
)

Attempts to append a data to the variable specified by icol.

Parameters:
icolindex of the col
valuevalue to set
Returns:
true if successful, false if an error is encountered.

Definition at line 323 of file STK_ReadWriteCsv.cpp.

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

Referenced by STK::operator>>().

{
  try
  {
    if (Arithmetic<String>::isNA(value))
      str_data_[icol].push_back(Arithmetic<String>::NA());
    else
      str_data_[icol].push_back(value);
    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...) { msg_error_ = ERRORCODES[0]; }
  return false;
}
bool STK::ReadWriteCsv::appendData ( Integer const &  icol,
const Variable< String > &  data 
)

Attempts to append values from data to the variable specified by icol.

Parameters:
icolindex of the col
datavalues to set
Returns:
true if successful, false if an error is encountered.
bool STK::ReadWriteCsv::eraseColumn ( Integer const &  icol)

Deletes the variable whose index is icol from a ReadWriteCsv.

Parameters:
icolindex of the column to erase
Returns:
true if successful, false if an error is encountered.

Definition at line 340 of file STK_ReadWriteCsv.cpp.

References STK::RecursiveArray1D< TYPE, Container1D >::erase(), STK::ERRORCODES, msg_error_, source_file_names_, and str_data_.

{
  try
  {
    // delete the variable from source_file_names_
    source_file_names_.erase(icol),

    // delete the variable from str_data_
    str_data_.erase(icol);

    return true;
  }
  catch(const std::exception& e) { msg_error_ = e.what(); }
  catch(...) { msg_error_ = ERRORCODES[0]; }
  return false;
}
bool STK::ReadWriteCsv::read ( std::string const &  file_name)

Reads the specified file with the specified read flags.

Parameters:
file_namename of the file to read
Returns:
true if successful, false if an error is encountered.

Definition at line 227 of file STK_ReadWriteCsv.cpp.

References STK::ERRORCODES, file_name_, and msg_error_.

{
  try
  {
    // update file_name
    file_name_ = file_name;
    // input file stream
    ifstream inFile;
    // open file
    inFile.open(file_name.c_str());
    // check error
    if (inFile.rdstate() & std::ios::failbit)
    {
      inFile.close();
      msg_error_ = ERRORCODES[4]; 
      msg_error_ += "\nFile: " + file_name;
      return false;
    }
    // read file
    inFile >> *this;
    // close file
    inFile.close();
    return true;
  }
  catch(const std::exception& e)
  {
    msg_error_ = e.what();
    msg_error_ += "\nIn ReadWriteCsv::read(" + file_name + ")";
  }
  catch(...)
  {
    msg_error_ = ERRORCODES[0];
    msg_error_ += "\nIn ReadWriteCsv::read(" + file_name + ")";
  }

  return false;
}
ReadWriteCsv & STK::ReadWriteCsv::operator= ( ReadWriteCsv const &  df)

Assigns a ReadWriteCsv equal to another ReadWriteCsv.

Parameters:
dfthe ReadWriteCsv to copy

Definition at line 156 of file STK_ReadWriteCsv.cpp.

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

{
  delimiter_         = df.delimiter_;
  reserve_           = df.reserve_;
  with_names_        = df.with_names_;
  file_name_         = df.file_name_;
  msg_error_         = df.msg_error_;
  source_file_names_ = df.source_file_names_;
  str_data_          = df.str_data_;

  return *this;
}
ReadWriteCsv & STK::ReadWriteCsv::operator+= ( ReadWriteCsv const &  df)

Appends a ReadWriteCsv to another ReadWriteCsv.

Parameters:
dfthe ReadWriteCsv to append

Definition at line 287 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_.

{
  for ( Integer i=df.source_file_names_.first()
      ; i<=df.source_file_names_.last()
      ; i++)
    source_file_names_.push_back(df.source_file_names_[i]);

  for ( Integer i=df.str_data_.first()
      ; i<=df.str_data_.last()
      ; i++)
    str_data_.push_back(df.str_data_[i]);

  return *this;
}
ReadWriteCsv STK::ReadWriteCsv::operator+ ( ReadWriteCsv const &  df) const

Combines ReadWriteCsv(s)

Parameters:
dfthe ReadWriteCsv to add

Definition at line 303 of file STK_ReadWriteCsv.cpp.

References df(), and ReadWriteCsv().

{ // copy this, add df and return the result
  return ReadWriteCsv((*this)) += df;
}
void STK::ReadWriteCsv::write ( ostream os) const

Write the csv to an output stream.

Parameters:
osthe output stream

Definition at line 214 of file STK_ReadWriteCsv.cpp.

References first(), firstVe(), last(), lastVe(), and writeSelection().

{
  writeSelection(os, firstVe(), lastVe(), first(), last());   
}
void STK::ReadWriteCsv::writeSelection ( ostream os,
Integer const &  top,
Integer const &  bottom,
Integer const &  left,
Integer const &  right 
) const

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

Parameters:
osthe output stream
topthe top index
bottomth bottom index
leftthe left index
rightthe right index

Definition at line 170 of file STK_ReadWriteCsv.cpp.

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

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

{
  // create a vector for the format of the output
  Array1D<Integer>  format(Range(left, right), 0);
  // for each var, find the largest size
  for(Integer iVar=left; iVar<=right; iVar++)
  {
    format.at(iVar)   = maxLength(str_data_.at(iVar));
    if (with_names_)
      format.at(iVar) = max( format.at(iVar)
                           , (Integer )str_data_.at(iVar).name().size());
  }  
  // write if needed names variables
  if (with_names_)
    for(Integer iVar=left; iVar<=right; iVar++)
    {
      os << std::setw(format[iVar]) << std::right
         << ConstProxy<String>(str_data_.at(iVar).name())
         << ((iVar==right) ? _T('\n') : delimiter_.at(0));
    }
  
  // write data
  for(Integer irow = top; irow<=bottom; irow++)
    for(Integer iVar = left; iVar<=right; iVar++)
    {
      try 
      {
        os << std::setw(format[iVar]) << std::right
           << ConstProxy<String>(str_data_.at(iVar).at(irow));
      }
      catch(...)
      {
        // if an error occur, we put NA value
        os << std::setw(format[iVar]) << std::right << STRING_NA;
      }
      os << ((iVar==right) ? _T('\n') : delimiter_.at(0));
    }
}
String& STK::ReadWriteCsv::operator() ( Integer const &  icol,
Integer const &  irow 
) [inline]

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

Parameters:
icolindex of the col
irowindex of thhe row

Definition at line 399 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol).at(irow);}
String const& STK::ReadWriteCsv::operator() ( Integer const &  icol,
Integer const &  irow 
) const [inline]

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

Parameters:
icolindex of the col
irowindex of the row

Definition at line 407 of file STK_ReadWriteCsv.h.

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

    { return str_data_.at(icol).at(irow);}
Integer STK::ReadWriteCsv::lookupVariableIndex ( String const &  name,
Integer const &  offset = 0 
) const [protected]

Protected member function for internal bookeeping.

Parameters:
namename of the variable to find
offsetoffset to apply
Returns:
-1 if the variable is not found

Definition at line 555 of file STK_ReadWriteCsv.cpp.

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

Referenced by colIndex(), and data().

{
  Integer it = str_data_.first() + offset;
  while (it <= str_data_.last())
  { if (str_data_[it].name() == variable_name) break;
    it++;
  }

  if(it == str_data_.last()+1) // variable Name was not found
    return -1;

  Integer retVal = str_data_.first();
  while(it != str_data_.first())
  {
    retVal++;
    it--;
  }
  return retVal;
}
Integer STK::ReadWriteCsv::largestNumberOfRows ( ) const [protected]

Returns the largest number of samples.

Definition at line 386 of file STK_ReadWriteCsv.cpp.

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

{
  Integer retVal = 0;

  for (Integer i=str_data_.first(); i<=str_data_.last(); i++)
    retVal = max(retVal, str_data_[i].size());

  return retVal;
}

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:
isinput stream
dfthe ReadWriteCsv to read
ostream& operator<< ( ostream os,
ReadWriteCsv const &  df 
) [friend]

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

Parameters:
osoutput stream
dfthe ReadWriteCsv to write

Member Data Documentation

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

Name of the Current file read.

Definition at line 413 of file STK_ReadWriteCsv.h.

Referenced by operator=(), STK::operator>>(), push_back(), push_front(), read(), and write().

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

Read and Write names of the variables.

Definition at line 415 of file STK_ReadWriteCsv.h.

Referenced by operator=(), STK::operator>>(), setWithNames(), and writeSelection().

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

Delimiter(s)

Definition at line 417 of file STK_ReadWriteCsv.h.

Referenced by operator=(), STK::operator>>(), setDelimiters(), and writeSelection().

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

Size of the buffer.

Definition at line 419 of file STK_ReadWriteCsv.h.

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

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

Contain the last error message.

Definition at line 421 of file STK_ReadWriteCsv.h.

Referenced by appendData(), clear(), colIndex(), data(), eraseColumn(), error(), name(), operator=(), STK::operator>>(), push_back(), push_front(), read(), setData(), setName(), and write().

Array for the source file_names.

Definition at line 424 of file STK_ReadWriteCsv.h.

Referenced by clear(), colIndex(), eraseColumn(), operator+=(), operator=(), STK::operator>>(), push_back(), and push_front().


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