1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * UCArray.cpp -- vector based string class
|
---|
4 | * Copyright (C) 1999 Rodger McNab
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | **************************************************************************/
|
---|
21 |
|
---|
22 | #include "UCArray.h"
|
---|
23 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
24 |
|
---|
25 |
|
---|
26 | void SetCStr (UCArray &text, const char *cStr) {
|
---|
27 | text.erase(text.begin(), text.end());
|
---|
28 |
|
---|
29 | while (*cStr != '\0') {
|
---|
30 | text.push_back (*cStr);
|
---|
31 | cStr++;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | char * GetCStr(UCArray text) {
|
---|
36 |
|
---|
37 | char *cstr = new char[text.size()+1];
|
---|
38 | UCArray::const_iterator here = text.begin();
|
---|
39 | UCArray::const_iterator end = text.end();
|
---|
40 |
|
---|
41 | int i = 0;
|
---|
42 | while (here != end) {
|
---|
43 | cstr[i] = text[i];
|
---|
44 | here++; i++;
|
---|
45 | }
|
---|
46 | cstr[i]='\0';
|
---|
47 | return cstr;
|
---|
48 | }
|
---|
49 |
|
---|
50 | ostream &operator<<(ostream &s, const UCArray &a) {
|
---|
51 | UCArray::const_iterator here = a.begin();
|
---|
52 | UCArray::const_iterator end = a.end();
|
---|
53 | while (here != end) {
|
---|
54 | s << *here;
|
---|
55 | here++;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return s;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | bool ReadVarLenUL (FILE *f, unsigned long &n) {
|
---|
63 | register unsigned long temp = 0;
|
---|
64 | register unsigned int bitPos = 0;
|
---|
65 | unsigned char b = 0;
|
---|
66 |
|
---|
67 | do {
|
---|
68 | b = fgetc (f);
|
---|
69 | if (feof(f)) return false;
|
---|
70 | temp |= (b & 0x7f) << bitPos;
|
---|
71 | bitPos += 7;
|
---|
72 | } while (b >= 0x80 && bitPos < 32);
|
---|
73 |
|
---|
74 | n = temp;
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | bool WriteVarLenUL (FILE *f, unsigned long n) {
|
---|
80 | register unsigned long temp = n;
|
---|
81 | register unsigned char b = 0;
|
---|
82 | do {
|
---|
83 | b = static_cast<unsigned char> (temp & 0x7f);
|
---|
84 | if (temp >= 0x80) b |= 0x80;
|
---|
85 | fputc (b, f);
|
---|
86 | if (ferror (f) != 0) return false;
|
---|
87 | } while ((temp = temp >> 7) > 0);
|
---|
88 |
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | bool ReadUL (FILE *f, unsigned long &n) {
|
---|
94 | if (fread (&n, sizeof (unsigned long), 1, f) <= 0) return false;
|
---|
95 | NTOHUL (n);
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | bool WriteUL (FILE *f, unsigned long n) {
|
---|
101 | HTONUL (n);
|
---|
102 | return (fwrite (&n, sizeof (unsigned long), 1, f) > 0);
|
---|
103 | }
|
---|
104 |
|
---|
105 | bool ReadF (FILE *f, float &n) {
|
---|
106 | if (fread (&n, sizeof (float), 1, f) <= 0) return false;
|
---|
107 | NTOHF(n);
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | bool WriteF (FILE *f, float n) {
|
---|
112 | HTONF(n);
|
---|
113 | return (fwrite (&n, sizeof (float), 1, f) > 0);
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool ReadD (FILE *f, double &n) {
|
---|
117 | if (fread (&n, sizeof (double), 1, f) <= 0) return false;
|
---|
118 | NTOHD(n);
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool WriteD (FILE *f, double n) {
|
---|
123 | HTOND(n);
|
---|
124 | return (fwrite (&n, sizeof (double), 1, f) > 0);
|
---|
125 | }
|
---|
126 |
|
---|
127 | bool ReadUCArray (FILE *f, UCArray &a) {
|
---|
128 | // clear the array in preparation
|
---|
129 | a.erase (a.begin(), a.end());
|
---|
130 |
|
---|
131 | // read in the array size
|
---|
132 | unsigned long arraySize = 0;
|
---|
133 | if (!ReadVarLenUL (f, arraySize)) return false;
|
---|
134 |
|
---|
135 | // read in the array
|
---|
136 | unsigned char b = 0;
|
---|
137 | while (arraySize > 0) {
|
---|
138 | b = fgetc (f);
|
---|
139 | if (feof(f)) return false;
|
---|
140 | a.push_back (b);
|
---|
141 |
|
---|
142 | arraySize--;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool WriteUCArray (FILE *f, const UCArray &a) {
|
---|
149 | // write out the array size
|
---|
150 | if (!WriteVarLenUL (f, a.size())) return false;
|
---|
151 |
|
---|
152 | UCArray::const_iterator here = a.begin();
|
---|
153 | UCArray::const_iterator end = a.end();
|
---|
154 | while (here != end) {
|
---|
155 | fputc (*here, f);
|
---|
156 | if (ferror (f) != 0) return false;
|
---|
157 |
|
---|
158 | here++;
|
---|
159 | }
|
---|
160 |
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * This array is designed for mapping upper and lower case letter
|
---|
166 | * together for a case independent comparison. The mappings are
|
---|
167 | * based upon ascii character sequences.
|
---|
168 | */
|
---|
169 | static unsigned char casecharmap[] = {
|
---|
170 | '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
|
---|
171 | '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
|
---|
172 | '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
|
---|
173 | '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
|
---|
174 | '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
|
---|
175 | '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
|
---|
176 | '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
|
---|
177 | '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
|
---|
178 | '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
---|
179 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
---|
180 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
---|
181 | '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
|
---|
182 | '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
---|
183 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
---|
184 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
---|
185 | '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
|
---|
186 | '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
|
---|
187 | '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
|
---|
188 | '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
|
---|
189 | '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
|
---|
190 | '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
|
---|
191 | '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
|
---|
192 | '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
|
---|
193 | '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
|
---|
194 | '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
|
---|
195 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
|
---|
196 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
|
---|
197 | '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
|
---|
198 | '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
|
---|
199 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
|
---|
200 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
|
---|
201 | '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
|
---|
202 | };
|
---|
203 |
|
---|
204 |
|
---|
205 | int DictCompare (const UCArray &a1, const UCArray &a2) {
|
---|
206 | unsigned int l1 = a1.size();
|
---|
207 | unsigned int l2 = a2.size();
|
---|
208 | unsigned int l = (l1 < l2) ? l1 : l2;
|
---|
209 | int pos = 0;
|
---|
210 | register int diff = 0;
|
---|
211 |
|
---|
212 | UCArray::const_iterator a1Here = a1.begin();
|
---|
213 | UCArray::const_iterator a2Here = a2.begin();
|
---|
214 |
|
---|
215 | while (l--) {
|
---|
216 | if ((diff = casecharmap[*a1Here] - casecharmap[*a2Here]) != 0)
|
---|
217 | return diff;
|
---|
218 | if (pos == 0 && (diff = *a1Here - *a2Here) != 0)
|
---|
219 | pos = diff;
|
---|
220 |
|
---|
221 | a1Here++;
|
---|
222 | a2Here++;
|
---|
223 | }
|
---|
224 |
|
---|
225 | return ((l1 - l2) ? (l1 - l2) : (pos));
|
---|
226 | }
|
---|
227 |
|
---|
228 | unsigned long PrefixLen (const UCArray &a1, const UCArray &a2) {
|
---|
229 | unsigned long l = (a1.size() < a2.size()) ? a1.size() : a2.size();
|
---|
230 | unsigned long i = 0;
|
---|
231 |
|
---|
232 | UCArray::const_iterator a1Here = a1.begin();
|
---|
233 | UCArray::const_iterator a2Here = a2.begin();
|
---|
234 |
|
---|
235 | while (i < l && *a1Here == *a2Here) {
|
---|
236 | i++; a1Here++; a2Here++;
|
---|
237 | }
|
---|
238 |
|
---|
239 | return i;
|
---|
240 | }
|
---|
241 |
|
---|
242 | bool WritePreSufStr (FILE *f, const UCArray *prev, const UCArray &a) {
|
---|
243 | unsigned char preLen;
|
---|
244 | unsigned char sufLen;
|
---|
245 |
|
---|
246 | if (prev != NULL) preLen = PrefixLen (*prev, a);
|
---|
247 | else preLen = 0;
|
---|
248 | sufLen = a.size() - preLen;
|
---|
249 |
|
---|
250 | // output the prefix length, suffix length, and the suffix
|
---|
251 | fputc (preLen, f);
|
---|
252 | if (ferror(f) != 0) return false;
|
---|
253 | fputc (sufLen, f);
|
---|
254 | if (ferror(f) != 0) return false;
|
---|
255 | char* tmp=GetCStr(a);
|
---|
256 | int ret=(fwrite (tmp+preLen, sizeof (char), sufLen, f) == sufLen);
|
---|
257 | delete (tmp);
|
---|
258 | return (ret);
|
---|
259 | }
|
---|
260 |
|
---|
261 | // a also used for prev
|
---|
262 | bool ReadPreSufStr (FILE *f, UCArray &a) {
|
---|
263 | unsigned char preLen = 0;
|
---|
264 | unsigned char sufLen = 0;
|
---|
265 |
|
---|
266 | preLen = fgetc(f);
|
---|
267 | sufLen = fgetc(f);
|
---|
268 |
|
---|
269 | if (a.size () > preLen) a.erase (a.begin()+preLen, a.end());
|
---|
270 | while (sufLen > 0) {
|
---|
271 | unsigned char c = fgetc (f);
|
---|
272 | a.push_back (c);
|
---|
273 | sufLen--;
|
---|
274 | }
|
---|
275 |
|
---|
276 | return true;
|
---|
277 | }
|
---|
278 |
|
---|