Back to Jetson Inference

Jetson Inference: jetson

docs/html/csvReader_8hpp_source.html

latest14.3 KB
Original Source

| | 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 {

34char* e;

35 errno = 0;

36

37const int x = strtol(string.c_str(), &e, 0);

38

39if( *e != '\0' || errno != 0 )

40return false;

41

42if( value != NULL )

43 *value = x;

44

45return true;

46 }

47

48 // toFloat()

49 inline bool csvData::toFloat( float* value ) const

50 {

51char* e;

52 errno = 0;

53

54const float x = strtof(string.c_str(), &e);

55

56if( *e != '\0' || errno != 0 )

57return false;

58

59if( value != NULL )

60 *value = x;

61

62return true;

63 }

64

65 // toDouble()

66 inline bool csvData::toDouble( double* value ) const

67 {

68char* e;

69 errno = 0;

70

71const float x = strtof(string.c_str(), &e);

72

73if( *e != '\0' || errno != 0 )

74return false;

75

76if( value != NULL )

77 *value = x;

78

79return true;

80 }

81

82 // toInt()

83 inline int csvData::toInt( bool* valid ) const

84 {

85int x=0;

86const bool v=toInt(&x);

87

88if( valid != NULL )

89 *valid=v;

90

91return x;

92 }

93

94 // toFloat()

95 inline float csvData::toFloat( bool* valid ) const

96 {

97float x=0.0f;

98const bool v=toFloat(&x);

99

100if( valid != NULL )

101 *valid=v;

102

103return x;

104 }

105

106 // toDouble()

107 inline double csvData::toDouble( bool* valid ) const

108 {

109double x=0.0f;

110const bool v=toDouble(&x);

111

112if( valid != NULL )

113 *valid=v;

114

115return x;

116 }

117

118 // operator >>

119 inline std::istream& operator >>(std::istream& in, csvData& obj)

120 {

121 in >> obj.string;

122return in;

123 }

124

125 // operator <<

126 inline std::ostream& operator <<(std::ostream& out, const csvData& obj)

127 {

128 out << obj.string;

129return out;

130 }

131

132 // Parse()

133 inline std::vector<csvData> csvData::Parse( const char* str, const char* delimiters )

134 {

135 std::vector<csvData> tokens;

136Parse(tokens, str, delimiters);

137return tokens;

138 }

139

140 // Parse

141 inline bool csvData::Parse( std::vector<csvData>& tokens, const char* str, const char* delimiters )

142 {

143if( !str || !delimiters )

144return false;

145

146 tokens.clear();

147

148const size_t str_length = strlen(str);

149char* str_tokens = (char*)malloc(str_length + 1);

150

151if( !str_tokens )

152return false;

153

154 strcpy(str_tokens, str);

155

156if( str_tokens[str_length] == '\n' )

157 str_tokens[str_length] = '\0';

158

159if( str_tokens[str_length-1] == '\n' )

160 str_tokens[str_length-1] = '\0';

161

162char* token = strtok(str_tokens, delimiters);

163

164while( token != NULL )

165 {

166 tokens.push_back(token);

167 token = strtok(NULL, delimiters);

168 }

169

170 free(str_tokens);

171return tokens.size() > 0;

172 }

173

174 //-------------------------------------------------------------------------------------

175 // constructor

176 csvReader::csvReader( const char* filename, const char* delimiters ) : mFile(NULL)

177 {

178if( !filename || !delimiters )

179return;

180

181 mFile = fopen(filename, "r");

182

183if( !mFile )

184 {

185LogError("csvReader -- failed to open file %s\n", filename);

186 perror("csvReader -- error");

187return;

188 }

189

190 mFilename = filename;

191 mDelimiters = delimiters;

192 }

193

194 // destructor

195 csvReader::~csvReader()

196 {

197Close();

198 }

199

200

201 // open

202 inline csvReader* csvReader::Open( const char* filename, const char* delimiters )

203 {

204if( !filename || !delimiters )

205return NULL;

206

207csvReader* csv = new csvReader(filename, delimiters);

208

209if( ! csv->IsOpen() )

210 {

211delete csv;

212return NULL;

213 }

214

215return csv;

216 }

217

218 // close

219 inline void csvReader::Close()

220 {

221if( IsClosed() )

222return;

223

224 fclose(mFile);

225 mFile = NULL;

226 }

227

228 // isOpen

229 inline bool csvReader::IsOpen() const

230 {

231return mFile != NULL;

232 }

233

234 // isClosed

235 inline bool csvReader::IsClosed() const

236 {

237return ! IsOpen();

238 }

239

240 // readLine

241 inline std::vector<csvData> csvReader::Read()

242 {

243return Read(mDelimiters.c_str());

244 }

245

246 // readLine

247 inline std::vector<csvData> csvReader::Read( const char* delimiters )

248 {

249 std::vector<csvData> tokens;

250Read(tokens, delimiters);

251 }

252

253 // readLine

254 inline bool csvReader::Read( std::vector<csvData>& data )

255 {

256return Read(data, mDelimiters.c_str());

257 }

258

259 // readLine

260 inline bool csvReader::Read( std::vector<csvData>& data, const char* delimiters )

261 {

262if( IsClosed() )

263return false;

264

265// read the next line

266char str[MaxLineLength];

267

268if( fgets(str, sizeof(str), mFile) == NULL )

269 {

270if( ferror(mFile) )

271 {

272LogError("csvReader -- error reading file %s\n", mFilename.c_str());

273 perror("csvReader -- error");

274 }

275

276Close();

277return false;

278 }

279

280// check if EOF was reached

281if( feof(mFile) == EOF )

282Close();

283

284// disregard comments

285if( str[0] == '#' )

286return Read(data, delimiters);

287

288return 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 {

300return mDelimiters.c_str();

301 }

302

303 // GetFilename

304 inline const char* csvReader::GetFilename() const

305 {

306return 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