STK++ 0.9.13
STK::TReadWriteCsv< Type > Class Template Reference

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

#include <STK_ReadWriteCsv.h>

Public Types

typedef Variable< Type > Var
 

Public Member Functions

 TReadWriteCsv ()
 The default constructor.
 
 TReadWriteCsv (std::string const &file_name, bool read_names=Csv::DEFAULT_READNAMES, String const &delimiters=Csv::DEFAULT_DELIMITER)
 Constructor with a specified file name.
 
 TReadWriteCsv (TReadWriteCsv const &rw)
 Copy constructor.
 
 ~TReadWriteCsv ()
 destructor.
 
void clear ()
 reset all fields to default value and reclaims any allocated memory.
 
int begin () const
 
int end () const
 
int size () const
 
Range cols () const
 
int beginCols () const
 
int endCols () const
 
int sizeCols () const
 
int firstRow (int icol) const
 
int endRow (int icol) const
 
int lastRow (int icol) const
 
int sizeRow (int icol) const
 
int beginRows () const
 
int endRows () const
 
int sizeRows () const
 
int lastIdxRows () const
 
Range rows () const
 
int lastIdx () const
 
int lastIdxCols () const
 
String consterror () const
 
String constdelimiters () const
 
bool withNames () const
 
void setDelimiters (String const &delimiters) const
 Sets the delimiters to use for parsing data (delimiters_ is mutable).
 
void setWithNames (bool with_names) const
 Sets the with_names_ value for reading/writing variables names.
 
void setWithMapping (bool with_mapping) const
 Sets the with_mapping_ value for reading/writing variables names.
 
void setLast_nl (bool with_last_nl) const
 Sets the with_last_nl_ value for writing a new line after the last line.
 
void setReserve (int const &reserve) const
 Sets the reserve value for data storage (reserve_ is mutable).
 
void setInMapping (std::map< String, Type > const &imapping)
 Set mapping to used when reading.
 
void setOutMapping (std::map< String, Type > const &omapping)
 Set mapping to used when writting.
 
bool setName (int const &icol, String const &name)
 Set the variable name name at the specified index.
 
Type & operator() (int const &iRow, int const &iCol)
 
Type const operator() (int const &iRow, int const &iCol) const
 
Varclone (int icol) const
 
Varvar (int j)
 
Var constvar (int icol) const
 
Varat (int icol)
 
Var constat (int icol) const
 
Varoperator[] (int const &icol)
 
Var const operator[] (int const &icol) const
 @ return a const reference to the value at the specified location.
 
Varfront ()
 
Var constfront () const
 
Varback ()
 
Var constback () const
 
bool push_back (Var const &data=Var())
 Attempts to add a column with the values contained in data.
 
bool push_front (Var const &data=Var())
 Attempts to add a column with the values contained in data.
 
void resize (int sizeRows, int sizeCols)
 resize the container str_data_.
 
bool appendData (int const &icol, Type const &value)
 Attempts to append a data to the variable specified by icol.
 
bool eraseColumn (int const &icol)
 Deletes the variable whose index is icol from a TReadWriteCsv.
 
TReadWriteCsvoperator= (TReadWriteCsv const &rw)
 Assigns a TReadWriteCsv equal to another TReadWriteCsv.
 
TReadWriteCsvoperator+= (TReadWriteCsv const &rw)
 Appends a TReadWriteCsv to another TReadWriteCsv.
 
TReadWriteCsv operator+ (TReadWriteCsv const &rw) const
 Combines TReadWriteCsv(s)
 
template<class Array >
void exporter (ArrayBase< Array > &array) const
 export to an array of the same Type
 
bool read ()
 Reads the default file with the specified read flags.
 
bool read (std::map< String, Type > const &mapping)
 Reads the default file with the specified read flags using a map.
 
bool read (std::string const &file_name)
 Reads the specified file with the specified read flags.
 
bool read (istream &inBuffer)
 Reads the specified input stream with the specified read flags.
 
bool write ()
 write to the default file with the specified write flags.
 
bool write (std::string const &file_name) const
 Attempts to write the TReadWriteCsv to the location specified by file_name using the delimiters specified by delimiters_.
 
void write (ostream &os) const
 Write the csv to an output stream.
 
void writeSelection (ostream &os, int top, int bottom, int left, int right) const
 Write to output stream a selection based on the coordinates passed (Think of it as highlighting cells in Excel).
 

Protected Member Functions

int countFields (String const &line, Array1D< Char > &listDelimiters)
 Counts the number of columns in a line stored in a String and return the position of the delimiters and its types.
 
int readCurrentLine (istream &inBuffer, String &currentLine, Array1D< Char > &listDelimiters)
 Read the current line from the buffer.
 
void readNames (istream &inBuffer)
 Read the names of rhe Csv file.
 
void initRead (istream &inBuffer)
 Initial read.
 
void fastSetData (int const &icol, int const &irow, Type const &value)
 Set without check the specified value to the element (icol, irow).
 
void resizeRows (Range const &rows)
 resize the TReadWriteCsv to the given range of rows.
 
void resizeCols (Range const &cols)
 resize the TReadWriteCsv to the given range of columns.
 

Protected Attributes

std::string file_name_
 Name of the Current file read.
 
bool with_names_
 Read or Write names of the variables.
 
bool with_mapping_
 map the values stored
 
bool with_last_nl_
 write a new line character at the end of the output
 
String delimiters_
 Delimiter(s)
 
int reserve_
 Size of the buffer.
 
String msg_error_
 Contain the last error message.
 
Array1D< Varstr_data_
 Array of array for the data.
 
std::map< String, Type > imapping_
 mapping String -> Type for (input)
 
std::map< Type, Stringomapping_
 mapping Type -> String (output)
 
int nbVars_
 The number of variables we manage.
 
int nbRows_
 The number of rows we manage.
 

Friends

istreamoperator>> (istream &in, TReadWriteCsv &rw)
 overload operator >> for rw
 
ostreamoperator<< (ostream &os, TReadWriteCsv const &rw)
 overload operator << for rw
 

Detailed Description

template<typename Type>
class STK::TReadWriteCsv< Type >

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

It is possible to merge two csv files and to extract sub-region of the file too. All data are stored in a Type format. if there exists mixed types in the file you want to read/write, used the String format and use the import/export utilities classes

See also
ExportToCsv, ImportFromCsv.

It is possible to use a mapping in order to write/read the data using an other encoding.

Definition at line 107 of file STK_ReadWriteCsv.h.

Member Typedef Documentation

◆ Var

template<typename Type >
typedef Variable<Type> STK::TReadWriteCsv< Type >::Var

Definition at line 110 of file STK_ReadWriteCsv.h.

Constructor & Destructor Documentation

◆ TReadWriteCsv() [1/3]

template<typename Type >
STK::TReadWriteCsv< Type >::TReadWriteCsv ( )
inline

The default constructor.

Instantiates an instance of TReadWriteCsv and initialize data members to default values.

