2 * Worldvisions Weaver Software:
3 * Copyright (C) 2002-2005 Net Integration Technologies, Inc.
5 * A UniConf generator that stores keys in memory.
7 #include "unitempgen.h"
10 #include "wvstringcache.h"
11 #include "unilistiter.h"
12 #include "wvlinkerhack.h"
16 static IUniConfGen
*creator(WvStringParm
, IObject
*)
18 return new UniTempGen();
21 static WvMoniker
<IUniConfGen
> reg("temp", creator
);
24 /***** UniTempGen *****/
26 UniTempGen::UniTempGen()
32 UniTempGen::~UniTempGen()
38 WvString
UniTempGen::get(const UniConfKey
&key
)
42 // Look for an empty section at the end.
43 if (!key
.isempty() && key
.last().isempty())
44 return WvString::null
;
45 UniConfValueTree
*node
= root
->find(key
);
49 return WvString::null
;
52 void UniTempGen::notify_deleted(const UniConfValueTree
*node
, void *)
54 delta(node
->fullkey(), WvString::null
);
57 void UniTempGen::set(const UniConfKey
&_key
, WvStringParm _value
)
59 WvString
value(scache
.get(_value
));
62 UniConfKey key
= _key
;
63 // FIXME: Use key.hastrailingslash(), it's shorter and easier and faster
64 bool trailing_slash
= false;
67 // Look for an empty section at the end.
68 UniConfKey last
= key
;
69 key
= last
.pop(last
.numsegments() - 1);
71 trailing_slash
= true;
81 UniConfValueTree
*node
= root
->find(key
);
85 // Issue notifications for every key that gets deleted.
86 node
->visit(wv::bind(&UniTempGen::notify_deleted
, this,
97 else if (!trailing_slash
)
99 UniConfValueTree
*node
= root
;
100 UniConfValueTree
*prev
= NULL
;
103 UniConfKey::Iter
it(key
);
107 bool more
= it
.next(); // not the last node in the key?
111 // we'll have to create the sub-node, since we couldn't
112 // find the most recent part of the key.
113 node
= new UniConfValueTree(prev
, prevkey
,
114 more
? WvString::empty
: value
);
116 if (!prev
) // we just created the root
119 delta(node
->fullkey(), WvString::empty
); // AUTO-VIVIFIED
122 delta(node
->fullkey(), value
); // ADDED
128 // don't have to create the most recent sub-node, but there
129 // are no more sub-nodes; that means we're changing the value
130 // of an existing node.
131 if (value
!= node
->value())
133 node
->setvalue(value
);
135 delta(node
->fullkey(), value
); // CHANGED
141 node
= prev
->findchild(prevkey
);
150 void UniTempGen::setv(const UniConfPairList
&pairs
)
156 bool UniTempGen::haschildren(const UniConfKey
&key
)
160 UniConfValueTree
*node
= root
->find(key
);
161 return node
!= NULL
&& node
->haschildren();
167 UniConfGen::Iter
*UniTempGen::iterator(const UniConfKey
&key
)
171 UniConfValueTree
*node
= root
->find(key
);
174 ListIter
*it
= new ListIter(this);
175 UniConfValueTree::Iter
i(*node
);
176 for (i
.rewind(); i
.next(); )
177 it
->add(i
->key(), i
->value());
185 void UniTempGen::commit()
187 UniConfGen::commit();
191 bool UniTempGen::refresh()
193 return UniConfGen::refresh();