1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * infodbclass.h --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 |
|
---|
27 | #ifndef INFODBCLASS_H
|
---|
28 | #define INFODBCLASS_H
|
---|
29 |
|
---|
30 |
|
---|
31 | #include "gsdlconf.h"
|
---|
32 | #include "text_t.h"
|
---|
33 |
|
---|
34 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
35 | # include <ospace\std\iostream>
|
---|
36 | # include <ospace\std\fstream>
|
---|
37 | #elif defined(GSDL_USE_IOS_H)
|
---|
38 | # include <iostream.h>
|
---|
39 | # include <fstream.h>
|
---|
40 | #else
|
---|
41 | # include <iostream>
|
---|
42 | # include <fstream>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #ifdef __WIN32__
|
---|
46 |
|
---|
47 | #ifdef __cplusplus
|
---|
48 | extern "C" {
|
---|
49 | #endif
|
---|
50 | #include "autoconf.h"
|
---|
51 | #include "systems.h"
|
---|
52 | #include "gdbmconst.h"
|
---|
53 | #include "gdbm.h"
|
---|
54 | #ifdef __cplusplus
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #else
|
---|
59 | #include <gdbm.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | typedef map<text_t, text_tarray, lttext_t> text_tarraymap;
|
---|
64 |
|
---|
65 |
|
---|
66 | // infodbclass is used to store information about a object
|
---|
67 | class infodbclass {
|
---|
68 | protected:
|
---|
69 | text_tarraymap info;
|
---|
70 |
|
---|
71 | public:
|
---|
72 | // type support for text_tarraymap
|
---|
73 | typedef text_tarraymap::iterator iterator;
|
---|
74 | typedef text_tarraymap::const_iterator const_iterator;
|
---|
75 | typedef text_tarraymap::reference reference;
|
---|
76 | typedef text_tarraymap::const_reference const_reference;
|
---|
77 | typedef text_tarraymap::size_type size_type;
|
---|
78 | typedef text_tarraymap::difference_type difference_type;
|
---|
79 | typedef text_tarraymap::const_reverse_iterator const_reverse_iterator;
|
---|
80 | typedef text_tarraymap::reverse_iterator reverse_iterator;
|
---|
81 |
|
---|
82 | // constructors
|
---|
83 | infodbclass ();
|
---|
84 |
|
---|
85 | // basic container support
|
---|
86 | iterator begin () {return info.begin();}
|
---|
87 | const_iterator begin () const {return info.begin();}
|
---|
88 | iterator end () {return info.end();}
|
---|
89 | const_iterator end () const {return info.end();}
|
---|
90 |
|
---|
91 | void erase(iterator pos) {info.erase(pos);}
|
---|
92 | void erase(iterator first, iterator last) {info.erase(first, last);}
|
---|
93 | infodbclass &operator=(const infodbclass &x) {info=x.info;return *this;}
|
---|
94 |
|
---|
95 | bool empty () const {return info.empty();}
|
---|
96 | size_type size() const {return info.size();}
|
---|
97 |
|
---|
98 |
|
---|
99 | // added functionality
|
---|
100 | void clear () {info.erase(info.begin(),info.end());}
|
---|
101 |
|
---|
102 | // the following functions deal with keys that can only
|
---|
103 | // have one value for compatibility
|
---|
104 |
|
---|
105 | // getinfo returns NULL if there isn't an entry with
|
---|
106 | // 'key' already defined, getintinfo returns 0 if there wasn't an
|
---|
107 | // entry with 'key' defined and operator[] returns "" if
|
---|
108 | // 'key' wasn't already defined (and sets 'key'="").
|
---|
109 | void setinfo (const text_t &key, const text_t &value);
|
---|
110 | void setintinfo (const text_t &key, int value);
|
---|
111 | void setcinfo (const text_t &key, unsigned short c);
|
---|
112 | text_t *getinfo (const text_t &key);
|
---|
113 | int getintinfo (const text_t &key);
|
---|
114 | text_t &operator[] (const text_t &key);
|
---|
115 |
|
---|
116 |
|
---|
117 | // the next set of functions allow you to set and access keys
|
---|
118 | // that can have more than one value
|
---|
119 |
|
---|
120 | // getmultinfo returns NULL if there isn't an entry with
|
---|
121 | // 'key' already defined
|
---|
122 | void addinfo (const text_t &key, const text_t &value);
|
---|
123 | void addintinfo (const text_t &key, int value);
|
---|
124 | void addcinfo (const text_t &key, unsigned short c);
|
---|
125 | text_tarray *getmultinfo (const text_t &key);
|
---|
126 | };
|
---|
127 |
|
---|
128 |
|
---|
129 | class gdbmclass {
|
---|
130 | public:
|
---|
131 | gdbmclass() {gdbmfile = NULL; logout = &cerr;};
|
---|
132 | ~gdbmclass();
|
---|
133 |
|
---|
134 | // returns true if opened
|
---|
135 | bool opendatabase (const text_t &filename, int mode, int num_retrys,
|
---|
136 | bool need_filelock);
|
---|
137 | void closedatabase ();
|
---|
138 |
|
---|
139 | // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
|
---|
140 | // it expects child, parent, etc. to exist if syntax has been used
|
---|
141 | // so you should test before using
|
---|
142 | text_t translate_OID (const text_t &OID, infodbclass &info);
|
---|
143 |
|
---|
144 | // returns true on success
|
---|
145 | bool getinfo (text_t key, infodbclass &info);
|
---|
146 | void setlogout (ostream *thelogout) {logout = thelogout;}
|
---|
147 |
|
---|
148 | // returns true if exists
|
---|
149 | bool exists (text_t key);
|
---|
150 |
|
---|
151 | // returns true on success
|
---|
152 | bool setinfo (const text_t &key, const infodbclass &info);
|
---|
153 | // returns true on success
|
---|
154 | bool setinfo (const text_t &key, const text_t &data);
|
---|
155 |
|
---|
156 | void deletekey (const text_t &key);
|
---|
157 |
|
---|
158 | // getfirstkey and getnextkey are used for traversing the database
|
---|
159 | // no insertions or deletions should be carried out while traversing
|
---|
160 | // the database. when there are no keys left to visit in the database
|
---|
161 | // an empty string is returned.
|
---|
162 | text_t getfirstkey ();
|
---|
163 | text_t getnextkey (const text_t &key);
|
---|
164 |
|
---|
165 | // returns true on success
|
---|
166 | bool getkeydata (text_t key, text_t &data);
|
---|
167 |
|
---|
168 | protected:
|
---|
169 | text_t openfile;
|
---|
170 | GDBM_FILE gdbmfile;
|
---|
171 | ostream *logout;
|
---|
172 |
|
---|
173 | void get_first_child (text_t &OID, infodbclass &info);
|
---|
174 | void get_last_child (text_t &OID, infodbclass &info);
|
---|
175 | void get_next_sibling (text_t &OID, infodbclass &info);
|
---|
176 | void get_previous_sibling (text_t &OID, infodbclass &info);
|
---|
177 |
|
---|
178 |
|
---|
179 | // returns true on success
|
---|
180 | bool getinfoline (text_t::iterator &here, text_t::iterator end,
|
---|
181 | text_t &key, text_t &value);
|
---|
182 | };
|
---|
183 |
|
---|
184 |
|
---|
185 | #endif
|
---|