Definition at line 114 of file STK_ReadWriteCsv.h.

114 : file_name_()
115 , with_names_(Csv::DEFAULT_READNAMES)
116 , with_mapping_(Csv::DEFAULT_MAPPING)
117 , with_last_nl_(Csv::DEFAULT_LAST_NL)
118 , delimiters_(Csv::DEFAULT_DELIMITER)
119 , reserve_(Csv::DEFAULT_RESERVE)
120 , msg_error_()
121 , nbVars_(0)
122 , nbRows_(0)
123
124 {}
std::string file_name_
Name of the Current file read.
String msg_error_
Contain the last error message.
int reserve_
Size of the buffer.
String delimiters_
Delimiter(s)
int nbVars_
The number of variables we manage.
int nbRows_
The number of rows we manage.
bool with_mapping_
map the values stored
bool with_names_
Read or Write names of the variables.
bool with_last_nl_
write a new line character at the end of the output

Referenced by STK::TReadWriteCsv< Type >::operator+().

◆ TReadWriteCsv() [2/3]

template<typename Type >
STK::TReadWriteCsv< Type >::TReadWriteCsv ( std::string const file_name,
bool  read_names = Csv::DEFAULT_READNAMES,
String const delimiters = Csv::DEFAULT_DELIMITER 
)
inline

Constructor with a specified file name.

Instantiates an instance of TReadWriteCsv with the specified read flags.

Parameters
file_namename of the file to read/write
read_namestrue if we want read/write the names of the variables at the first line of the file
delimitersThe delimiters used in the fileo(if read) or the delimiter to use (if write)

Definition at line 133 of file STK_ReadWriteCsv.h.

137 : file_name_(file_name)
138 , with_names_(read_names)
139 , with_mapping_(Csv::DEFAULT_MAPPING)
140 , with_last_nl_(Csv::DEFAULT_LAST_NL)
142 , reserve_(Csv::DEFAULT_RESERVE)
143 , msg_error_()
144 , nbVars_(0)
145 , nbRows_(0)
146 {}
String const & delimiters() const

◆ TReadWriteCsv() [3/3]

template<typename Type >
STK::TReadWriteCsv< Type >::TReadWriteCsv ( TReadWriteCsv< Type > const rw)
inline

Copy constructor.

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

Parameters
rwthe TReadWriteCsv to copy

Definition at line 151 of file STK_ReadWriteCsv.h.

151{ *this = rw;}

◆ ~TReadWriteCsv()

template<typename Type >
STK::TReadWriteCsv< Type >::~TReadWriteCsv ( )
inline

destructor.

Definition at line 153 of file STK_ReadWriteCsv.h.

153{}

Member Function Documentation

◆ appendData()

template<typename Type >
bool STK::TReadWriteCsv< Type >::appendData ( int const icol,
Type const value 
)
inline

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 383 of file STK_ReadWriteCsv.h.

384 {
385 try
386 {
387 if (Arithmetic<Type>::isNA(value))
388 str_data_[icol].push_back(Arithmetic<Type>::NA());
389 else
390 str_data_[icol].push_back(value);
391 return true;
392 }
393 catch(const Exception& e) { msg_error_ = e.error(); }
394 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
395 return false;
396 }
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
Array1D< Var > str_data_
Array of array for the data.
static Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)

References STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::str_data_.

◆ at() [1/2]

template<typename Type >
Var & STK::TReadWriteCsv< Type >::at ( int  icol)
inline
Returns
the element with the index icol.
Parameters
icolindex of the col

Definition at line 317 of file STK_ReadWriteCsv.h.

317{ return str_data_.at(icol); }

References STK::TReadWriteCsv< Type >::str_data_.

◆ at() [2/2]

template<typename Type >
Var const & STK::TReadWriteCsv< Type >::at ( int  icol) const
inline
Returns
a constant reference of the icol variable.
Parameters
icolindex of the col

Definition at line 321 of file STK_ReadWriteCsv.h.

321{ return str_data_.at(icol); }

References STK::TReadWriteCsv< Type >::str_data_.

◆ back() [1/2]

template<typename Type >
Var & STK::TReadWriteCsv< Type >::back ( )
inline
Returns
the last variable.

Definition at line 335 of file STK_ReadWriteCsv.h.

335{ return str_data_.at(lastIdx());}

References STK::TReadWriteCsv< Type >::lastIdx(), and STK::TReadWriteCsv< Type >::str_data_.

◆ back() [2/2]

template<typename Type >
Var const & STK::TReadWriteCsv< Type >::back ( ) const
inline
Returns
the last variable (const).

Definition at line 337 of file STK_ReadWriteCsv.h.

337{ return str_data_.at(lastIdx());}

References STK::TReadWriteCsv< Type >::lastIdx(), and STK::TReadWriteCsv< Type >::str_data_.

◆ begin()

◆ beginCols()

template<typename Type >
int STK::TReadWriteCsv< Type >::beginCols ( ) const
inline
Returns
the index of the first variable (should be baseIdx).

Definition at line 175 of file STK_ReadWriteCsv.h.

175{ return str_data_.begin(); }

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::exporter().

◆ beginRows()

template<typename Type >
int STK::TReadWriteCsv< Type >::beginRows ( ) const
inline
Returns
the first index of the samples.

Definition at line 198 of file STK_ReadWriteCsv.h.

199 {
200 if (size()<= 0) return baseIdx;
201 int retVal = firstRow(begin());
202 for (int i=begin()+1; i<=lastIdx(); i++)
203 { retVal = std::min(retVal, firstRow(i));}
204 return retVal;
205 }
int firstRow(int icol) const
const int baseIdx
base index of the containers created in STK++.

References STK::baseIdx, STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::firstRow(), STK::TReadWriteCsv< Type >::lastIdx(), and STK::TReadWriteCsv< Type >::size().

Referenced by STK::TReadWriteCsv< Type >::exporter(), STK::TReadWriteCsv< Type >::rows(), STK::TReadWriteCsv< Type >::write(), and STK::TReadWriteCsv< Type >::write().

◆ clear()

template<typename Type >
void STK::TReadWriteCsv< Type >::clear ( )
inline

reset all fields to default value and reclaims any allocated memory.

Definition at line 155 of file STK_ReadWriteCsv.h.

156 {
157 msg_error_.clear();
158 str_data_.clear();
159 with_names_ = Csv::DEFAULT_READNAMES;
160 with_mapping_ = Csv::DEFAULT_MAPPING;
161 with_last_nl_ = Csv::DEFAULT_LAST_NL;
162 delimiters_ = Csv::DEFAULT_DELIMITER;
163 reserve_ = Csv::DEFAULT_RESERVE;
164 }

References STK::TReadWriteCsv< Type >::delimiters_, STK::TReadWriteCsv< Type >::msg_error_, STK::TReadWriteCsv< Type >::reserve_, STK::TReadWriteCsv< Type >::str_data_, STK::TReadWriteCsv< Type >::with_last_nl_, STK::TReadWriteCsv< Type >::with_mapping_, and STK::TReadWriteCsv< Type >::with_names_.

