GestureRecognitionToolkit  Version: 0.2.0
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 <deque>
32 #include "../DataStructures/Vector.h"
33 #include "InfoLog.h"
34 
35 GRT_BEGIN_NAMESPACE
36 
37 class FileParser{
38 public:
39  FileParser():infoLog("[FileParser]"),warningLog("[WARNING FileParser]"){
40  clear();
41  }
42  ~FileParser(){
43  }
44 
45  Vector< std::string >& operator[](const unsigned int &index){
46  return fileContents[index];
47  }
48 
49  bool parseCSVFile(std::string filename,bool removeNewLineCharacter=true){
50  return parseFile(filename,removeNewLineCharacter,',');
51  }
52 
53  bool parseTSVFile(std::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  std::deque< Vector< std::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 std::string &row, Vector< std::string > &cols, const char seperator = ',' ){
86 
87  const unsigned int N = (unsigned int)row.length();
88  if( N == 0 ) return false;
89 
90  size_t lastSize = cols.size();
91  cols.clear();
92  if( lastSize > 0 ) cols.reserve( lastSize ); //Reserve the previous column size
93  std::string columnString = "";
94  const int sepValue = seperator;
95  for(unsigned int i=0; i<N; i++){
96  if( int(row[i]) == sepValue ){
97  cols.push_back( columnString );
98  columnString = "";
99  }else columnString += row[i];
100  }
101 
102  //Add the last column
103  cols.push_back( columnString );
104 
105  //Remove the new line char from the string in the last column
106  if( cols.size() >= 1 ){
107  size_t K = cols.size()-1;
108  size_t foundA = cols[ K ].find('\n');
109  size_t foundB = cols[ K ].find('\r');
110  if( foundA != std::string::npos || foundB != std::string::npos ){
111  cols[ K ] = cols[ K ].substr(0,cols[K].length()-1);
112  }
113  }
114 
115  return true;
116  }
117 
118 protected:
119 
120  bool parseFile(const std::string &filename,const bool removeNewLineCharacter,const char seperator){
121 
122  //Clear any previous data
123  clear();
124 
125  std::ifstream file( filename.c_str(), std::ifstream::in );
126  if ( !file.is_open() ){
127  warningLog << "parseFile(...) - Failed to open file: " << filename << std::endl;
128  return false;
129  }
130 
131  //Get the size of the file
132  std::streampos begin,end;
133  begin = file.tellg();
134  file.seekg (0, std::ios::end);
135  end = file.tellg();
136  file.seekg (0, std::ios::beg); //Reset the file pointer to the start of the file so we can read it
137 
139  std::string line;
140  unsigned int lineCounter = 0;
141 
142  //Loop over each line of data and parse the contents
143  while ( getline( file, line ) )
144  {
145  if( !parseColumn(line, vec, seperator) ){
146  clear();
147  warningLog << "parseFile(...) - Failed to parse column!" << std::endl;
148  file.close();
149  return false;
150  }
151 
152  //Check to make sure all the columns are consistent
153  if( columnSize == 0 ){
154  consistentColumnSize = true;
155  columnSize = vec.getSize();
156  }else if( columnSize != vec.getSize() ) consistentColumnSize = false;
157 
158  fileContents.push_back( vec );
159  }
160 
161  //Close the file
162  file.close();
163 
164  //Flag that we have parsed the file
165  fileParsed = true;
166 
167  return true;
168  }
169 
170  bool fileParsed;
171  bool consistentColumnSize;
172  unsigned int columnSize;
173  InfoLog infoLog;
174  WarningLog warningLog;
175  std::deque< Vector< std::string > > fileContents;
176 
177 };
178 
179 GRT_END_NAMESPACE
180 
181 #endif //GRT_FILE_PARSER_HEADER
UINT getSize() const
Definition: Vector.h:191
Definition: Vector.h:41