source: trunk/indexers/mg/lib/local_strings.c@ 13654

Last change on this file since 13654 was 13654, checked in by kjdon, 17 years ago

tidied up the top comments, removed Ids, and old log messages

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/**************************************************************************
2 *
3 * local_strings -- provides some extra string routines
4 * Copyright (C) 1994 Authors
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 "sysfuncs.h"
23
24#include "local_strings.h"
25#include "memlib.h"
26
27/* =========================================================================
28 * Function: arg_atoi
29 * Description:
30 * Converts an ascii string to an integer
31 * but allows a scale string immediately following.
32 * i.e. "10Mb" or "20Kb" or "40Megabytes"
33 * There must be no gap between the last digit and the scale.
34 * Only the fist letter is significant.
35 * K = Kilo
36 * M = Mega
37 * G = Giga
38 * Input:
39 * string of form: xxxxxxK or xxxxxxxM or xxxxxxxG
40 * where x is a digit
41 * Output:
42 * a scaled integer
43 * ========================================================================= */
44
45#define KILO 1024
46#define MEGA 1024*1024
47#define GIGA 1024*1024*1024
48
49int
50arg_atoi (char *str)
51{
52 char *str_ptr = str;
53 char ch = '\0';
54 long scale = 1;
55
56 /* find any scale letter immediately after digits */
57 ch = *str_ptr;
58 while (!isalpha (ch) && (ch != '\0'))
59 ch = *str_ptr++;
60
61 /* replace scale letter by end-of-string character */
62 *str_ptr = '\0';
63
64 /* set scale appropriately */
65 switch (ch)
66 {
67 case 'K':
68 scale = KILO;
69 break;
70 case 'M':
71 scale = MEGA;
72 break;
73 case 'G':
74 scale = GIGA;
75 break;
76 default:
77 scale = 1;
78 break;
79 }
80
81 return (atoi (str) * scale);
82
83}
84
85/* ===============================================================================
86 * Function: compare
87 * Description: Compare two strings.
88 * Input: Two binary strings, first byte of string indicates length.
89 * Output: Like standard strcmp.
90 * = 0 if s1 = s2
91 * < 0 if s1 < s2
92 * > 0 if s1 > s2
93 * =============================================================================== */
94int
95compare (u_char * s1, u_char * s2)
96{
97 int l1 = *s1, l2 = *s2;
98 int l = (l1 < l2) ? l1 : l2;
99
100 while (l--)
101 if (*++s1 != *++s2)
102 return (*s1 - *s2);
103 return (l1 - l2);
104
105}
106
107/*
108 * This array is designed for mapping upper and lower case letter
109 * together for a case independent comparison. The mappings are
110 * based upon ascii character sequences.
111 */
112static unsigned char casecharmap[] = {
113 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
114 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
115 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
116 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
117 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
118 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
119 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
120 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
121 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
122 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
123 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
124 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
125 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
126 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
127 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
128 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
129 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
130 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
131 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
132 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
133 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
134 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
135 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
136 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
137 '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
138 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
139 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
140 '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
141 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
142 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
143 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
144 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
145};
146
147/* ===============================================================================
148 * Function: casecompare [RPAP - Jan 97: Stem Index Change]
149 * Description: Compare two strings in dictionary order
150 * Input: Two binary strings, first byte of string indicates length.
151 * Output:
152 * = 0 if s1 = s2
153 * < 0 if s1 < s2
154 * > 0 if s1 > s2
155 * =============================================================================== */
156int
157casecompare (u_char * s1, u_char * s2)
158{
159 int l1 = *s1, l2 = *s2;
160 int l = (l1 < l2) ? l1 : l2;
161 int pos = 0;
162 register int diff = 0;
163
164 while (l--)
165 if ((diff = casecharmap[*++s1] - casecharmap[*++s2]) != 0)
166 return (diff);
167 else if ((diff = *s1 - *s2) != 0)
168 if (!pos)
169 pos = diff;
170
171 return ((l1 - l2) ? (l1 - l2) : (pos));
172
173}
174
175/* =========================================================================
176 * Function: copy_string
177 * Description:
178 * Copies a string with the len in 1st byte
179 * Input:
180 * Output:
181 * ========================================================================= */
182
183u_char *
184copy_string (u_char * src_str)
185{
186 u_char *copy = Xmalloc (src_str[0] + 1);
187
188 if (!copy)
189 return NULL;
190
191 bcopy ((char *) src_str, (char *) (copy), src_str[0] + 1);
192 return copy;
193
194}
195
196
197/* ===============================================================================
198 * Function: prefixlen
199 * Description: Returns length of common prefix string between s1 and s2.
200 * Input: two binary strings
201 * Output: prefix length
202 * =============================================================================== */
203int
204prefixlen (u_char * s1, u_char * s2)
205{
206 int l = (*s1 < *s2) ? *s1 : *s2;
207 int i = 0;
208
209 while (i < l && *++s1 == *++s2)
210 i++;
211 return (i);
212}
213
214
215/* ===============================================================================
216 * Function: char2str
217 * Description:
218 * Ensures that non-printable characters are converted to a string
219 * which is printable i.e. it indentifies the character.
220 * e.g. carriage-return = "\n".
221 * Input: character (may be non-printable)
222 * Output: pointer to static string
223 * =============================================================================== */
224
225char *
226char2str (u_char c)
227{
228 static char buf[10];
229 switch (c)
230 {
231 case '\n':
232 sprintf (buf, "\\n");
233 break;
234 case '\b':
235 sprintf (buf, "\\b");
236 break;
237 case '\f':
238 sprintf (buf, "\\f");
239 break;
240 case '\t':
241 sprintf (buf, "\\t");
242 break;
243 default:
244 if (c >= ' ' && c <= 126)
245 switch (c)
246 {
247 case '\\':
248 sprintf (buf, "\\\\");
249 break;
250 case '\"':
251 sprintf (buf, "\\\"");
252 break;
253 case '\'':
254 sprintf (buf, "\\\'");
255 break;
256 default:
257 buf[0] = c;
258 buf[1] = '\0';
259 break;
260 }
261 else
262 sprintf (buf, "\\x%02x", c);
263 break;
264 }
265 return buf;
266}
267
268/* ===============================================================================
269 * Function: word2str
270 * Description:
271 * Converts a string with possible non-printable characters into a string
272 * consisting entirely of printable characters.
273 * Input: string, length stored in first byte
274 * Output: pointer to static string
275 * =============================================================================== */
276
277char *
278word2str (u_char * s)
279{
280 static char buf[1024];
281 char *p = buf;
282 int i, len = (int) *s++;
283
284 bzero (buf, sizeof (buf));
285 for (i = 0; i < len; i++)
286 {
287 strcpy (p, char2str (s[i]));
288 p = strchr (p, '\0');
289 }
290 return buf;
291}
292
293
294/* =========================================================================
295 * Function: de_escape_string
296 * Description:
297 * Turn a string containing \ escapes into real characters
298 * Note this destroys the string s in the process
299 * Input: string to de-escape
300 * Output: the de-escaped string
301 * ========================================================================= */
302char *
303de_escape_string (char *s)
304{
305 char *org = s;
306 char *d = s;
307 while (*s)
308 {
309 if (*s == '\\')
310 {
311 s++;
312 switch (*s++)
313 {
314 case '\0':
315 *d++ = '\\';
316 s--;
317 break;
318 case '\\':
319 *d++ = '\\';
320 break;
321 case 'b':
322 *d++ = '\b';
323 break;
324 case 'f':
325 *d++ = '\f';
326 break;
327 case 'n':
328 *d++ = '\n';
329 break;
330 case 'r':
331 *d++ = '\r';
332 break;
333 case 't':
334 *d++ = '\r';
335 break;
336 case '\"':
337 *d++ = '\"';
338 break;
339 case '\'':
340 *d++ = '\'';
341 break;
342 case 'x':
343 {
344 int num = 0;
345 if (isxdigit (*s))
346 {
347 char ch = toupper (*s++);
348 num = ch <= '9' ? ch - '0' : ch - 'A' + 10;
349 }
350 if (isxdigit (*s))
351 {
352 char ch = toupper (*s++);
353 num = (num << 4) + (ch <= '9' ? ch - '0' : ch - 'A' + 10);
354 }
355 *d++ = num;
356 break;
357 }
358 case '0':
359 case '1':
360 case '2':
361 case '3':
362 case '4':
363 case '5':
364 case '6':
365 case '7':
366 {
367 int num = *(s - 1) - '0';
368 if (*s >= '0' && *s <= '7')
369 num = (num << 3) + *s++ - '0';
370 if (*s >= '0' && *s <= '7')
371 num = (num << 3) + *s++ - '0';
372 *d++ = num;
373 break;
374 }
375 }
376 }
377 else
378 *d++ = *s++;
379 }
380 *d = '\0';
381 return (org);
382}
383
384/* =========================================================================
385 * Function: str255_to_string
386 * Description:
387 * Input:
388 * Output:
389 * ========================================================================= */
390
391char *
392str255_to_string (u_char * str255, char *str)
393{
394 static char buf[256];
395 char *str_base = NULL;
396 int len = *str255++;
397 int i = 0;
398
399 /* if haven't been given a str to write to then use local buffer */
400 if (!str)
401 str = &(buf[0]);
402
403 str_base = str;
404 for (i = 0; i < len; i++)
405 {
406 *str++ = *str255++;
407 }
408 *str = '\0';
409
410 return str_base;
411}
Note: See TracBrowser for help on using the repository browser.