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