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