WvStreams
uniwvconfgen.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2002 Net Integration Technologies, Inc.
4  *
5  * A generator to make a UniConf object out of a WvConf.
6  */
7 #include "wvconf.h"
8 #include "uniwvconfgen.h"
9 #include "wvmoniker.h"
10 
15 {
16 protected:
17  WvConfigSection::Iter i;
18 
19 public:
21 
22  /***** Overridden members *****/
23 
24  virtual void rewind();
25  virtual bool next();
26  virtual UniConfKey key() const;
27  virtual WvString value() const;
28 };
29 
30 
31 static IUniConfGen *creator(WvStringParm s, IObject*)
32 {
33  return new UniWvConfGen(new WvConf(s));
34 }
35 
36 static WvMoniker<IUniConfGen> reg("wvconf", creator);
37 
38 
39 void UniWvConfGen::notify(void *userdata, WvStringParm section,
40  WvStringParm entry, WvStringParm oldval,
41  WvStringParm newval)
42 {
43  UniConfKey key(section, entry);
44 
45  tempvalue = newval;
46  tempkey = &key;
47  delta(key, newval);
48  tempkey = NULL;
49 }
50 
51 
52 UniWvConfGen::UniWvConfGen(WvConf *_cfg):
53  tempkey(NULL), tempvalue(), cfg(_cfg)
54 {
55  cfg->add_callback(wv::bind(&UniWvConfGen::notify, this, _1, _2, _3, _4, _5),
56  NULL, "", "", this);
57 }
58 
59 
60 UniWvConfGen::~UniWvConfGen()
61 {
62  if (cfg)
63  delete cfg;
64 }
65 
66 
68 {
69  if (tempkey && key == *tempkey)
70  return tempvalue;
71  else
72  return cfg->get(key.first(), key.last(key.numsegments() - 1));
73 }
74 
75 
76 void UniWvConfGen::set(const UniConfKey &key, WvStringParm value)
77 {
78  WvString section = key.first();
79  WvString keyname = key.last(key.numsegments() - 1);
80 
81  WvConfigSection *sect = (*cfg)[section];
82  if (value == WvString::null && sect)
83  cfg->delete_section(key);
84  else
85  cfg->set(section, keyname, value);
86 }
87 
88 
89 void UniWvConfGen::setv(const UniConfPairList &pairs)
90 {
91  setv_naive(pairs);
92 }
93 
94 
96 {
97  WvConfigSection *sect = (*cfg)[key];
98  if (sect)
99  return true;
100  return false;
101 }
102 
103 
105 {
106  WvConfigSection *sect = (*cfg)[key];
107 
108  if (sect)
109  return new WvConfIter(sect);
110  else
111  return NULL;
112 }
113 
114 
115 
116 /***** UniWvConfGen::WvConfIter *****/
117 
118 UniWvConfGen::WvConfIter::WvConfIter(WvConfigSection *sect)
119  : i(*sect)
120 {
121 }
122 
123 
125 {
126  i.rewind();
127 }
128 
129 
131 {
132  return i.next();
133 }
134 
135 
137 {
138  return i->name;
139 }
140 
141 
143 {
144  return i->value;
145 }