◆ clone()

template<typename Type >
Var * STK::TReadWriteCsv< Type >::clone ( int  icol) const
inline
Returns
the variable with the index icol.
Parameters
icolindex of the col

Definition at line 305 of file STK_ReadWriteCsv.h.

305{ return str_data_.elt(icol).clone(); }

References STK::TReadWriteCsv< Type >::str_data_.

◆ cols()

template<typename Type >
Range STK::TReadWriteCsv< Type >::cols ( ) const
inline
Returns
The range of the variables of the TReadWriteCsv

Definition at line 173 of file STK_ReadWriteCsv.h.

173{ return str_data_.range(); }

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::resizeCols().

◆ countFields()

template<typename Type >
int STK::TReadWriteCsv< Type >::countFields ( String const line,
Array1D< Char > &  listDelimiters 
)
inlineprotected

Counts the number of columns in a line stored in a String and return the position of the delimiters and its types.

Parameters
lineThe String to parse
listDelimitersarray with the delimiters found in the line
Returns
the number of field in the line

Definition at line 588 of file STK_ReadWriteCsv.h.

589 {
590 listDelimiters.clear();
591 listDelimiters.reserve(nbVars_);
592 // position of the delimiters in the file
593 String::size_type idx =0;
594 do
595 {
596 // Find delimiter
597 idx = line.find_first_of(delimiters_, idx);
598 // if the position of the delimiter char is not found
599 if (idx == line.npos) break;
600 listDelimiters.push_back(line[idx]);
601 ++idx;
602 } while(1);
603 // last delimiter is the end of line
604 listDelimiters.push_back(CHAR_NL);
605 listDelimiters.shift(1);
606 // return the number of fields
607 return listDelimiters.size();
608 }

References STK::TReadWriteCsv< Type >::delimiters_, and STK::TReadWriteCsv< Type >::nbVars_.

Referenced by STK::TReadWriteCsv< Type >::readCurrentLine().

◆ delimiters()

template<typename Type >
String const & STK::TReadWriteCsv< Type >::delimiters ( ) const
inline
Returns
the delimiters used in the Csv file

Definition at line 244 of file STK_ReadWriteCsv.h.

244{ return delimiters_; }

References STK::TReadWriteCsv< Type >::delimiters_.

Referenced by STK::TReadWriteCsv< Type >::setDelimiters().

◆ end()

template<typename Type >
int STK::TReadWriteCsv< Type >::end ( ) const
inline
Returns
the ending index of the variables

Definition at line 168 of file STK_ReadWriteCsv.h.

168{ return str_data_.end(); }

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::endRows(), STK::TReadWriteCsv< Type >::lastIdxRows(), and STK::TReadWriteCsv< Type >::sizeRows().

◆ endCols()

template<typename Type >
int STK::TReadWriteCsv< Type >::endCols ( ) const
inline
Returns
the ending index of the variables

Definition at line 177 of file STK_ReadWriteCsv.h.

177{ return str_data_.end(); }

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::exporter().

◆ endRow()

template<typename Type >
int STK::TReadWriteCsv< Type >::endRow ( int  icol) const
inline
Parameters
icolindex of the column
Returns
the ending index of the column icol

Definition at line 188 of file STK_ReadWriteCsv.h.

188{ return str_data_.at(icol).end();}

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::endRows().

◆ endRows()

template<typename Type >
int STK::TReadWriteCsv< Type >::endRows ( ) const
inline
Returns
the ending index of the samples.

Definition at line 207 of file STK_ReadWriteCsv.h.

208 {
209 if (size()<= 0) return baseIdx;
210 int retVal = lastRow(begin());
211 for (int i=begin()+1; i<end(); i++)
212 { retVal = std::max(retVal, endRow(i));}
213 return retVal;
214 }
int endRow(int icol) const
int lastRow(int icol) const

References STK::baseIdx, STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::end(), STK::TReadWriteCsv< Type >::endRow(), STK::TReadWriteCsv< Type >::lastRow(), and STK::TReadWriteCsv< Type >::size().

Referenced by STK::TReadWriteCsv< Type >::exporter().

◆ eraseColumn()

template<typename Type >
bool STK::TReadWriteCsv< Type >::eraseColumn ( int const icol)
inline

Deletes the variable whose index is icol from a TReadWriteCsv.

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

Definition at line 401 of file STK_ReadWriteCsv.h.

402 {
403 try
404 {
405 str_data_.erase(icol);
406 return true;
407 }
408 catch( Exception const& e) { msg_error_ = e.error(); }
409 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
410 return false;
411 }

References STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::str_data_.

◆ error()

template<typename Type >
String const & STK::TReadWriteCsv< Type >::error ( ) const
inline
Returns
the last error encountered

Definition at line 242 of file STK_ReadWriteCsv.h.

242{ return msg_error_; }

References STK::TReadWriteCsv< Type >::msg_error_.

◆ exporter()

template<typename Type >
template<class Array >
void STK::TReadWriteCsv< Type >::exporter ( ArrayBase< Array > &  array) const
inline

export to an array of the same Type

Definition at line 443 of file STK_ReadWriteCsv.h.

444 {
445 array.asDerived().resize(sizeRows(), sizeCols());
446 for (int j= beginCols(); j < endCols(); ++j)
447 {
448 for (int i= beginRows(); i < endRows(); ++i)
449 {
450 array.elt(i,j) = var(j)[i];
451 }
452
453 }
454 }

References STK::TReadWriteCsv< Type >::beginCols(), STK::TReadWriteCsv< Type >::beginRows(), STK::TReadWriteCsv< Type >::endCols(), STK::TReadWriteCsv< Type >::endRows(), STK::TReadWriteCsv< Type >::sizeCols(), STK::TReadWriteCsv< Type >::sizeRows(), and STK::TReadWriteCsv< Type >::var().

◆ fastSetData()

template<typename Type >
void STK::TReadWriteCsv< Type >::fastSetData ( int const icol,
int const irow,
Type const value 
)
inlineprotected

Set without check the specified value to the element (icol, irow).

Parameters
icolindex of the column
irowindex of the row
valuethe value to set

Definition at line 689 of file STK_ReadWriteCsv.h.

690 { str_data_.elt(icol).elt(irow) = value;}

References STK::TReadWriteCsv< Type >::str_data_.

◆ firstRow()

template<typename Type >
int STK::TReadWriteCsv< Type >::firstRow ( int  icol) const
inline
Parameters
icolindex of the variable
Returns
the first index in the column icol

Definition at line 184 of file STK_ReadWriteCsv.h.

184{ return str_data_.at(icol).begin();}

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::beginRows().

◆ front() [1/2]

template<typename Type >
Var & STK::TReadWriteCsv< Type >::front ( )
inline
Returns
the first variable.

