1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * maptools.cpp --
|
---|
4 | * Copyright (C) 1999 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 | * $Id: maptools.cpp 534 1999-09-07 04:57:43Z sjboddie $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.3 1999/09/07 04:57:22 sjboddie
|
---|
31 | added gpl notice
|
---|
32 |
|
---|
33 | Revision 1.2 1999/08/31 22:39:28 rjmcnab
|
---|
34 | Changes for AIX.
|
---|
35 |
|
---|
36 | Revision 1.1 1999/03/09 20:53:13 rjmcnab
|
---|
37 | Moved from src/recpt.
|
---|
38 |
|
---|
39 | Revision 1.1 1999/03/08 19:45:53 rjmcnab
|
---|
40 | Initial revision.
|
---|
41 |
|
---|
42 | */
|
---|
43 |
|
---|
44 |
|
---|
45 | #include "maptools.h"
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | void stringmap::clear () {
|
---|
50 | fromorder.erase (fromorder.begin(), fromorder.end());
|
---|
51 | mapfrom.erase (mapfrom.begin(), mapfrom.end());
|
---|
52 | }
|
---|
53 |
|
---|
54 | void stringmap::importmap (const text_tarray &maparray) {
|
---|
55 | clear ();
|
---|
56 |
|
---|
57 | text_tarray::const_iterator here = maparray.begin();
|
---|
58 | text_tarray::const_iterator end = maparray.end();
|
---|
59 | text_t from;
|
---|
60 | text_t to;
|
---|
61 |
|
---|
62 | while (here != end) {
|
---|
63 | splitmapentry (*here, from, to);
|
---|
64 | fromorder.push_back (from);
|
---|
65 | mapfrom[from] = to;
|
---|
66 | here++;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | void stringmap::exportmap (text_tarray &maparray) const {
|
---|
71 | maparray.erase (maparray.begin(), maparray.end());
|
---|
72 |
|
---|
73 | text_tarray::const_iterator here = fromorder.begin();
|
---|
74 | text_tarray::const_iterator end = fromorder.end();
|
---|
75 | text_tmap::const_iterator toptr;
|
---|
76 | text_t tempmap;
|
---|
77 |
|
---|
78 | while (here != end) {
|
---|
79 | toptr = mapfrom.find (*here);
|
---|
80 | if (toptr != mapfrom.end()) {
|
---|
81 | joinmapentry ((*toptr).first, (*toptr).second, tempmap);
|
---|
82 | maparray.push_back (tempmap);
|
---|
83 | }
|
---|
84 |
|
---|
85 | here++;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | void stringmap::getfromarray (text_tarray &fromarray) const {
|
---|
90 | fromarray = fromorder;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void stringmap::gettoarray (text_tarray &toarray) const {
|
---|
94 | toarray.erase(toarray.begin(), toarray.end());
|
---|
95 |
|
---|
96 | text_tarray::const_iterator here = fromorder.begin();
|
---|
97 | text_tarray::const_iterator end = fromorder.end();
|
---|
98 | text_tmap::const_iterator toptr;
|
---|
99 |
|
---|
100 | while (here != end) {
|
---|
101 | toptr = mapfrom.find (*here);
|
---|
102 | if (toptr != mapfrom.end()) {
|
---|
103 | toarray.push_back((*toptr).second);
|
---|
104 | }
|
---|
105 | here++;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | bool stringmap::fromexists (const text_t &from) const {
|
---|
110 | return (mapfrom.find (from) != mapfrom.end ());
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool stringmap::toexists (const text_t &to) const {
|
---|
114 | text_tarray::const_iterator here = fromorder.begin();
|
---|
115 | text_tarray::const_iterator end = fromorder.end();
|
---|
116 | text_tmap::const_iterator toptr;
|
---|
117 |
|
---|
118 | while (here != end) {
|
---|
119 | toptr = mapfrom.find (*here);
|
---|
120 | if (toptr != mapfrom.end() && (*toptr).second == to) return true;
|
---|
121 | here++;
|
---|
122 | }
|
---|
123 |
|
---|
124 | // it was not found
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool stringmap::from2to (const text_t &from, text_t &to) const {
|
---|
129 | text_tmap::const_iterator toptr = mapfrom.find (from);
|
---|
130 | if (toptr == mapfrom.end()) {
|
---|
131 | to.clear();
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 | to = (*toptr).second;
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | bool stringmap::to2from (const text_t &to, text_t &from) const {
|
---|
140 | text_tarray::const_iterator here = fromorder.begin();
|
---|
141 | text_tarray::const_iterator end = fromorder.end();
|
---|
142 | text_tmap::const_iterator toptr;
|
---|
143 |
|
---|
144 | while (here != end) {
|
---|
145 | toptr = mapfrom.find (*here);
|
---|
146 | if (toptr != mapfrom.end() && (*toptr).second == to) {
|
---|
147 | from = (*toptr).first;
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 | here++;
|
---|
151 | }
|
---|
152 |
|
---|
153 | // it was not found
|
---|
154 | from.clear();
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 | void splitmapentry (const text_t &mapentry, text_t &from, text_t &to) {
|
---|
161 | from.clear ();
|
---|
162 | to.clear();
|
---|
163 |
|
---|
164 | text_t::const_iterator here = mapentry.begin();
|
---|
165 | text_t::const_iterator end = mapentry.end();
|
---|
166 |
|
---|
167 | // get the "from" part of the map
|
---|
168 | while (here != end) {
|
---|
169 | if (*here == '-') {
|
---|
170 | here++;
|
---|
171 | if (here != end && *here == '>') {
|
---|
172 | here++;
|
---|
173 | break; // found "->"
|
---|
174 | }
|
---|
175 |
|
---|
176 | // didn't find "->"
|
---|
177 | from.push_back('-');
|
---|
178 |
|
---|
179 | } else {
|
---|
180 | from.push_back(*here);
|
---|
181 | here++;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | // get the "to" part of the map
|
---|
186 | while (here != end) {
|
---|
187 | to.push_back(*here);
|
---|
188 | here++;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | void joinmapentry (const text_t &from, const text_t &to, text_t &mapentry) {
|
---|
193 | mapentry = from;
|
---|
194 | mapentry += "->";
|
---|
195 | mapentry += to;
|
---|
196 | }
|
---|