GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
FileParser.h
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #ifndef GRT_FILE_PARSER_HEADER
22 #define GRT_FILE_PARSER_HEADER
23 
24 #include <iostream> // cout, endl
25 #include <fstream> // fstream
26 #include <vector>
27 #include <string>
28 #include <algorithm> // copy
29 #include <iterator> // ostream_operator
30 #include <sstream>
31 #include "MatrixDouble.h"
32 
33 namespace GRT {
34 
35 using namespace std;
36 
37 class FileParser{
38 public:
39  FileParser():warningLog("[FileParser]"){
40  clear();
41  }
42  ~FileParser(){
43  }
44 
45  vector< string >& operator[](const unsigned int &index){
46  return fileContents[index];
47  }
48 
49  bool parseCSVFile(string filename,bool removeNewLineCharacter=true){
50  return parseFile(filename,removeNewLineCharacter,',');
51  }
52 
53  bool parseTSVFile(string filename,bool removeNewLineCharacter=true){
54  return parseFile(filename,removeNewLineCharacter,'\t');
55  }
56 
57  bool getFileParsed(){
58  return fileParsed;
59  }
60 
61  bool getConsistentColumnSize(){
62  return consistentColumnSize;
63  }
64 
65  unsigned int getRowSize(){
66  return (unsigned int)fileContents.size();
67  }
68 
69  unsigned int getColumnSize(){
70  return columnSize;
71  }
72 
73  vector< vector< string > > getFileContents(){
74  return fileContents;
75  }
76 
77  bool clear(){
78  fileParsed = false;
79  consistentColumnSize = false;
80  columnSize = 0;
81  fileContents.clear();
82  return true;
83  }
84 
85  static bool parseColumn( const string &row, vector< string > &cols, const char seperator = ',' ){
86 
87  cols.clear();
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 );
94  columnString = "";
95  }else columnString += row[i];
96  }
97 
98  //Add the last column
99  cols.push_back( columnString );
100 
101  //Remove the new line char from the string in the last column
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);
108  }
109  }
110 
111  return true;
112  }
113 
114 protected:
115 
116  bool parseFile(string filename,bool removeNewLineCharacter,const char seperator){
117 
118  //Clear any previous data
119  clear();
120 
121  ifstream file( filename.c_str(), ifstream::in );
122  if ( !file.is_open() ){
123  warningLog << "parseFile(...) - Failed to open file: " << filename << endl;
124  return false;
125  }
126 
127  vector< string > vec;
128  string line;
129 
130  //Loop over each line of data and parse the contents
131  while ( getline(file,line) )
132  {
133  if( !parseColumn(line, vec,seperator) ){
134  clear();
135  warningLog << "parseFile(...) - Failed to parse column!" << endl;
136  file.close();
137  return false;
138  }
139 
140  //Check to make sure all the columns are consistent
141  if( columnSize == 0 ){
142  consistentColumnSize = true;
143  columnSize = (unsigned int)vec.size();
144  }else if( columnSize != vec.size() ) consistentColumnSize = false;
145 
146  fileContents.push_back( vec );
147  }
148 
149  //Close the file
150  file.close();
151 
152  //Flag that we have parsed the file
153  fileParsed = true;
154 
155  return true;
156  }
157 
158  bool fileParsed;
159  bool consistentColumnSize;
160  unsigned int columnSize;
161  WarningLog warningLog;
162  vector< vector< string > > fileContents;
163 
164 };
165 
166 }//End of namespace GRT
167 
168 #endif //GRT_FILE_PARSER_HEADER
Definition: AdaBoost.cpp:25