Changeset 18050
- Timestamp:
- 2008-12-04T10:15:10+13:00 (15 years ago)
- Location:
- gsdl/trunk/common-src/src/lib
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
gsdl/trunk/common-src/src/lib/gdbmclass.cpp
r17988 r18050 69 69 } 70 70 71 if (gdbm_mode == GDBM_READER) { 71 if (gdbm_mode == GDBM_READER) 72 { 73 if (!file_exists(filename)) 74 { 75 generate_from_txtgz(filename); 76 } 77 } 78 79 char *namebuffer = filename.getcstr(); 80 do { 81 #ifdef __WIN32__ 82 gdbmfile = gdbm_open (namebuffer, block_size, gdbm_mode, 00664, NULL, (need_filelock) ? 1 : 0); 83 #else 84 gdbmfile = gdbm_open (namebuffer, block_size, gdbm_mode, 00664, NULL); 85 #endif 86 --num_retrys; 87 } while (num_retrys>0 && gdbmfile==NULL && 88 (gdbm_errno==GDBM_CANT_BE_READER || gdbm_errno==GDBM_CANT_BE_WRITER)); 89 delete []namebuffer; 90 91 if (gdbmfile == NULL && logout != NULL) { 92 outconvertclass text_t2ascii; 93 (*logout) << text_t2ascii << "database open failed on: " << filename << "\n"; 94 } 95 96 return (gdbmfile != NULL); 97 } 98 99 100 void gdbmclass::closedatabase () 101 { 102 if (gdbmfile == NULL) return; 103 104 gdbm_close (gdbmfile); 105 gdbmfile = NULL; 106 openfile.clear(); 107 } 108 109 110 void gdbmclass::deletekey (const text_t &key) 111 { 112 if (gdbmfile == NULL) return; 113 114 // get a utf-8 encoded c string of the unicode key 115 datum key_data; 116 key_data.dptr = (to_utf8(key)).getcstr(); 117 if (key_data.dptr == NULL) return; 118 key_data.dsize = strlen (key_data.dptr); 119 120 // delete the key 121 gdbm_delete (gdbmfile, key_data); 122 123 // free up the key memory 124 delete []key_data.dptr; 125 } 126 127 128 // returns file extension string 129 text_t gdbmclass::getfileextension () 130 { 131 if (littleEndian()) return ".ldb"; 132 return ".bdb"; 133 } 134 135 136 // returns true on success 137 bool gdbmclass::getkeydata (const text_t& key, text_t &data) 138 { 139 datum key_data; 140 datum return_data; 141 142 if (gdbmfile == NULL) return false; 143 144 // get a utf-8 encoded c string of the unicode key 145 key_data.dptr = (to_utf8(key)).getcstr(); 146 if (key_data.dptr == NULL) { 147 if (logout != NULL) (*logout) << "gdbmclass: out of memory" << endl; 148 return false; 149 } 150 key_data.dsize = strlen (key_data.dptr); 151 152 // fetch the result 153 return_data = gdbm_fetch (gdbmfile, key_data); 154 delete []key_data.dptr; 155 156 if (return_data.dptr == NULL) return false; 157 158 data.setcarr (return_data.dptr, return_data.dsize); 159 free (return_data.dptr); 160 data = to_uni(data); // convert to unicode 161 162 return true; 163 } 164 165 166 // returns array of keys 167 text_tarray gdbmclass::getkeys () 168 { 169 text_tarray keys; 170 171 text_t key = getfirstkey(); 172 while (!key.empty()) 173 { 174 keys.push_back(key); 175 key = getnextkey(key); 176 } 177 178 return keys; 179 } 180 181 182 // returns true on success 183 bool gdbmclass::setkeydata (const text_t &key, const text_t &data) 184 { 185 if (gdbmfile == NULL) return false; 186 187 // store the value 188 datum key_data; 189 datum data_data; 190 191 // get a utf-8 encoded c string of the unicode key 192 key_data.dptr = (to_utf8(key)).getcstr(); 193 if (key_data.dptr == NULL) { 194 if (logout != NULL) (*logout) << "gdbmclass: out of memory" << endl; 195 return false; 196 } 197 key_data.dsize = strlen (key_data.dptr); 198 199 data_data.dptr = (to_utf8(data)).getcstr(); 200 if (data_data.dptr == NULL) { 201 if (logout != NULL) (*logout) << "gdbmclass: out of memory" << endl; 202 delete []key_data.dptr; 203 return false; 204 } 205 data_data.dsize = strlen (data_data.dptr); 206 207 int ret = gdbm_store (gdbmfile, key_data, data_data, GDBM_REPLACE); 208 delete []key_data.dptr; 209 delete []data_data.dptr; 210 211 return (ret == 0); 212 } 213 214 215 // ---------------------------------------------------------------------------------------- 216 // GDBM-ONLY FUNCTIONS 217 // ---------------------------------------------------------------------------------------- 218 219 // getfirstkey and getnextkey are used for traversing the database 220 // no insertions or deletions should be carried out while traversing 221 // the database. when there are no keys left to visit in the database 222 // an empty string is returned. 223 text_t gdbmclass::getfirstkey () 224 { 225 if (gdbmfile == NULL) return g_EmptyText; 226 227 // get the first key 228 datum firstkey_data = gdbm_firstkey (gdbmfile); 229 if (firstkey_data.dptr == NULL) return g_EmptyText; 230 231 // convert it to text_t 232 text_t firstkey; 233 firstkey.setcarr (firstkey_data.dptr, firstkey_data.dsize); 234 free (firstkey_data.dptr); 235 return to_uni(firstkey); // convert to unicode 236 } 237 238 239 text_t gdbmclass::getnextkey (const text_t &key) 240 { 241 if (gdbmfile == NULL || key.empty()) return g_EmptyText; 242 243 // get a utf-8 encoded c string of the unicode key 244 datum key_data; 245 key_data.dptr = (to_utf8(key)).getcstr(); 246 if (key_data.dptr == NULL) return g_EmptyText; 247 key_data.dsize = strlen (key_data.dptr); 248 249 // get the next key 250 datum nextkey_data = gdbm_nextkey (gdbmfile, key_data); 251 if (nextkey_data.dptr == NULL) { 252 delete []key_data.dptr; 253 return g_EmptyText; 254 } 255 256 // convert it to text_t 257 text_t nextkey; 258 nextkey.setcarr (nextkey_data.dptr, nextkey_data.dsize); 259 free (nextkey_data.dptr); 260 delete []key_data.dptr; 261 return to_uni(nextkey); // convert to unicode 262 } 263 264 265 // DEPRECATED -- No longer necessary now that the GDBM library reads both little-endian and big-endian 266 void gdbmclass::generate_from_txtgz (text_t filename) 267 { 72 268 // Looking to read in the database 73 269 // => check to see if .ldb/.bdb file already there 74 270 // if not (first time) then generate using txt2db 75 if (!file_exists(filename)) {76 271 77 272 // need to generate architecture native GDBM file using txt2db … … 138 333 } 139 334 } 140 } 141 } 142 143 char *namebuffer = filename.getcstr(); 144 do { 145 #ifdef __WIN32__ 146 gdbmfile = gdbm_open (namebuffer, block_size, gdbm_mode, 00664, NULL, (need_filelock) ? 1 : 0); 147 #else 148 gdbmfile = gdbm_open (namebuffer, block_size, gdbm_mode, 00664, NULL); 149 #endif 150 --num_retrys; 151 } while (num_retrys>0 && gdbmfile==NULL && 152 (gdbm_errno==GDBM_CANT_BE_READER || gdbm_errno==GDBM_CANT_BE_WRITER)); 153 delete []namebuffer; 154 155 if (gdbmfile == NULL && logout != NULL) { 156 outconvertclass text_t2ascii; 157 (*logout) << text_t2ascii << "database open failed on: " << filename << "\n"; 158 } 159 160 return (gdbmfile != NULL); 161 } 162 163 164 void gdbmclass::closedatabase () 165 { 166 if (gdbmfile == NULL) return; 167 168 gdbm_close (gdbmfile); 169 gdbmfile = NULL; 170 openfile.clear(); 171 } 172 173 174 void gdbmclass::deletekey (const text_t &key) 175 { 176 if (gdbmfile == NULL) return; 177 178 // get a utf-8 encoded c string of the unicode key 179 datum key_data; 180 key_data.dptr = (to_utf8(key)).getcstr(); 181 if (key_data.dptr == NULL) return; 182 key_data.dsize = strlen (key_data.dptr); 183 184 // delete the key 185 gdbm_delete (gdbmfile, key_data); 186 187 // free up the key memory 188 delete []key_data.dptr; 189 } 190 191 192 // returns file extension string 193 text_t gdbmclass::getfileextension () 194 { 195 if (littleEndian()) return ".ldb"; 196 return ".bdb"; 197 } 198 199 200 // returns true on success 201 bool gdbmclass::getkeydata (const text_t& key, text_t &data) 202 { 203 datum key_data; 204 datum return_data; 205 206 if (gdbmfile == NULL) return false; 207 208 // get a utf-8 encoded c string of the unicode key 209 key_data.dptr = (to_utf8(key)).getcstr(); 210 if (key_data.dptr == NULL) { 211 if (logout != NULL) (*logout) << "gdbmclass: out of memory" << endl; 212 return false; 213 } 214 key_data.dsize = strlen (key_data.dptr); 215 216 // fetch the result 217 return_data = gdbm_fetch (gdbmfile, key_data); 218 delete []key_data.dptr; 219 220 if (return_data.dptr == NULL) return false; 221 222 data.setcarr (return_data.dptr, return_data.dsize); 223 free (return_data.dptr); 224 data = to_uni(data); // convert to unicode 225 226 return true; 227 } 228 229 230 // returns array of keys 231 text_tarray gdbmclass::getkeys () 232 { 233 text_tarray keys; 234 235 text_t key = getfirstkey(); 236 while (!key.empty()) 237 { 238 keys.push_back(key); 239 key = getnextkey(key); 240 } 241 242 return keys; 243 } 244 245 246 // returns true on success 247 bool gdbmclass::setkeydata (const text_t &key, const text_t &data) 248 { 249 if (gdbmfile == NULL) return false; 250 251 // store the value 252 datum key_data; 253 datum data_data; 254 255 // get a utf-8 encoded c string of the unicode key 256 key_data.dptr = (to_utf8(key)).getcstr(); 257 if (key_data.dptr == NULL) { 258 if (logout != NULL) (*logout) << "gdbmclass: out of memory" << endl; 259 return false; 260 } 261 key_data.dsize = strlen (key_data.dptr); 262 263 data_data.dptr = (to_utf8(data)).getcstr(); 264 if (data_data.dptr == NULL) { 265 if (logout != NULL) (*logout) << "gdbmclass: out of memory" << endl; 266 delete []key_data.dptr; 267 return false; 268 } 269 data_data.dsize = strlen (data_data.dptr); 270 271 int ret = gdbm_store (gdbmfile, key_data, data_data, GDBM_REPLACE); 272 delete []key_data.dptr; 273 delete []data_data.dptr; 274 275 return (ret == 0); 276 } 277 278 279 // ---------------------------------------------------------------------------------------- 280 // GDBM-ONLY FUNCTIONS 281 // ---------------------------------------------------------------------------------------- 282 283 // getfirstkey and getnextkey are used for traversing the database 284 // no insertions or deletions should be carried out while traversing 285 // the database. when there are no keys left to visit in the database 286 // an empty string is returned. 287 text_t gdbmclass::getfirstkey () 288 { 289 if (gdbmfile == NULL) return g_EmptyText; 290 291 // get the first key 292 datum firstkey_data = gdbm_firstkey (gdbmfile); 293 if (firstkey_data.dptr == NULL) return g_EmptyText; 294 295 // convert it to text_t 296 text_t firstkey; 297 firstkey.setcarr (firstkey_data.dptr, firstkey_data.dsize); 298 free (firstkey_data.dptr); 299 return to_uni(firstkey); // convert to unicode 300 } 301 302 303 text_t gdbmclass::getnextkey (const text_t &key) 304 { 305 if (gdbmfile == NULL || key.empty()) return g_EmptyText; 306 307 // get a utf-8 encoded c string of the unicode key 308 datum key_data; 309 key_data.dptr = (to_utf8(key)).getcstr(); 310 if (key_data.dptr == NULL) return g_EmptyText; 311 key_data.dsize = strlen (key_data.dptr); 312 313 // get the next key 314 datum nextkey_data = gdbm_nextkey (gdbmfile, key_data); 315 if (nextkey_data.dptr == NULL) { 316 delete []key_data.dptr; 317 return g_EmptyText; 318 } 319 320 // convert it to text_t 321 text_t nextkey; 322 nextkey.setcarr (nextkey_data.dptr, nextkey_data.dsize); 323 free (nextkey_data.dptr); 324 delete []key_data.dptr; 325 return to_uni(nextkey); // convert to unicode 326 } 335 } -
gsdl/trunk/common-src/src/lib/gdbmclass.h
r15679 r18050 84 84 text_t getfirstkey (); 85 85 text_t getnextkey (const text_t &key); 86 87 // DEPRECATED -- No longer necessary now that the GDBM library reads both little-endian and big-endian 88 void generate_from_txtgz (text_t filename); 86 89 }; 87 90
Note:
See TracChangeset
for help on using the changeset viewer.