Jetson Inference
DNN Vision Library
csvReader.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * 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
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __CSV_READER_HPP_
24 #define __CSV_READER_HPP_
25 
26 
27 #include "csvReader.h"
28 #include "logging.h"
29 
30 
31 // toInt()
32 inline bool csvData::toInt( int* value ) const
33 {
34  char* e;
35  errno = 0;
36 
37  const int x = strtol(string.c_str(), &e, 0);
38 
39  if( *e != '\0' || errno != 0 )
40  return false;
41 
42  if( value != NULL )
43  *value = x;
44 
45  return true;
46 }
47 
48 // toFloat()
49 inline bool csvData::toFloat( float* value ) const
50 {
51  char* e;
52  errno = 0;
53 
54  const float x = strtof(string.c_str(), &e);
55 
56  if( *e != '\0' || errno != 0 )
57  return false;
58 
59  if( value != NULL )
60  *value = x;
61 
62  return true;
63 }
64 
65 // toDouble()
66 inline bool csvData::toDouble( double* value ) const
67 {
68  char* e;
69  errno = 0;
70 
71  const float x = strtof(string.c_str(), &e);
72 
73  if( *e != '\0' || errno != 0 )
74  return false;
75 
76  if( value != NULL )
77  *value = x;
78 
79  return true;
80 }
81 
82 // toInt()
83 inline int csvData::toInt( bool* valid ) const
84 {
85  int x=0;
86  const bool v=toInt(&x);
87 
88  if( valid != NULL )
89  *valid=v;
90 
91  return x;
92 }
93 
94 // toFloat()
95 inline float csvData::toFloat( bool* valid ) const
96 {
97  float x=0.0f;
98  const bool v=toFloat(&x);
99 
100  if( valid != NULL )
101  *valid=v;
102 
103  return x;
104 }
105 
106 // toDouble()
107 inline double csvData::toDouble( bool* valid ) const
108 {
109  double x=0.0f;
110  const bool v=toDouble(&x);
111 
112  if( valid != NULL )
113  *valid=v;
114 
115  return x;
116 }
117 
118 // operator >>
119 inline std::istream& operator >> (std::istream& in, csvData& obj)
120 {
121  in >> obj.string;
122  return in;
123 }
124 
125 // operator <<
126 inline std::ostream& operator << (std::ostream& out, const csvData& obj)
127 {
128  out << obj.string;
129  return out;
130 }
131 
132 // Parse()
133 inline std::vector<csvData> csvData::Parse( const char* str, const char* delimiters )
134 {
135  std::vector<csvData> tokens;
136  Parse(tokens, str, delimiters);
137  return tokens;
138 }
139 
140 // Parse
141 inline bool csvData::Parse( std::vector<csvData>& tokens, const char* str, const char* delimiters )
142 {
143  if( !str || !delimiters )
144  return false;
145 
146  tokens.clear();
147 
148  const size_t str_length = strlen(str);
149  char* str_tokens = (char*)malloc(str_length + 1);
150 
151  if( !str_tokens )
152  return false;
153 
154  strcpy(str_tokens, str);
155 
156  if( str_tokens[str_length] == '\n' )
157  str_tokens[str_length] = '\0';
158 
159  if( str_tokens[str_length-1] == '\n' )
160  str_tokens[str_length-1] = '\0';
161 
162  char* token = strtok(str_tokens, delimiters);
163 
164  while( token != NULL )
165  {
166  tokens.push_back(token);
167  token = strtok(NULL, delimiters);
168  }
169 
170  free(str_tokens);
171  return tokens.size() > 0;
172 }
173 
174 //-------------------------------------------------------------------------------------
175 // constructor
176 csvReader::csvReader( const char* filename, const char* delimiters ) : mFile(NULL)
177 {
178  if( !filename || !delimiters )
179  return;
180 
181  mFile = fopen(filename, "r");
182 
183  if( !mFile )
184  {
185  LogError("csvReader -- failed to open file %s\n", filename);
186  perror("csvReader -- error");
187  return;
188  }
189 
190  mFilename = filename;
191  mDelimiters = delimiters;
192 }
193 
194 // destructor
196 {
197  Close();
198 }
199 
200 
201 // open
202 inline csvReader* csvReader::Open( const char* filename, const char* delimiters )
203 {
204  if( !filename || !delimiters )
205  return NULL;
206 
207  csvReader* csv = new csvReader(filename, delimiters);
208 
209  if( !csv->IsOpen() )
210  {
211  delete csv;
212  return NULL;
213  }
214 
215  return csv;
216 }
217 
218 // close
219 inline void csvReader::Close()
220 {
221  if( IsClosed() )
222  return;
223 
224  fclose(mFile);
225  mFile = NULL;
226 }
227 
228 // isOpen
229 inline bool csvReader::IsOpen() const
230 {
231  return mFile != NULL;
232 }
233 
234 // isClosed
235 inline bool csvReader::IsClosed() const
236 {
237  return !IsOpen();
238 }
239 
240 // readLine
241 inline std::vector<csvData> csvReader::Read()
242 {
243  return Read(mDelimiters.c_str());
244 }
245 
246 // readLine
247 inline std::vector<csvData> csvReader::Read( const char* delimiters )
248 {
249  std::vector<csvData> tokens;
250  Read(tokens, delimiters);
251 }
252 
253 // readLine
254 inline bool csvReader::Read( std::vector<csvData>& data )
255 {
256  return Read(data, mDelimiters.c_str());
257 }
258 
259 // readLine
260 inline bool csvReader::Read( std::vector<csvData>& data, const char* delimiters )
261 {
262  if( IsClosed() )
263  return false;
264 
265  // read the next line
266  char str[MaxLineLength];
267 
268  if( fgets(str, sizeof(str), mFile) == NULL )
269  {
270  if( ferror(mFile) )
271  {
272  LogError("csvReader -- error reading file %s\n", mFilename.c_str());
273  perror("csvReader -- error");
274  }
275 
276  Close();
277  return false;
278  }
279 
280  // check if EOF was reached
281  if( feof(mFile) == EOF )
282  Close();
283 
284  // disregard comments
285  if( str[0] == '#' )
286  return Read(data, delimiters);
287 
288  return csvData::Parse(data, str, delimiters);
289 }
290 
291 // SetDelimiters
292 inline void csvReader::SetDelimiters( const char* delimiters )
293 {
294  mDelimiters = delimiters;
295 }
296 
297 // GetDelimiters
298 inline const char* csvReader::GetDelimiters() const
299 {
300  return mDelimiters.c_str();
301 }
302 
303 // GetFilename
304 inline const char* csvReader::GetFilename() const
305 {
306  return mFilename.c_str();
307 }
308 
309 #endif
310 
csvData::toInt
bool toInt(int *value) const
Definition: csvReader.hpp:32
csvReader
csvReader
Definition: csvReader.h:90
csvData::toFloat
bool toFloat(float *value) const
Definition: csvReader.hpp:49
csv
csv stream manipulators
Definition: csvWriter.h:103
csvReader::SetDelimiters
void SetDelimiters(const char *delimiters)
Definition: csvReader.hpp:292
csvReader::IsOpen
bool IsOpen() const
Definition: csvReader.hpp:229
csvReader::csvReader
csvReader(const char *filename, const char *delimiters=",;\t ")
Definition: csvReader.hpp:176
operator<<
std::ostream & operator<<(std::ostream &out, const csvData &obj)
Definition: csvReader.hpp:126
csvReader::MaxLineLength
const size_t MaxLineLength
Definition: csvReader.h:125
csvReader.h
operator>>
std::istream & operator>>(std::istream &in, csvData &obj)
Definition: csvReader.hpp:119
csvData::toDouble
bool toDouble(double *value) const
Definition: csvReader.hpp:66
csvReader::IsClosed
bool IsClosed() const
Definition: csvReader.hpp:235
csvReader::Close
void Close()
Definition: csvReader.hpp:219
csvReader::GetDelimiters
const char * GetDelimiters() const
Definition: csvReader.hpp:298
csvReader::~csvReader
~csvReader()
Definition: csvReader.hpp:195
csvReader::GetFilename
const char * GetFilename() const
Definition: csvReader.hpp:304
csvReader::Open
static csvReader * Open(const char *filename, const char *delimiters=",;\t ")
Definition: csvReader.hpp:202
csvData::Parse
static std::vector< csvData > Parse(const char *str, const char *delimiters=",;\t ")
Definition: csvReader.hpp:133
logging.h
csvData::string
std::string string
Definition: csvReader.h:82
csvReader::Read
std::vector< csvData > Read()
Definition: csvReader.hpp:241
LogError
#define LogError(format, args...)
Log a printf-style error message (Log::ERROR)
Definition: logging.h:150
csvData
csvData
Definition: csvReader.h:39