TokenWriter module
Module contents
-
class TokenWriter
Write tokens to a stream
Supply your tokens in a form that can be converted to strings. Set your record delimiter (usually newline or similar) and your field delimiter (tab, comma or similar). Done.
Public Types
-
typedef string_type::value_type char_type
Public Functions
-
inline TokenWriter()
Instantiate a default token writer.
-
inline TokenWriter(std::ostream &_output)
Instantiate a writer using a
std::ostream- Parameters:
_output – [in] Stream to output to
-
inline TokenWriter(TokenWriter const &other)
Copy contructor, create a writer with a copy of another
- Parameters:
other – [in] TokenWriter to copy from
-
inline ~TokenWriter()
Destructor.
-
inline TokenWriter &operator=(TokenWriter const &other)
Assign a TokenWriter to the value of another.
- Parameters:
other – [in] TokenWriter to assign value of
- Returns:
Writer with the new assigned value
-
inline bool operator==(TokenWriter const &other) const
Check whether one writer is equal to another by comparing all the properties.
Two writers are equal if all of their streams are properties.
- Parameters:
other – [in] TokenWriter for comparison
- Returns:
Boolean indicating equivalency
-
inline bool operator!=(TokenWriter const &other) const
Check whether two TokenWriter are unequal.
- Parameters:
other – [in] TokenWriter for comparison
- Returns:
Boolean indicating equivalency
-
inline void set_output(std::ostream &out)
Set the stream where tokens will be written
This can be any
std::ostream.Note
You are resposible for ensuring that the stream does not go out of scope until you are done writing tokens.
- Parameters:
out – [in] Stream where tokens will be written
-
inline std::ostream &output() const
Return the stream where tokens will be written
- Returns:
output stream
-
inline void set_field_delimiter(string_type const &delimiter)
Set the field delimiter
This string will be inserted between each field as tokens are written.
- Parameters:
delim – [in] Delimiter string
-
inline string_type field_delimiter() const
Return the field delimiter
- Returns:
Field delimiter
-
inline void set_record_delimiter(string_type const &end_of_line)
Set the record separator (end-of-line string)
This string will be written after each token. By default it’s
std::endl(the newline string).- Parameters:
sep – [in] String separator
-
inline string_type record_delimiter() const
Retrieve the record separator (end-of-line string)
- Returns:
Return the record separator (end-of-line string)
-
inline void set_quote_character(string_type const "es)
Set the quote character
This character may be used to enclose a field containing lots of characters that would otherwise need to be escaped. We have to know what it is so that we can escape it ourselves when we encounter the quote character inside fields.
- Parameters:
quotes – [in] Zero or one character to be used as quotation marks
-
inline string_type quote_character() const
Return the current quote characters
- Returns:
Current quote character
-
template<typename token_iter_t>
inline void write_record(token_iter_t begin, token_iter_t end) Write the tokens to the ouput stream
- Parameters:
begin – [in] Point to start writing at
end – [in] Point to stop writing at
Private Functions
-
inline string_type escape_delimiters(string_type const &string_to_escape) const
Escape delimiters when writing
- Parameters:
string_to_escape – [in] Delimiter to escape
- Returns:
The escaped delimiter
-
inline void rebuild_delimiter_regex()
Build the delimiter regex with the field, record and quote delimiters
-
inline string_type escape_characters_for_set(string_type const &in_string)
Escape characters for a given set
- Parameters:
in_string – [in] String to escape character from
Private Members
-
string_type FieldDelimiter
-
std::ostream *OutputStream
-
string_type QuoteCharacter
-
string_type RecordDelimiter
-
regex_type DelimiterRegex
-
typedef string_type::value_type char_type