source: gsdl/trunk/lib/dbclass.cpp@ 15652

Last change on this file since 15652 was 15652, checked in by mdewsnip, 16 years ago

(Adding new DB support) Moved the exists() function into dbclass because it can be shared between gdbmclass and sqlitedbclass.

File size: 4.9 KB
Line 
1/**********************************************************************
2 *
3 * dbclass.cpp --
4 * Copyright (C) 1999-2008 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#include "dbclass.h"
27#include "OIDtools.h"
28
29
30dbclass::~dbclass() {}
31
32
33// May be overwritten by subclasses; returns true if exists
34bool dbclass::exists (const text_t& key)
35{
36 text_t data;
37 return getkeydata (key, data);
38}
39
40
41// replaces the .fc, .lc, .pr, .rt, .ns and .ps syntax (first child,
42// last child, parent, root, next sibling, previous sibling)
43// it expects child, parent, etc. to exist if syntax has been used
44// so you should test before using
45text_t dbclass::translate_OID (const text_t &inOID, infodbclass &info)
46{
47 if (inOID.size() < 4) return inOID;
48 if (findchar (inOID.begin(), inOID.end(), '.') == inOID.end()) return inOID;
49
50 text_t OID = inOID;
51 text_tarray tailarray;
52 text_t tail = substr (OID.end()-3, OID.end());
53 if (tail == ".rt") {
54 get_top (inOID, OID);
55 return OID;
56 }
57 while (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
58 tail == ".ns" || tail == ".ps") {
59 tailarray.push_back(tail);
60 OID.erase (OID.end()-3, OID.end());
61 tail = substr (OID.end()-3, OID.end());
62 if (tail == ".rt") {
63 get_top (inOID, OID);
64 return OID;
65 }
66 }
67
68 if (tailarray.empty()) return inOID;
69 text_tarray::const_iterator begin = tailarray.begin();
70 text_tarray::const_iterator here = tailarray.end() - 1;
71
72 while (here >= begin) {
73
74 if (*here == ".fc")
75 get_first_child (OID, info);
76 else if (*here == ".lc")
77 get_last_child (OID, info);
78 else if (*here == ".pr")
79 OID = get_parent (OID);
80 else if (*here == ".ns")
81 get_next_sibling (OID, info);
82 else if (*here == ".ps")
83 get_previous_sibling (OID, info);
84
85 if (here == begin)
86 break;
87 --here;
88 }
89
90 return OID;
91}
92
93
94void dbclass::get_first_child (text_t &OID, infodbclass &info)
95{
96 text_t firstchild;
97 if (getinfo (OID, info)) {
98 text_t &contains = info["contains"];
99 if (!contains.empty()) {
100 text_t parent = OID;
101 getdelimitstr (contains.begin(), contains.end(), ';', firstchild);
102 if (firstchild.empty()) OID = contains;
103 else OID = firstchild;
104 if (*(OID.begin()) == '"') translate_parent (OID, parent);
105 }
106 }
107}
108
109
110void dbclass::get_last_child (text_t &OID, infodbclass &info)
111{
112 text_tarray children;
113 if (getinfo (OID, info)) {
114 text_t &contains = info["contains"];
115 if (!contains.empty()) {
116 text_t parent = OID;
117 splitchar (contains.begin(), contains.end(), ';', children);
118 OID = children.back();
119 if (*(OID.begin()) == '"') translate_parent (OID, parent);
120 }
121 }
122}
123
124
125void dbclass::get_next_sibling (text_t &OID, infodbclass &info)
126{
127 text_tarray siblings;
128 text_t parent = get_parent (OID);
129
130 if (getinfo (parent, info)) {
131 text_t &contains = info["contains"];
132 if (!contains.empty()) {
133 splitchar (contains.begin(), contains.end(), ';', siblings);
134 text_tarray::const_iterator here = siblings.begin();
135 text_tarray::const_iterator end = siblings.end();
136 text_t shrunk_OID = OID;
137 shrink_parent (shrunk_OID);
138 while (here != end) {
139 if (*here == shrunk_OID && (here+1 != end)) {
140 OID = *(here+1);
141 if (*(OID.begin()) == '"') translate_parent (OID, parent);
142 break;
143 }
144 ++here;
145 }
146 }
147 }
148}
149
150
151void dbclass::get_previous_sibling (text_t &OID, infodbclass &info)
152{
153 text_tarray siblings;
154 text_t parent = get_parent (OID);
155
156 if (getinfo (parent, info)) {
157 text_t &contains = info["contains"];
158 if (!contains.empty()) {
159 splitchar (contains.begin(), contains.end(), ';', siblings);
160 text_tarray::const_iterator here = siblings.begin();
161 text_tarray::const_iterator end = siblings.end();
162 text_t shrunk_OID = OID;
163 shrink_parent (shrunk_OID);
164 while (here != end) {
165 if (*here == shrunk_OID && (here != siblings.begin())) {
166 OID = *(here-1);
167 if (*(OID.begin()) == '"') translate_parent (OID, parent);
168 break;
169 }
170 ++here;
171 }
172 }
173 }
174}
Note: See TracBrowser for help on using the repository browser.