Definition at line 331 of file STK_ReadWriteCsv.h.

331{ return str_data_.at(begin());}

References STK::TReadWriteCsv< Type >::begin(), and STK::TReadWriteCsv< Type >::str_data_.

◆ front() [2/2]

template<typename Type >
Var const & STK::TReadWriteCsv< Type >::front ( ) const
inline
Returns
the first variable (const).

Definition at line 333 of file STK_ReadWriteCsv.h.

333{ return str_data_.at(begin());}

References STK::TReadWriteCsv< Type >::begin(), and STK::TReadWriteCsv< Type >::str_data_.

◆ initRead()

template<typename Type >
void STK::TReadWriteCsv< Type >::initRead ( istream inBuffer)
inlineprotected

Initial read.

Will compute an initial value for nbRows_ and nbVars_ and eventually read the name of the variables in the first line of the file.

Parameters
inBufferthe input stream to start to read

Definition at line 660 of file STK_ReadWriteCsv.h.

661 {
662 Array1D<Char> listDelimiters;
663 String currentLine;
664 // clear previous ReadCvs if any
665 str_data_.clear();
666 // get current position in the stream
667 std::streampos pos = inBuffer.tellg();
668 // Count the number of rows of the input stream
669 nbRows_ = DManager::nbEndOfLine(inBuffer);
671 // revert to the current position in the stream
672 inBuffer.seekg(pos);
673 // initialize the initial number of variables to 0
674 nbVars_ = 0;
675 // If the names are at the top line
676 if (with_names_) { readNames(inBuffer);}
677 // get current position
678 pos = inBuffer.tellg();
679 // count number of fields in the first line
680 int nbField = readCurrentLine(inBuffer, currentLine, listDelimiters);
681 nbVars_ = std::max(nbVars_, nbField);
682 inBuffer.seekg(pos);
683 }
void setReserve(int const &reserve) const
Sets the reserve value for data storage (reserve_ is mutable).
int readCurrentLine(istream &inBuffer, String &currentLine, Array1D< Char > &listDelimiters)
Read the current line from the buffer.
void readNames(istream &inBuffer)
Read the names of rhe Csv file.
int nbEndOfLine(istream &is)
std::basic_string< Char > String
STK fundamental type of a String.

References STK::DManager::nbEndOfLine(), STK::TReadWriteCsv< Type >::nbRows_, STK::TReadWriteCsv< Type >::nbVars_, STK::TReadWriteCsv< Type >::readCurrentLine(), STK::TReadWriteCsv< Type >::readNames(), STK::TReadWriteCsv< Type >::setReserve(), STK::TReadWriteCsv< Type >::str_data_, and STK::TReadWriteCsv< Type >::with_names_.

◆ lastIdx()

◆ lastIdxCols()

template<typename Type >
int STK::TReadWriteCsv< Type >::lastIdxCols ( ) const
inline
Returns
the index of the last variable

Definition at line 239 of file STK_ReadWriteCsv.h.

239{ return str_data_.lastIdx(); }

References STK::TReadWriteCsv< Type >::str_data_.

◆ lastIdxRows()

template<typename Type >
int STK::TReadWriteCsv< Type >::lastIdxRows ( ) const
inline
Returns
the last index of the samples.

Definition at line 225 of file STK_ReadWriteCsv.h.

226 {
227 if (size()<= 0) return baseIdx-1;
228 int retVal = lastRow(begin());
229 for (int i=begin()+1; i<end(); i++)
230 { retVal = std::max(retVal, lastRow(i));}
231 return retVal;
232 }

References STK::baseIdx, STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::end(), STK::TReadWriteCsv< Type >::lastRow(), and STK::TReadWriteCsv< Type >::size().

Referenced by STK::TReadWriteCsv< Type >::rows(), STK::TReadWriteCsv< Type >::write(), and STK::TReadWriteCsv< Type >::write().

◆ lastRow()

template<typename Type >
int STK::TReadWriteCsv< Type >::lastRow ( int  icol) const
inline
Parameters
icolindex of the column
Returns
the last index in the column icol

Definition at line 192 of file STK_ReadWriteCsv.h.

192{ return str_data_.at(icol).lastIdx();}

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::endRows(), and STK::TReadWriteCsv< Type >::lastIdxRows().

◆ operator()() [1/2]

template<typename Type >
Type & STK::TReadWriteCsv< Type >::operator() ( int const iRow,
int const iCol 
)
inline
Returns
a reference of the value specified by the given coordinates.
Parameters
iColindex of the column
iRowindex of the row

Definition at line 294 of file STK_ReadWriteCsv.h.

295 { return str_data_.elt(iCol).elt(iRow);}

References STK::TReadWriteCsv< Type >::str_data_.

◆ operator()() [2/2]

template<typename Type >
Type const STK::TReadWriteCsv< Type >::operator() ( int const iRow,
int const iCol 
) const
inline
Returns
a constant reference of the value specified by the given coordinates.
Parameters
iColindex of the column
iRowindex of the row

Definition at line 300 of file STK_ReadWriteCsv.h.

301 { return str_data_.elt(iCol).elt(iRow);}

References STK::TReadWriteCsv< Type >::str_data_.

◆ operator+()

template<typename Type >
TReadWriteCsv STK::TReadWriteCsv< Type >::operator+ ( TReadWriteCsv< Type > const rw) const
inline

Combines TReadWriteCsv(s)

Parameters
rwthe TReadWriteCsv to add

Definition at line 439 of file STK_ReadWriteCsv.h.

440 { return TReadWriteCsv((*this)) += rw;}
TReadWriteCsv()
The default constructor.

References STK::TReadWriteCsv< Type >::TReadWriteCsv().

◆ operator+=()

template<typename Type >
TReadWriteCsv & STK::TReadWriteCsv< Type >::operator+= ( TReadWriteCsv< Type > const rw)
inline

Appends a TReadWriteCsv to another TReadWriteCsv.

Parameters
rwthe TReadWriteCsv to append

Definition at line 430 of file STK_ReadWriteCsv.h.

431 {
432 for ( int i=rw.begin(); i<=rw.lastIdx(); i++)
433 { str_data_.push_back(rw.str_data_[i]);}
434 return *this;
435 }

References STK::TReadWriteCsv< Type >::str_data_.

◆ operator=()

template<typename Type >
TReadWriteCsv & STK::TReadWriteCsv< Type >::operator= ( TReadWriteCsv< Type > const rw)
inline

Assigns a TReadWriteCsv equal to another TReadWriteCsv.

Parameters
rwthe TReadWriteCsv to copy

Definition at line 415 of file STK_ReadWriteCsv.h.

416 {
417 file_name_ = rw.file_name_;
418 with_names_ = rw.with_names_;
419 delimiters_ = rw.delimiters_;
420 reserve_ = rw.reserve_;
421 msg_error_ = rw.msg_error_;
422 str_data_ = rw.str_data_;
423 nbVars_ = rw.nbVars_;
424 nbRows_ = rw.nbRows_;
425 return *this;
426 }

