WvStreams
cfgsection.cc
1/*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Implementation of the WvConfigSection class.
6 *
7 * Created: Sept 28 1997 D. Coombs
8 *
9 */
10#include "wvconf.h"
11
12
13WvConfigSection::WvConfigSection(WvStringParm _name)
14 : name(_name)
15{
16}
17
18
19WvConfigSection::~WvConfigSection()
20{
21 // the WvConfigEntryList destructor automatically deletes all its
22 // entries, so no need to worry about doing that.
23}
24
25
26WvConfigEntry *WvConfigSection::operator[] (WvStringParm ename)
27{
28 Iter i(*this);
29
30 for (i.rewind(); i.next();)
31 {
32 if (strcasecmp(i().name, ename) == 0)
33 return &i();
34 }
35
36 return NULL;
37}
38
39
40const char *WvConfigSection::get(WvStringParm entry, const char *def_val)
41{
42 WvConfigEntry *e = (*this)[entry];
43 return e ? (const char *)e->value : def_val;
44}
45
46
47void WvConfigSection::set(WvStringParm entry, WvStringParm value)
48{
49 WvString clean_entry = entry;
50 trim_string(clean_entry.edit());
51 WvConfigEntry *e = (*this)[clean_entry];
52
53 // need to delete the entry?
54 if (!value || !value[0])
55 {
56 if (e) unlink(e);
57 return;
58 }
59
60 // otherwise, add the entry requested
61 if (e)
62 e->set(value);
63 else
64 append(new WvConfigEntry(clean_entry, value), true);
65}
66
67
68void WvConfigSection::quick_set(WvStringParm entry, WvStringParm value)
69{
70 WvString clean_entry = entry;
71 trim_string(clean_entry.edit());
72 append(new WvConfigEntry(clean_entry, value), true);
73}
74
75
76void WvConfigSection::dump(WvStream &fp)
77{
78 Iter i(*this);
79
80 for (i.rewind(); i.next(); )
81 {
82 WvConfigEntry &e = *i;
83 if (e.value && e.value[0])
84 fp.print("%s = %s\n", e.name, e.value);
85 else
86 fp.print("%s =\n", e.name);
87 }
88}
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition wvstring.h:94
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition wvstream.h:25
WvString is an implementation of a simple and efficient printable-string class.
Definition wvstring.h:330
char * edit()
make the string editable, and return a non-const (char*)
Definition wvstring.h:397
char * trim_string(char *string)
Trims whitespace from the beginning and end of the character string, including carriage return / line...
Definition strutils.cc:59