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