References STK::TReadWriteCsv< Type >::delimiters_, STK::TReadWriteCsv< Type >::file_name_, STK::TReadWriteCsv< Type >::msg_error_, STK::IRunnerBase::msg_error_, STK::TReadWriteCsv< Type >::nbRows_, STK::TReadWriteCsv< Type >::nbVars_, STK::TReadWriteCsv< Type >::reserve_, STK::TReadWriteCsv< Type >::str_data_, and STK::TReadWriteCsv< Type >::with_names_.

◆ operator[]() [1/2]

template<typename Type >
Var & STK::TReadWriteCsv< Type >::operator[] ( int const icol)
inline
Returns
a reference of the icol variable
Parameters
icolindex of the col

Definition at line 325 of file STK_ReadWriteCsv.h.

325{ return str_data_[icol]; }

References STK::TReadWriteCsv< Type >::str_data_.

◆ operator[]() [2/2]

template<typename Type >
Var const STK::TReadWriteCsv< Type >::operator[] ( int const icol) const
inline

@ return a const reference to the value at the specified location.

Parameters
icolindex of the col

Definition at line 329 of file STK_ReadWriteCsv.h.

329{ return str_data_[icol]; }

References STK::TReadWriteCsv< Type >::str_data_.

◆ push_back()

template<typename Type >
bool STK::TReadWriteCsv< Type >::push_back ( Var const data = Var())
inline

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 342 of file STK_ReadWriteCsv.h.

343 {
344 try
345 {
346 str_data_.push_back(data);
347 //str_data_.back().reserve(reserve_);
348 return true;
349 }
350 catch(const Exception& e) { msg_error_ = e.error(); }
351 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
352 return false;
353 }

References STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::readNames().

◆ push_front()

template<typename Type >
bool STK::TReadWriteCsv< Type >::push_front ( Var const data = Var())
inline

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 358 of file STK_ReadWriteCsv.h.

359 {
360 try
361 {
362 str_data_.push_front(data);
363 return true;
364 }
365 catch(const Exception& e) { msg_error_ = e.error(); }
366 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
367 return false;
368 }

References STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::str_data_.

◆ read() [1/4]

template<typename Type >
bool STK::TReadWriteCsv< Type >::read ( )
inline

Reads the default file with the specified read flags.

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

Definition at line 458 of file STK_ReadWriteCsv.h.

458{ return read(file_name_);}
bool read()
Reads the default file with the specified read flags.

References STK::TReadWriteCsv< Type >::file_name_, and STK::TReadWriteCsv< Type >::read().

Referenced by STK::TReadWriteCsv< Type >::read(), STK::TReadWriteCsv< Type >::read(), and STK::TReadWriteCsv< Type >::read().

◆ read() [2/4]

template<typename Type >
bool STK::TReadWriteCsv< Type >::read ( istream inBuffer)

Reads the specified input stream with the specified read flags.

Parameters
inBuffername of the stream to read
Returns
true if successful, false if an error is encountered.

Definition at line 711 of file STK_ReadWriteCsv.h.

712{
713 Array1D<Char> listDelimiters;
714 String currentLine;
715 try
716 {
717 initRead(inBuffer);
720 // Read data : loop for all rows
721 int currentRow=beginRows()-1, countRows = 0;
722 for (; !inBuffer.eof();)
723 {
724 int nbField = readCurrentLine(inBuffer, currentLine, listDelimiters);
725 if (nbField == 0) { break;}
726 // add on the non-existing columns: will be done if (nbField >= nbVars_)
727 for (int iField=nbVars_+1; iField<=nbField; iField++)
729 nbVars_++;
730 }
731 // first loop on the existing columns with data
732 currentRow++; countRows++;
733 istringstream instream(currentLine);
734 int icol=begin();
735 for (int iField=1 ; iField<=nbField; iField++, icol++)
736 {
737 String field;
738 DManager::getField( instream, field, listDelimiters.elt(iField));
739 // append Data to the column
741 fastSetData(icol, currentRow, imapping_.count(field) ? imapping_.find(field)->second : Arithmetic<Type>::NA())
742 : fastSetData(icol, currentRow, stringToType<Type>(field));
743 }
744 // Append NA values if the row is not complete
745 for (; icol<end(); icol++) { fastSetData(icol, currentRow, Arithmetic<Type>::NA());}
746 }
747 // resize in case there exists too much rows
748 resizeRows(Range(countRows));
749 return true;
750 }
751 catch( Exception const& e) { msg_error_ = e.error(); }
752 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
753 return false;
754}
void fastSetData(int const &icol, int const &irow, Type const &value)
Set without check the specified value to the element (icol, irow).
void resizeCols(Range const &cols)
resize the TReadWriteCsv to the given range of columns.
bool push_back(Var const &data=Var())
Attempts to add a column with the values contained in data.
void initRead(istream &inBuffer)
Initial read.
std::map< String, Type > imapping_
mapping String -> Type for (input)
void resizeRows(Range const &rows)
resize the TReadWriteCsv to the given range of rows.
Variable< Type > Var
bool stringToType(Type &t, String const &s, std::ios_base &(*f)(std::ios_base &)=std::dec)
convert a String to Type
Definition STK_String.h:195
String stringNa
Representation of a Not Available value.
istream & getField(istream &is, String &value, Char delimiter)
Get the current field from the input stream.
std::basic_istringstream< Char > istringstream
istringstream for Char
Definition STK_Stream.h:63
TRange< UnknownSize > Range
Definition STK_Range.h:59

References STK::IRunnerBase::error(), STK::DManager::getField(), STK::IRunnerBase::msg_error_, STK::Arithmetic< Type >::NA(), and STK::stringNa.

◆ read() [3/4]

template<typename Type >
bool STK::TReadWriteCsv< Type >::read ( std::map< String, Type > const mapping)
inline

Reads the default file with the specified read flags using a map.

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

Definition at line 462 of file STK_ReadWriteCsv.h.

463 { return read(file_name_);}

References STK::TReadWriteCsv< Type >::file_name_, and STK::TReadWriteCsv< Type >::read().

◆ read() [4/4]

template<typename Type >
bool STK::TReadWriteCsv< Type >::read ( std::string const file_name)
inline

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 468 of file STK_ReadWriteCsv.h.

469 {
470 try
471 {
472 // update current file_name
473 file_name_ = file_name;
474 // input file stream
475 ifstream inFile;
476 // open file
477 inFile.open(file_name.c_str(),std::ios::in);
478 // check error
479 if (inFile.rdstate() & std::ios::failbit)
480 {
481 inFile.close();
482 msg_error_ = Csv::ERRORCODES[4];
483 msg_error_ += "\nFile: " + file_name;
484 return false;
485 }
486 read(inFile); // and read it
487 inFile.close();
488 // return
489 return true;
490 }
491 catch(const Exception& e)
492 { msg_error_ = e.error();}
493 catch(...)
494 { msg_error_ = Csv::ERRORCODES[1];}
495 return false;
496 }
std::basic_ifstream< Char > ifstream
ifstream for Char
Definition STK_Stream.h:71

References STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::file_name_, STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::read().

◆ readCurrentLine()

template<typename Type >
int STK::TReadWriteCsv< Type >::readCurrentLine ( istream inBuffer,
String currentLine,
Array1D< Char > &  listDelimiters 
)
inlineprotected

Read the current line from the buffer.

Returns
the number of field found in the current line
Parameters
inBufferthe input stream to read
currentLinethe line to read
listDelimitersthe delimiters between the fields in the current line

Definition at line 615 of file STK_ReadWriteCsv.h.

616 {
617 if (inBuffer.eof()) return 0;
618 int nbField =0;
619 do
620 {
621 currentLine.clear();
622 std::getline(inBuffer, currentLine);
623 if (currentLine.size() == 0) {nbField = 0;}
624 else
625 {
626 DManager::removeCharBeforeAndAfter(currentLine, CHAR_BLANK);
627 nbField = (currentLine.size() == 0)
628 ? 0 : countFields( currentLine, listDelimiters);
629 }
630 }
631 while ((nbField == 0)&&(!inBuffer.eof()));
632 return nbField;
633 }
int countFields(String const &line, Array1D< Char > &listDelimiters)
Counts the number of columns in a line stored in a String and return the position of the delimiters a...
void removeCharBeforeAndAfter(String &str, Char c)
remove all occurrences of the char c at the beginning and the end of the string str.

References STK::TReadWriteCsv< Type >::countFields(), and STK::DManager::removeCharBeforeAndAfter().

Referenced by STK::TReadWriteCsv< Type >::initRead(), and STK::TReadWriteCsv< Type >::readNames().

◆ readNames()

template<typename Type >
void STK::TReadWriteCsv< Type >::readNames ( istream inBuffer)
inlineprotected

Read the names of rhe Csv file.

They are stored in the first line of the file.

Parameters
inBufferthe input stream to read

Definition at line 637 of file STK_ReadWriteCsv.h.

638 {
639 Array1D<Char> listDelimiters;
640 String currentLine;
641 // Count the number of names of the first line
642 int nbField = readCurrentLine(inBuffer, currentLine, listDelimiters);
643 if (nbField == 0) return;
644 // Declare an input string stream
645 istringstream instream(currentLine);
646 for(int icol=1; icol<=nbField; icol++)
647 {
648 // Append a Col
649 String name;
650 DManager::getField( instream, name, listDelimiters.elt(icol));
651 push_back(Var(nbRows_, name));
652 }
653 // Update the number of var
654 nbVars_ = std::max(nbVars_, nbField);
655 }

References STK::DManager::getField(), STK::TReadWriteCsv< Type >::nbRows_, STK::TReadWriteCsv< Type >::nbVars_, STK::TReadWriteCsv< Type >::push_back(), and STK::TReadWriteCsv< Type >::readCurrentLine().

Referenced by STK::TReadWriteCsv< Type >::initRead().

◆ resize()

template<typename Type >
void STK::TReadWriteCsv< Type >::resize ( int  sizeRows,
int  sizeCols 
)
inline

resize the container str_data_.

Parameters
sizeColsnumber of columns
sizeRowsnumber of rows

Definition at line 373 of file STK_ReadWriteCsv.h.

374 {
377 }

References STK::TReadWriteCsv< Type >::resizeCols(), STK::TReadWriteCsv< Type >::resizeRows(), STK::TReadWriteCsv< Type >::sizeCols(), and STK::TReadWriteCsv< Type >::sizeRows().

◆ resizeCols()

template<typename Type >
void STK::TReadWriteCsv< Type >::resizeCols ( Range const cols)
inlineprotected

resize the TReadWriteCsv to the given range of columns.

Parameters
colsthe range of columns to set

Definition at line 702 of file STK_ReadWriteCsv.h.

703 { str_data_.resize(cols);}

References STK::TReadWriteCsv< Type >::cols(), and STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::resize().

◆ resizeRows()

template<typename Type >
void STK::TReadWriteCsv< Type >::resizeRows ( Range const rows)
inlineprotected

resize the TReadWriteCsv to the given range of rows.

Parameters
rowsthe range of the rows

Definition at line 694 of file STK_ReadWriteCsv.h.

695 {
696 for (int iVar=begin(); iVar<=lastIdx(); iVar++ )
697 { str_data_.elt(iVar).resize(rows);}
698 }

References STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::lastIdx(), STK::TReadWriteCsv< Type >::rows(), and STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::resize().

◆ rows()

template<typename Type >
Range STK::TReadWriteCsv< Type >::rows ( ) const
inline
Returns
The range of the samples of the TReadWriteCsv

Definition at line 234 of file STK_ReadWriteCsv.h.

234{ return Range(beginRows(), lastIdxRows(), 0); }

References STK::TReadWriteCsv< Type >::beginRows(), and STK::TReadWriteCsv< Type >::lastIdxRows().

Referenced by STK::TReadWriteCsv< Type >::resizeRows().

◆ setDelimiters()

template<typename Type >
void STK::TReadWriteCsv< Type >::setDelimiters ( String const delimiters) const
inline

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

Parameters
delimitersdelimiters to use

Definition at line 250 of file STK_ReadWriteCsv.h.

References STK::TReadWriteCsv< Type >::delimiters(), and STK::TReadWriteCsv< Type >::delimiters_.

◆ setInMapping()

template<typename Type >
void STK::TReadWriteCsv< Type >::setInMapping ( std::map< String, Type > const imapping)
inline

Set mapping to used when reading.

Definition at line 268 of file STK_ReadWriteCsv.h.

269 { imapping_ = imapping;}

References STK::TReadWriteCsv< Type >::imapping_.

◆ setLast_nl()

template<typename Type >
void STK::TReadWriteCsv< Type >::setLast_nl ( bool  with_last_nl) const
inline

Sets the with_last_nl_ value for writing a new line after the last line.

Parameters
with_last_nltrue if we want to write a new line after the last line

Definition at line 262 of file STK_ReadWriteCsv.h.

262{ with_last_nl_ = with_last_nl; }

References STK::TReadWriteCsv< Type >::with_last_nl_.

◆ setName()

template<typename Type >
bool STK::TReadWriteCsv< Type >::setName ( int const icol,
String const name 
)
inline

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 278 of file STK_ReadWriteCsv.h.

279 {
280 try
281 {
282 str_data_.at(icol).setName(name);
283 return true;
284 }
285 catch( Exception const& e) { msg_error_ = e.error(); }
286 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
287 return false;
288 }

References STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::str_data_.

◆ setOutMapping()

template<typename Type >
void STK::TReadWriteCsv< Type >::setOutMapping ( std::map< String, Type > const omapping)
inline

