first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / configuration / configlet.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : configlet                                                         *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2001-$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 "configlet.h"
16 #include "configurator.h"
17
18 #include <basis/astring.h>
19 #include <basis/functions.h>
20
21 using namespace basis;
22
23 namespace configuration {
24
25 const astring bogus_default = "OOPS: not supposed to ever be seen.  d'oh!";
26
27 //////////////
28
29 configlet::configlet(const astring &section, const astring &entry)
30 : _section(new astring(section)),
31   _entry(new astring(entry))
32 {}
33
34 configlet::configlet(const configlet &to_copy)
35 : _section(new astring(*to_copy._section)),
36   _entry(new astring(*to_copy._entry))
37 {}
38
39 configlet::~configlet()
40 {
41   WHACK(_section);
42   WHACK(_entry);
43 }
44
45 configlet &configlet::operator =(const configlet &to_copy)
46 {
47   if (this == &to_copy) return *this;
48   *_section = *to_copy._section;
49   *_entry = *to_copy._entry;
50   return *this;
51 }
52
53 const astring &configlet::section() const { return *_section; }
54
55 const astring &configlet::entry() const { return *_entry; }
56
57 void configlet::section(const astring &new_section) const
58 { *_section = new_section; }
59
60 void configlet::entry(const astring &new_entry) const
61 { *_entry = new_entry; }
62
63 //////////////
64
65 string_configlet::string_configlet(const astring &section, const astring &entry,
66     const astring &current_value, const astring &default_value)
67 : configlet(section, entry),
68   _current(new astring(current_value)),
69   _default(new astring(default_value))
70 {}
71
72 string_configlet::string_configlet(const string_configlet &to_copy)
73 : configlet(to_copy.section(), to_copy.entry()),
74   _current(new astring(*to_copy._current)),
75   _default(new astring(*to_copy._default))
76 {
77 }
78
79 string_configlet::~string_configlet()
80 {
81   WHACK(_current);
82   WHACK(_default);
83 }
84
85 string_configlet &string_configlet::operator =(const string_configlet &to_copy)
86 {
87   if (this == &to_copy) return *this;
88   (configlet &)*this = to_copy;
89   *_current = *to_copy._current;
90   *_default = *to_copy._default;
91   return *this;
92 }
93
94 const astring &string_configlet::current_value() const { return *_current; }
95
96 const astring &string_configlet::default_value() const { return *_default; }
97
98 void string_configlet::current_value(const astring &new_current)
99 { *_current = new_current; }
100
101 void string_configlet::default_value(const astring &new_default)
102 { *_default = new_default; }
103   
104 bool string_configlet::load(configurator &config)
105 {
106   if (config.get(section(), entry(), *_current)) return true;  // success.
107   // we failed to read the value...
108   *_current = *_default;
109   if (config.behavior() == configurator::AUTO_STORE)
110     config.put(section(), entry(), *_current);
111   return false;  // don't hide that it wasn't there.
112 }
113
114 bool string_configlet::store(configurator &config) const
115 { return config.put(section(), entry(), *_current); }
116
117 configlet *string_configlet::duplicate() const
118 { return new string_configlet(section(), entry(), *_current, *_default); }
119
120 //////////////
121
122 int_configlet::int_configlet(const astring &section, const astring &entry,
123     int current_value, int default_value)
124 : configlet(section, entry),
125   _current(current_value),
126   _default(default_value)
127 {
128 }
129
130 int_configlet::~int_configlet() {}
131
132 void int_configlet::current_value(int new_current)
133 { _current = new_current; }
134
135 bool int_configlet::load(configurator &config)
136 {
137   astring temp;
138   bool to_return = config.get(section(), entry(), temp);
139     // just check if it was already there.
140   int temp_c = config.load(section(), entry(), _default);
141   current_value(temp_c);
142   return to_return;
143 }
144
145 bool int_configlet::store(configurator &config) const
146 { return config.store(section(), entry(), _current); }
147
148 configlet *int_configlet::duplicate() const
149 { return new int_configlet(*this); }
150
151 //////////////
152
153 bounded_int_configlet::bounded_int_configlet(const astring &section,
154     const astring &entry, int current_value, int default_value,
155     int minimum, int maximum)
156 : int_configlet(section, entry, current_value, default_value),
157   _minimum(minimum),
158   _maximum(maximum)
159 {
160 }
161
162 bounded_int_configlet::~bounded_int_configlet() {}
163
164 void bounded_int_configlet::current_value(int new_current)
165 {
166   if (new_current < _minimum)
167     new_current = default_value();
168   if (new_current > _maximum)
169     new_current = default_value();
170   int_configlet::current_value(new_current);
171 }
172
173 configlet *bounded_int_configlet::duplicate() const
174 { return new bounded_int_configlet(*this); }
175
176 } //namespace.
177