Jetson Inference
DNN Vision Library
csvReader.h
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_H_
24 #define __CSV_READER_H_
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 
30 #include <string>
31 #include <vector>
32 #include <iostream>
33 
34 
39 class csvData
40 {
41 public:
42  // constructors
43  csvData( char* str ) { string = str; }
44  csvData( const char* str ) { string = str; }
45  csvData( std::string& str ) { string = str; }
46 
47  // assignment
48  template<typename T> csvData( T value ) { set(value); }
49  template<typename T> void set( T value ) { string = std::to_string(value); }
50 
51  // cast to string
52  inline operator std::string&() { return string; }
53  inline operator const std::string&() const { return string; }
54  inline operator const char*() const { return string.c_str(); }
55 
56  // cast to number
57  inline operator int() const { return toInt(); }
58  inline operator float() const { return toFloat(); }
59  inline operator double() const { return toDouble(); }
60 
61  // convert to number (return true if valid)
62  inline bool toInt( int* value ) const;
63  inline bool toFloat( float* value ) const;
64  inline bool toDouble( double* value ) const;
65 
66  // convert to number (valid->false on error)
67  inline int toInt( bool* valid=NULL ) const;
68  inline float toFloat( bool* valid=NULL ) const;
69  inline double toDouble( bool* valid=NULL ) const;
70 
71  // stream insertion/extraction operators
72  inline friend std::istream& operator >> (std::istream& in, csvData& obj);
73  inline friend std::ostream& operator << (std::ostream& out, const csvData& obj);
74 
75  // split string by delimiters into list of tokens
76  inline static std::vector<csvData> Parse( const char* str, const char* delimiters=",;\t " );
77 
78  // fill list of tokens with string split by delimiters
79  inline static bool Parse( std::vector<csvData>& data, const char* str, const char* delimiters=",;\t " );
80 
81  // data storage
82  std::string string;
83 };
84 
85 
90 class csvReader
91 {
92 public:
93  // constructor/destructor
94  csvReader( const char* filename, const char* delimiters=",;\t " );
95  ~csvReader();
96 
97  // open
98  inline static csvReader* Open( const char* filename, const char* delimiters=",;\t " );
99 
100  // close
101  inline void Close();
102 
103  // is open and not EOF
104  inline bool IsOpen() const;
105  inline bool IsClosed() const;
106 
107  // read line, return list of tokens
108  inline std::vector<csvData> Read();
109  inline std::vector<csvData> Read( const char* delimiters );
110 
111  // read line, fill list of tokens
112  inline bool Read( std::vector<csvData>& data );
113  inline bool Read( std::vector<csvData>& data, const char* delimiters );
114 
115  // set default delimiters
116  inline void SetDelimiters( const char* delimiters );
117 
118  // retrieve default delimiters
119  inline const char* GetDelimiters() const;
120 
121  // retrieve the filename
122  inline const char* GetFilename() const;
123 
124  // maximum line length
125  const size_t MaxLineLength=2048;
126 
127 private:
128  FILE* mFile;
129 
130  std::string mFilename;
131  std::string mDelimiters;
132 };
133 
134 
135 // internal functions
136 #include "csvReader.hpp"
137 
138 #endif
139 
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
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
csvReader::MaxLineLength
const size_t MaxLineLength
Definition: csvReader.h:125
csvData::csvData
csvData(std::string &str)
Definition: csvReader.h:45
csvData::csvData
csvData(const char *str)
Definition: csvReader.h:44
csvData::toDouble
bool toDouble(double *value) const
Definition: csvReader.hpp:66
csvReader::IsClosed
bool IsClosed() const
Definition: csvReader.hpp:235
csvData::set
void set(T value)
Definition: csvReader.h:49
csvReader::Close
void Close()
Definition: csvReader.hpp:219
csvData::csvData
csvData(T value)
Definition: csvReader.h:48
csvReader::GetDelimiters
const char * GetDelimiters() const
Definition: csvReader.hpp:298
csvReader::~csvReader
~csvReader()
Definition: csvReader.hpp:195
csvData::csvData
csvData(char *str)
Definition: csvReader.h:43
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
csvReader.hpp
csvData::operator<<
friend std::ostream & operator<<(std::ostream &out, const csvData &obj)
Definition: csvReader.hpp:126
csvData::Parse
static std::vector< csvData > Parse(const char *str, const char *delimiters=",;\t ")
Definition: csvReader.hpp:133
csvData::operator>>
friend std::istream & operator>>(std::istream &in, csvData &obj)
Definition: csvReader.hpp:119
csvData::string
std::string string
Definition: csvReader.h:82
csvReader::Read
std::vector< csvData > Read()
Definition: csvReader.hpp:241
csvData
csvData
Definition: csvReader.h:39