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 375 1999-07-13 23:24:06Z 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 | #include "autoconf.h"
|
---|
22 | #include "systems.h"
|
---|
23 | #include "gdbmconst.h"
|
---|
24 | #include "gdbm.h"
|
---|
25 |
|
---|
26 | #else
|
---|
27 | #include <gdbm.h>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 |
|
---|
31 | // infodbclass is used to store information about a object
|
---|
32 | class infodbclass {
|
---|
33 | protected:
|
---|
34 | text_tmap info;
|
---|
35 |
|
---|
36 | public:
|
---|
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;
|
---|
46 |
|
---|
47 | // constructors
|
---|
48 | infodbclass ();
|
---|
49 |
|
---|
50 | // basic container support
|
---|
51 | iterator begin () {return info.begin();}
|
---|
52 | const_iterator begin () const {return info.begin();}
|
---|
53 | iterator end () {return info.end();}
|
---|
54 | const_iterator end () const {return info.end();}
|
---|
55 |
|
---|
56 | void erase(iterator pos) {info.erase(pos);}
|
---|
57 | void erase(iterator first, iterator last) {info.erase(first, last);}
|
---|
58 | infodbclass &operator=(const infodbclass &x) {info=x.info;return *this;}
|
---|
59 |
|
---|
60 | bool empty () const {return info.empty();}
|
---|
61 | size_type size() const {return info.size();}
|
---|
62 |
|
---|
63 |
|
---|
64 | // added functionality
|
---|
65 | void clear () {info.erase(info.begin(),info.end());}
|
---|
66 |
|
---|
67 | // getinfo returns NULL if there isn't an entry with
|
---|
68 | // 'key' already defined, getintinfo returns 0 if there wasn't an
|
---|
69 | // entry with 'key' defined and operator[] returns "" if
|
---|
70 | // 'key' wasn't already defined (and sets 'key'="").
|
---|
71 | void setinfo (const text_t &key, const text_t &value);
|
---|
72 | void setintinfo (const text_t &key, int value);
|
---|
73 | void setcinfo (const text_t &key, unsigned short c);
|
---|
74 | text_t *getinfo (const text_t &key);
|
---|
75 | int getintinfo (const text_t &key);
|
---|
76 | text_t &operator[] (const text_t &key) {return info[key];}
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 | class gdbmclass {
|
---|
81 | public:
|
---|
82 | gdbmclass() {gdbmfile = NULL; logout = &cerr;};
|
---|
83 | ~gdbmclass() {};
|
---|
84 |
|
---|
85 | // returns true if opened
|
---|
86 | bool opendatabase (const text_t &filename, int mode=GDBM_READER, int num_retrys=1);
|
---|
87 | void closedatabase ();
|
---|
88 |
|
---|
89 | // replaces the .c, .p, .n, .l syntax (child, parent, next, previous)
|
---|
90 | // it expects child, parent, etc. to exist if syntax has been used
|
---|
91 | // so you should test before using
|
---|
92 | text_t translate_OID (const text_t &OID, infodbclass &info);
|
---|
93 |
|
---|
94 | // returns true on success
|
---|
95 | bool getinfo (text_t key, infodbclass &info);
|
---|
96 | void setlogout (ostream *thelogout) {logout = thelogout;}
|
---|
97 |
|
---|
98 | // returns true if exists
|
---|
99 | bool exists (text_t key);
|
---|
100 |
|
---|
101 | // returns true on success
|
---|
102 | bool setinfo (const text_t &key, const infodbclass &info);
|
---|
103 |
|
---|
104 | void deletekey (const text_t &key);
|
---|
105 |
|
---|
106 | // getfirstkey and getnextkey are used for traversing the database
|
---|
107 | // no insertions or deletions should be carried out while traversing
|
---|
108 | // the database. when there are no keys left to visit in the database
|
---|
109 | // an empty string is returned.
|
---|
110 | text_t getfirstkey ();
|
---|
111 | text_t getnextkey (const text_t &key);
|
---|
112 |
|
---|
113 | protected:
|
---|
114 | text_t openfile;
|
---|
115 | GDBM_FILE gdbmfile;
|
---|
116 | ostream *logout;
|
---|
117 |
|
---|
118 | void get_first_child (text_t &OID, infodbclass &info);
|
---|
119 | void get_last_child (text_t &OID, infodbclass &info);
|
---|
120 | void get_next_sibling (text_t &OID, infodbclass &info);
|
---|
121 | void get_previous_sibling (text_t &OID, infodbclass &info);
|
---|
122 |
|
---|
123 | // returns true on success
|
---|
124 | bool getkeydata (text_t key, text_t &data);
|
---|
125 |
|
---|
126 | // returns true on success
|
---|
127 | bool getinfoline (text_t::iterator &here, text_t::iterator end,
|
---|
128 | text_t &key, text_t &value);
|
---|
129 | };
|
---|
130 |
|
---|
131 |
|
---|
132 | #endif
|
---|