first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / configuration / configurator.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : configurator                                                      *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "configurator.h"
16
17 #include <basis/astring.h>
18 #include <structures/string_table.h>
19 #include <structures/set.h>
20
21 using namespace basis;
22 using namespace structures;
23
24 namespace configuration {
25
26 configurator::~configurator() {}
27
28 astring configurator::load(const astring &section, const astring &entry,
29         const astring &default_string)
30 {
31   astring to_return;
32   if (!get(section, entry, to_return)) {
33     to_return = default_string;
34     if (_behavior == AUTO_STORE) put(section, entry, to_return);
35       // save the entry back if we're in autostore mode.
36   }
37   return to_return;
38 }
39
40 bool configurator::store(const astring &section, const astring &entry,
41     const astring &to_store)
42 { return put(section, entry, to_store); }
43
44 bool configurator::store(const astring &section, const astring &entry,
45     int value)
46 {
47   return store(section, entry, astring(astring::SPRINTF, "%d", value));
48 }
49
50 void configurator::sections(string_array &list)
51 {
52   // default implementation does nothing.
53   list = string_array();
54 }
55
56 void configurator::section_set(string_set &list)
57 {
58   string_array temp;
59   sections(temp);
60   list = temp;
61 }
62
63 int configurator::load(const astring &section, const astring &entry,
64     int def_value)
65 {
66   astring value_string;
67   if (!get(section, entry, value_string)) {
68     if (_behavior == AUTO_STORE) store(section, entry, def_value);
69     return def_value;
70   }
71   return value_string.convert(def_value);
72 }
73
74 bool configurator::section_exists(const astring &section)
75 {
76   string_table infos;
77   // heavy-weight call here...
78   return get_section(section, infos);
79 }
80
81 } //namespace.
82