38#ifndef STK_READWRITECSV_H
39#define STK_READWRITECSV_H
49template<
typename Type>
class TReadWriteCsv;
60 static const bool DEFAULT_READNAMES =
true;
63 static const bool DEFAULT_MAPPING =
false;
66 static const bool DEFAULT_LAST_NL =
true;
69 static const int DEFAULT_RESERVE =
int(0x0FFFF);
72 static const String DEFAULT_DELIMITER =
_T(
",");
75 static const String ERRORCODES[] =
78 _T(
"An unknown error occurred!"),
79 _T(
"Variable name not found!"),
80 _T(
"Filename not set!"),
81 _T(
"File not found!"),
82 _T(
"The Number of names is different from the Number of Data Columns!")
106template<
typename Type>
218 if (
size()<= 0)
return 0;
286 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
351 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
366 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
394 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
409 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
432 for (
int i=
rw.begin();
i<=
rw.lastIdx();
i++)
442 template<
class Array>
479 if (
inFile.rdstate() & std::ios::failbit)
522 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
550 {
rw.writeSelection(
os,
rw.beginRows(),
rw.lastIdxRows(),
rw.begin(),
rw.lastIdx());}
553 catch(...) {
throw Exception(Csv::ERRORCODES[1]); }
593 String::size_type idx =0;
599 if (idx ==
line.npos)
break;
667 std::streampos pos =
inBuffer.tellg();
710template <
typename Type>
752 catch(...) {
msg_error_ = Csv::ERRORCODES[1]; }
756template <
typename Type>
772 << delimiters_.at(0);
790 os << delimiters_.at(0);
810 os << delimiters_.at(0);
819 if (with_last_nl_) {
os <<
_T(
"\n");}
In this file we define and implement the final class Array1D.
In this file we define the base class for Arrays.
#define _T(x)
Let x unmodified.
Implement the interface class IVariable as Variable.
Sdk class for all library Exceptions.
String msg_error_
String with the last error message.
String const & error() const
get the last error message.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
the TReadWriteCsv class : allow to write and/or to read a csv file.
Var & operator[](int const &icol)
std::map< Type, String > omapping_
mapping Type -> String (output)
TReadWriteCsv()
The default constructor.
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...
std::string file_name_
Name of the Current file read.
TReadWriteCsv & operator+=(TReadWriteCsv const &rw)
Appends a TReadWriteCsv to another TReadWriteCsv.
String msg_error_
Contain the last error message.
Var const & at(int icol) const
int sizeRow(int icol) const
void setReserve(int const &reserve) const
Sets the reserve value for data storage (reserve_ is mutable).
int readCurrentLine(istream &inBuffer, String ¤tLine, Array1D< Char > &listDelimiters)
Read the current line from the buffer.
void setInMapping(std::map< String, Type > const &imapping)
Set mapping to used when reading.
void clear()
reset all fields to default value and reclaims any allocated memory.
int reserve_
Size of the buffer.
int endRow(int icol) const
bool read(istream &inBuffer)
Reads the specified input stream with the specified read flags.
int lastRow(int icol) const
void exporter(ArrayBase< Array > &array) const
export to an array of the same Type
void resize(int sizeRows, int sizeCols)
resize the container str_data_.
void fastSetData(int const &icol, int const &irow, Type const &value)
Set without check the specified value to the element (icol, irow).
TReadWriteCsv & operator=(TReadWriteCsv const &rw)
Assigns a TReadWriteCsv equal to another TReadWriteCsv.
bool push_front(Var const &data=Var())
Attempts to add a column with the values contained in data.
String delimiters_
Delimiter(s)
void setLast_nl(bool with_last_nl) const
Sets the with_last_nl_ value for writing a new line after the last line.
int nbVars_
The number of variables we manage.
int nbRows_
The number of rows we manage.
~TReadWriteCsv()
destructor.
Type const operator()(int const &iRow, int const &iCol) const
Array1D< Var > str_data_
Array of array for the data.
Var * clone(int icol) const
String const & delimiters() const
int firstRow(int icol) const
bool appendData(int const &icol, Type const &value)
Attempts to append a data to the variable specified by icol.
bool read()
Reads the default file with the specified read flags.
void setOutMapping(std::map< String, Type > const &omapping)
Set mapping to used when writting.
void resizeCols(Range const &cols)
resize the TReadWriteCsv to the given range of columns.
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...
bool with_mapping_
map the values stored
bool write(std::string const &file_name) const
Attempts to write the TReadWriteCsv to the location specified by file_name using the delimiters speci...
bool read(std::map< String, Type > const &mapping)
Reads the default file with the specified read flags using a map.
TReadWriteCsv(std::string const &file_name, bool read_names=Csv::DEFAULT_READNAMES, String const &delimiters=Csv::DEFAULT_DELIMITER)
Constructor with a specified file name.
String const & error() const
TReadWriteCsv operator+(TReadWriteCsv const &rw) const
Combines TReadWriteCsv(s)
void write(ostream &os) const
Write the csv to an output stream.
void setWithNames(bool with_names) const
Sets the with_names_ value for reading/writing variables names.
bool push_back(Var const &data=Var())
Attempts to add a column with the values contained in data.
Var const & front() const
friend istream & operator>>(istream &in, TReadWriteCsv &rw)
overload operator >> for rw
bool with_names_
Read or Write names of the variables.
bool setName(int const &icol, String const &name)
Set the variable name name at the specified index.
bool with_last_nl_
write a new line character at the end of the output
void initRead(istream &inBuffer)
Initial read.
Var const operator[](int const &icol) const
@ return a const reference to the value at the specified location.
bool eraseColumn(int const &icol)
Deletes the variable whose index is icol from a TReadWriteCsv.
void readNames(istream &inBuffer)
Read the names of rhe Csv file.
bool write()
write to the default file with the specified write flags.
Type & operator()(int const &iRow, int const &iCol)
void setWithMapping(bool with_mapping) const
Sets the with_mapping_ value for reading/writing variables names.
std::map< String, Type > imapping_
mapping String -> Type for (input)
void resizeRows(Range const &rows)
resize the TReadWriteCsv to the given range of rows.
TReadWriteCsv(TReadWriteCsv const &rw)
Copy constructor.
Var const & var(int icol) const
void setDelimiters(String const &delimiters) const
Sets the delimiters to use for parsing data (delimiters_ is mutable).
bool read(std::string const &file_name)
Reads the specified file with the specified read flags.
friend ostream & operator<<(ostream &os, TReadWriteCsv const &rw)
overload operator << for rw
String stringNa
Representation of a Not Available value.
readflags
TODO: These flags determine the behavior of the reading methods.
int nbEndOfLine(istream &is)
void removeCharBeforeAndAfter(String &str, Char c)
remove all occurrences of the char c at the beginning and the end of the string str.
istream & getField(istream &is, String &value, Char delimiter)
Get the current field from the input stream.
std::basic_string< Char > String
STK fundamental type of a String.
const int baseIdx
base index of the containers created in STK++.
std::basic_ostream< Char > ostream
ostream for Char
std::basic_ifstream< Char > ifstream
ifstream for Char
std::basic_istringstream< Char > istringstream
istringstream for Char
std::basic_istream< Char > istream
istream for Char
std::basic_ofstream< Char > ofstream
ofstream for Char
The namespace STK is the main domain space of the Statistical ToolKit project.
TRange< UnknownSize > Range
Arithmetic properties of STK fundamental types.
static Type NA()
Adding a Non Available (NA) special number.