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