unilistiter.cc

00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 2002 Net Integration Technologies, Inc.
00004  * 
00005  * A simple implementation of a UniConfGen iterator.  See unilistiter.h.
00006  */
00007 #include "unilistiter.h"
00008 
00009 const WvString UniListIter::noval = "whatever";
00010 
00011 
00012 UniSmartKey::UniSmartKey(const UniSmartKey *_parent, const UniConfKey &_child)
00013     : parent(_parent), child(_child)
00014 { 
00015     // nothing special
00016 }
00017 
00018 
00019 UniConfKey UniSmartKey::key() const
00020 {
00021     if (parent)
00022         return UniConfKey(parent->key(), child);
00023     else
00024         return child;
00025 }
00026     
00027 
00028 bool UniSmartKey::operator== (const UniSmartKey &k) const
00029 {
00030     return &k == this || k.key() == key();
00031 }
00032 
00033 
00034 unsigned WvHash(const UniSmartKey &k)
00035 {
00036     return ::WvHash(k.key());
00037 }
00038 
00039 
00040 UniListIter::UniListIter(IUniConfGen *_gen)
00041     : ki(keys), vi(values)
00042 {
00043     gen = _gen;
00044     no_more_values = false;
00045 }
00046 
00047 
00048 UniSmartKey *UniListIter::new_smart_key(const UniConfKey &k)
00049 {
00050     UniSmartKey *s = keylook[UniSmartKey(NULL, k.removelast())];
00051     if (s)
00052         return new UniSmartKey(s, scache.get(k.last()));
00053     else
00054         return new UniSmartKey(NULL, scache.get(k));
00055 }
00056     
00057 
00058 void UniListIter::add(WvStringParm k, WvStringParm v)
00059 { 
00060     UniSmartKey *sk = new_smart_key(k);
00061     // UniSmartKey *sk = new UniSmartKey(NULL, k);
00062     keys.append(sk, true);
00063     keylook.add(sk, false);
00064     if (v.cstr() != noval.cstr())
00065     {
00066         assert(!no_more_values);
00067         values.append(new WvString(scache.get(v)), true);
00068     }
00069     else
00070         no_more_values = true;
00071 }
00072 
00073 
00074 void UniListIter::autofill(IUniConfGen::Iter *_source)
00075 {
00076     IUniConfGen::Iter &source(*_source);
00077     for (source.rewind(); source.next(); )
00078         add(source.key(), source.value());
00079 }
00080 
00081 
00082 void UniListIter::rewind()
00083 {
00084     ki.rewind();
00085     vi.rewind();
00086 }
00087 
00088 
00089 bool UniListIter::next()
00090 {
00091     if (vi.cur())
00092         vi.next();
00093     return ki.next();
00094 }
00095 
00096 
00097 UniConfKey UniListIter::key() const
00098 {
00099     return ki->key();
00100 }
00101 
00102 
00103 WvString UniListIter::value() const
00104 {
00105     if (vi.cur())
00106         return *vi;
00107     else
00108         return gen->get(ki->key());
00109 }

Generated on Thu May 25 21:51:02 2006 for WvStreams by  doxygen 1.4.6