2008-04-07 19:49:17 +02:00
|
|
|
/*
|
2009-03-25 15:21:04 +01:00
|
|
|
* Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
|
2009-04-30 18:14:03 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) version 3.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
2008-04-07 19:49:17 +02:00
|
|
|
*/
|
|
|
|
|
2009-10-05 14:49:32 +02:00
|
|
|
#include <syncevo/SafeConfigNode.h>
|
|
|
|
#include <syncevo/SyncContext.h>
|
2008-04-07 19:49:17 +02:00
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2009-10-05 14:49:32 +02:00
|
|
|
#include <syncevo/declarations.h>
|
2009-10-02 17:23:53 +02:00
|
|
|
SE_BEGIN_CXX
|
|
|
|
|
2008-04-07 19:49:17 +02:00
|
|
|
SafeConfigNode::SafeConfigNode(const boost::shared_ptr<ConfigNode> &node) :
|
|
|
|
m_node(node),
|
2009-04-15 20:58:58 +02:00
|
|
|
m_readOnlyNode(node),
|
|
|
|
m_strictMode(true)
|
2008-04-07 19:49:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SafeConfigNode::SafeConfigNode(const boost::shared_ptr<const ConfigNode> &node) :
|
2009-04-15 20:58:58 +02:00
|
|
|
m_readOnlyNode(node),
|
|
|
|
m_strictMode(true)
|
2008-04-07 19:49:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
string SafeConfigNode::readProperty(const string &property) const
|
|
|
|
{
|
|
|
|
return unescape(m_readOnlyNode->readProperty(escape(property)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SafeConfigNode::setProperty(const string &property,
|
|
|
|
const string &value,
|
|
|
|
const string &comment,
|
|
|
|
const string *defValue)
|
|
|
|
{
|
|
|
|
m_node->setProperty(escape(property),
|
|
|
|
escape(value),
|
|
|
|
comment,
|
|
|
|
defValue);
|
|
|
|
}
|
|
|
|
|
2008-04-10 21:54:02 +02:00
|
|
|
void SafeConfigNode::readProperties(map<string, string> &props) const
|
2008-04-07 19:49:17 +02:00
|
|
|
{
|
2008-04-10 21:54:02 +02:00
|
|
|
map<string, string> original;
|
|
|
|
m_readOnlyNode->readProperties(original);
|
2008-04-07 19:49:17 +02:00
|
|
|
|
2008-07-11 22:25:02 +02:00
|
|
|
BOOST_FOREACH(const StringPair &prop, original) {
|
|
|
|
string key = unescape(prop.first);
|
|
|
|
string value = unescape(prop.second);
|
2008-04-07 19:49:17 +02:00
|
|
|
|
2008-07-06 22:36:29 +02:00
|
|
|
props[key] = value;
|
2008-04-07 19:49:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SafeConfigNode::removeProperty(const string &property)
|
|
|
|
{
|
|
|
|
m_node->removeProperty(escape(property));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SafeConfigNode::flush()
|
|
|
|
{
|
|
|
|
if (!m_node.get()) {
|
2009-10-05 14:49:32 +02:00
|
|
|
SyncContext::throwError(getName() + ": read-only, flushing not allowed");
|
2008-04-07 19:49:17 +02:00
|
|
|
}
|
|
|
|
m_node->flush();
|
|
|
|
}
|
|
|
|
|
2009-04-15 20:58:58 +02:00
|
|
|
string SafeConfigNode::escape(const string &str) const
|
2008-04-07 19:49:17 +02:00
|
|
|
{
|
|
|
|
string res;
|
|
|
|
char buffer[4];
|
2009-04-15 20:58:58 +02:00
|
|
|
bool isLeadingSpace = true;
|
2008-04-07 19:49:17 +02:00
|
|
|
res.reserve(str.size() * 3);
|
|
|
|
|
|
|
|
BOOST_FOREACH(char c, str) {
|
2009-04-15 20:58:58 +02:00
|
|
|
if(m_strictMode ?
|
|
|
|
(isalnum(c) ||
|
|
|
|
c == '-' ||
|
|
|
|
c == '_') :
|
|
|
|
!((isLeadingSpace && isspace(c)) ||
|
2009-04-16 14:45:03 +02:00
|
|
|
c == '=' ||
|
2009-04-15 20:58:58 +02:00
|
|
|
c == '\n')) {
|
2008-04-07 19:49:17 +02:00
|
|
|
res += c;
|
2009-04-15 20:58:58 +02:00
|
|
|
if (!isspace(c)) {
|
|
|
|
isLeadingSpace = false;
|
|
|
|
}
|
2008-04-07 19:49:17 +02:00
|
|
|
} else {
|
|
|
|
sprintf(buffer, "!%02x",
|
|
|
|
(unsigned int)(unsigned char)c);
|
|
|
|
res += buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-15 20:58:58 +02:00
|
|
|
// also encode trailing space?
|
|
|
|
if (!m_strictMode) {
|
|
|
|
size_t numspaces = 0;
|
|
|
|
ssize_t off = res.size() - 1;
|
|
|
|
while (off >= 0 && isspace(res[off])) {
|
|
|
|
off--;
|
|
|
|
numspaces++;
|
|
|
|
}
|
|
|
|
res.resize(res.size() - numspaces);
|
|
|
|
BOOST_FOREACH(char c, str.substr(str.size() - numspaces)) {
|
|
|
|
sprintf(buffer, "!%02x",
|
|
|
|
(unsigned int)(unsigned char)c);
|
|
|
|
res += buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 19:49:17 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-04-15 20:58:58 +02:00
|
|
|
string SafeConfigNode::unescape(const string &str) const
|
2008-04-07 19:49:17 +02:00
|
|
|
{
|
|
|
|
string res;
|
|
|
|
size_t curr;
|
|
|
|
|
|
|
|
res.reserve(str.size());
|
|
|
|
|
|
|
|
curr = 0;
|
|
|
|
while (curr < str.size()) {
|
|
|
|
if (str[curr] == '!') {
|
|
|
|
string hex = str.substr(curr + 1, 2);
|
|
|
|
res += (char)strtol(hex.c_str(), NULL, 16);
|
|
|
|
curr += 3;
|
|
|
|
} else {
|
|
|
|
res += str[curr];
|
|
|
|
curr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2009-10-02 17:23:53 +02:00
|
|
|
|
|
|
|
SE_END_CXX
|