00001
00002
00003
00004
00005
00006
00007 #include "uniconfgen.h"
00008 #include "strutils.h"
00009
00010
00011
00012 UniConfGen::UniConfGen() :
00013 cbdata(NULL), hold_nesting(0)
00014 {
00015 }
00016
00017
00018 UniConfGen::~UniConfGen()
00019 {
00020 }
00021
00022
00023 void UniConfGen::hold_delta()
00024 {
00025 hold_nesting++;
00026 }
00027
00028
00029 void UniConfGen::unhold_delta()
00030 {
00031 assert(hold_nesting > 0);
00032 if (hold_nesting == 1)
00033 flush_delta();
00034 hold_nesting--;
00035 }
00036
00037
00038 void UniConfGen::clear_delta()
00039 {
00040 deltas.zap();
00041 }
00042
00043
00044 void UniConfGen::flush_delta()
00045 {
00046 UniConfPairList::Iter it(deltas);
00047 for (;;)
00048 {
00049 it.rewind();
00050 if (! it.next())
00051 break;
00052
00053 UniConfKey key((*it).key());
00054 WvString value((*it).value());
00055
00056 it.xunlink();
00057 dispatch_delta(key, value);
00058 }
00059 }
00060
00061
00062 void UniConfGen::dispatch_delta(const UniConfKey &key, WvStringParm value)
00063 {
00064 if (cb)
00065 cb(key, value, cbdata);
00066 }
00067
00068
00069 void UniConfGen::delta(const UniConfKey &key, WvStringParm value)
00070 {
00071 if (hold_nesting == 0)
00072 {
00073
00074 dispatch_delta(key, value);
00075 }
00076 else
00077 {
00078 hold_delta();
00079 deltas.add(new UniConfPair(key, value), true);
00080 unhold_delta();
00081 }
00082 }
00083
00084
00085 bool UniConfGen::haschildren(const UniConfKey &key)
00086 {
00087 hold_delta();
00088
00089 Iter *it = iterator(key);
00090 it->rewind();
00091 bool children = it->next();
00092 delete it;
00093
00094 unhold_delta();
00095 return children;
00096 }
00097
00098
00099 bool UniConfGen::exists(const UniConfKey &key)
00100 {
00101 return ! get(key).isnull();
00102 }
00103
00104
00105 int UniConfGen::str2int(WvStringParm value, int defvalue) const
00106 {
00107
00108 const char *strs[] = {
00109 "true", "yes", "on", "enabled",
00110 "false", "no", "off", "disabled"
00111 };
00112 const size_t numtruestrs = 4;
00113
00114 if (!value.isnull())
00115 {
00116
00117 char *end;
00118 int num = strtol(value.cstr(), &end, 0);
00119 if (end != value.cstr())
00120 return num;
00121
00122
00123 for (size_t i = 0; i < sizeof(strs) / sizeof(const char*); ++i)
00124 if (strcasecmp(value, strs[i]) == 0)
00125 return i < numtruestrs;
00126 }
00127 return defvalue;
00128 }
00129
00130
00131 bool UniConfGen::isok()
00132 {
00133 return true;
00134 }
00135
00136
00137 void UniConfGen::setcallback(const UniConfGenCallback &callback,
00138 void *userdata)
00139 {
00140 cb = callback;
00141 cbdata = userdata;
00142 }