00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "SafeConfigNode.h"
00021 #include "EvolutionSyncClient.h"
00022
00023 #include <boost/foreach.hpp>
00024
00025 SafeConfigNode::SafeConfigNode(const boost::shared_ptr<ConfigNode> &node) :
00026 m_node(node),
00027 m_readOnlyNode(node)
00028 {
00029 }
00030
00031 SafeConfigNode::SafeConfigNode(const boost::shared_ptr<const ConfigNode> &node) :
00032 m_readOnlyNode(node)
00033 {
00034 }
00035
00036 string SafeConfigNode::readProperty(const string &property) const
00037 {
00038 return unescape(m_readOnlyNode->readProperty(escape(property)));
00039 }
00040
00041 void SafeConfigNode::setProperty(const string &property,
00042 const string &value,
00043 const string &comment,
00044 const string *defValue)
00045 {
00046 m_node->setProperty(escape(property),
00047 escape(value),
00048 comment,
00049 defValue);
00050 }
00051
00052 void SafeConfigNode::readProperties(map<string, string> &props) const
00053 {
00054 map<string, string> original;
00055 m_readOnlyNode->readProperties(original);
00056
00057 BOOST_FOREACH(const StringPair &prop, original) {
00058 string key = unescape(prop.first);
00059 string value = unescape(prop.second);
00060
00061 props[key] = value;
00062 }
00063 }
00064
00065 void SafeConfigNode::removeProperty(const string &property)
00066 {
00067 m_node->removeProperty(escape(property));
00068 }
00069
00070 void SafeConfigNode::flush()
00071 {
00072 if (!m_node.get()) {
00073 EvolutionSyncClient::throwError(getName() + ": read-only, flushing not allowed");
00074 }
00075 m_node->flush();
00076 }
00077
00078 string SafeConfigNode::escape(const string &str)
00079 {
00080 string res;
00081 char buffer[4];
00082 res.reserve(str.size() * 3);
00083
00084 BOOST_FOREACH(char c, str) {
00085 if(isalnum(c) ||
00086 c == '-' ||
00087 c == '_') {
00088 res += c;
00089 } else {
00090 sprintf(buffer, "!%02x",
00091 (unsigned int)(unsigned char)c);
00092 res += buffer;
00093 }
00094 }
00095
00096 return res;
00097 }
00098
00099 string SafeConfigNode::unescape(const string &str)
00100 {
00101 string res;
00102 size_t curr;
00103
00104 res.reserve(str.size());
00105
00106 curr = 0;
00107 while (curr < str.size()) {
00108 if (str[curr] == '!') {
00109 string hex = str.substr(curr + 1, 2);
00110 res += (char)strtol(hex.c_str(), NULL, 16);
00111 curr += 3;
00112 } else {
00113 res += str[curr];
00114 curr++;
00115 }
00116 }
00117
00118 return res;
00119 }