2 * Worldvisions Weaver Software:
3 * Copyright (C) 2002 Net Integration Technologies, Inc.
5 * An abstract data container that backs a UniConf tree.
7 #include "uniconfgen.h"
10 // FIXME: interfaces (IUniConfGen) shouldn't have implementations!
11 IUniConfGen::~IUniConfGen()
15 UUID_MAP_BEGIN(UniConfGen
)
16 UUID_MAP_ENTRY(IObject
)
17 UUID_MAP_ENTRY(IUniConfGen
)
20 UniConfGen::UniConfGen()
26 UniConfGen::~UniConfGen()
28 assert(cblist
.isempty());
32 void UniConfGen::hold_delta()
38 void UniConfGen::unhold_delta()
40 assert(hold_nesting
> 0);
41 if (hold_nesting
== 1)
47 void UniConfGen::clear_delta()
53 void UniConfGen::flush_delta()
55 UniConfPairList::Iter
it(deltas
);
62 UniConfKey
key((*it
).key());
63 WvString
value((*it
).value());
66 dispatch_delta(key
, value
);
71 void UniConfGen::dispatch_delta(const UniConfKey
&key
, WvStringParm value
)
77 void UniConfGen::delta(const UniConfKey
&key
, WvStringParm value
)
79 if (hold_nesting
== 0)
81 // not nested, dispatch immediately
82 dispatch_delta(key
, value
);
87 deltas
.add(new UniConfPair(key
, value
), true);
93 void UniConfGen::setv_naive(const UniConfPairList
&pairs
)
95 UniConfPairList::Iter
pair(pairs
);
96 for (pair
.rewind(); pair
.next(); )
97 set(pair
->key(), pair
->value());
101 bool UniConfGen::haschildren(const UniConfKey
&key
)
103 bool children
= false;
107 Iter
*it
= iterator(key
);
111 if (it
->next()) children
= true;
120 bool UniConfGen::exists(const UniConfKey
&key
)
122 return !get(key
).isnull();
126 int UniConfGen::str2int(WvStringParm value
, int defvalue
) const
128 // also recognize bool strings as integers
129 const char *strs
[] = {
130 "true", "yes", "on", "enabled",
131 "false", "no", "off", "disabled"
133 const size_t numtruestrs
= 4;
137 // try to recognize an integer
139 int num
= strtol(value
.cstr(), &end
, 0);
140 if (end
!= value
.cstr())
141 return num
; // was a valid integer
143 // try to recognize a special string
144 for (size_t i
= 0; i
< sizeof(strs
) / sizeof(const char*); ++i
)
145 if (strcasecmp(value
, strs
[i
]) == 0)
146 return i
< numtruestrs
;
152 bool UniConfGen::isok()
158 void UniConfGen::add_callback(void *cookie
,
159 const UniConfGenCallback
&callback
)
161 cblist
.add(callback
, cookie
);
165 void UniConfGen::del_callback(void *cookie
)
172 class _UniConfGenRecursiveIter
: public IUniConfGen::Iter
174 WvList
<IUniConfGen::Iter
> itlist
;
176 UniConfKey top
, current
;
180 _UniConfGenRecursiveIter(IUniConfGen
*_gen
, const UniConfKey
&_top
)
187 virtual ~_UniConfGenRecursiveIter() { }
189 virtual void rewind()
195 Iter
*subi
= gen
->iterator(top
);
199 itlist
.prepend(subi
, true);
205 //assert(!itlist.isempty()); // trying to seek past the end is illegal!
211 UniConfKey
subkey(itlist
.first()->key());
212 UniConfKey
newkey(current
, subkey
);
213 //fprintf(stderr, "subiter: '%s'\n", newkey.cstr());
214 Iter
*newsub
= gen
->iterator(UniConfKey(top
, newkey
));
217 current
.append(subkey
);
218 //fprintf(stderr, "current is now: '%s'\n", current.cstr());
220 itlist
.prepend(newsub
, true);
224 WvList
<IUniConfGen::Iter
>::Iter
i(itlist
);
225 for (i
.rewind(); i
.next(); )
227 if (i
->next()) // NOTE: not the same as i.next()
229 // set up so next time, we go into its subtree
234 // otherwise, this iterator is empty; move up the tree
235 current
= current
.removelast();
236 //fprintf(stderr, "current is now: '%s'\n", current.cstr());
244 virtual UniConfKey
key() const
246 //fprintf(stderr, "current is now: '%s'\n", current.cstr());
247 if (!itlist
.isempty())
248 return UniConfKey(current
, itlist
.first()->key());
253 virtual WvString
value() const
255 return gen
->get(UniConfKey(top
, key()));
260 UniConfGen::Iter
*UniConfGen::recursiveiterator(const UniConfKey
&key
)
262 return new _UniConfGenRecursiveIter(this, key
);