Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/property_tree/detail/info_parser_write.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // ----------------------------------------------------------------------------
0002 // Copyright (C) 2002-2006 Marcin Kalicinski
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. 
0005 // (See accompanying file LICENSE_1_0.txt or copy at 
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // For more information, see www.boost.org
0009 // ----------------------------------------------------------------------------
0010 #ifndef BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_WRITE_HPP_INCLUDED
0011 #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_WRITE_HPP_INCLUDED
0012 
0013 #include <boost/property_tree/detail/info_parser_error.hpp>
0014 #include <boost/property_tree/detail/info_parser_utils.hpp>
0015 #include <boost/property_tree/detail/info_parser_writer_settings.hpp>
0016 #include <boost/property_tree/ptree.hpp>
0017 
0018 #include <string>
0019 
0020 namespace boost { namespace property_tree { namespace info_parser
0021 {
0022     template<class Ch>
0023     void write_info_indent(std::basic_ostream<Ch> &stream,
0024           int indent,
0025           const info_writer_settings<Ch> &settings
0026           )
0027     {
0028         stream << std::basic_string<Ch>(indent * settings.indent_count, settings.indent_char);
0029     }
0030     
0031     // Create necessary escape sequences from illegal characters
0032     template<class Ch>
0033     std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s)
0034     {
0035         std::basic_string<Ch> result;
0036         typename std::basic_string<Ch>::const_iterator b = s.begin();
0037         typename std::basic_string<Ch>::const_iterator e = s.end();
0038         while (b != e)
0039         {
0040             if (*b == Ch('\0')) result += Ch('\\'), result += Ch('0');
0041             else if (*b == Ch('\a')) result += Ch('\\'), result += Ch('a');
0042             else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
0043             else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
0044             else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
0045             else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
0046             else if (*b == Ch('\v')) result += Ch('\\'), result += Ch('v');
0047             else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
0048             else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
0049             else
0050                 result += *b;
0051             ++b;
0052         }
0053         return result;
0054     }
0055 
0056     template<class Ch>
0057     bool is_simple_key(const std::basic_string<Ch> &key)
0058     {
0059         const static std::basic_string<Ch> chars = convert_chtype<Ch, char>(" \t{};\n\"");
0060         return !key.empty() && key.find_first_of(chars) == key.npos;
0061     }
0062     
0063     template<class Ch>
0064     bool is_simple_data(const std::basic_string<Ch> &data)
0065     {
0066         const static std::basic_string<Ch> chars = convert_chtype<Ch, char>(" \t{};\n\"");
0067         return !data.empty() && data.find_first_of(chars) == data.npos;
0068     }
0069 
0070     template<class Ptree>
0071     void write_info_helper(std::basic_ostream<typename Ptree::key_type::value_type> &stream, 
0072                            const Ptree &pt, 
0073                            int indent,
0074                            const info_writer_settings<typename Ptree::key_type::value_type> &settings)
0075     {
0076 
0077         // Character type
0078         typedef typename Ptree::key_type::value_type Ch;
0079         
0080         // Write data
0081         if (indent >= 0)
0082         {
0083             if (!pt.data().empty())
0084             {
0085                 std::basic_string<Ch> data = create_escapes(pt.template get_value<std::basic_string<Ch> >());
0086                 if (is_simple_data(data))
0087                     stream << Ch(' ') << data << Ch('\n');
0088                 else
0089                     stream << Ch(' ') << Ch('\"') << data << Ch('\"') << Ch('\n');
0090             }
0091             else if (pt.empty())
0092                 stream << Ch(' ') << Ch('\"') << Ch('\"') << Ch('\n');
0093             else
0094                 stream << Ch('\n');
0095         }
0096         
0097         // Write keys
0098         if (!pt.empty())
0099         {
0100             
0101             // Open brace
0102             if (indent >= 0)
0103             {
0104                 write_info_indent( stream, indent, settings);
0105                 stream << Ch('{') << Ch('\n');
0106             }
0107             
0108             // Write keys
0109             typename Ptree::const_iterator it = pt.begin();
0110             for (; it != pt.end(); ++it)
0111             {
0112 
0113                 // Output key
0114                 std::basic_string<Ch> key = create_escapes(it->first);
0115                 write_info_indent( stream, indent+1, settings);
0116                 if (is_simple_key(key))
0117                     stream << key;
0118                 else
0119                     stream << Ch('\"') << key << Ch('\"');
0120 
0121                 // Output data and children  
0122                 write_info_helper(stream, it->second, indent + 1, settings);
0123 
0124             }
0125             
0126             // Close brace
0127             if (indent >= 0)
0128             {
0129                 write_info_indent( stream, indent, settings);
0130                 stream << Ch('}') << Ch('\n');
0131             }
0132 
0133         }
0134     }
0135 
0136     // Write ptree to info stream
0137     template<class Ptree>
0138     void write_info_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream, 
0139                              const Ptree &pt,
0140                              const std::string &filename,
0141                              const info_writer_settings<typename Ptree::key_type::value_type> &settings)
0142     {
0143         write_info_helper(stream, pt, -1, settings);
0144         stream.flush();
0145         if (!stream.good())
0146             BOOST_PROPERTY_TREE_THROW(info_parser_error("write error", filename, 0));
0147     }
0148 
0149 } } }
0150 
0151 #endif