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 
13 WvConfigSection::WvConfigSection(WvStringParm _name)
14  : name(_name)
15 {
16 }
17 
18 
19 WvConfigSection::~WvConfigSection()
20 {
21  // the WvConfigEntryList destructor automatically deletes all its
22  // entries, so no need to worry about doing that.
23 }
24 
25 
26 WvConfigEntry *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 
40 const 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 
47 void 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 
68 void 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 
76 void 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 }
WvString::edit
char * edit()
make the string editable, and return a non-const (char*)
Definition: wvstring.h:397
trim_string
char * trim_string(char *string)
Trims whitespace from the beginning and end of the character string, including carriage return / line...
Definition: strutils.cc:59
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvStream
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:24
WvConfigEntry
Definition: wvconf.h:35