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 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #include "SafeConfigNode.h"
21 #include "EvolutionSyncClient.h"
23 #include <boost/foreach.hpp>
25 SafeConfigNode::SafeConfigNode(const boost::shared_ptr<ConfigNode> &node) :
31 SafeConfigNode::SafeConfigNode(const boost::shared_ptr<const ConfigNode> &node) :
36 string SafeConfigNode::readProperty(const string &property) const
38 return unescape(m_readOnlyNode->readProperty(escape(property)));
41 void SafeConfigNode::setProperty(const string &property,
43 const string &comment,
44 const string *defValue)
46 m_node->setProperty(escape(property),
52 void SafeConfigNode::readProperties(map<string, string> &props) const
54 map<string, string> original;
55 m_readOnlyNode->readProperties(original);
57 BOOST_FOREACH(const StringPair &prop, original) {
58 string key = unescape(prop.first);
59 string value = unescape(prop.second);
65 void SafeConfigNode::removeProperty(const string &property)
67 m_node->removeProperty(escape(property));
70 void SafeConfigNode::flush()
73 EvolutionSyncClient::throwError(getName() + ": read-only, flushing not allowed");
78 string SafeConfigNode::escape(const string &str)
82 res.reserve(str.size() * 3);
84 BOOST_FOREACH(char c, str) {
90 sprintf(buffer, "!%02x",
91 (unsigned int)(unsigned char)c);
99 string SafeConfigNode::unescape(const string &str)
104 res.reserve(str.size());
107 while (curr < str.size()) {
108 if (str[curr] == '!') {
109 string hex = str.substr(curr + 1, 2);
110 res += (char)strtol(hex.c_str(), NULL, 16);