source: gsdl/trunk/common-src/src/lib/OIDtools.cpp@ 18050

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

(Untangling colservr/recpt) Split recpt/OIDtools into two: lib/OIDtools.cpp/h contains the purely string-based functions (may be used by the colservr), and recpt/recptprototools.cpp/h contains the functions requiring a call to the colservr (get_info() etc.).

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1/**********************************************************************
2 *
3 * OIDtools.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 "OIDtools.h"
27
28
29// returns (in top) the top level of OID (i.e. everything
30// up until the first dot)
31void get_top (const text_t &OID, text_t &top)
32{
33 top.clear();
34 if (OID.empty()) return;
35
36 text_t::const_iterator begin = OID.begin();
37 text_t::const_iterator end = OID.end();
38
39 top.appendrange (begin, findchar(begin, end, '.'));
40}
41
42
43// checks if OID is top level (i.e. contains no dots)
44bool is_top (const text_t &OID)
45{
46 if (OID.empty()) return true;
47
48 text_t::const_iterator here = OID.begin();
49 text_t::const_iterator end = OID.end();
50 here = findchar (here, end, '.');
51
52 if (here == end) return true;
53 return false;
54}
55
56
57// get_parents_array loads the parents array with all the parents of the
58// document or classification specified by OID (not including OID itself)
59void get_parents_array (const text_t &OID, text_tarray &parents)
60{
61 text_t::const_iterator here = OID.begin ();
62 text_t::const_iterator end = OID.end ();
63 text_t thisparent;
64
65 while (here != end) {
66 if (*here == '.') parents.push_back(thisparent);
67 thisparent.push_back(*here);
68 ++here;
69 }
70}
71
72
73// get_parent returns the parent of the document or classification
74// specified by OID
75text_t get_parent (const text_t& OID)
76{
77 if (OID.empty() || is_top (OID)) return g_EmptyText;
78
79 text_t::const_iterator begin = OID.begin();
80 text_t::const_iterator here = (OID.end() - 1);
81
82 while (here >= begin) {
83 if (*here == '.')
84 break;
85 if (here == begin)
86 break;
87 --here;
88 }
89
90 if (here != begin) {
91 text_t parentOID;
92 parentOID.appendrange(begin, here);
93 return parentOID;
94 }
95
96 return g_EmptyText;
97}
98
99
100// takes an OID like ".2 and replaces the " with parent
101void translate_parent (text_t &OID, const text_t &parent)
102{
103 text_t::const_iterator here = OID.begin();
104 text_t::const_iterator end = OID.end();
105 text_t temp;
106
107 while (here != end) {
108 if (*here == '"') temp += parent;
109 else temp.push_back (*here);
110 ++here;
111 }
112 OID = temp;
113}
114
115
116// shrink_parent does the opposite to translate_parent
117void shrink_parent (text_t &OID)
118{
119 text_tarray tmp;
120 splitchar (OID.begin(), OID.end(), '.', tmp);
121 OID = "\"." + tmp.back();
122}
123
124
125// checks if OID uses ".fc", ".lc", ".pr", "rt", ".ns",
126// or ".ps" syntax (first child, last child, parent, root,
127// next sibling, previous sibling)
128bool needs_translating (const text_t &OID)
129{
130 if (OID.size() < 4) return false;
131
132 text_t tail = substr (OID.end()-3, OID.end());
133 if (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
134 tail == ".rt" || tail == ".ns" || tail == ".ps") return true;
135
136 return false;
137}
138
139
140// strips the ".fc", ".lc", ".pr", ".ns",
141// or ".ps" suffix from the end of OID
142void strip_suffix (text_t &OID)
143{
144 text_t tail = substr (OID.end()-3, OID.end());
145 while (tail == ".fc" || tail == ".lc" || tail == ".pr" ||
146 tail == ".rt" || tail == ".ns" || tail == ".ps") {
147 OID.erase (OID.end()-3, OID.end());
148 tail = substr (OID.end()-3, OID.end());
149 }
150}
151
152
153// is_child_of returns true if OID2 is a child of OID1
154bool is_child_of(const text_t &OID1, const text_t &OID2)
155{
156 text_t parent = get_parent(OID2);
157
158 while (!parent.empty()) {
159 if (parent == OID1) return true;
160 parent = get_parent(parent);
161 }
162
163 return false;
164}
Note: See TracBrowser for help on using the repository browser.