Changeset 510 for trunk


Ignore:
Timestamp:
1999-09-02T12:26:11+12:00 (25 years ago)
Author:
rjmcnab
Message:

now there can be multiple values for a single key

Location:
trunk/gsdl/src/recpt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/recpt/infodbclass.cpp

    r375 r510  
    1212/*
    1313   $Log$
     14   Revision 1.3  1999/09/02 00:26:10  rjmcnab
     15   now there can be multiple values for a single key
     16
    1417   Revision 1.2  1999/07/13 23:24:05  rjmcnab
    1518   Added functionality to modify a gdbm database.
     
    3841#include "fileutil.h"
    3942#include "OIDtools.h"
    40  
     43#include <stdlib.h>
     44
     45
     46
     47
    4148// constructors
    4249infodbclass::infodbclass () {
     
    4451
    4552void infodbclass::setinfo (const text_t &key, const text_t &value) {
    46   info[key] = value;
     53  text_tarray &tarr = info[key];
     54  tarr.erase(tarr.begin(), tarr.end());
     55  tarr.push_back(value);
    4756}
    4857
     
    5362void infodbclass::setcinfo (const text_t &key, unsigned short c) {
    5463  text_t t;
    55   t.push_back(c);
    56   setinfo(key,t);
     64  t.push_back (c);
     65  setinfo (key, t);
    5766}
    5867
     
    6170  if (here == info.end()) return NULL;
    6271
    63   return &((*here).second);
     72  if ((*here).second.empty()) return NULL;
     73 
     74  return &((*here).second[0]);
    6475}
    6576
     
    7081}
    7182
     83text_t &infodbclass::operator[] (const text_t &key) {
     84  text_tarray &tarr = info[key];
     85  if (tarr.empty()) {
     86    text_t e;
     87    tarr.push_back(e);
     88  }
     89  return tarr[0];
     90}
     91
     92
     93void infodbclass::addinfo (const text_t &key, const text_t &value) {
     94  text_tarray &tarr = info[key];
     95  tarr.push_back (value);
     96}
     97
     98void infodbclass::addintinfo (const text_t &key, int value) {
     99  addinfo (key, value);
     100}
     101
     102void infodbclass::addcinfo (const text_t &key, unsigned short c) {
     103  text_t t;
     104  t.push_back(c);
     105  addinfo (key, t);
     106}
     107
     108text_tarray *infodbclass::getmultinfo (const text_t &key) {
     109  iterator here = info.find (key);
     110  if (here == info.end()) return NULL;
     111
     112  return &((*here).second);
     113}
     114
    72115
    73116
    74117
    75118// returns true if opened
    76 bool gdbmclass::opendatabase (const text_t &filename, int mode, int num_retrys) {
     119bool gdbmclass::opendatabase (const text_t &filename, int mode, int num_retrys,
     120                  bool need_filelock) {
    77121  text_t data_location;
    78122  int block_size = 512;
     
    87131  char *namebuffer = filename.getcstr();
    88132  do {
     133#ifdef __WIN32__
     134      gdbmfile = gdbm_open (namebuffer, block_size, mode, 00664, NULL, (need_filelock) ? 1 : 0);
     135#else
    89136    gdbmfile = gdbm_open (namebuffer, block_size, mode, 00664, NULL);
     137#endif
    90138    num_retrys--;
    91139  } while (num_retrys>0 && gdbmfile==NULL &&
     
    114162  if (gdbmfile == NULL) return false;
    115163
     164  text_t subkey;
    116165  text_t data;
    117166
     
    121170  while (info_here != info_end) {
    122171    // add the key
    123     data.push_back('<');
     172    subkey.clear();
     173    subkey.push_back('<');
    124174    text_t::const_iterator subkey_here = (*info_here).first.begin();
    125175    text_t::const_iterator subkey_end = (*info_here).first.end();
    126176    while (subkey_here != subkey_end) {
    127177      if (*subkey_here == '>') {
    128     data.push_back('\\'); data.push_back('>');
     178    subkey.push_back('\\'); subkey.push_back('>');
    129179      } else if (*subkey_here == '\n') {
    130     data.push_back('\\'); data.push_back('n');
     180    subkey.push_back('\\'); subkey.push_back('n');
    131181      } else if (*subkey_here == '\r') {
    132     data.push_back('\\'); data.push_back('r');
     182    subkey.push_back('\\'); subkey.push_back('r');
    133183      } else {
    134     data.push_back (*subkey_here);
     184    subkey.push_back (*subkey_here);
    135185      }
    136186      subkey_here++;
    137187    }
    138     data.push_back('>');
    139 
    140     // add the value
    141     text_t::const_iterator subvalue_here = (*info_here).second.begin();
    142     text_t::const_iterator subvalue_end = (*info_here).second.end();
     188    subkey.push_back('>');
     189
     190    // add the values
     191    text_tarray::const_iterator subvalue_here = (*info_here).second.begin();
     192    text_tarray::const_iterator subvalue_end = (*info_here).second.end();
    143193    while (subvalue_here != subvalue_end) {
    144       if (*subvalue_here == '>') {
    145     data.push_back('\\'); data.push_back('>');
    146       } else if (*subvalue_here == '\n') {
    147     data.push_back('\\'); data.push_back('n');
    148       } else if (*subvalue_here == '\r') {
    149     data.push_back('\\'); data.push_back('r');
    150       } else {
    151     data.push_back (*subvalue_here);
     194      data += subkey;
     195     
     196      text_t::const_iterator thissubvalue_here = (*subvalue_here).begin();
     197      text_t::const_iterator thissubvalue_end = (*subvalue_here).end();
     198      while (thissubvalue_here != thissubvalue_end) {
     199    if (*thissubvalue_here == '>') {
     200      data.push_back('\\'); data.push_back('>');
     201    } else if (*thissubvalue_here == '\n') {
     202      data.push_back('\\'); data.push_back('n');
     203    } else if (*thissubvalue_here == '\r') {
     204      data.push_back('\\'); data.push_back('r');
     205    } else {
     206      data.push_back (*thissubvalue_here);
     207    }
     208   
     209    thissubvalue_here++;
    152210      }
     211     
     212      data.push_back('\n');
    153213      subvalue_here++;
    154214    }
    155     data.push_back('\n');
    156    
     215
    157216    info_here++;
    158217  }
     
    376435  info.clear (); // reset info
    377436 
    378   while (getinfoline(here, end, ikey, ivalue)) {
    379     info[ikey] = ivalue;
     437  while (getinfoline (here, end, ikey, ivalue)) {
     438    info.addinfo (ikey, ivalue);
    380439  }
    381440 
  • trunk/gsdl/src/recpt/infodbclass.h

    r375 r510  
    1919
    2020#ifdef __WIN32__
     21
     22#ifdef __cplusplus
     23extern "C" {
     24#endif
    2125#include "autoconf.h"
    2226#include "systems.h"
    2327#include "gdbmconst.h"
    2428#include "gdbm.h"
     29#ifdef __cplusplus
     30}
     31#endif
    2532
    2633#else
     
    2936
    3037
     38typedef map<text_t, text_tarray, lttext_t> text_tarraymap;
     39
     40
    3141// infodbclass is used to store information about a object
    3242class infodbclass {
    3343protected:
    34   text_tmap info;
     44  text_tarraymap info;
    3545
    3646public:
    37   //type support for text_tmap
    38   typedef text_tmap::iterator iterator;
    39   typedef text_tmap::const_iterator const_iterator;
    40   typedef text_tmap::reference reference;
    41   typedef text_tmap::const_reference const_reference;
    42   typedef text_tmap::size_type size_type;
    43   typedef text_tmap::difference_type difference_type;
    44   typedef text_tmap::const_reverse_iterator const_reverse_iterator;
    45   typedef text_tmap::reverse_iterator reverse_iterator;
     47  // type support for text_tarraymap
     48  typedef text_tarraymap::iterator iterator;
     49  typedef text_tarraymap::const_iterator const_iterator;
     50  typedef text_tarraymap::reference reference;
     51  typedef text_tarraymap::const_reference const_reference;
     52  typedef text_tarraymap::size_type size_type;
     53  typedef text_tarraymap::difference_type difference_type;
     54  typedef text_tarraymap::const_reverse_iterator const_reverse_iterator;
     55  typedef text_tarraymap::reverse_iterator reverse_iterator;
    4656 
    4757  // constructors
     
    6575  void clear () {info.erase(info.begin(),info.end());}
    6676
     77  // the following functions deal with keys that can only
     78  // have one value for compatibility
     79 
    6780  // getinfo returns NULL if there isn't an entry with
    6881  // 'key' already defined, getintinfo returns 0 if there wasn't an
     
    7487  text_t *getinfo (const text_t &key);
    7588  int getintinfo (const text_t &key);
    76   text_t &operator[] (const text_t &key) {return info[key];}
     89  text_t &operator[] (const text_t &key);
     90
     91
     92  // the next set of functions allow you to set and access keys
     93  // that can have more than one value
     94
     95  // getmultinfo returns NULL if there isn't an entry with
     96  // 'key' already defined
     97  void addinfo (const text_t &key, const text_t &value);
     98  void addintinfo (const text_t &key, int value);
     99  void addcinfo (const text_t &key, unsigned short c);
     100  text_tarray *getmultinfo (const text_t &key);
    77101};
    78102
     
    84108 
    85109  // returns true if opened
    86   bool opendatabase (const text_t &filename, int mode=GDBM_READER, int num_retrys=1);
     110  bool opendatabase (const text_t &filename, int mode, int num_retrys,
     111             bool need_filelock);
    87112  void closedatabase ();
    88113
Note: See TracChangeset for help on using the changeset viewer.