1 | /****************************************************************************
|
---|
2 | ** varlist.cpp - class for handling a list of string vars
|
---|
3 | ** Copyright (C) 2001, 2002 Justin Karneges
|
---|
4 | **
|
---|
5 | ** This program is free software; you can redistribute it and/or
|
---|
6 | ** modify it under the terms of the GNU General Public License
|
---|
7 | ** as published by the Free Software Foundation; either version 2
|
---|
8 | ** of the License, or (at your option) any later version.
|
---|
9 | **
|
---|
10 | ** This program is distributed in the hope that it will be useful,
|
---|
11 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | ** GNU General Public License for more details.
|
---|
14 | **
|
---|
15 | ** You should have received a copy of the GNU General Public License
|
---|
16 | ** along with this program; if not, write to the Free Software
|
---|
17 | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
---|
18 | **
|
---|
19 | ****************************************************************************/
|
---|
20 |
|
---|
21 | #include"varlist.h"
|
---|
22 |
|
---|
23 |
|
---|
24 | VarList::VarList()
|
---|
25 | :QValueList<VarListItem>()
|
---|
26 | {
|
---|
27 | }
|
---|
28 |
|
---|
29 | QStringList VarList::varsToStringList()
|
---|
30 | {
|
---|
31 | QStringList list;
|
---|
32 | for(VarList::Iterator it = begin(); it != end(); ++it)
|
---|
33 | list.append((*it).key());
|
---|
34 | return list;
|
---|
35 | }
|
---|
36 |
|
---|
37 | QDomElement VarList::toXml(QDomDocument &doc, const QString &tagName)
|
---|
38 | {
|
---|
39 | QDomElement base = doc.createElement(tagName);
|
---|
40 | for(VarList::Iterator it = begin(); it != end(); ++it) {
|
---|
41 | QDomElement tag = doc.createElement("item");
|
---|
42 | tag.setAttribute("name", (*it).key());
|
---|
43 | QDomText text = doc.createTextNode((*it).data());
|
---|
44 | tag.appendChild(text);
|
---|
45 | base.appendChild(tag);
|
---|
46 | }
|
---|
47 | return base;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void VarList::fromXml(const QDomElement &e)
|
---|
51 | {
|
---|
52 | clear();
|
---|
53 |
|
---|
54 | for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
---|
55 | QDomElement i = n.toElement();
|
---|
56 | if(i.isNull())
|
---|
57 | continue;
|
---|
58 | if(i.tagName() == "item") {
|
---|
59 | QString var, val;
|
---|
60 | var = i.attribute("name");
|
---|
61 | QDomText t = i.firstChild().toText();
|
---|
62 | if(!t.isNull())
|
---|
63 | val = t.data();
|
---|
64 | set(var, val);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | void VarList::set(const QString &key, const QString &data)
|
---|
70 | {
|
---|
71 | VarList::Iterator it = findByKey(key);
|
---|
72 | if(it == end()) {
|
---|
73 | VarListItem v;
|
---|
74 | v.setKey(key);
|
---|
75 | v.setData(data);
|
---|
76 | append(v);
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | (*it).setData(data);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | void VarList::unset(const QString &key)
|
---|
84 | {
|
---|
85 | VarList::Iterator it = findByKey(key);
|
---|
86 | if(it == end())
|
---|
87 | return;
|
---|
88 | remove(it);
|
---|
89 | }
|
---|
90 |
|
---|
91 | const QString & VarList::get(const QString &key)
|
---|
92 | {
|
---|
93 | VarList::Iterator it = findByKey(key);
|
---|
94 | return (*it).data();
|
---|
95 | }
|
---|
96 |
|
---|
97 | VarList::Iterator VarList::findByKey(const QString &key)
|
---|
98 | {
|
---|
99 | VarList::Iterator it;
|
---|
100 | for(it = begin(); it != end(); ++it) {
|
---|
101 | if((*it).key() == key)
|
---|
102 | break;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return it;
|
---|
106 | }
|
---|
107 |
|
---|
108 | VarList::Iterator VarList::findByNum(int x)
|
---|
109 | {
|
---|
110 | int n = 0;
|
---|
111 | VarList::Iterator it;
|
---|
112 | for(it = begin(); it != end(); ++it) {
|
---|
113 | if(n >= x)
|
---|
114 | break;
|
---|
115 | ++n;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return it;
|
---|
119 | }
|
---|