21 #ifndef GRT_FILE_PARSER_HEADER
22 #define GRT_FILE_PARSER_HEADER
31 #include "MatrixDouble.h"
45 vector< string >& operator[](
const unsigned int &index){
46 return fileContents[index];
49 bool parseCSVFile(
string filename,
bool removeNewLineCharacter=
true){
50 return parseFile(filename,removeNewLineCharacter,
',');
53 bool parseTSVFile(
string filename,
bool removeNewLineCharacter=
true){
54 return parseFile(filename,removeNewLineCharacter,
'\t');
61 bool getConsistentColumnSize(){
62 return consistentColumnSize;
65 unsigned int getRowSize(){
66 return (
unsigned int)fileContents.size();
69 unsigned int getColumnSize(){
73 vector< vector< string > > getFileContents(){
79 consistentColumnSize =
false;
85 static bool parseColumn(
const string &row, vector< string > &cols,
const char seperator =
',' ){
88 string columnString =
"";
89 const int sepValue = seperator;
90 const unsigned int N = (
unsigned int)row.length();
91 for(
unsigned int i=0; i<N; i++){
92 if(
int(row[i]) == sepValue ){
93 cols.push_back( columnString );
95 }
else columnString += row[i];
99 cols.push_back( columnString );
102 if( cols.size() >= 1 ){
103 size_t K = cols.size()-1;
104 size_t foundA = cols[ K ].find(
'\n');
105 size_t foundB = cols[ K ].find(
'\r');
106 if( foundA != std::string::npos || foundB != std::string::npos ){
107 cols[ K ] = cols[ K ].substr(0,cols[K].length()-1);
116 bool parseFile(
string filename,
bool removeNewLineCharacter,
const char seperator){
121 ifstream file( filename.c_str(), ifstream::in );
122 if ( !file.is_open() ){
123 warningLog <<
"parseFile(...) - Failed to open file: " << filename << endl;
127 vector< string > vec;
131 while ( getline(file,line) )
133 if( !parseColumn(line, vec,seperator) ){
135 warningLog <<
"parseFile(...) - Failed to parse column!" << endl;
141 if( columnSize == 0 ){
142 consistentColumnSize =
true;
143 columnSize = (
unsigned int)vec.size();
144 }
else if( columnSize != vec.size() ) consistentColumnSize =
false;
146 fileContents.push_back( vec );
159 bool consistentColumnSize;
160 unsigned int columnSize;
162 vector< vector< string > > fileContents;
168 #endif //GRT_FILE_PARSER_HEADER