1 /*****************************************************************************\
4 * Author : Chris Koeritz *
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 \*****************************************************************************/
15 #include "configlet.h"
16 #include "configurator.h"
18 #include <basis/astring.h>
19 #include <basis/functions.h>
21 using namespace basis;
23 namespace configuration {
25 const astring bogus_default = "OOPS: not supposed to ever be seen. d'oh!";
29 configlet::configlet(const astring §ion, const astring &entry)
30 : _section(new astring(section)),
31 _entry(new astring(entry))
34 configlet::configlet(const configlet &to_copy)
35 : _section(new astring(*to_copy._section)),
36 _entry(new astring(*to_copy._entry))
39 configlet::~configlet()
45 configlet &configlet::operator =(const configlet &to_copy)
47 if (this == &to_copy) return *this;
48 *_section = *to_copy._section;
49 *_entry = *to_copy._entry;
53 const astring &configlet::section() const { return *_section; }
55 const astring &configlet::entry() const { return *_entry; }
57 void configlet::section(const astring &new_section) const
58 { *_section = new_section; }
60 void configlet::entry(const astring &new_entry) const
61 { *_entry = new_entry; }
65 string_configlet::string_configlet(const astring §ion, const astring &entry,
66 const astring ¤t_value, const astring &default_value)
67 : configlet(section, entry),
68 _current(new astring(current_value)),
69 _default(new astring(default_value))
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))
79 string_configlet::~string_configlet()
85 string_configlet &string_configlet::operator =(const string_configlet &to_copy)
87 if (this == &to_copy) return *this;
88 (configlet &)*this = to_copy;
89 *_current = *to_copy._current;
90 *_default = *to_copy._default;
94 const astring &string_configlet::current_value() const { return *_current; }
96 const astring &string_configlet::default_value() const { return *_default; }
98 void string_configlet::current_value(const astring &new_current)
99 { *_current = new_current; }
101 void string_configlet::default_value(const astring &new_default)
102 { *_default = new_default; }
104 bool string_configlet::load(configurator &config)
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.
114 bool string_configlet::store(configurator &config) const
115 { return config.put(section(), entry(), *_current); }
117 configlet *string_configlet::duplicate() const
118 { return new string_configlet(section(), entry(), *_current, *_default); }
122 int_configlet::int_configlet(const astring §ion, const astring &entry,
123 int current_value, int default_value)
124 : configlet(section, entry),
125 _current(current_value),
126 _default(default_value)
130 int_configlet::~int_configlet() {}
132 void int_configlet::current_value(int new_current)
133 { _current = new_current; }
135 bool int_configlet::load(configurator &config)
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);
145 bool int_configlet::store(configurator &config) const
146 { return config.store(section(), entry(), _current); }
148 configlet *int_configlet::duplicate() const
149 { return new int_configlet(*this); }
153 bounded_int_configlet::bounded_int_configlet(const astring §ion,
154 const astring &entry, int current_value, int default_value,
155 int minimum, int maximum)
156 : int_configlet(section, entry, current_value, default_value),
162 bounded_int_configlet::~bounded_int_configlet() {}
164 void bounded_int_configlet::current_value(int new_current)
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);
173 configlet *bounded_int_configlet::duplicate() const
174 { return new bounded_int_configlet(*this); }