summaryrefslogtreecommitdiffstats
path: root/src/parser/parameter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser/parameter.cpp')
-rw-r--r--src/parser/parameter.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/parser/parameter.cpp b/src/parser/parameter.cpp
new file mode 100644
index 0000000..ef5e9de
--- /dev/null
+++ b/src/parser/parameter.cpp
@@ -0,0 +1,90 @@
+/*
+ Copyright (C) 2005-2009 Michel de Boer <michel@twinklephone.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include "parameter.h"
+#include "util.h"
+
+t_parameter::t_parameter() {
+ type = VALUE;
+}
+
+t_parameter::t_parameter(const string &n) {
+ type = NOVALUE;
+ name = n;
+}
+
+t_parameter::t_parameter(const string &n, const string &v) {
+ type = VALUE;
+ name = n;
+ value = v;
+}
+
+string t_parameter::encode(void) const {
+ string s;
+
+ s += name;
+
+ if (type == VALUE) {
+ s += '=';
+ if (must_quote(value)) {
+ s += '\"' + value + '\"';
+ } else {
+ s += value;
+ }
+ }
+
+ return s;
+}
+
+bool t_parameter::operator==(const t_parameter &rhs) {
+ return (type == rhs.type && name == rhs.name);
+}
+
+t_parameter str2param(const string &s) {
+ vector<string> l = split_on_first(s, '=');
+ if (l.size() == 1) {
+ return t_parameter(s);
+ } else {
+ return t_parameter(trim(l[0]), trim(l[1]));
+ }
+}
+
+string param_list2str(const list<t_parameter> &l) {
+ string s;
+
+ for (list<t_parameter>::const_iterator i = l.begin();
+ i != l.end(); i++)
+ {
+ s += ';';
+ s += i->encode();
+ }
+
+ return s;
+}
+
+list<t_parameter> str2param_list(const string &s) {
+ list<t_parameter> result;
+
+ vector<string> l = split(s, ';');
+ for (vector<string>::const_iterator i = l.begin(); i != l.end(); i++) {
+ t_parameter p = str2param(trim(*i));
+ result.push_back(p);
+ }
+
+ return result;
+}