1 #include <hocon/config_parse_options.hpp>
2 #include <internal/simple_config_document.hpp>
3 #include <hocon/config_exception.hpp>
4 #include <internal/tokenizer.hpp>
5 #include <internal/config_document_parser.hpp>
6 #include <sstream>
7 #include <boost/algorithm/string.hpp>
8 #include <leatherman/locale/locale.hpp>
9 
10 // Mark string for translation (alias for leatherman::locale::format)
11 using leatherman::locale::_;
12 
13 using namespace std;
14 
15 namespace hocon {
simple_config_document(shared_ptr<const config_node_root> root,config_parse_options opts)16     simple_config_document::simple_config_document(shared_ptr<const config_node_root> root,
17                                                    config_parse_options opts)
18         : _config_node_tree(move(root)), _parse_options(move(opts)) {}
19 
with_value_text(string path,string new_value) const20     unique_ptr<config_document> simple_config_document::with_value_text(string path, string new_value) const
21     {
22         if (new_value.empty()) {
23             throw new config_exception(_("empty value for {1} passed to with_value_text", path));
24         }
25 
26         shared_origin origin = make_shared<simple_config_origin>("single value parsing");
27         token_iterator tokens {origin, unique_ptr<istream>{new stringstream(new_value)}, _parse_options.get_syntax()};
28         shared_node_value parsed_value = config_document_parser::parse_value(move(tokens), origin, _parse_options);
29 
30         return unique_ptr<config_document>{new simple_config_document(
31                 _config_node_tree->set_value(path, parsed_value, _parse_options.get_syntax()),
32                 _parse_options)};
33     }
34 
with_value(string path,shared_ptr<config_value> new_value) const35     unique_ptr<config_document> simple_config_document::with_value(string path,
36                                                                    shared_ptr<config_value> new_value) const
37     {
38         if (!new_value) {
39             throw config_exception(_("null value for {1} passed to with_value", path));
40         }
41         config_render_options options = config_render_options();
42         options = options.set_origin_comments(false);
43         string rendered = new_value->render(options);
44         boost::trim(rendered);
45         return with_value_text(path, rendered);
46     }
47 
without_path(string path) const48     unique_ptr<config_document> simple_config_document::without_path(string path) const
49     {
50         return unique_ptr<config_document>{new simple_config_document(
51                 _config_node_tree->set_value(path, nullptr, _parse_options.get_syntax()), _parse_options)};
52     }
53 
has_path(string const & path) const54     bool simple_config_document::has_path(string const& path) const
55     {
56         return _config_node_tree->has_value(path);
57     }
58 
render() const59     string simple_config_document::render() const
60     {
61         return _config_node_tree->render();
62     }
63 
operator ==(config_document const & lhs,config_document const & rhs)64     bool operator==(config_document const& lhs, config_document const& rhs)
65     {
66         return lhs.render() == rhs.render();
67     }
68 
69 }  // namespace hocon
70