]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/config.cc
Fixed a couple of glitches, added on/off values for boolean
[mikachu/openbox.git] / util / epist / config.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // config.cc for Epistrophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 #include "config.hh"
24
25 using std::string;
26
27 Config::Config() {}
28
29 Config::~Config()
30 {
31   // deallocate memory for the 3 lists
32   BoolItemList::const_iterator b_it, b_end = bool_items.end();
33   for (b_it = bool_items.begin(); b_it != b_end; ++b_it)
34     delete *b_it;
35   bool_items.clear();
36
37   NumberItemList::const_iterator n_it, n_end = number_items.end();
38   for (n_it = number_items.begin(); n_it != n_end; ++n_it)
39     delete *n_it;
40   number_items.clear();
41
42   StringItemList::const_iterator s_it, s_end = string_items.end();
43   for (s_it = string_items.begin(); s_it != s_end; ++s_it)
44     delete *s_it;
45   string_items.clear();
46 }
47
48
49 bool Config::getValue(Config::StringType type, string &ret) const
50 {
51   StringItemList::const_iterator it = string_items.begin(), end = string_items.end();
52   for (; it != end; ++it) {
53     if ((*it)->type == type) {
54       ret = (*it)->value;
55       return true;
56     }
57   }
58   return false;
59 }
60
61
62 bool Config::getValue(Config::NumberType type, int &ret) const
63 {
64   NumberItemList::const_iterator it = number_items.begin(), end = number_items.end();
65   for (; it != end; ++it) {
66     if ((*it)->type == type) {
67       ret = (*it)->value;
68       return true;
69     }
70   }
71   return false;
72 }
73
74
75 bool Config::getValue(Config::BoolType type, bool &ret) const
76 {
77   BoolItemList::const_iterator it = bool_items.begin(), end = bool_items.end();
78   for (; it != end; ++it) {
79     if ((*it)->type == type) {
80       ret = (*it)->value;
81       return true;
82     }
83   }
84   return false;
85 }
86
87
88 void Config::addOption(const std::string &name, const std::string &value)
89 {
90   const struct {
91     const char *name;
92     Config::BoolType type;
93   }
94   bool_options[] = {
95     { "stackedcycling", Config::stackedCycling },
96     { "", NUM_BOOL_TYPES }
97   };
98
99   const struct {
100     const char *name;
101     Config::StringType type;
102   }
103   string_options[] = {
104     { "", NUM_STRING_TYPES }
105   };
106
107   const struct {
108     const char *name;
109     Config::NumberType type;
110   }
111   number_options[] = {
112     { "chaintimeout", chainTimeout },
113     { "workspacecolumns", workspaceColumns },
114     { "", NUM_NUMBER_TYPES }
115   };
116
117   // if it's bool option, add it to the bool_items list
118   size_t i = 0;
119   while (bool_options[i].type != NUM_BOOL_TYPES) {
120     if (strcasecmp(name.c_str(), bool_options[i].name) == 0) {
121       BoolItem *item = new BoolItem;
122       const char *tmp = value.c_str();
123
124       item->type = bool_options[i].type;
125
126       if (strcasecmp(tmp, "true") == 0 || strcasecmp(tmp, "1") == 0 ||
127           strcasecmp(tmp, "on") == 0)
128         item->value = true;
129       else
130         item->value = false;
131
132       bool_items.push_back(item);
133       return;
134     }
135     i++;
136   }
137
138   // if it's a string, add it to the string_items list
139   i = 0;
140   while (string_options[i].type != NUM_STRING_TYPES) {
141     if (strcasecmp(name.c_str(), string_options[i].name) == 0) {
142       StringItem *item = new StringItem;
143       item->type = string_options[i].type;
144       item->value = value;
145
146       string_items.push_back(item);
147       return;
148     }
149     i++;
150   }
151
152   // if it's a number, add it to the number_items list
153   i = 0;
154   while (number_options[i].type != NUM_NUMBER_TYPES) {
155     if (strcasecmp(name.c_str(), number_options[i].name) == 0) {
156       NumberItem *item = new NumberItem;
157       item->type = number_options[i].type;
158       item->value = atoi( value.c_str() );
159
160       number_items.push_back(item);
161       return;
162     }
163     i++;
164   }
165 }