Set mapping to used when writting.

Definition at line 271 of file STK_ReadWriteCsv.h.

272 { omapping_ = omapping;}
std::map< Type, String > omapping_
mapping Type -> String (output)

References STK::TReadWriteCsv< Type >::omapping_.

◆ setReserve()

template<typename Type >
void STK::TReadWriteCsv< Type >::setReserve ( int const reserve) const
inline

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

Parameters
reservenumber of place to reserve

Definition at line 266 of file STK_ReadWriteCsv.h.

266{ reserve_ = reserve; }

References STK::TReadWriteCsv< Type >::reserve_.

Referenced by STK::TReadWriteCsv< Type >::initRead().

◆ setWithMapping()

template<typename Type >
void STK::TReadWriteCsv< Type >::setWithMapping ( bool  with_mapping) const
inline

Sets the with_mapping_ value for reading/writing variables names.

Parameters
with_mappingtrue if we want to read the data using a map

Definition at line 258 of file STK_ReadWriteCsv.h.

258{ with_mapping_ = with_mapping; }

References STK::TReadWriteCsv< Type >::with_mapping_.

◆ setWithNames()

template<typename Type >
void STK::TReadWriteCsv< Type >::setWithNames ( bool  with_names) const
inline

Sets the with_names_ value for reading/writing variables names.

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

Definition at line 254 of file STK_ReadWriteCsv.h.

254{ with_names_ = with_names; }

References STK::TReadWriteCsv< Type >::with_names_.

◆ size()

template<typename Type >
int STK::TReadWriteCsv< Type >::size ( ) const
inline

◆ sizeCols()

template<typename Type >
int STK::TReadWriteCsv< Type >::sizeCols ( ) const
inline
Returns
The current number of variables of the TReadWriteCsv

Definition at line 179 of file STK_ReadWriteCsv.h.

179{ return str_data_.size(); }

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::exporter(), and STK::TReadWriteCsv< Type >::resize().

◆ sizeRow()

template<typename Type >
int STK::TReadWriteCsv< Type >::sizeRow ( int  icol) const
inline
Parameters
icolindex of the column
Returns
the number of element in the column icol

Definition at line 196 of file STK_ReadWriteCsv.h.

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

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::sizeRows().

◆ sizeRows()

template<typename Type >
int STK::TReadWriteCsv< Type >::sizeRows ( ) const
inline
Returns
the size of the samples.

Definition at line 216 of file STK_ReadWriteCsv.h.

217 {
218 if (size()<= 0) return 0;
219 int retVal = sizeRow(begin());
220 for (int i=begin()+1; i<end(); i++)
221 { retVal = std::max(retVal, sizeRow(i));}
222 return retVal;
223 }
int sizeRow(int icol) const

References STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::end(), STK::TReadWriteCsv< Type >::size(), and STK::TReadWriteCsv< Type >::sizeRow().

Referenced by STK::TReadWriteCsv< Type >::exporter(), and STK::TReadWriteCsv< Type >::resize().

◆ var() [1/2]

template<typename Type >
Var const & STK::TReadWriteCsv< Type >::var ( int  icol) const
inline
Returns
a constant reference of the icol variable.
Parameters
icolindex of the col

Definition at line 313 of file STK_ReadWriteCsv.h.

313{ return str_data_.elt(icol); }

References STK::TReadWriteCsv< Type >::str_data_.

◆ var() [2/2]

template<typename Type >
Var & STK::TReadWriteCsv< Type >::var ( int  j)
inline
Returns
the variable with the index icol.
Parameters
jindex of the column

Definition at line 309 of file STK_ReadWriteCsv.h.

309{ return str_data_.elt(j); }

References STK::TReadWriteCsv< Type >::str_data_.

Referenced by STK::TReadWriteCsv< Type >::exporter().

◆ withNames()

template<typename Type >
bool STK::TReadWriteCsv< Type >::withNames ( ) const
inline
Returns
with_names value

Definition at line 246 of file STK_ReadWriteCsv.h.

246{ return with_names_; }

References STK::TReadWriteCsv< Type >::with_names_.

◆ write() [1/3]

template<typename Type >
bool STK::TReadWriteCsv< Type >::write ( )
inline

write to the default file with the specified write flags.

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

Definition at line 505 of file STK_ReadWriteCsv.h.

505{ return write(file_name_);}
bool write()
write to the default file with the specified write flags.

References STK::TReadWriteCsv< Type >::file_name_, and STK::TReadWriteCsv< Type >::write().

Referenced by STK::TReadWriteCsv< Type >::write().

◆ write() [2/3]

template<typename Type >
void STK::TReadWriteCsv< Type >::write ( ostream os) const
inline

Write the csv to an output stream.

Parameters
osthe output stream

Definition at line 528 of file STK_ReadWriteCsv.h.

