source: trunk/gsdl/src/mgpp/text/UCArray.cpp@ 855

Last change on this file since 855 was 855, checked in by sjboddie, 24 years ago

Rodgers new C++ mg

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
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 * $Id: UCArray.cpp 855 2000-01-14 02:17:52Z sjboddie $
21 *
22 **************************************************************************/
23
24#include "UCArray.h"
25#include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
26
27
28void SetCStr (UCArray &text, const char *cStr) {
29 text.erase(text.begin(), text.end());
30
31 while (*cStr != '\0') {
32 text.push_back (*cStr);
33 cStr++;
34 }
35}
36
37
38ostream &operator<<(ostream &s, const UCArray &a) {
39 UCArray::const_iterator here = a.begin();
40 UCArray::const_iterator end = a.end();
41 while (here != end) {
42 s << *here;
43 here++;
44 }
45
46 return s;
47}
48
49
50bool ReadVarLenUL (FILE *f, unsigned long &n) {
51 register unsigned long temp = 0;
52 register unsigned int bitPos = 0;
53 unsigned char b = 0;
54
55 do {
56 b = fgetc (f);
57 if (feof(f)) return false;
58 temp |= (b & 0x7f) << bitPos;
59 bitPos += 7;
60 } while (b >= 0x80 && bitPos < 32);
61
62 n = temp;
63
64 return true;
65}
66
67bool WriteVarLenUL (FILE *f, unsigned long n) {
68 register unsigned long temp = n;
69 register unsigned char b = 0;
70 do {
71 b = static_cast<unsigned char> (temp & 0x7f);
72 if (temp >= 0x80) b |= 0x80;
73 fputc (b, f);
74 if (ferror (f) != 0) return false;
75 } while ((temp = temp >> 7) > 0);
76
77 return true;
78}
79
80
81bool ReadUL (FILE *f, unsigned long &n) {
82 if (fread (&n, sizeof (unsigned long), 1, f) <= 0) return false;
83 NTOHUL (n);
84 return true;
85}
86
87
88bool WriteUL (FILE *f, unsigned long n) {
89 HTONUL (n);
90 return (fwrite (&n, sizeof (unsigned long), 1, f) > 0);
91}
92
93bool ReadF (FILE *f, float &n) {
94 if (fread (&n, sizeof (float), 1, f) <= 0) return false;
95 NTOHF(n);
96 return true;
97}
98
99bool WriteF (FILE *f, float n) {
100 HTONF(n);
101 return (fwrite (&n, sizeof (float), 1, f) > 0);
102}
103
104bool ReadD (FILE *f, double &n) {
105 if (fread (&n, sizeof (double), 1, f) <= 0) return false;
106 NTOHD(n);
107 return true;
108}
109
110bool WriteD (FILE *f, double n) {
111 HTOND(n);
112 return (fwrite (&n, sizeof (double), 1, f) > 0);
113}
114
115bool ReadUCArray (FILE *f, UCArray &a) {
116 // clear the array in preparation
117 a.erase (a.begin(), a.end());
118
119 // read in the array size
120 unsigned long arraySize = 0;
121 if (!ReadVarLenUL (f, arraySize)) return false;
122
123 // read in the array
124 unsigned char b = 0;
125 while (arraySize > 0) {
126 b = fgetc (f);
127 if (feof(f)) return false;
128 a.push_back (b);
129
130 arraySize--;
131 }
132
133 return true;
134}
135
136bool WriteUCArray (FILE *f, const UCArray &a) {
137 // write out the array size
138 if (!WriteVarLenUL (f, a.size())) return false;
139
140 UCArray::const_iterator here = a.begin();
141 UCArray::const_iterator end = a.end();
142 while (here != end) {
143 fputc (*here, f);
144 if (ferror (f) != 0) return false;
145
146 here++;
147 }
148
149 return true;
150}
151
152/*
153 * This array is designed for mapping upper and lower case letter
154 * together for a case independent comparison. The mappings are
155 * based upon ascii character sequences.
156 */
157static unsigned char casecharmap[] = {
158 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
159 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
160 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
161 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
162 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
163 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
164 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
165 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
166 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
167 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
168 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
169 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
170 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
171 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
172 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
173 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
174 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
175 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
176 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
177 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
178 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
179 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
180 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
181 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
182 '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
183 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
184 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
185 '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
186 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
187 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
188 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
189 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
190};
191
192
193int DictCompare (const UCArray &a1, const UCArray &a2) {
194 unsigned int l1 = a1.size();
195 unsigned int l2 = a2.size();
196 unsigned int l = (l1 < l2) ? l1 : l2;
197 int pos = 0;
198 register int diff = 0;
199
200 UCArray::const_iterator a1Here = a1.begin();
201 UCArray::const_iterator a2Here = a2.begin();
202
203 while (l--) {
204 if ((diff = casecharmap[*a1Here] - casecharmap[*a2Here]) != 0)
205 return diff;
206 if (pos == 0 && (diff = *a1Here - *a2Here) != 0)
207 pos = diff;
208
209 a1Here++;
210 a2Here++;
211 }
212
213 return ((l1 - l2) ? (l1 - l2) : (pos));
214}
215
216
217unsigned long PrefixLen (const UCArray &a1, const UCArray &a2) {
218 unsigned long l = (a1.size() < a2.size()) ? a1.size() : a2.size();
219 unsigned long i = 0;
220
221 UCArray::const_iterator a1Here = a1.begin();
222 UCArray::const_iterator a2Here = a2.begin();
223
224 while (i < l && *a1Here == *a2Here) {
225 i++; a1Here++; a2Here++;
226 }
227
228 return i;
229}
230
231bool WritePreSufStr (FILE *f, const UCArray *prev, const UCArray &a) {
232 unsigned char preLen;
233 unsigned char sufLen;
234
235 if (prev != NULL) preLen = PrefixLen (*prev, a);
236 else preLen = 0;
237 sufLen = a.size() - preLen;
238
239 // output the prefix length, suffix length, and the suffix
240 fputc (preLen, f);
241 if (ferror(f) != 0) return false;
242 fputc (sufLen, f);
243 if (ferror(f) != 0) return false;
244 return (fwrite ((char *)a.begin()+preLen, sizeof (char), sufLen, f) == sufLen);
245}
246
247// a also used for prev
248bool ReadPreSufStr (FILE *f, UCArray &a) {
249 unsigned char preLen = 0;
250 unsigned char sufLen = 0;
251
252 preLen = fgetc(f);
253 sufLen = fgetc(f);
254
255 if (a.size () > preLen) a.erase (a.begin()+preLen, a.end());
256 while (sufLen > 0) {
257 unsigned char c = fgetc (f);
258 a.push_back (c);
259 sufLen--;
260 }
261
262 return true;
263}
264
Note: See TracBrowser for help on using the repository browser.