Ignore:
Timestamp:
2008-05-22T15:35:03+12:00 (16 years ago)
Author:
mdewsnip
Message:

Rearranging functions and tidying up.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/lib/gdbmclass.cpp

    r15630 r15644  
    3434  closedatabase();
    3535}
     36
    3637
    3738// returns true if opened
     
    9394  gdbmfile = NULL;
    9495  openfile.clear();
     96}
     97
     98
     99void 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
     118bool gdbmclass::exists (const text_t& key)
     119{
     120  text_t data;
     121  return getkeydata (key, data);
     122}
     123
     124
     125// returns true on success
     126bool 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
     146bool 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
     176text_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;
    95188}
    96189
     
    188281
    189282
    190 //returns true on success
     283// returns true on success
    191284bool gdbmclass::setinfo (const text_t &key, const text_t &data)
    192285{
     
    221314
    222315
    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// ----------------------------------------------------------------------------------------
    255319
    256320// getfirstkey and getnextkey are used for traversing the database
     
    297361  delete []key_data.dptr;
    298362  return to_uni(nextkey);  // convert to unicode
    299 }
    300 
    301 
    302 // returns true on success
    303 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 info
    313  
    314   while (getinfoline (here, end, ikey, ivalue)) {
    315     info.addinfo (ikey, ivalue);
    316   }
    317  
    318   return true;
    319 }
    320 
    321 
    322 // returns true if exists
    323 bool gdbmclass::exists (const text_t& key)
    324 {
    325   text_t data;
    326   return getkeydata (key, data);
    327 }
    328 
    329 
    330 // returns true on success
    331 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 key
    339   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 result
    347   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 unicode
    355 
    356   return true;
    357363}
    358364
Note: See TracChangeset for help on using the changeset viewer.