]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/config.cc
took out some debug messages
[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 #ifdef    HAVE_CONFIG_H
24 #  include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 #include "config.hh"
28
29 using std::string;
30
31 Config::Config() {}
32
33 Config::~Config()
34 {
35   // deallocate memory for the 3 lists
36   BoolItemList::const_iterator b_it, b_end = bool_items.end();
37   for (b_it = bool_items.begin(); b_it != b_end; ++b_it)
38     delete *b_it;
39   bool_items.clear();
40
41   NumberItemList::const_iterator n_it, n_end = number_items.end();
42   for (n_it = number_items.begin(); n_it != n_end; ++n_it)
43     delete *n_it;
44   number_items.clear();
45
46   StringItemList::const_iterator s_it, s_end = string_items.end();
47   for (s_it = string_items.begin(); s_it != s_end; ++s_it)
48     delete *s_it;
49   string_items.clear();
50 }
51
52
53 bool Config::getValue(Config::StringType type, string &ret) const
54 {
55   StringItemList::const_iterator it = string_items.begin(), end = string_items.end();
56   for (; it != end; ++it) {
57     if ((*it)->type == type) {
58       ret = (*it)->value;
59       return true;
60     }
61   }
62   return false;
63 }
64
65
66 bool Config::getValue(Config::NumberType type, int &ret) const
67 {
68   NumberItemList::const_iterator it = number_items.begin(), end = number_items.end();
69   for (; it != end; ++it) {
70     if ((*it)->type == type) {
71       ret = (*it)->value;
72       return true;
73     }
74   }
75   return false;
76 }
77
78
79 bool Config::getValue(Config::BoolType type, bool &ret) const
80 {
81   BoolItemList::const_iterator it = bool_items.begin(), end = bool_items.end();
82   for (; it != end; ++it) {
83     if ((*it)->type == type) {
84       ret = (*it)->value;
85       return true;
86     }
87   }
88   return false;
89 }
90
91
92 void Config::addOption(const std::string &name, const std::string &value)
93 {
94   const struct {
95     const char *name;
96     Config::BoolType type;
97   }
98   bool_options[] = {
99     { "stackedcycling", Config::stackedCycling },
100     { "", NUM_BOOL_TYPES }
101   };
102
103   const struct {
104     const char *name;
105     Config::StringType type;
106   }
107   string_options[] = {
108     { "", NUM_STRING_TYPES }
109   };
110
111   const struct {
112     const char *name;
113     Config::NumberType type;
114   }
115   number_options[] = {
116     { "chaintimeout", chainTimeout },
117     { "workspacecolumns", workspaceColumns },
118     { "", NUM_NUMBER_TYPES }
119   };
120
121   // if it's bool option, add it to the bool_items list
122   size_t i = 0;
123   while (bool_options[i].type != NUM_BOOL_TYPES) {
124     if (strcasecmp(name.c_str(), bool_options[i].name) == 0) {
125       BoolItem *item = new BoolItem;
126       const char *tmp = value.c_str();
127
128       item->type = bool_options[i].type;
129
130       if (strcasecmp(tmp, "true") == 0 || strcasecmp(tmp, "1") == 0 ||
131           strcasecmp(tmp, "on") == 0)
132         item->value = true;
133       else
134         item->value = false;
135
136       bool_items.push_back(item);
137       return;
138     }
139     i++;
140   }
141
142   // if it's a string, add it to the string_items list
143   i = 0;
144   while (string_options[i].type != NUM_STRING_TYPES) {
145     if (strcasecmp(name.c_str(), string_options[i].name) == 0) {
146       StringItem *item = new StringItem;
147       item->type = string_options[i].type;
148       item->value = value;
149
150       string_items.push_back(item);
151       return;
152     }
153     i++;
154   }
155
156   // if it's a number, add it to the number_items list
157   i = 0;
158   while (number_options[i].type != NUM_NUMBER_TYPES) {
159     if (strcasecmp(name.c_str(), number_options[i].name) == 0) {
160       NumberItem *item = new NumberItem;
161       item->type = number_options[i].type;
162       item->value = atoi( value.c_str() );
163
164       number_items.push_back(item);
165       return;
166     }
167     i++;
168   }
169 }