Changeset 15644
- Timestamp:
- 2008-05-22T15:35:03+12:00 (16 years ago)
- Location:
- gsdl/trunk/lib
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
gsdl/trunk/lib/gdbmclass.cpp
r15630 r15644 34 34 closedatabase(); 35 35 } 36 36 37 37 38 // returns true if opened … … 93 94 gdbmfile = NULL; 94 95 openfile.clear(); 96 } 97 98 99 void gdbmclass::deletekey (const text_t &key) 100 { 101 if (gdbmfile == NULL) return; 102 103 // get a utf-8 encoded c string of the unicode key 104 datum key_data; 105 key_data.dptr = (to_utf8(key)).getcstr(); 106 if (key_data.dptr == NULL) return; 107 key_data.dsize = strlen (key_data.dptr); 108 109 // delete the key 110 gdbm_delete (gdbmfile, key_data); 111 112 // free up the key memory 113 delete []key_data.dptr; 114 } 115 116 117 // returns true if exists 118 bool gdbmclass::exists (const text_t& key) 119 { 120 text_t data; 121 return getkeydata (key, data); 122 } 123 124 125 // returns true on success 126 bool gdbmclass::getinfo (const text_t& key, infodbclass &info) 127 { 128 text_t data; 129 130 if (!getkeydata (key, data)) return false; 131 text_t::iterator here = data.begin (); 132 text_t::iterator end = data.end (); 133 134 text_t ikey, ivalue; 135 info.clear (); // reset info 136 137 while (getinfoline (here, end, ikey, ivalue)) { 138 info.addinfo (ikey, ivalue); 139 } 140 141 return true; 142 } 143 144 145 // returns true on success 146 bool gdbmclass::getkeydata (const text_t& key, text_t &data) 147 { 148 datum key_data; 149 datum return_data; 150 151 if (gdbmfile == NULL) return false; 152 153 // get a utf-8 encoded c string of the unicode key 154 key_data.dptr = (to_utf8(key)).getcstr(); 155 if (key_data.dptr == NULL) { 156 if (logout != NULL) (*logout) << "gdbmclass: out of memory\n"; 157 return false; 158 } 159 key_data.dsize = strlen (key_data.dptr); 160 161 // fetch the result 162 return_data = gdbm_fetch (gdbmfile, key_data); 163 delete []key_data.dptr; 164 165 if (return_data.dptr == NULL) return false; 166 167 data.setcarr (return_data.dptr, return_data.dsize); 168 free (return_data.dptr); 169 data = to_uni(data); // convert to unicode 170 171 return true; 172 } 173 174 175 // returns array of keys 176 text_tarray gdbmclass::getkeys () 177 { 178 text_tarray keys; 179 180 text_t key = getfirstkey(); 181 while (!key.empty()) 182 { 183 keys.push_back(key); 184 key = getnextkey(key); 185 } 186 187 return keys; 95 188 } 96 189 … … 188 281 189 282 190 // returns true on success283 // returns true on success 191 284 bool gdbmclass::setinfo (const text_t &key, const text_t &data) 192 285 { … … 221 314 222 315 223 void gdbmclass::deletekey (const text_t &key) 224 { 225 if (gdbmfile == NULL) return; 226 227 // get a utf-8 encoded c string of the unicode key 228 datum key_data; 229 key_data.dptr = (to_utf8(key)).getcstr(); 230 if (key_data.dptr == NULL) return; 231 key_data.dsize = strlen (key_data.dptr); 232 233 // delete the key 234 gdbm_delete (gdbmfile, key_data); 235 236 // free up the key memory 237 delete []key_data.dptr; 238 } 239 240 241 text_tarray gdbmclass::getkeys () 242 { 243 text_tarray keys; 244 245 text_t key = getfirstkey(); 246 while (!key.empty()) 247 { 248 keys.push_back(key); 249 key = getnextkey(key); 250 } 251 252 return keys; 253 } 254 316 // ---------------------------------------------------------------------------------------- 317 // GDBM-ONLY FUNCTIONS 318 // ---------------------------------------------------------------------------------------- 255 319 256 320 // getfirstkey and getnextkey are used for traversing the database … … 297 361 delete []key_data.dptr; 298 362 return to_uni(nextkey); // convert to unicode 299 }300 301 302 // returns true on success303 bool gdbmclass::getinfo (const text_t& key, infodbclass &info)304 {305 text_t data;306 307 if (!getkeydata (key, data)) return false;308 text_t::iterator here = data.begin ();309 text_t::iterator end = data.end ();310 311 text_t ikey, ivalue;312 info.clear (); // reset info313 314 while (getinfoline (here, end, ikey, ivalue)) {315 info.addinfo (ikey, ivalue);316 }317 318 return true;319 }320 321 322 // returns true if exists323 bool gdbmclass::exists (const text_t& key)324 {325 text_t data;326 return getkeydata (key, data);327 }328 329 330 // returns true on success331 bool gdbmclass::getkeydata (const text_t& key, text_t &data)332 {333 datum key_data;334 datum return_data;335 336 if (gdbmfile == NULL) return false;337 338 // get a utf-8 encoded c string of the unicode key339 key_data.dptr = (to_utf8(key)).getcstr();340 if (key_data.dptr == NULL) {341 if (logout != NULL) (*logout) << "gdbmclass: out of memory\n";342 return false;343 }344 key_data.dsize = strlen (key_data.dptr);345 346 // fetch the result347 return_data = gdbm_fetch (gdbmfile, key_data);348 delete []key_data.dptr;349 350 if (return_data.dptr == NULL) return false;351 352 data.setcarr (return_data.dptr, return_data.dsize);353 free (return_data.dptr);354 data = to_uni(data); // convert to unicode355 356 return true;357 363 } 358 364 -
gsdl/trunk/lib/gdbmclass.h
r15630 r15644 54 54 gdbmclass() { gdbmfile = NULL; }; 55 55 ~gdbmclass(); 56 56 57 57 // returns true if opened 58 bool opendatabase (const text_t &filename, int mode, int num_retrys, 59 bool need_filelock); 58 bool opendatabase (const text_t &filename, int mode, int num_retrys, bool need_filelock); 59 60 60 void closedatabase (); 61 61 62 // returns true on success 63 bool getinfo (const text_t& key, infodbclass &info); 62 void deletekey (const text_t &key); 64 63 65 64 // returns true if exists … … 67 66 68 67 // returns true on success 68 bool getinfo (const text_t& key, infodbclass &info); 69 70 // returns true on success 71 bool getkeydata (const text_t& key, text_t &data); 72 73 // returns array of keys 74 text_tarray getkeys (); 75 76 // returns true on success 69 77 bool setinfo (const text_t &key, const infodbclass &info); 70 78 // returns true on success 71 79 bool setinfo (const text_t &key, const text_t &data); 72 73 void deletekey (const text_t &key);74 75 text_tarray getkeys ();76 77 // returns true on success78 bool getkeydata (const text_t& key, text_t &data);79 80 80 81 protected: … … 90 91 91 92 // returns true on success 92 bool getinfoline (text_t::iterator &here, text_t::iterator end, 93 text_t &key, text_t &value); 93 bool getinfoline (text_t::iterator &here, text_t::iterator end, text_t &key, text_t &value); 94 94 }; 95 95
Note:
See TracChangeset
for help on using the changeset viewer.