docs/html/csvWriter_8hpp_source.html
| | Jetson Inference
DNN Vision Library |
csvWriter.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_WRITER_HPP_
24 #define __CSV_WRITER_HPP_
25
26
27 #include "csvWriter.h"
28 #include "logging.h"
29
30
31 // constructor
32 csvWriter::csvWriter( const char* filename, const char* delimiter )
33 {
34if( !filename || !delimiter )
35return;
36
37 mFile.open(filename);
38
39if( ! IsOpen() )
40 {
41LogError("csvWriter -- failed to open file %s\n", filename);
42return;
43 }
44
45 mFilename = filename;
46 mDelimiter = delimiter;
47 mNewLine = true;
48 }
49
50
51 // destructor
53 {
54Close();
55 }
56
57
58 // open
59 inline csvWriter* csvWriter::Open( const char* filename, const char* delimiter )
60 {
61if( !filename || !delimiter )
62return NULL;
63
64csvWriter* csv = new csvWriter(filename, delimiter);
65
66if( ! csv->IsOpen() )
67 {
68delete csv;
69return NULL;
70 }
71
72return csv;
73 }
74
75 // close
76 inline void csvWriter::Close()
77 {
78if( IsClosed() )
79return;
80
81Flush();
82 mFile.close();
83 }
84
85 // flush
86 inline void csvWriter::Flush()
87 {
88 mFile.flush();
89 }
90
91 // isOpen
92 inline bool csvWriter::IsOpen() const
93 {
94return mFile.is_open();
95 }
96
97 // isClosed
98 inline bool csvWriter::IsClosed() const
99 {
100return ! IsOpen();
101 }
102
103 // EndLine
104 inline void csvWriter::EndLine()
105 {
106 mFile << std::endl;
107 mNewLine = true;
108 }
109
110 // Write
111 template<typename T>
112 inline csvWriter& csvWriter::Write( const T& value )
113 {
114if( !mNewLine )
115 mFile << mDelimiter;
116else
117 mNewLine = false;
118
119 mFile << value;
120return *this;
121 }
122
123 // Write
124 template<typename T, typename... Args>
125 inline csvWriter& csvWriter::Write( const T& value, const Args&... args )
126 {
127Write(value);
128Write(args...);
129return *this;
130 }
131
132 // WriteLine
133 template<typename T, typename... Args>
134 inline csvWriter& csvWriter::WriteLine( const T& value, const Args&... args )
135 {
136Write(value);
137Write(args...);
138EndLine();
139return *this;
140 }
141
142 // stream insertion
143 template<typename T>
144 inline csvWriter& csvWriter::operator <<( const T& value )
145 {
146return Write(value);
147 }
148
149 // stream manipulator
150 inline csvWriter& csvWriter::operator <<( csvWriter& (*value)(csvWriter&) )
151 {
152return value(*this);
153 }
154
155 // SetDelimiter
156 inline void csvWriter::SetDelimiter( const char* delimiter )
157 {
158 mDelimiter = delimiter;
159 }
160
161 // GetDelimiter
162 inline const char* csvWriter::GetDelimiter() const
163 {
164return mDelimiter.c_str();
165 }
166
167 // GetFilename
168 inline const char* csvWriter::GetFilename() const
169 {
170return mFilename.c_str();
171 }
172
173 //----------------------------------------------------------------
174 namespace csv
175 {
176
177 // endl
178 inline static csvWriter& endl( csvWriter& file )
179 {
180 file.EndLine();
181return file;
182 }
183
184 // flush
185 inline static csvWriter& flush( csvWriter& file )
186 {
187 file.Flush();
188return file;
189 }
190
191 }
192
193 #endif
194
void EndLine()
Definition: csvWriter.hpp:104
bool IsClosed() const
Definition: csvWriter.hpp:98
csv stream manipulators
Definition: csvWriter.h:103
csvWriter & Write(const T &value)
Definition: csvWriter.hpp:112
void Flush()
Definition: csvWriter.hpp:86
const char * GetDelimiter() const
Definition: csvWriter.hpp:162
static csvWriter * Open(const char *filename, const char *delimiter=", ")
Definition: csvWriter.hpp:59
csvWriter & WriteLine(const T &value, const Args &... args)
Definition: csvWriter.hpp:134
const char * GetFilename() const
Definition: csvWriter.hpp:168
void Close()
Definition: csvWriter.hpp:76
void SetDelimiter(const char *delimiters)
Definition: csvWriter.hpp:156
csvWriter(const char *filename, const char *delimiter=", ")
Definition: csvWriter.hpp:32
csvWriter
Definition: csvWriter.h:42
csvWriter & operator<<(const T &value)
Definition: csvWriter.hpp:144
#define LogError(format, args...)
Log a printf-style error message (Log::ERROR)
Definition: logging.h:150
~csvWriter()
Definition: csvWriter.hpp:52
bool IsOpen() const
Definition: csvWriter.hpp:92