ug4
base64_file_writer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Authors: Torbjoern Klatt, Martin Scherer
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__COMMON__UTIL__BASE64_FILE_WRITER__
34 #define __H__UG__COMMON__UTIL__BASE64_FILE_WRITER__
35 
36 #include <sstream>
37 #include <fstream>
38 #include <vector>
39 
40 namespace ug {
41 
44 
76 public:
77 
81  enum fmtflag {
87  normal
88  };
89 
92 
101  Base64FileWriter(const char* filename,
102  const std::ios_base::openmode mode = (std::ios_base::out |
103  std::ios_base::trunc));
104 
109 
110 public:
111 
112  void open(const char *filename,
113  const std::ios_base::openmode mode = std::ios_base::out );
114 
118  fmtflag format() const;
119 
123  void close();
124 
130 
131  // insert plain standard types to this filewriter
133  Base64FileWriter& operator<<(char c);
134  Base64FileWriter& operator<<(const char* cstr);
135  Base64FileWriter& operator<<(const std::string& str);
136 
137  Base64FileWriter& operator<<(float f);
138  Base64FileWriter& operator<<(double d);
139  Base64FileWriter& operator<<(long l);
140  Base64FileWriter& operator<<(size_t s);
141 
142 private:
150  template <typename T>
151  void dispatch(const T& value);
152 
156  std::fstream m_fStream;
164  std::stringstream m_inBuffer;
165 
166  /*
167  * input byte size during a format block
168  */
170 
174  std::vector<char> m_tmpBuff;
175 
180 
185  void flushInputBuffer(bool force = false);
186 
192  inline void assertFileOpen();
193 };
194 
195 // end group ugbase_common_io
196 
197 } // namespace: ug
198 
199 #endif // __H__UG__COMMON__UTIL__BASE64_FILE_WRITER__
File writer allowing selective base64 encoding of arbitrary data.
Definition: base64_file_writer.h:75
std::stringstream m_inBuffer
Internal input buffer for the encoder.
Definition: base64_file_writer.h:164
void dispatch(const T &value)
Writes given data to the output file and encodes it if Base64FileWriter::base64 is set.
Definition: base64_file_writer.cpp:182
Base64FileWriter()
Definition: base64_file_writer.cpp:130
Base64FileWriter & operator<<(const std::string &str)
void close()
Closes the file writer properly and encodes any remaining buffer content.
Definition: base64_file_writer.cpp:312
void assertFileOpen()
Check on readiness of the file stream.
Definition: base64_file_writer.cpp:216
size_t m_lastInputByteSize
Definition: base64_file_writer.h:169
size_t m_numBytesWritten
Definition: base64_file_writer.h:179
Base64FileWriter & operator<<(const fmtflag format)
Switch between normal and base64 encoded output.
Definition: base64_file_writer.cpp:70
std::fstream m_fStream
File stream to write everything to.
Definition: base64_file_writer.h:156
fmtflag format() const
gets the current set format
Definition: base64_file_writer.cpp:66
std::vector< char > m_tmpBuff
Definition: base64_file_writer.h:174
fmtflag m_currFormat
Current write format (base64 or normal)
Definition: base64_file_writer.h:160
void flushInputBuffer(bool force=false)
Flushes input buffer.
Definition: base64_file_writer.cpp:223
fmtflag
Format flags to enable deactivation of base64 encoding selectivly.
Definition: base64_file_writer.h:81
~Base64FileWriter()
Destructor, which properly flushs encoder's internal buffer and closes file stream.
Definition: base64_file_writer.cpp:149
void open(const char *filename, const std::ios_base::openmode mode=std::ios_base::out)
Definition: base64_file_writer.cpp:155
Base64FileWriter(const char *filename, const std::ios_base::openmode mode=(std::ios_base::out|std::ios_base::trunc))
Constructor with name of file to write to.
@ base64_binary
encode given values in binary base64
Definition: base64_file_writer.h:85
@ base64_ascii
enables base64 encoding of all following data until Base64FileWriter::normal is set
Definition: base64_file_writer.h:83
@ normal
behaves as usual std::ofstream
Definition: base64_file_writer.h:87
the ug namespace