ug4
ug::Base64FileWriter Class Reference

File writer allowing selective base64 encoding of arbitrary data. More...

#include <base64_file_writer.h>

Public Types

enum  fmtflag { base64_ascii , base64_binary , normal }
 Format flags to enable deactivation of base64 encoding selectivly. More...
 

Public Member Functions

 Base64FileWriter ()
 
 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. More...
 
void close ()
 Closes the file writer properly and encodes any remaining buffer content. More...
 
fmtflag format () const
 gets the current set format More...
 
void open (const char *filename, const std::ios_base::openmode mode=std::ios_base::out)
 
Base64FileWriteroperator<< (char c)
 
Base64FileWriteroperator<< (const char *cstr)
 
Base64FileWriteroperator<< (const fmtflag format)
 Switch between normal and base64 encoded output. More...
 
Base64FileWriteroperator<< (const std::string &str)
 
Base64FileWriteroperator<< (double d)
 
Base64FileWriteroperator<< (float f)
 
Base64FileWriteroperator<< (int i)
 
Base64FileWriteroperator<< (long l)
 
Base64FileWriteroperator<< (size_t s)
 
 ~Base64FileWriter ()
 Destructor, which properly flushs encoder's internal buffer and closes file stream. More...
 

Private Member Functions

void assertFileOpen ()
 Check on readiness of the file stream. More...
 
template<typename T >
void dispatch (const T &value)
 Writes given data to the output file and encodes it if Base64FileWriter::base64 is set. More...
 
void flushInputBuffer (bool force=false)
 Flushes input buffer. More...
 

Private Attributes

fmtflag m_currFormat
 Current write format (base64 or normal) More...
 
std::fstream m_fStream
 File stream to write everything to. More...
 
std::stringstream m_inBuffer
 Internal input buffer for the encoder. More...
 
size_t m_lastInputByteSize
 
size_t m_numBytesWritten
 
std::vector< char > m_tmpBuff
 

Detailed Description

File writer allowing selective base64 encoding of arbitrary data.

This provides a convenient and well tested way of writing selectivly base64 encoded data to an arbitrary file.

After initialization with a file name

Base64FileWriter writer( "output_file.txt" );
Base64FileWriter()
Definition: base64_file_writer.cpp:130

arbitrary data can be written to it solely using the <<-operator:

int myInt = 3;
writer << "My int is: " << myInt;

The default behaviour is to write base64 binary or ascii encoded data from the very beginning. This can be overridden by passing Base64FileWriter::normal as data to it:

writer << Base64FileWriter::normal << "Base64 is not always useful.";
@ normal
behaves as usual std::ofstream
Definition: base64_file_writer.h:87

Switching back to base64 encoding is simple as

writer << Base64FileWriter::base64_binary << "and back to base64 encoding!";
@ base64_binary
encode given values in binary base64
Definition: base64_file_writer.h:85

To finish off writing, just close the writer:

writer.close();

The documentation for this class was generated from the following files: