Adonthell
0.4
|
00001 /* 00002 $Id: storage.cc,v 1.11 2002/07/01 13:53:59 ksterker Exp $ 00003 00004 Copyright (C) 2000/2001 Kai Sterker <kaisterker@linuxgames.com> 00005 Part of the Adonthell Project http://adonthell.linuxgames.com 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 /** 00016 * @file storage.cc 00017 * @author Kai Sterker <kaisterker@linuxgames.com> 00018 * 00019 * @brief Defines the storage and objects classes. 00020 * 00021 * 00022 */ 00023 00024 #ifdef _DEBUG_ 00025 #include <iostream> 00026 #endif 00027 00028 #include "storage.h" 00029 00030 00031 storage::~storage () 00032 { 00033 } 00034 00035 00036 // Set a variable to a new value; delete key if value is zero to save space 00037 void storage::set_val (string key, s_int32 value) 00038 { 00039 #ifdef _DEBUG_ 00040 std::cout << "storage::set_val \"" << key << "\" = " << value << std::endl; 00041 #endif 00042 if (!value) data.erase (key); 00043 else 00044 data[key] = value; 00045 00046 changed = 1; 00047 } 00048 00049 // Get the value of a variable; if key not found then variable is zero 00050 s_int32 storage::get_val (string key) 00051 { 00052 #ifdef _DEBUG_ 00053 if (data.find (key) != data.end ()) 00054 std::cout << "storage::get_val \"" << key << "\" = " << data[key] << std::endl; 00055 else 00056 std::cout << "storage::get_val no such key \"" << key << "\"" << std::endl; 00057 #endif 00058 if (data.find (key) == data.end ()) return 0; 00059 else return data[key]; 00060 } 00061 00062 // [] Operator 00063 s_int32& storage::operator[] (string key) 00064 { 00065 return data[key]; 00066 } 00067 00068 // Iterate over the array 00069 pair<string, s_int32> storage::next () 00070 { 00071 if (changed) 00072 { 00073 changed = 0; 00074 i = data.begin (); 00075 } 00076 00077 if (i == data.end ()) 00078 { 00079 changed = 1; 00080 return pair<string, s_int32> (NULL, 0); 00081 } 00082 00083 return *i++; 00084 } 00085 00086 00087 // Insert a new object for access from the interpreter 00088 void objects::set_val (const char* key, storage *val) 00089 { 00090 map<const char*, storage*, ltstr>::iterator j; 00091 00092 // Check whether that key already exists -> if so, that is bad! 00093 for (j = data.begin (); j != data.end (); j++) 00094 if (strcmp ((*j).first, key) == 0) 00095 { 00096 #ifdef _DEBUG_ 00097 std::cout << "*** objects::set: key already exists: '" << key << "'\n"; 00098 std::cout << "*** container contents: "; 00099 00100 for (j = data.begin (); j != data.end (); j++) 00101 std::cout << "'" << (*j).first << "', "; 00102 00103 std::cout << "\n\n" << flush; 00104 #endif // _DEBUG_ 00105 00106 return; 00107 } 00108 00109 data[key] = val; 00110 changed = 1; 00111 } 00112 00113 // Retrieve a object from the map 00114 storage* objects::get_val (const char* key) 00115 { 00116 map<const char*, storage*, ltstr>::iterator j; 00117 00118 // Check whether the key exists 00119 for (j = data.begin (); j != data.end (); j++) 00120 if (strcmp ((*j).first, key) == 0) 00121 return (*j).second; 00122 00123 #ifdef _DEBUG_ 00124 std::cout << "*** objects::get: key does not exist: '" << key << "'\n"; 00125 std::cout << "*** container contents: "; 00126 00127 for (j = data.begin (); j != data.end (); j++) 00128 cout << "'" << (*j).first << "', "; 00129 00130 cout << "\n\n" << flush; 00131 #endif // _DEBUG_ 00132 00133 // That probably causes a segfault, but if we can't get the 00134 // required object, we are in trouble anyway. 00135 return NULL; 00136 } 00137 00138 // Delete a key from the array 00139 void objects::erase (const char *key) 00140 { 00141 // Check whether the key exists 00142 if (data.find (key) != data.end ()) 00143 { 00144 data.erase (key); 00145 changed = 1; 00146 } 00147 } 00148 00149 // Iterate over the array 00150 storage *objects::next () 00151 { 00152 if (changed) 00153 { 00154 changed = 0; 00155 i = data.begin (); 00156 } 00157 00158 if (i == data.end ()) 00159 { 00160 changed = 1; 00161 return NULL; 00162 } 00163 00164 return (*i++).second; 00165 } 00166