18 #include <boost/foreach.hpp>
29 assert_in(
false,
"Tried to write list data inside a map");
36 assert_in(
false,
"Tried to write list data inside a map");
43 assert_in(
true,
"Tried to write map data inside a list");
51 assert_in(
true,
"Tried to write map data inside a list");
70 for (
size_t i =
depth(); i > desired_depth; --i )
end_raw();
81 static string bare_chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_. ";
82 static string hex =
"0123456789ABCDEF";
84 needs_quotes_out = !(s.find_first_not_of(bare_chars) == string::npos);
86 if ( s.empty() || s[0] ==
' ' || s[s.size()-1] ==
' ' ) needs_quotes_out =
true;
87 if ( !needs_quotes_out )
return s;
91 BOOST_FOREACH (
char ch, s ) {
92 if (
' ' <= ch && ch <=
'~' ) o << ch;
93 else if ( ch ==
'"' ) o <<
"\\\"";
94 else if ( ch ==
'\\' ) o <<
"\\\\";
95 else if ( ch ==
'\n' ) o <<
"\\n";
96 else if ( ch ==
'\r' ) o <<
"\\r";
97 else if ( ch ==
'\t' ) o <<
"\\t";
98 else if ( ch ==
'\f' ) o <<
"\\f";
99 else if ( ch ==
'\b' ) o <<
"\\b";
102 o << hex[ ((ch>>24)&0x000F) ];
103 o << hex[ ((ch>>16)&0x000F) ];
104 o << hex[ ((ch>> 8)&0x000F) ];
105 o << hex[ ((ch )&0x000F) ];
int depth() const
Number of closing brackets/braces required to end document (non-negative)
void end_list()
Counterpart to start_list() – writes closing bracket.
bool assert_in(bool in_map, std::string const &msg)
Check that we're in the expected context (either map or list)
void end_map()
Counterpart to start_map() – writes closing brace.
void start_list(bool indent=true)
Write method for use inside lists / arrays.
void end(size_t desired_depth=0)
By default, closes all open maps/lists, ending the document.
void start_map(bool indent=true)
Write method for use inside lists / arrays.
virtual void start_raw(bool is_map, bool indent)=0
Actual implementation of start_map() and start_list().
Lightweight class to ease writting YAML documents.
virtual void end_raw()=0
Actual implementation of end_map() and end_list().
virtual void do_indent(bool write_comma=true)=0
Handle pretty-printing indentation. Don't want to use commas for opening/closing brace/bracket.
void write_label(std::string const &label)
Write the key part of a key-value pair.
std::string escape_string(std::string const &s, bool &needs_quotes_out)
Converts special characters (newlines, etc) to escape codes ( , etc).