|
STK++ 1.0
|
the ReadWriteCsv class : allow to write and/or to read a csv file. More...
#include <STK_ReadWriteCsv.h>
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. | |
| ReadWriteCsv & | operator= (ReadWriteCsv const &df) |
| Assigns a ReadWriteCsv equal to another ReadWriteCsv. | |
| ReadWriteCsv & | operator+= (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). | |
| String & | operator() (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 | |
| istream & | operator>> (istream &is, ReadWriteCsv &df) |
| Read the data from the stream and returns the stream when done. | |
| ostream & | operator<< (ostream &os, ReadWriteCsv const &df) |
| write the data into the stream and returns the stream when done. | |
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.
| STK::ReadWriteCsv::ReadWriteCsv | ( | bool | read_names = true | ) |
The default constructor.
Instantiates an instance of ReadWriteCsv and initialize data members to default values.
| read_names | true 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+().
: file_name_("") , with_names_(read_names) , delimiter_(DEFAULT_DELIMITER) , reserve_(DEFAULT_RESERVE) , msg_error_("") { }
| 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.
| 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 delimiters |
| reserve | the 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.
| df | the 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();}
| 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).
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).
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.
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(); }
Returns the first index of the samples of the variable icol.
| icol | index of the variable |
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();}
Returns the last index of the samples of the variable icol.
| icol | index of 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();}
Returns the number of samples currently in the Variable icol.
| icol | index of 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 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().
| String const& STK::ReadWriteCsv::error | ( | ) | const [inline] |
Returns the last error encountered by the class.
Definition at line 166 of file STK_ReadWriteCsv.h.
References msg_error_.
{ return msg_error_; }
return the element with the index icol.
| icol | index 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); }
return the element with the index icol (const).
| icol | index 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); }
Returns a reference to the value at the specified location.
| icol | index 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); }
Returns a const reference to the value at the specified location.
| icol | index 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); }
return the first element.
Definition at line 194 of file STK_ReadWriteCsv.h.
References STK::ITContainer1D< TYPE, TContainer1D >::at(), first(), and str_data_.
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 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 the last element (const).
Definition at line 206 of file STK_ReadWriteCsv.h.
References STK::ITContainer1D< TYPE, TContainer1D >::at(), last(), and str_data_.
| 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_.
| file_name | name of the file to write |
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.
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_);
}
Attempts to add a column with the values contained in data.
| data | the column to push back |
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;
}
Attempts to add a column with the values contained in data.
| data | the column to push front |
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.
| variable_name | Name of the variable to search |
| iStartingIndex | first index |
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.
| variable_name | Name of the variable to search |
| sourceFilename | Name of the file to search |
| iStartingIndex | first index |
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);
}
Assigns the variable name at the specified index to rStr.
| icol | index of the variable |
| rStr | result name of the variable |
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;
}
Set the variable name name at the specified index.
| icol | index of the variable |
| name | name of the variable to set |
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.
| icol | index of the col |
| irow | index of the row |
| lpStr | returned value |
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.
| variable_name | name of the variable |
| irow | index of the row |
| lpStr | returned value |
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;
}
Assigns rVector with the data at the target variable.
| icol | index of the col |
| rVector | vector of the returned data |
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.
| variable_name | name of the variable |
| rVector | vector of the returned data |
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).
| delimiters | delimiters 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).
| with_names | true 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).
| reserve | number 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).
| icol | index of the col |
| irow | index of the row |
| value | the value to set |
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;
}
Attempts to append a data to the variable specified by icol.
| icol | index of the col |
| value | value to set |
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;
}
Attempts to append values from data to the variable specified by icol.
| icol | index of the col |
| data | values to set |
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.
| icol | index of the column to erase |
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.
| file_name | name of the file to read |
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.
| df | the 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.
| df | the 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_.
| ReadWriteCsv STK::ReadWriteCsv::operator+ | ( | ReadWriteCsv const & | df | ) | const |
Combines ReadWriteCsv(s)
| df | the 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.
| os | the 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).
| os | the output stream |
| top | the top index |
| bottom | th bottom index |
| left | the left index |
| right | the 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));
}
}
Returns a reference of the value specified by the given coordinates.
| icol | index of the col |
| irow | index 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.
| icol | index of the col |
| irow | index 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.
| name | name of the variable to find |
| offset | offset to apply |
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_.
| istream& operator>> | ( | istream & | is, |
| ReadWriteCsv & | df | ||
| ) | [friend] |
Read the data from the stream and returns the stream when done.
| is | input stream |
| df | the ReadWriteCsv to read |
| ostream& operator<< | ( | ostream & | os, |
| ReadWriteCsv const & | df | ||
| ) | [friend] |
write the data into the stream and returns the stream when done.
| os | output stream |
| df | the ReadWriteCsv to write |
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().
Array1D<std::string> STK::ReadWriteCsv::source_file_names_ [protected] |
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().
Array1D< Variable<String> > STK::ReadWriteCsv::str_data_ [protected] |
Array of array for the data.
Definition at line 426 of file STK_ReadWriteCsv.h.
Referenced by appendData(), at(), back(), clear(), data(), eraseColumn(), first(), firstVe(), front(), largestNumberOfRows(), last(), lastVe(), lookupVariableIndex(), name(), operator()(), operator+=(), STK::operator<<(), operator=(), STK::operator>>(), operator[](), push_back(), push_front(), setData(), setName(), size(), sizeVe(), write(), and writeSelection().