void writeSelection(ostream &os, int top, int bottom, int left, int right) const
Write to output stream a selection based on the coordinates passed (Think of it as highlighting cells...

References STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::beginRows(), STK::TReadWriteCsv< Type >::lastIdx(), STK::TReadWriteCsv< Type >::lastIdxRows(), and STK::TReadWriteCsv< Type >::writeSelection().

◆ write() [3/3]

template<typename Type >
bool STK::TReadWriteCsv< Type >::write ( std::string const file_name) const
inline

Attempts to write the TReadWriteCsv to the location specified by file_name using the delimiters specified by delimiters_.

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

Definition at line 511 of file STK_ReadWriteCsv.h.

512 {
513 file_name_ = file_name;
514 try
515 {
516 ofstream os(file_name.c_str());
518 os.close();
519 return true;
520 }
521 catch(const Exception& e) { msg_error_ = e.error(); }
522 catch(...) { msg_error_ = Csv::ERRORCODES[1]; }
523 return false;
524 }
std::basic_ofstream< Char > ofstream
ofstream for Char
Definition STK_Stream.h:73

References STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::beginRows(), STK::IRunnerBase::error(), STK::TReadWriteCsv< Type >::file_name_, STK::TReadWriteCsv< Type >::lastIdx(), STK::TReadWriteCsv< Type >::lastIdxRows(), STK::TReadWriteCsv< Type >::msg_error_, and STK::TReadWriteCsv< Type >::writeSelection().

◆ writeSelection()

template<typename Type >
void STK::TReadWriteCsv< Type >::writeSelection ( ostream os,
int  top,
int  bottom,
int  left,
int  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 757 of file STK_ReadWriteCsv.h.

758{
759 if ((right < left)|| (bottom < top)) return;
760 // create a vector for the format of the output
761 Array1D<String::size_type> format(Range(left, right, 0), 0);
762 // for each variable, find the largest size
763 for(int iVar=left; iVar<=right; iVar++)
764 { format[iVar] = str_data_[iVar].maxLength(with_names_);}
765 // write if needed names variables
766 if (with_names_)
767 {
768 for(int iVar=left; iVar<right; iVar++)
769 {
770 os << std::setw(int(format[iVar])) << std::right
771 << Proxy<String>(str_data_.at(iVar).name())
772 << delimiters_.at(0);
773 }
774 os << std::setw(int(format[right])) << std::right
775 << Proxy<String>(str_data_[right].name())
776 << _T("\n");
777 }
778 // write data
779 for(int irow = top; irow<bottom; irow++)
780 {
781 for(int iVar = left; iVar<right; iVar++)
782 {
783 try
784 {
785 os << std::setw(format[iVar]) << std::right
786 << Proxy<Type>(str_data_[iVar].at(irow));
787 }
788 catch(...) // if an error occur, we put NA value
789 { os << std::setw(format[iVar]) << std::right << stringNa;}
790 os << delimiters_.at(0);
791 }
792 try
793 {
794 os << std::setw(format[right]) << std::right
795 << Proxy<Type>(str_data_[right].at(irow));
796 }
797 catch(...) // if an error occur, we put NA value
798 { os << std::setw(format[right]) << std::right << stringNa;}
799 os << _T("\n");
800 }
801 for(int iVar = left; iVar<right; iVar++)
802 {
803 try
804 {
805 os << std::setw(format[iVar]) << std::right
806 << Proxy<Type>(str_data_[iVar].at(bottom));
807 }
808 catch(...) // if an error occur, we put NA value
809 { os << std::setw(format[iVar]) << std::right << stringNa;}
810 os << delimiters_.at(0);
811 }
812 try
813 {
814 os << std::setw(format[right]) << std::right
815 << Proxy<Type>(str_data_[right].at(bottom));
816 }
817 catch(...) // if an error occur, we put NA value
818 { os << std::setw(format[right]) << std::right << stringNa;}
819 if (with_last_nl_) { os << _T("\n");}
820}
#define _T(x)
Let x unmodified.

References _T, and STK::stringNa.

Referenced by STK::TReadWriteCsv< Type >::write(), and STK::TReadWriteCsv< Type >::write().

Friends And Related Symbol Documentation

◆ operator<<

template<typename Type >
ostream & operator<< ( ostream os,
TReadWriteCsv< Type > const rw 
)
friend

overload operator << for rw

Definition at line 547 of file STK_ReadWriteCsv.h.

548 {
549 try
550 { rw.writeSelection( os, rw.beginRows(), rw.lastIdxRows(), rw.begin(), rw.lastIdx());}
551 // catch and re-throw any Exceptions
552 catch(const Exception& e) { throw e; }
553 catch(...) { throw Exception(Csv::ERRORCODES[1]); }
554 return os;
555 }

◆ operator>>

template<typename Type >
istream & operator>> ( istream in,
TReadWriteCsv< Type > &  rw 
)
friend

overload operator >> for rw

Definition at line 541 of file STK_ReadWriteCsv.h.

542 {
543 if (!rw.read(in)) throw Exception(rw.msg_error_);
544 return in;
545 }

Member Data Documentation

◆ delimiters_

◆ file_name_

◆ imapping_

template<typename Type >
std::map<String, Type> STK::TReadWriteCsv< Type >::imapping_
protected

mapping String -> Type for (input)

Definition at line 575 of file STK_ReadWriteCsv.h.

Referenced by STK::TReadWriteCsv< Type >::setInMapping().

◆ msg_error_

◆ nbRows_

template<typename Type >
int STK::TReadWriteCsv< Type >::nbRows_
protected

◆ nbVars_

template<typename Type >
int STK::TReadWriteCsv< Type >::nbVars_
protected

◆ omapping_

template<typename Type >
std::map<Type, String> STK::TReadWriteCsv< Type >::omapping_
protected

mapping Type -> String (output)

Definition at line 577 of file STK_ReadWriteCsv.h.

Referenced by STK::TReadWriteCsv< Type >::setOutMapping().

◆ reserve_

template<typename Type >
int STK::TReadWriteCsv< Type >::reserve_
mutableprotected

◆ str_data_

template<typename Type >
Array1D< Var > STK::TReadWriteCsv< Type >::str_data_
protected

Array of array for the data.

Definition at line 573 of file STK_ReadWriteCsv.h.

Referenced by STK::TReadWriteCsv< Type >::appendData(), STK::TReadWriteCsv< Type >::at(), STK::TReadWriteCsv< Type >::at(), STK::TReadWriteCsv< Type >::back(), STK::TReadWriteCsv< Type >::back(), STK::TReadWriteCsv< Type >::begin(), STK::TReadWriteCsv< Type >::beginCols(), STK::TReadWriteCsv< Type >::clear(), STK::TReadWriteCsv< Type >::clone(), STK::TReadWriteCsv< Type >::cols(), STK::TReadWriteCsv< Type >::end(), STK::TReadWriteCsv< Type >::endCols(), STK::TReadWriteCsv< Type >::endRow(), STK::TReadWriteCsv< Type >::eraseColumn(), STK::TReadWriteCsv< Type >::fastSetData(), STK::TReadWriteCsv< Type >::firstRow(), STK::TReadWriteCsv< Type >::front(), STK::TReadWriteCsv< Type >::front(), STK::TReadWriteCsv< Type >::initRead(), STK::TReadWriteCsv< Type >::lastIdx(), STK::TReadWriteCsv< Type >::lastIdxCols(), STK::TReadWriteCsv< Type >::lastRow(), STK::TReadWriteCsv< Type >::operator()(), STK::TReadWriteCsv< Type >::operator()(), STK::TReadWriteCsv< Type >::operator+=(), STK::TReadWriteCsv< Type >::operator=(), STK::TReadWriteCsv< Type >::operator[](), STK::TReadWriteCsv< Type >::operator[](), STK::TReadWriteCsv< Type >::push_back(), STK::TReadWriteCsv< Type >::push_front(), STK::TReadWriteCsv< Type >::resizeCols(), STK::TReadWriteCsv< Type >::resizeRows(), STK::TReadWriteCsv< Type >::setName(), STK::TReadWriteCsv< Type >::size(), STK::TReadWriteCsv< Type >::sizeCols(), STK::TReadWriteCsv< Type >::sizeRow(), STK::TReadWriteCsv< Type >::var(), and STK::TReadWriteCsv< Type >::var().

◆ with_last_nl_

template<typename Type >
bool STK::TReadWriteCsv< Type >::with_last_nl_
mutableprotected

write a new line character at the end of the output

Definition at line 565 of file STK_ReadWriteCsv.h.

Referenced by STK::TReadWriteCsv< Type >::clear(), and STK::TReadWriteCsv< Type >::setLast_nl().

◆ with_mapping_

template<typename Type >
bool STK::TReadWriteCsv< Type >::with_mapping_
mutableprotected

map the values stored

Definition at line 563 of file STK_ReadWriteCsv.h.

Referenced by STK::TReadWriteCsv< Type >::clear(), and STK::TReadWriteCsv< Type >::setWithMapping().

◆ with_names_


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