WvStreams
cfgsection.cc
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Implementation of the WvConfigSection class. 
00006  *
00007  * Created:     Sept 28 1997            D. Coombs
00008  *
00009  */
00010 #include "wvconf.h"
00011 
00012 
00013 WvConfigSection::WvConfigSection(WvStringParm _name)
00014         : name(_name)
00015 {
00016 }
00017 
00018 
00019 WvConfigSection::~WvConfigSection()
00020 {
00021     // the WvConfigEntryList destructor automatically deletes all its
00022     // entries, so no need to worry about doing that.
00023 }
00024 
00025 
00026 WvConfigEntry *WvConfigSection::operator[] (WvStringParm ename)
00027 {
00028     Iter i(*this);
00029 
00030     for (i.rewind(); i.next();)
00031     {
00032         if (strcasecmp(i().name, ename) == 0)
00033             return &i();
00034     }
00035 
00036     return NULL;
00037 }
00038 
00039 
00040 const char *WvConfigSection::get(WvStringParm entry, const char *def_val)
00041 {
00042     WvConfigEntry *e = (*this)[entry];
00043     return e ? (const char *)e->value : def_val;
00044 }
00045 
00046 
00047 void WvConfigSection::set(WvStringParm entry, WvStringParm value)
00048 {
00049     WvString clean_entry = entry;
00050     trim_string(clean_entry.edit());
00051     WvConfigEntry *e = (*this)[clean_entry];
00052     
00053     // need to delete the entry?
00054     if (!value || !value[0])
00055     {
00056         if (e) unlink(e);
00057         return;
00058     }
00059     
00060     // otherwise, add the entry requested
00061     if (e)
00062         e->set(value);
00063     else
00064         append(new WvConfigEntry(clean_entry, value), true);
00065 }
00066 
00067 
00068 void WvConfigSection::quick_set(WvStringParm entry, WvStringParm value)
00069 {
00070     WvString clean_entry = entry;
00071     trim_string(clean_entry.edit());
00072     append(new WvConfigEntry(clean_entry, value), true);
00073 }
00074 
00075 
00076 void WvConfigSection::dump(WvStream &fp)
00077 {
00078     Iter i(*this);
00079 
00080     for (i.rewind(); i.next(); )
00081     {
00082         WvConfigEntry &e = *i;
00083         if (e.value && e.value[0])
00084             fp.print("%s = %s\n", e.name, e.value);
00085         else
00086             fp.print("%s =\n", e.name);
00087     }
00088 }