Changeset 15655


Ignore:
Timestamp:
2008-05-22T16:44:51+12:00 (16 years ago)
Author:
mdewsnip
Message:

(Adding new DB support) Created default implementations of getinfo() and setinfo() in dbclass so they don't necessarily be re-implemented for every subclass.

Location:
gsdl/trunk/lib
Files:
6 edited

Legend:

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

    r15652 r15655  
    2626#include "dbclass.h"
    2727#include "OIDtools.h"
     28#include "unitool.h"
    2829
    2930
     
    3637  text_t data;
    3738  return getkeydata (key, data);
     39}
     40
     41
     42// May be overwritten by subclasses; returns true on success
     43bool dbclass::getinfo (const text_t& key, infodbclass &info)
     44{
     45  text_t data;
     46  if (!getkeydata(key, data)) return false;
     47
     48  // Use getinfoline() to parse the data value into the infodbclass object
     49  text_t::iterator data_iterator = data.begin();
     50  text_t ikey, ivalue;
     51  info.clear();
     52  while (getinfoline(data_iterator, data.end(), ikey, ivalue))
     53  {
     54    info.addinfo(ikey, ivalue);
     55  }
     56
     57  return true;
     58}
     59
     60
     61// returns true on success
     62bool dbclass::getinfoline (text_t::iterator &here, text_t::iterator end,
     63               text_t &key, text_t &value)
     64{
     65  key.clear();
     66  value.clear();
     67
     68  // ignore white space
     69  while (here != end && is_unicode_space (*here)) ++here;
     70
     71  // get the '<'
     72  if (here == end || *here != '<') return false;
     73  ++here;
     74 
     75  // get the key
     76  while (here != end && *here != '>') {
     77    key.push_back(*here);
     78    ++here;
     79  }
     80 
     81  // get the '>'
     82  if (here == end || *here != '>') return false;
     83  ++here;
     84 
     85  // get the value
     86  while (here != end && *here != '\n') {
     87    if (*here == '\\') {
     88      // found escape character
     89      ++here;
     90      if (here != end) {
     91    if (*here == 'n') value.push_back ('\n');
     92    else if (*here == 'r') value.push_back ('\r');
     93    else value.push_back(*here);
     94      }
     95
     96    } else {
     97      // a normal character
     98      value.push_back(*here);
     99    }
     100
     101    ++here;
     102  }
     103
     104  return true;
     105}
     106
     107
     108// returns true on success
     109bool dbclass::setinfo (const text_t &key, const infodbclass &info)
     110{
     111  text_t subkey;
     112  text_t data;
     113
     114  // get all the keys and values
     115  infodbclass::const_iterator info_here = info.begin();
     116  infodbclass::const_iterator info_end = info.end();
     117  while (info_here != info_end) {
     118    // add the key
     119    subkey.clear();
     120    subkey.push_back('<');
     121    text_t::const_iterator subkey_here = (*info_here).first.begin();
     122    text_t::const_iterator subkey_end = (*info_here).first.end();
     123    while (subkey_here != subkey_end) {
     124      if (*subkey_here == '>') {
     125    subkey.push_back('\\'); subkey.push_back('>');
     126      } else if (*subkey_here == '\n') {
     127    subkey.push_back('\\'); subkey.push_back('n');
     128      } else if (*subkey_here == '\r') {
     129    subkey.push_back('\\'); subkey.push_back('r');
     130      } else if (*subkey_here == '\\') {
     131    subkey.push_back('\\'); subkey.push_back('\\');
     132      } else {
     133    subkey.push_back (*subkey_here);
     134      }
     135      ++subkey_here;
     136    }
     137    subkey.push_back('>');
     138
     139    // add the values
     140    text_tarray::const_iterator subvalue_here = (*info_here).second.begin();
     141    text_tarray::const_iterator subvalue_end = (*info_here).second.end();
     142    while (subvalue_here != subvalue_end) {
     143      data += subkey;
     144     
     145      text_t::const_iterator thissubvalue_here = (*subvalue_here).begin();
     146      text_t::const_iterator thissubvalue_end = (*subvalue_here).end();
     147      while (thissubvalue_here != thissubvalue_end) {
     148    if (*thissubvalue_here == '>') {
     149      data.push_back('\\'); data.push_back('>');
     150    } else if (*thissubvalue_here == '\n') {
     151      data.push_back('\\'); data.push_back('n');
     152    } else if (*thissubvalue_here == '\r') {
     153      data.push_back('\\'); data.push_back('r');
     154    } else if (*thissubvalue_here == '\\') {
     155      data.push_back('\\'); data.push_back('\\');
     156    } else {
     157      data.push_back (*thissubvalue_here);
     158    }
     159   
     160    ++thissubvalue_here;
     161      }
     162     
     163      data.push_back('\n');
     164      ++subvalue_here;
     165    }
     166
     167    ++info_here;
     168  }
     169
     170  return setkeydata(key, data);
    38171}
    39172
  • gsdl/trunk/lib/dbclass.h

    r15652 r15655  
    5454  virtual ~dbclass();
    5555
    56   // MUST be implemented by subclasses; returns true if opened
     56  void setlogout (ostream *logout_arg) { logout = logout_arg; }
     57
     58
     59  // -----------------------------------------------------------------------------------------------
     60  // These functions MUST be implemented by subclasses, as they are database-specific
     61
     62  // returns true if opened
    5763  virtual bool opendatabase (const text_t &filename, int mode, int num_retrys, bool need_filelock) = 0;
    5864
    59   // MUST be implemented by subclasses
    6065  virtual void closedatabase () = 0;
    6166
    62   // MUST be implemented by subclasses
    6367  virtual void deletekey (const text_t &key) = 0;
    6468
    65   // May be overwritten by subclasses; returns true if exists
    66   virtual bool exists (const text_t& key);
    67 
    68   // Must be implemented by subclasses; returns true on success
    69   virtual bool getinfo (const text_t& key, infodbclass &info) = 0;
    70 
    71   // MUST be implemented by subclasses; returns true on success
     69  // returns true on success
    7270  virtual bool getkeydata (const text_t& key, text_t &data) = 0;
    7371
    74   // MUST be implemented by subclasses; returns array of keys
     72  // returns array of keys
    7573  virtual text_tarray getkeys () = 0;
    7674
    77   // Must be implemented by subclasses; returns true on success
    78   virtual bool setinfo (const text_t &key, const infodbclass &info) = 0;
    79 
    80   // MUST be implemented by subclasses; returns true on success
     75  // returns true on success
    8176  virtual bool setkeydata (const text_t &key, const text_t &data) = 0;
    8277
    8378
    84   void setlogout (ostream *logout_arg) { logout = logout_arg; }
     79  // -----------------------------------------------------------------------------------------------
     80  // These functions may be overwritten by subclasses, but a default implementation is provided
     81
     82  // returns true if exists
     83  virtual bool exists (const text_t& key);
     84
     85  // returns true on success
     86  virtual bool getinfo (const text_t& key, infodbclass &info);
     87
     88  // returns true on success
     89  virtual bool setinfo (const text_t &key, const infodbclass &info);
     90
    8591
    8692  // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
     
    9298  ostream *logout;
    9399
     100  // returns true on success
     101  bool getinfoline (text_t::iterator &here, text_t::iterator end, text_t &key, text_t &value);
     102
    94103  void get_first_child (text_t &OID, infodbclass &info);
    95104  void get_last_child (text_t &OID, infodbclass &info);
  • gsdl/trunk/lib/gdbmclass.cpp

    r15652 r15655  
    116116
    117117// returns true on success
    118 bool gdbmclass::getinfo (const text_t& key, infodbclass &info)
    119 {
    120   text_t data;
    121   if (!getkeydata(key, data)) return false;
    122 
    123   // Use getinfoline() to parse the data value into the infodbclass object
    124   text_t::iterator data_iterator = data.begin();
    125   text_t ikey, ivalue;
    126   info.clear();
    127   while (getinfoline(data_iterator, data.end(), ikey, ivalue))
    128   {
    129     info.addinfo(ikey, ivalue);
    130   }
    131 
    132   return true;
    133 }
    134 
    135 
    136 // returns true on success
    137118bool gdbmclass::getkeydata (const text_t& key, text_t &data)
    138119{
     
    181162
    182163// returns true on success
    183 bool gdbmclass::setinfo (const text_t &key, const infodbclass &info)
    184 {
    185   if (gdbmfile == NULL) return false;
    186 
    187   text_t subkey;
    188   text_t data;
    189 
    190   // get all the keys and values
    191   infodbclass::const_iterator info_here = info.begin();
    192   infodbclass::const_iterator info_end = info.end();
    193   while (info_here != info_end) {
    194     // add the key
    195     subkey.clear();
    196     subkey.push_back('<');
    197     text_t::const_iterator subkey_here = (*info_here).first.begin();
    198     text_t::const_iterator subkey_end = (*info_here).first.end();
    199     while (subkey_here != subkey_end) {
    200       if (*subkey_here == '>') {
    201     subkey.push_back('\\'); subkey.push_back('>');
    202       } else if (*subkey_here == '\n') {
    203     subkey.push_back('\\'); subkey.push_back('n');
    204       } else if (*subkey_here == '\r') {
    205     subkey.push_back('\\'); subkey.push_back('r');
    206       } else if (*subkey_here == '\\') {
    207     subkey.push_back('\\'); subkey.push_back('\\');
    208       } else {
    209     subkey.push_back (*subkey_here);
    210       }
    211       ++subkey_here;
    212     }
    213     subkey.push_back('>');
    214 
    215     // add the values
    216     text_tarray::const_iterator subvalue_here = (*info_here).second.begin();
    217     text_tarray::const_iterator subvalue_end = (*info_here).second.end();
    218     while (subvalue_here != subvalue_end) {
    219       data += subkey;
    220      
    221       text_t::const_iterator thissubvalue_here = (*subvalue_here).begin();
    222       text_t::const_iterator thissubvalue_end = (*subvalue_here).end();
    223       while (thissubvalue_here != thissubvalue_end) {
    224     if (*thissubvalue_here == '>') {
    225       data.push_back('\\'); data.push_back('>');
    226     } else if (*thissubvalue_here == '\n') {
    227       data.push_back('\\'); data.push_back('n');
    228     } else if (*thissubvalue_here == '\r') {
    229       data.push_back('\\'); data.push_back('r');
    230     } else if (*thissubvalue_here == '\\') {
    231       data.push_back('\\'); data.push_back('\\');
    232     } else {
    233       data.push_back (*thissubvalue_here);
    234     }
    235    
    236     ++thissubvalue_here;
    237       }
    238      
    239       data.push_back('\n');
    240       ++subvalue_here;
    241     }
    242 
    243     ++info_here;
    244   }
    245 
    246   return setkeydata(key, data);
    247 }
    248 
    249 
    250 // returns true on success
    251164bool gdbmclass::setkeydata (const text_t &key, const text_t &data)
    252165{
     
    329242  return to_uni(nextkey);  // convert to unicode
    330243}
    331 
    332 
    333 // returns true on success
    334 bool gdbmclass::getinfoline (text_t::iterator &here, text_t::iterator end,
    335                  text_t &key, text_t &value)
    336 {
    337   key.clear();
    338   value.clear();
    339 
    340   // ignore white space
    341   while (here != end && is_unicode_space (*here)) ++here;
    342 
    343   // get the '<'
    344   if (here == end || *here != '<') return false;
    345   ++here;
    346  
    347   // get the key
    348   while (here != end && *here != '>') {
    349     key.push_back(*here);
    350     ++here;
    351   }
    352  
    353   // get the '>'
    354   if (here == end || *here != '>') return false;
    355   ++here;
    356  
    357   // get the value
    358   while (here != end && *here != '\n') {
    359     if (*here == '\\') {
    360       // found escape character
    361       ++here;
    362       if (here != end) {
    363     if (*here == 'n') value.push_back ('\n');
    364     else if (*here == 'r') value.push_back ('\r');
    365     else value.push_back(*here);
    366       }
    367 
    368     } else {
    369       // a normal character
    370       value.push_back(*here);
    371     }
    372 
    373     ++here;
    374   }
    375 
    376   return true;
    377 }
  • gsdl/trunk/lib/gdbmclass.h

    r15652 r15655  
    6363
    6464  // returns true on success
    65   bool getinfo (const text_t& key, infodbclass &info);
    66 
    67   // returns true on success
    6865  bool getkeydata (const text_t& key, text_t &data);
    6966
    7067  // returns array of keys
    7168  text_tarray getkeys ();
    72 
    73   // returns true on success
    74   bool setinfo (const text_t &key, const infodbclass &info);
    7569
    7670  // returns true on success
     
    8781  text_t getfirstkey ();
    8882  text_t getnextkey (const text_t &key);
    89 
    90   // returns true on success
    91   bool getinfoline (text_t::iterator &here, text_t::iterator end, text_t &key, text_t &value);
    9283};
    9384
  • gsdl/trunk/lib/sqlitedbclass.cpp

    r15652 r15655  
    9090
    9191// returns true on success
    92 bool sqlitedbclass::getinfo (const text_t& key, infodbclass &info)
    93 {
    94   text_t data;
    95   if (!getkeydata(key, data)) return false;
    96 
    97   // Use getinfoline() to parse the data value into the infodbclass object
    98   text_t::iterator data_iterator = data.begin();
    99   text_t ikey, ivalue;
    100   info.clear();
    101   while (getinfoline(data_iterator, data.end(), ikey, ivalue))
    102   {
    103     info.addinfo(ikey, ivalue);
    104   }
    105 
    106   return true;
    107 }
    108 
    109 
    110 // returns true on success
    11192bool sqlitedbclass::getkeydata (const text_t& key, text_t &data)
    11293{
     
    151132
    152133// returns true on success
    153 bool sqlitedbclass::setinfo (const text_t &key, const infodbclass &info)
    154 {
    155   text_t subkey;
    156   text_t data;
    157 
    158   // get all the keys and values
    159   infodbclass::const_iterator info_here = info.begin();
    160   infodbclass::const_iterator info_end = info.end();
    161   while (info_here != info_end) {
    162     // add the key
    163     subkey.clear();
    164     subkey.push_back('<');
    165     text_t::const_iterator subkey_here = (*info_here).first.begin();
    166     text_t::const_iterator subkey_end = (*info_here).first.end();
    167     while (subkey_here != subkey_end) {
    168       if (*subkey_here == '>') {
    169     subkey.push_back('\\'); subkey.push_back('>');
    170       } else if (*subkey_here == '\n') {
    171     subkey.push_back('\\'); subkey.push_back('n');
    172       } else if (*subkey_here == '\r') {
    173     subkey.push_back('\\'); subkey.push_back('r');
    174       } else if (*subkey_here == '\\') {
    175     subkey.push_back('\\'); subkey.push_back('\\');
    176       } else {
    177     subkey.push_back (*subkey_here);
    178       }
    179       ++subkey_here;
    180     }
    181     subkey.push_back('>');
    182 
    183     // add the values
    184     text_tarray::const_iterator subvalue_here = (*info_here).second.begin();
    185     text_tarray::const_iterator subvalue_end = (*info_here).second.end();
    186     while (subvalue_here != subvalue_end) {
    187       data += subkey;
    188      
    189       text_t::const_iterator thissubvalue_here = (*subvalue_here).begin();
    190       text_t::const_iterator thissubvalue_end = (*subvalue_here).end();
    191       while (thissubvalue_here != thissubvalue_end) {
    192     if (*thissubvalue_here == '>') {
    193       data.push_back('\\'); data.push_back('>');
    194     } else if (*thissubvalue_here == '\n') {
    195       data.push_back('\\'); data.push_back('n');
    196     } else if (*thissubvalue_here == '\r') {
    197       data.push_back('\\'); data.push_back('r');
    198     } else if (*thissubvalue_here == '\\') {
    199       data.push_back('\\'); data.push_back('\\');
    200     } else {
    201       data.push_back (*thissubvalue_here);
    202     }
    203    
    204     ++thissubvalue_here;
    205       }
    206      
    207       data.push_back('\n');
    208       ++subvalue_here;
    209     }
    210 
    211     ++info_here;
    212   }
    213 
    214   return setkeydata(key, data);
    215 }
    216 
    217 
    218 // returns true on success
    219134bool sqlitedbclass::setkeydata (const text_t &key, const text_t &data)
    220135{
     
    233148
    234149
    235 // returns true on success
    236 bool sqlitedbclass::getinfoline (text_t::iterator &here, text_t::iterator end,
    237                  text_t &key, text_t &value)
    238 {
    239   key.clear();
    240   value.clear();
    241 
    242   // ignore white space
    243   while (here != end && is_unicode_space (*here)) ++here;
    244 
    245   // get the '<'
    246   if (here == end || *here != '<') return false;
    247   ++here;
    248  
    249   // get the key
    250   while (here != end && *here != '>') {
    251     key.push_back(*here);
    252     ++here;
    253   }
    254  
    255   // get the '>'
    256   if (here == end || *here != '>') return false;
    257   ++here;
    258  
    259   // get the value
    260   while (here != end && *here != '\n') {
    261     if (*here == '\\') {
    262       // found escape character
    263       ++here;
    264       if (here != end) {
    265     if (*here == 'n') value.push_back ('\n');
    266     else if (*here == 'r') value.push_back ('\r');
    267     else value.push_back(*here);
    268       }
    269 
    270     } else {
    271       // a normal character
    272       value.push_back(*here);
    273     }
    274 
    275     ++here;
    276   }
    277 
    278   return true;
    279 }
    280 
    281 
    282150// ----------------------------------------------------------------------------------------
    283 //   CORE SQL FUNCTIONS
     151//   SQLITE-ONLY FUNCTIONS
    284152// ----------------------------------------------------------------------------------------
    285153
  • gsdl/trunk/lib/sqlitedbclass.h

    r15652 r15655  
    4646
    4747  // returns true on success
    48   bool getinfo (const text_t& key, infodbclass &info);
    49 
    50   // returns true on success
    5148  bool getkeydata (const text_t& key, text_t &data);
    5249
    5350  // returns array of keys
    5451  text_tarray getkeys ();
    55 
    56   // returns true on success
    57   bool setinfo (const text_t &key, const infodbclass &info);
    5852
    5953  // returns true on success
     
    6458  sqlite3* sqlitefile;
    6559
    66   bool getinfoline (text_t::iterator &here, text_t::iterator end, text_t &key, text_t &value);
    67 
    6860  bool sqlexec (const text_t &sql_cmd);
    6961  bool sqlgetarray (const text_t &sql_cmd, vector<text_tmap> &sql_results);
Note: See TracChangeset for help on using the changeset viewer.