- Timestamp:
- 1999-09-02T12:26:11+12:00 (25 years ago)
- Location:
- trunk/gsdl/src/recpt
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/src/recpt/infodbclass.cpp
r375 r510 12 12 /* 13 13 $Log$ 14 Revision 1.3 1999/09/02 00:26:10 rjmcnab 15 now there can be multiple values for a single key 16 14 17 Revision 1.2 1999/07/13 23:24:05 rjmcnab 15 18 Added functionality to modify a gdbm database. … … 38 41 #include "fileutil.h" 39 42 #include "OIDtools.h" 40 43 #include <stdlib.h> 44 45 46 47 41 48 // constructors 42 49 infodbclass::infodbclass () { … … 44 51 45 52 void 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); 47 56 } 48 57 … … 53 62 void infodbclass::setcinfo (const text_t &key, unsigned short c) { 54 63 text_t t; 55 t.push_back (c);56 setinfo (key,t);64 t.push_back (c); 65 setinfo (key, t); 57 66 } 58 67 … … 61 70 if (here == info.end()) return NULL; 62 71 63 return &((*here).second); 72 if ((*here).second.empty()) return NULL; 73 74 return &((*here).second[0]); 64 75 } 65 76 … … 70 81 } 71 82 83 text_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 93 void infodbclass::addinfo (const text_t &key, const text_t &value) { 94 text_tarray &tarr = info[key]; 95 tarr.push_back (value); 96 } 97 98 void infodbclass::addintinfo (const text_t &key, int value) { 99 addinfo (key, value); 100 } 101 102 void infodbclass::addcinfo (const text_t &key, unsigned short c) { 103 text_t t; 104 t.push_back(c); 105 addinfo (key, t); 106 } 107 108 text_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 72 115 73 116 74 117 75 118 // returns true if opened 76 bool gdbmclass::opendatabase (const text_t &filename, int mode, int num_retrys) { 119 bool gdbmclass::opendatabase (const text_t &filename, int mode, int num_retrys, 120 bool need_filelock) { 77 121 text_t data_location; 78 122 int block_size = 512; … … 87 131 char *namebuffer = filename.getcstr(); 88 132 do { 133 #ifdef __WIN32__ 134 gdbmfile = gdbm_open (namebuffer, block_size, mode, 00664, NULL, (need_filelock) ? 1 : 0); 135 #else 89 136 gdbmfile = gdbm_open (namebuffer, block_size, mode, 00664, NULL); 137 #endif 90 138 num_retrys--; 91 139 } while (num_retrys>0 && gdbmfile==NULL && … … 114 162 if (gdbmfile == NULL) return false; 115 163 164 text_t subkey; 116 165 text_t data; 117 166 … … 121 170 while (info_here != info_end) { 122 171 // add the key 123 data.push_back('<'); 172 subkey.clear(); 173 subkey.push_back('<'); 124 174 text_t::const_iterator subkey_here = (*info_here).first.begin(); 125 175 text_t::const_iterator subkey_end = (*info_here).first.end(); 126 176 while (subkey_here != subkey_end) { 127 177 if (*subkey_here == '>') { 128 data.push_back('\\'); data.push_back('>');178 subkey.push_back('\\'); subkey.push_back('>'); 129 179 } else if (*subkey_here == '\n') { 130 data.push_back('\\'); data.push_back('n');180 subkey.push_back('\\'); subkey.push_back('n'); 131 181 } else if (*subkey_here == '\r') { 132 data.push_back('\\'); data.push_back('r');182 subkey.push_back('\\'); subkey.push_back('r'); 133 183 } else { 134 data.push_back (*subkey_here);184 subkey.push_back (*subkey_here); 135 185 } 136 186 subkey_here++; 137 187 } 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(); 143 193 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++; 152 210 } 211 212 data.push_back('\n'); 153 213 subvalue_here++; 154 214 } 155 data.push_back('\n'); 156 215 157 216 info_here++; 158 217 } … … 376 435 info.clear (); // reset info 377 436 378 while (getinfoline (here, end, ikey, ivalue)) {379 info [ikey] = ivalue;437 while (getinfoline (here, end, ikey, ivalue)) { 438 info.addinfo (ikey, ivalue); 380 439 } 381 440 -
trunk/gsdl/src/recpt/infodbclass.h
r375 r510 19 19 20 20 #ifdef __WIN32__ 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 21 25 #include "autoconf.h" 22 26 #include "systems.h" 23 27 #include "gdbmconst.h" 24 28 #include "gdbm.h" 29 #ifdef __cplusplus 30 } 31 #endif 25 32 26 33 #else … … 29 36 30 37 38 typedef map<text_t, text_tarray, lttext_t> text_tarraymap; 39 40 31 41 // infodbclass is used to store information about a object 32 42 class infodbclass { 33 43 protected: 34 text_t map info;44 text_tarraymap info; 35 45 36 46 public: 37 // type support for text_tmap38 typedef text_t map::iterator iterator;39 typedef text_t map::const_iterator const_iterator;40 typedef text_t map::reference reference;41 typedef text_t map::const_reference const_reference;42 typedef text_t map::size_type size_type;43 typedef text_t map::difference_type difference_type;44 typedef text_t map::const_reverse_iterator const_reverse_iterator;45 typedef text_t map::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; 46 56 47 57 // constructors … … 65 75 void clear () {info.erase(info.begin(),info.end());} 66 76 77 // the following functions deal with keys that can only 78 // have one value for compatibility 79 67 80 // getinfo returns NULL if there isn't an entry with 68 81 // 'key' already defined, getintinfo returns 0 if there wasn't an … … 74 87 text_t *getinfo (const text_t &key); 75 88 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); 77 101 }; 78 102 … … 84 108 85 109 // 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); 87 112 void closedatabase (); 88 113
Note:
See TracChangeset
for help on using the changeset viewer.