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

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

(Adding new DB support) Moved some non-GDBM-specific code out of gdbmclass and into dbclass.

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