Adonthell  0.4
storage.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000/2001 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file storage.cc
21  * @author Kai Sterker <kai.sterker@gmail.com>
22  *
23  * @brief Defines the storage and objects classes.
24  *
25  *
26  */
27 
28 #ifdef _DEBUG_
29 #include <iostream>
30 #endif
31 
32 #include "storage.h"
33 
34 
36 {
37 }
38 
39 
40 // Set a variable to a new value; delete key if value is zero to save space
41 void storage::set_val (string key, s_int32 value)
42 {
43 #ifdef _DEBUG_
44  std::cout << "storage::set_val \"" << key << "\" = " << value << std::endl;
45 #endif
46  if (!value) data.erase (key);
47  else
48  data[key] = value;
49 
50  changed = 1;
51 }
52 
53 // Get the value of a variable; if key not found then variable is zero
55 {
56 #ifdef _DEBUG_
57  if (data.find (key) != data.end ())
58  std::cout << "storage::get_val \"" << key << "\" = " << data[key] << std::endl;
59  else
60  std::cout << "storage::get_val no such key \"" << key << "\"" << std::endl;
61 #endif
62  if (data.find (key) == data.end ()) return 0;
63  else return data[key];
64 }
65 
66 // [] Operator
68 {
69  return data[key];
70 }
71 
72 // Iterate over the array
73 pair<string, s_int32> storage::next ()
74 {
75  if (changed)
76  {
77  changed = 0;
78  i = data.begin ();
79  }
80 
81  if (i == data.end ())
82  {
83  changed = 1;
84  return pair<string, s_int32> (NULL, 0);
85  }
86 
87  return *i++;
88 }
89 
90 
91 // Insert a new object for access from the interpreter
92 void objects::set_val (const char* key, storage *val)
93 {
94  map<const char*, storage*, ltstr>::iterator j;
95 
96  // Check whether that key already exists -> if so, that is bad!
97  for (j = data.begin (); j != data.end (); j++)
98  if (strcmp ((*j).first, key) == 0)
99  {
100 #ifdef _DEBUG_
101  std::cout << "*** objects::set: key already exists: '" << key << "'\n";
102  std::cout << "*** container contents: ";
103 
104  for (j = data.begin (); j != data.end (); j++)
105  std::cout << "'" << (*j).first << "', ";
106 
107  std::cout << "\n\n" << flush;
108 #endif // _DEBUG_
109 
110  return;
111  }
112 
113  data[key] = val;
114  changed = 1;
115 }
116 
117 // Retrieve a object from the map
118 storage* objects::get_val (const char* key)
119 {
120  map<const char*, storage*, ltstr>::iterator j;
121 
122  // Check whether the key exists
123  for (j = data.begin (); j != data.end (); j++)
124  if (strcmp ((*j).first, key) == 0)
125  return (*j).second;
126 
127 #ifdef _DEBUG_
128  std::cout << "*** objects::get: key does not exist: '" << key << "'\n";
129  std::cout << "*** container contents: ";
130 
131  for (j = data.begin (); j != data.end (); j++)
132  cout << "'" << (*j).first << "', ";
133 
134  cout << "\n\n" << flush;
135 #endif // _DEBUG_
136 
137  // That probably causes a segfault, but if we can't get the
138  // required object, we are in trouble anyway.
139  return NULL;
140 }
141 
142 // Delete a key from the array
143 void objects::erase (const char *key)
144 {
145  // Check whether the key exists
146  if (data.find (key) != data.end ())
147  {
148  data.erase (key);
149  changed = 1;
150  }
151 }
152 
153 // Iterate over the array
155 {
156  if (changed)
157  {
158  changed = 0;
159  i = data.begin ();
160  }
161 
162  if (i == data.end ())
163  {
164  changed = 1;
165  return NULL;
166  }
167 
168  return (*i++).second;
169 }
170 
storage * next()
Returns the next storage in the object.
Definition: storage.cc:154
#define s_int32
32 bits long signed integer
Definition: types.h:50
void erase(const char *key)
Erases a storage from it&#39;s key.
Definition: storage.cc:143
~storage()
Destructor.
Definition: storage.cc:35
s_int32 & operator[](string key)
Returns the value of a key.
Definition: storage.cc:67
Base storage class.
Definition: storage.h:51
storage * get_val(const char *key)
Returns a storage associated to a key.
Definition: storage.cc:118
void set_val(string key, s_int32 value)
Sets key to value.
Definition: storage.cc:41
s_int32 get_val(string key)
Returns the value of a key.
Definition: storage.cc:54
pair< string, s_int32 > next()
Returns the next (key, value) pair of the storage.
Definition: storage.cc:73
Declares the storage and objects classes.
void set_val(const char *key, storage *val)
Associates an object to a key.
Definition: storage.cc:92