1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * infodbclass.h --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * PUT COPYRIGHT NOTICE HERE
|
---|
7 | *
|
---|
8 | * $Id: infodbclass.h 510 1999-09-02 00:26:11Z rjmcnab $
|
---|
9 | *
|
---|
10 | *********************************************************************/
|
---|
11 |
|
---|
12 |
|
---|
13 | #ifndef INFODBCLASS_H
|
---|
14 | #define INFODBCLASS_H
|
---|
15 |
|
---|
16 |
|
---|
17 | #include "gsdlconf.h"
|
---|
18 | #include "text_t.h"
|
---|
19 |
|
---|
20 | #ifdef __WIN32__
|
---|
21 |
|
---|
22 | #ifdef __cplusplus
|
---|
23 | extern "C" {
|
---|
24 | #endif
|
---|
25 | #include "autoconf.h"
|
---|
26 | #include "systems.h"
|
---|
27 | #include "gdbmconst.h"
|
---|
28 | #include "gdbm.h"
|
---|
29 | #ifdef __cplusplus
|
---|
30 | }
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #else
|
---|
34 | #include <gdbm.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 |
|
---|
38 | typedef map<text_t, text_tarray, lttext_t> text_tarraymap;
|
---|
39 |
|
---|
40 |
|
---|
41 | // infodbclass is used to store information about a object
|
---|
42 | class infodbclass {
|
---|
43 | protected:
|
---|
44 | text_tarraymap info;
|
---|
45 |
|
---|
46 | public:
|
---|
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;
|
---|
56 |
|
---|
57 | // constructors
|
---|
58 | infodbclass ();
|
---|
59 |
|
---|
60 | // basic container support
|
---|
61 | iterator begin () {return info.begin();}
|
---|
62 | const_iterator begin () const {return info.begin();}
|
---|
63 | iterator end () {return info.end();}
|
---|
64 | const_iterator end () const {return info.end();}
|
---|
65 |
|
---|
66 | void erase(iterator pos) {info.erase(pos);}
|
---|
67 | void erase(iterator first, iterator last) {info.erase(first, last);}
|
---|
68 | infodbclass &operator=(const infodbclass &x) {info=x.info;return *this;}
|
---|
69 |
|
---|
70 | bool empty () const {return info.empty();}
|
---|
71 | size_type size() const {return info.size();}
|
---|
72 |
|
---|
73 |
|
---|
74 | // added functionality
|
---|
75 | void clear () {info.erase(info.begin(),info.end());}
|
---|
76 |
|
---|
77 | // the following functions deal with keys that can only
|
---|
78 | // have one value for compatibility
|
---|
79 |
|
---|
80 | // getinfo returns NULL if there isn't an entry with
|
---|
81 | // 'key' already defined, getintinfo returns 0 if there wasn't an
|
---|
82 | // entry with 'key' defined and operator[] returns "" if
|
---|
83 | // 'key' wasn't already defined (and sets 'key'="").
|
---|
84 | void setinfo (const text_t &key, const text_t &value);
|
---|
85 | void setintinfo (const text_t &key, int value);
|
---|
86 | void setcinfo (const text_t &key, unsigned short c);
|
---|
87 | text_t *getinfo (const text_t &key);
|
---|
88 | int getintinfo (const text_t &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);
|
---|
101 | };
|
---|
102 |
|
---|
103 |
|
---|
104 | class gdbmclass {
|
---|
105 | public:
|
---|
106 | gdbmclass() {gdbmfile = NULL; logout = &cerr;};
|
---|
107 | ~gdbmclass() {};
|
---|
108 |
|
---|
109 | // returns true if opened
|
---|
110 | bool opendatabase (const text_t &filename, int mode, int num_retrys,
|
---|
111 | bool need_filelock);
|
---|
112 | void closedatabase ();
|
---|
113 |
|
---|
114 | // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
|
---|
115 | // it expects child, parent, etc. to exist if syntax has been used
|
---|
116 | // so you should test before using
|
---|
117 | text_t translate_OID (const text_t &OID, infodbclass &info);
|
---|
118 |
|
---|
119 | // returns true on success
|
---|
120 | bool getinfo (text_t key, infodbclass &info);
|
---|
121 | void setlogout (ostream *thelogout) {logout = thelogout;}
|
---|
122 |
|
---|
123 | // returns true if exists
|
---|
124 | bool exists (text_t key);
|
---|
125 |
|
---|
126 | // returns true on success
|
---|
127 | bool setinfo (const text_t &key, const infodbclass &info);
|
---|
128 |
|
---|
129 | void deletekey (const text_t &key);
|
---|
130 |
|
---|
131 | // getfirstkey and getnextkey are used for traversing the database
|
---|
132 | // no insertions or deletions should be carried out while traversing
|
---|
133 | // the database. when there are no keys left to visit in the database
|
---|
134 | // an empty string is returned.
|
---|
135 | text_t getfirstkey ();
|
---|
136 | text_t getnextkey (const text_t &key);
|
---|
137 |
|
---|
138 | protected:
|
---|
139 | text_t openfile;
|
---|
140 | GDBM_FILE gdbmfile;
|
---|
141 | ostream *logout;
|
---|
142 |
|
---|
143 | void get_first_child (text_t &OID, infodbclass &info);
|
---|
144 | void get_last_child (text_t &OID, infodbclass &info);
|
---|
145 | void get_next_sibling (text_t &OID, infodbclass &info);
|
---|
146 | void get_previous_sibling (text_t &OID, infodbclass &info);
|
---|
147 |
|
---|
148 | // returns true on success
|
---|
149 | bool getkeydata (text_t key, text_t &data);
|
---|
150 |
|
---|
151 | // returns true on success
|
---|
152 | bool getinfoline (text_t::iterator &here, text_t::iterator end,
|
---|
153 | text_t &key, text_t &value);
|
---|
154 | };
|
---|
155 |
|
---|
156 |
|
---|
157 | #endif
|
---|