2 * Copyright (C) 2008 Patrick Ohly
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 #include "SafeConfigNode.h"
20 #include "EvolutionSyncClient.h"
22 #include <boost/foreach.hpp>
24 SafeConfigNode::SafeConfigNode(const boost::shared_ptr<ConfigNode> &node) :
30 SafeConfigNode::SafeConfigNode(const boost::shared_ptr<const ConfigNode> &node) :
35 string SafeConfigNode::readProperty(const string &property) const
37 return unescape(m_readOnlyNode->readProperty(escape(property)));
40 void SafeConfigNode::setProperty(const string &property,
42 const string &comment,
43 const string *defValue)
45 m_node->setProperty(escape(property),
51 void SafeConfigNode::readProperties(map<string, string> &props) const
53 map<string, string> original;
54 m_readOnlyNode->readProperties(original);
56 BOOST_FOREACH(const StringPair &prop, original) {
57 string key = unescape(prop.first);
58 string value = unescape(prop.second);
64 void SafeConfigNode::removeProperty(const string &property)
66 m_node->removeProperty(escape(property));
69 void SafeConfigNode::flush()
72 EvolutionSyncClient::throwError(getName() + ": read-only, flushing not allowed");
77 string SafeConfigNode::escape(const string &str)
81 res.reserve(str.size() * 3);
83 BOOST_FOREACH(char c, str) {
89 sprintf(buffer, "!%02x",
90 (unsigned int)(unsigned char)c);
98 string SafeConfigNode::unescape(const string &str)
103 res.reserve(str.size());
106 while (curr < str.size()) {
107 if (str[curr] == '!') {
108 string hex = str.substr(curr + 1, 2);
109 res += (char)strtol(hex.c_str(), NULL, 16);