1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile stringh.c:
|
---|
4 | * contains string/text helper functions. These are good for
|
---|
5 | * parsing/splitting strings and other stuff used throughout
|
---|
6 | * XWorkplace.
|
---|
7 | *
|
---|
8 | * Note that these functions are really a bunch of very mixed
|
---|
9 | * up string helpers, which you may or may not find helpful.
|
---|
10 | * If you're looking for string functions with memory
|
---|
11 | * management, look at xstring.c instead.
|
---|
12 | *
|
---|
13 | * Usage: All OS/2 programs.
|
---|
14 | *
|
---|
15 | * Function prefixes (new with V0.81):
|
---|
16 | * -- strh* string helper functions.
|
---|
17 | *
|
---|
18 | * Note: Version numbering in this file relates to XWorkplace version
|
---|
19 | * numbering.
|
---|
20 | *
|
---|
21 | *@@header "helpers\stringh.h"
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * Copyright (C) 1997-2016 Ulrich Mller.
|
---|
26 | * Parts Copyright (C) 1991-1999 iMatix Corporation.
|
---|
27 | * This file is part of the "XWorkplace helpers" source package.
|
---|
28 | * This is free software; you can redistribute it and/or modify
|
---|
29 | * it under the terms of the GNU General Public License as published
|
---|
30 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
31 | * "COPYING" file of the XWorkplace main distribution.
|
---|
32 | * This program is distributed in the hope that it will be useful,
|
---|
33 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
34 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
35 | * GNU General Public License for more details.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #define OS2EMX_PLAIN_CHAR
|
---|
39 | // this is needed for "os2emx.h"; if this is defined,
|
---|
40 | // emx will define PSZ as _signed_ char, otherwise
|
---|
41 | // as unsigned char
|
---|
42 |
|
---|
43 | #define INCL_WINSHELLDATA
|
---|
44 | #define INCL_DOSERRORS
|
---|
45 | #include <os2.h>
|
---|
46 |
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <string.h>
|
---|
50 | #include <ctype.h>
|
---|
51 | #include <math.h>
|
---|
52 |
|
---|
53 | #include "setup.h" // code generation and debugging options
|
---|
54 |
|
---|
55 | #define DONT_REPLACE_STRINGH_MALLOC
|
---|
56 | #include "helpers\stringh.h"
|
---|
57 | #include "helpers\xstring.h" // extended string helpers
|
---|
58 |
|
---|
59 | #pragma hdrstop
|
---|
60 |
|
---|
61 | /*
|
---|
62 | *@@category: Helpers\C helpers\String management
|
---|
63 | * See stringh.c and xstring.c.
|
---|
64 | */
|
---|
65 |
|
---|
66 | /*
|
---|
67 | *@@category: Helpers\C helpers\String management\C string helpers
|
---|
68 | * See stringh.c.
|
---|
69 | */
|
---|
70 |
|
---|
71 | #ifdef __DEBUG_MALLOC_ENABLED__
|
---|
72 |
|
---|
73 | /*
|
---|
74 | *@@ strhStoreDebug:
|
---|
75 | * memory debug version of strhStore.
|
---|
76 | *
|
---|
77 | *@@added V0.9.16 (2001-12-08) [umoeller]
|
---|
78 | */
|
---|
79 |
|
---|
80 | APIRET (strhStoreDebug)(PSZ *ppszTarget,
|
---|
81 | PCSZ pcszSource,
|
---|
82 | PULONG pulLength, // out: length of new string (ptr can be NULL)
|
---|
83 | PCSZ pcszSourceFile,
|
---|
84 | unsigned long ulLine,
|
---|
85 | PCSZ pcszFunction)
|
---|
86 | {
|
---|
87 | ULONG ulLength = 0;
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | if (ppszTarget)
|
---|
92 | {
|
---|
93 | if (*ppszTarget)
|
---|
94 | free(*ppszTarget);
|
---|
95 |
|
---|
96 | if ( (pcszSource)
|
---|
97 | && (ulLength = strlen(pcszSource))
|
---|
98 | )
|
---|
99 | {
|
---|
100 | if (*ppszTarget = (PSZ)memdMalloc(ulLength + 1,
|
---|
101 | pcszSourceFile,
|
---|
102 | ulLine,
|
---|
103 | pcszFunction))
|
---|
104 | memcpy(*ppszTarget, pcszSource, ulLength + 1);
|
---|
105 | else
|
---|
106 | return ERROR_NOT_ENOUGH_MEMORY;
|
---|
107 | }
|
---|
108 | else
|
---|
109 | *ppszTarget = NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (pulLength)
|
---|
113 | *pulLength = ulLength;
|
---|
114 |
|
---|
115 | return NO_ERROR;
|
---|
116 | }
|
---|
117 |
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | /*
|
---|
121 | *@@ strhStore:
|
---|
122 | * stores a copy of the given string in the specified
|
---|
123 | * buffer. Uses strdup internally.
|
---|
124 | *
|
---|
125 | * If *ppszTarget != NULL, the previous string is freed
|
---|
126 | * and set to NULL.
|
---|
127 | * If pcszSource != NULL, a copy of it is stored in the
|
---|
128 | * buffer.
|
---|
129 | *
|
---|
130 | *@@added V0.9.16 (2001-12-06) [umoeller]
|
---|
131 | */
|
---|
132 |
|
---|
133 | APIRET strhStore(PSZ *ppszTarget,
|
---|
134 | PCSZ pcszSource,
|
---|
135 | PULONG pulLength) // out: length of new string (ptr can be NULL)
|
---|
136 | {
|
---|
137 | ULONG ulLength = 0;
|
---|
138 |
|
---|
139 | if (ppszTarget)
|
---|
140 | {
|
---|
141 | if (*ppszTarget)
|
---|
142 | free(*ppszTarget);
|
---|
143 |
|
---|
144 | if ( (pcszSource)
|
---|
145 | && (ulLength = strlen(pcszSource))
|
---|
146 | )
|
---|
147 | {
|
---|
148 | if (*ppszTarget = (PSZ)malloc(ulLength + 1))
|
---|
149 | memcpy(*ppszTarget, pcszSource, ulLength + 1);
|
---|
150 | else
|
---|
151 | return ERROR_NOT_ENOUGH_MEMORY;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | *ppszTarget = NULL;
|
---|
155 | }
|
---|
156 | else
|
---|
157 | return ERROR_INVALID_PARAMETER;
|
---|
158 |
|
---|
159 | if (pulLength)
|
---|
160 | *pulLength = ulLength;
|
---|
161 |
|
---|
162 | return NO_ERROR;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /*
|
---|
166 | *@@ strhcpy:
|
---|
167 | * like strdup, but this one doesn't crash if string2 is NULL,
|
---|
168 | * but sets the first byte in string1 to \0 instead.
|
---|
169 | *
|
---|
170 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
---|
171 | */
|
---|
172 |
|
---|
173 | PSZ strhcpy(PSZ string1, PCSZ string2)
|
---|
174 | {
|
---|
175 | if (string2)
|
---|
176 | return strcpy(string1, string2);
|
---|
177 |
|
---|
178 | *string1 = '\0';
|
---|
179 | return string1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /*
|
---|
183 | *@@ strhCopyBuf:
|
---|
184 | * copies pcszSource to pszTarget, taking
|
---|
185 | * its length into account.
|
---|
186 | *
|
---|
187 | * Returns:
|
---|
188 | *
|
---|
189 | * -- NO_ERROR
|
---|
190 | *
|
---|
191 | * -- ERROR_INVALID_PARAMETER: pcszSource is
|
---|
192 | * null or points to a null byte.
|
---|
193 | *
|
---|
194 | * -- ERROR_FILENAME_EXCED_RANGE: pcszSource
|
---|
195 | * is too long to fit into pszTarget.
|
---|
196 | *
|
---|
197 | *@@added V1.0.1 (2003-01-05) [umoeller]
|
---|
198 | */
|
---|
199 |
|
---|
200 | APIRET strhCopyBuf(PSZ pszTarget,
|
---|
201 | PCSZ pcszSource,
|
---|
202 | ULONG cbTarget)
|
---|
203 | {
|
---|
204 | ULONG cb;
|
---|
205 | if (!pcszSource || !*pcszSource)
|
---|
206 | return ERROR_INVALID_PARAMETER;
|
---|
207 | cb = strlen(pcszSource) + 1;
|
---|
208 | if (cb > cbTarget)
|
---|
209 | return ERROR_FILENAME_EXCED_RANGE;
|
---|
210 |
|
---|
211 | memcpy(pszTarget,
|
---|
212 | pcszSource,
|
---|
213 | cb);
|
---|
214 | return NO_ERROR;
|
---|
215 | }
|
---|
216 |
|
---|
217 | #ifdef __DEBUG_MALLOC_ENABLED__
|
---|
218 |
|
---|
219 | /*
|
---|
220 | *@@ strhdupDebug:
|
---|
221 | * memory debug version of strhdup.
|
---|
222 | *
|
---|
223 | *@@added V0.9.0 [umoeller]
|
---|
224 | */
|
---|
225 |
|
---|
226 | PSZ strhdupDebug(PCSZ pcszSource,
|
---|
227 | unsigned long *pulLength,
|
---|
228 | PCSZ pcszSourceFile,
|
---|
229 | unsigned long ulLine,
|
---|
230 | PCSZ pcszFunction)
|
---|
231 | {
|
---|
232 | PSZ pszReturn = NULL;
|
---|
233 | ULONG ulLength = 0;
|
---|
234 |
|
---|
235 | if ( (pcszSource)
|
---|
236 | && (ulLength = strlen(pcszSource))
|
---|
237 | )
|
---|
238 | {
|
---|
239 | if (pszReturn = (PSZ)memdMalloc(ulLength + 1,
|
---|
240 | pcszSourceFile, // fixed V0.9.16 (2001-12-08) [umoeller]
|
---|
241 | ulLine,
|
---|
242 | pcszFunction))
|
---|
243 | memcpy(pszReturn, pcszSource, ulLength + 1);
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (pulLength)
|
---|
247 | *pulLength = ulLength;
|
---|
248 |
|
---|
249 | return pszReturn;
|
---|
250 | }
|
---|
251 |
|
---|
252 | #endif // __DEBUG_MALLOC_ENABLED__
|
---|
253 |
|
---|
254 | /*
|
---|
255 | *@@ strhdup:
|
---|
256 | * like strdup, but this one doesn't crash if pszSource
|
---|
257 | * is NULL. Instead, this returns NULL if pcszSource is
|
---|
258 | * NULL or points to a null byte. In addition, this
|
---|
259 | * can report the length of the string (V0.9.16).
|
---|
260 | *
|
---|
261 | *@@added V0.9.0 [umoeller]
|
---|
262 | *@@changed V0.9.16 (2001-10-25) [umoeller]: added pulLength
|
---|
263 | */
|
---|
264 |
|
---|
265 | PSZ strhdup(PCSZ pcszSource,
|
---|
266 | unsigned long *pulLength) // out: length of string excl. null terminator (ptr can be NULL)
|
---|
267 | {
|
---|
268 | PSZ pszReturn = NULL;
|
---|
269 | ULONG ulLength = 0;
|
---|
270 |
|
---|
271 | if ( (pcszSource)
|
---|
272 | && (ulLength = strlen(pcszSource))
|
---|
273 | )
|
---|
274 | {
|
---|
275 | if (pszReturn = (PSZ)malloc(ulLength + 1))
|
---|
276 | memcpy(pszReturn, pcszSource, ulLength + 1);
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (pulLength)
|
---|
280 | *pulLength = ulLength;
|
---|
281 |
|
---|
282 | return pszReturn;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | *@@ strhcmp:
|
---|
287 | * better strcmp. This doesn't crash if any of the
|
---|
288 | * string pointers are NULL, but returns a proper
|
---|
289 | * value then.
|
---|
290 | *
|
---|
291 | * Besides, this is guaranteed to only return -1, 0,
|
---|
292 | * or +1, while strcmp can return any positive or
|
---|
293 | * negative value. This is useful for tree comparison
|
---|
294 | * funcs.
|
---|
295 | *
|
---|
296 | *@@added V0.9.9 (2001-02-16) [umoeller]
|
---|
297 | */
|
---|
298 |
|
---|
299 | int strhcmp(PCSZ p1, PCSZ p2)
|
---|
300 | {
|
---|
301 | if (p1 && p2)
|
---|
302 | {
|
---|
303 | int i = strcmp(p1, p2);
|
---|
304 | if (i < 0) return -1;
|
---|
305 | if (i > 0) return +1;
|
---|
306 | }
|
---|
307 | else if (p1)
|
---|
308 | // but p2 is NULL: p1 greater than p2 then
|
---|
309 | return +1;
|
---|
310 | else if (p2)
|
---|
311 | // but p1 is NULL: p1 less than p2 then
|
---|
312 | return -1;
|
---|
313 |
|
---|
314 | // return 0 if strcmp returned 0 above or both strings are NULL
|
---|
315 | return 0;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*
|
---|
319 | *@@ strhicmp:
|
---|
320 | * like strhcmp, but compares without respect
|
---|
321 | * to case.
|
---|
322 | *
|
---|
323 | *@@added V0.9.9 (2001-04-07) [umoeller]
|
---|
324 | */
|
---|
325 |
|
---|
326 | int strhicmp(PCSZ p1, PCSZ p2)
|
---|
327 | {
|
---|
328 | if (p1 && p2)
|
---|
329 | {
|
---|
330 | int i = stricmp(p1, p2);
|
---|
331 | if (i < 0) return -1;
|
---|
332 | if (i > 0) return +1;
|
---|
333 | }
|
---|
334 | else if (p1)
|
---|
335 | // but p2 is NULL: p1 greater than p2 then
|
---|
336 | return +1;
|
---|
337 | else if (p2)
|
---|
338 | // but p1 is NULL: p1 less than p2 then
|
---|
339 | return -1;
|
---|
340 |
|
---|
341 | // return 0 if strcmp returned 0 above or both strings are NULL
|
---|
342 | return 0;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /*
|
---|
346 | *@@ strhistr:
|
---|
347 | * like strstr, but case-insensitive.
|
---|
348 | *
|
---|
349 | *@@changed V0.9.0 [umoeller]: crashed if null pointers were passed, thanks Rdiger Ihle
|
---|
350 | */
|
---|
351 |
|
---|
352 | PSZ strhistr(PCSZ string1, PCSZ string2)
|
---|
353 | {
|
---|
354 | PSZ prc = NULL;
|
---|
355 |
|
---|
356 | if ((string1) && (string2))
|
---|
357 | {
|
---|
358 | PSZ pszSrchIn = strdup(string1);
|
---|
359 | PSZ pszSrchFor = strdup(string2);
|
---|
360 |
|
---|
361 | if ((pszSrchIn) && (pszSrchFor))
|
---|
362 | {
|
---|
363 | strupr(pszSrchIn);
|
---|
364 | strupr(pszSrchFor);
|
---|
365 |
|
---|
366 | if (prc = strstr(pszSrchIn, pszSrchFor))
|
---|
367 | {
|
---|
368 | // prc now has the first occurence of the string,
|
---|
369 | // but in pszSrchIn; we need to map this
|
---|
370 | // return value to the original string
|
---|
371 | prc = (prc-pszSrchIn) // offset in pszSrchIn
|
---|
372 | + (PSZ)string1;
|
---|
373 | }
|
---|
374 | }
|
---|
375 | if (pszSrchFor)
|
---|
376 | free(pszSrchFor);
|
---|
377 | if (pszSrchIn)
|
---|
378 | free(pszSrchIn);
|
---|
379 | }
|
---|
380 |
|
---|
381 | return prc;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /*
|
---|
385 | *@@ strhncpy0:
|
---|
386 | * like strncpy, but always appends a 0 character.
|
---|
387 | *
|
---|
388 | *@@changed V0.9.16 (2002-01-09) [umoeller]: fixed crash on null pszSource
|
---|
389 | */
|
---|
390 |
|
---|
391 | ULONG strhncpy0(PSZ pszTarget,
|
---|
392 | PCSZ pszSource,
|
---|
393 | ULONG cbSource)
|
---|
394 | {
|
---|
395 | ULONG ul = 0;
|
---|
396 | PSZ pTarget = pszTarget,
|
---|
397 | pSource;
|
---|
398 |
|
---|
399 | if (pSource = (PSZ)pszSource) // V0.9.16 (2002-01-09) [umoeller]
|
---|
400 | {
|
---|
401 | for (ul = 0; ul < cbSource; ul++)
|
---|
402 | if (*pSource)
|
---|
403 | *pTarget++ = *pSource++;
|
---|
404 | else
|
---|
405 | break;
|
---|
406 | }
|
---|
407 |
|
---|
408 | *pTarget = 0;
|
---|
409 |
|
---|
410 | return ul;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /*
|
---|
414 | *@@ strlcpy:
|
---|
415 | * copies src to string dst of size siz. At most siz-1 characters
|
---|
416 | * will be copied. Always NUL terminates, unless siz == 0.
|
---|
417 | *
|
---|
418 | * Returns strlen(src); if retval >= siz, truncation occurred.
|
---|
419 | *
|
---|
420 | * Taken from the OpenBSD sources at
|
---|
421 | *
|
---|
422 | + ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.c
|
---|
423 | *
|
---|
424 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
---|
425 | * All rights reserved.
|
---|
426 | *
|
---|
427 | * OpenBSD licence applies (see top of that file).
|
---|
428 | *
|
---|
429 | *@@added V1.0.1 (2003-01-29) [umoeller]
|
---|
430 | */
|
---|
431 |
|
---|
432 | size_t strlcpy(char *dst,
|
---|
433 | const char *src,
|
---|
434 | size_t siz)
|
---|
435 | {
|
---|
436 | register char *d = dst;
|
---|
437 | register const char *s = src;
|
---|
438 | register size_t n = siz;
|
---|
439 |
|
---|
440 | /* Copy as many bytes as will fit */
|
---|
441 | if (n != 0 && --n != 0)
|
---|
442 | {
|
---|
443 | do
|
---|
444 | {
|
---|
445 | if ((*d++ = *s++) == 0)
|
---|
446 | break;
|
---|
447 | } while (--n != 0);
|
---|
448 | }
|
---|
449 |
|
---|
450 | /* Not enough room in dst, add NUL and traverse rest of src */
|
---|
451 | if (n == 0)
|
---|
452 | {
|
---|
453 | if (siz != 0)
|
---|
454 | *d = '\0'; /* NUL-terminate dst */
|
---|
455 | while (*s++)
|
---|
456 | ;
|
---|
457 | }
|
---|
458 |
|
---|
459 | return (s - src - 1); /* count does not include NUL */
|
---|
460 | }
|
---|
461 |
|
---|
462 | /*
|
---|
463 | *@@ strlcat:
|
---|
464 | * appends src to string dst of size siz. Unlike strncat,
|
---|
465 | * siz is the full size of dst, not space left. At most
|
---|
466 | * siz-1 characters will be copied. Always NUL terminates,
|
---|
467 | * unless siz <= strlen(dst).
|
---|
468 | *
|
---|
469 | * Returns strlen(src) + MIN(siz, strlen(initial dst)),
|
---|
470 | * in other words, strlen(dst) after the concatenation.
|
---|
471 | * If retval >= siz, truncation occurred.
|
---|
472 | *
|
---|
473 | * Taken from the OpenBSD sources at
|
---|
474 | *
|
---|
475 | + ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcat.c
|
---|
476 | *
|
---|
477 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
---|
478 | * All rights reserved.
|
---|
479 | *
|
---|
480 | * OpenBSD licence applies (see top of that file).
|
---|
481 | *
|
---|
482 | *@@added V1.0.1 (2003-01-29) [umoeller]
|
---|
483 | */
|
---|
484 |
|
---|
485 | size_t strlcat(char *dst,
|
---|
486 | const char *src,
|
---|
487 | size_t siz)
|
---|
488 | {
|
---|
489 | register char *d = dst;
|
---|
490 | register const char *s = src;
|
---|
491 | register size_t n = siz;
|
---|
492 | size_t dlen;
|
---|
493 |
|
---|
494 | /* Find the end of dst and adjust bytes left but don't go past end */
|
---|
495 | while (n-- != 0 && *d != '\0')
|
---|
496 | d++;
|
---|
497 | dlen = d - dst;
|
---|
498 | n = siz - dlen;
|
---|
499 |
|
---|
500 | if (n == 0)
|
---|
501 | return(dlen + strlen(s));
|
---|
502 | while (*s != '\0')
|
---|
503 | {
|
---|
504 | if (n != 1)
|
---|
505 | {
|
---|
506 | *d++ = *s;
|
---|
507 | n--;
|
---|
508 | }
|
---|
509 | s++;
|
---|
510 | }
|
---|
511 | *d = '\0';
|
---|
512 |
|
---|
513 | return (dlen + (s - src)); /* count does not include NUL */
|
---|
514 | }
|
---|
515 |
|
---|
516 | /*
|
---|
517 | *@@ strhlen:
|
---|
518 | * like strlen, but doesn't crash on
|
---|
519 | * null strings, but returns 0 also.
|
---|
520 | *
|
---|
521 | *@@added V0.9.19 (2002-04-02) [umoeller]
|
---|
522 | */
|
---|
523 |
|
---|
524 | ULONG strhlen(PCSZ pcsz)
|
---|
525 | {
|
---|
526 | if (pcsz)
|
---|
527 | return strlen(pcsz);
|
---|
528 |
|
---|
529 | return 0;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /*
|
---|
533 | *@@ strhSize:
|
---|
534 | * returns the size of the given string, which
|
---|
535 | * is the memory required to allocate a copy,
|
---|
536 | * including the null terminator.
|
---|
537 | *
|
---|
538 | * Returns 0 only if pcsz is NULL. If pcsz
|
---|
539 | * points to a null character, this returns 1.
|
---|
540 | *
|
---|
541 | *@@added V0.9.18 (2002-02-13) [umoeller]
|
---|
542 | *@@changed V0.9.18 (2002-03-27) [umoeller]: now returning 1 for ptr to null byte
|
---|
543 | */
|
---|
544 |
|
---|
545 | ULONG strhSize(PCSZ pcsz)
|
---|
546 | {
|
---|
547 | if (pcsz) // && *pcsz) // V0.9.18 (2002-03-27) [umoeller]
|
---|
548 | return strlen(pcsz) + 1;
|
---|
549 |
|
---|
550 | return 0;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * strhCount:
|
---|
555 | * this counts the occurences of c in pszSearch.
|
---|
556 | */
|
---|
557 |
|
---|
558 | ULONG strhCount(PCSZ pszSearch,
|
---|
559 | CHAR c)
|
---|
560 | {
|
---|
561 | PCSZ p = pszSearch;
|
---|
562 | ULONG ulCount = 0;
|
---|
563 | while (TRUE)
|
---|
564 | {
|
---|
565 | if (!(p = strchr(p, c)))
|
---|
566 | return ulCount;
|
---|
567 |
|
---|
568 | ulCount++;
|
---|
569 | p++;
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | /*
|
---|
574 | *@@ strhIsDecimal:
|
---|
575 | * returns TRUE if psz consists of decimal digits only.
|
---|
576 | */
|
---|
577 |
|
---|
578 | BOOL strhIsDecimal(PSZ psz)
|
---|
579 | {
|
---|
580 | PSZ p = psz;
|
---|
581 | while (*p != 0)
|
---|
582 | {
|
---|
583 | if (isdigit(*p) == 0)
|
---|
584 | return FALSE;
|
---|
585 | p++;
|
---|
586 | }
|
---|
587 |
|
---|
588 | return TRUE;
|
---|
589 | }
|
---|
590 |
|
---|
591 | #ifdef __DEBUG_MALLOC_ENABLED__
|
---|
592 |
|
---|
593 | /*
|
---|
594 | *@@ strhSubstrDebug:
|
---|
595 | * memory debug version of strhSubstr.
|
---|
596 | *
|
---|
597 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
---|
598 | */
|
---|
599 |
|
---|
600 | PSZ strhSubstrDebug(PCSZ pBegin, // in: first char
|
---|
601 | PCSZ pEnd, // in: last char (not included)
|
---|
602 | PCSZ pcszSourceFile,
|
---|
603 | unsigned long ulLine,
|
---|
604 | PCSZ pcszFunction)
|
---|
605 | {
|
---|
606 | PSZ pszSubstr = NULL;
|
---|
607 |
|
---|
608 | if (pEnd > pBegin) // V0.9.9 (2001-04-04) [umoeller]
|
---|
609 | {
|
---|
610 | ULONG cbSubstr = (pEnd - pBegin);
|
---|
611 | if (pszSubstr = (PSZ)memdMalloc(cbSubstr + 1,
|
---|
612 | pcszSourceFile,
|
---|
613 | ulLine,
|
---|
614 | pcszFunction))
|
---|
615 | {
|
---|
616 | // strhncpy0(pszSubstr, pBegin, cbSubstr);
|
---|
617 | memcpy(pszSubstr, pBegin, cbSubstr); // V0.9.9 (2001-04-04) [umoeller]
|
---|
618 | *(pszSubstr + cbSubstr) = '\0';
|
---|
619 | }
|
---|
620 | }
|
---|
621 |
|
---|
622 | return pszSubstr;
|
---|
623 | }
|
---|
624 |
|
---|
625 | #endif // __DEBUG_MALLOC_ENABLED__
|
---|
626 |
|
---|
627 | /*
|
---|
628 | *@@ strhSubstr:
|
---|
629 | * this creates a new PSZ containing the string
|
---|
630 | * from pBegin to pEnd, excluding the pEnd character.
|
---|
631 | * The new string is null-terminated. The caller
|
---|
632 | * must free() the new string after use.
|
---|
633 | *
|
---|
634 | * Example:
|
---|
635 | + "1234567890"
|
---|
636 | + ^ ^
|
---|
637 | + p1 p2
|
---|
638 | + strhSubstr(p1, p2)
|
---|
639 | * would return a new string containing "2345678".
|
---|
640 | *
|
---|
641 | *@@changed V0.9.9 (2001-04-04) [umoeller]: fixed crashes with invalid pointers
|
---|
642 | *@@changed V0.9.9 (2001-04-04) [umoeller]: now using memcpy for speed
|
---|
643 | */
|
---|
644 |
|
---|
645 | PSZ strhSubstr(PCSZ pBegin, // in: first char
|
---|
646 | PCSZ pEnd) // in: last char (not included)
|
---|
647 | {
|
---|
648 | PSZ pszSubstr = NULL;
|
---|
649 |
|
---|
650 | if (pEnd > pBegin) // V0.9.9 (2001-04-04) [umoeller]
|
---|
651 | {
|
---|
652 | ULONG cbSubstr = (pEnd - pBegin);
|
---|
653 | if (pszSubstr = (PSZ)malloc(cbSubstr + 1))
|
---|
654 | {
|
---|
655 | memcpy(pszSubstr, pBegin, cbSubstr); // V0.9.9 (2001-04-04) [umoeller]
|
---|
656 | *(pszSubstr + cbSubstr) = '\0';
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 | return pszSubstr;
|
---|
661 | }
|
---|
662 |
|
---|
663 | /*
|
---|
664 | *@@ strhExtract:
|
---|
665 | * searches pszBuf for the cOpen character and returns
|
---|
666 | * the data in between cOpen and cClose, excluding
|
---|
667 | * those two characters, in a newly allocated buffer
|
---|
668 | * which you must free() afterwards.
|
---|
669 | *
|
---|
670 | * Spaces and newlines/linefeeds are skipped.
|
---|
671 | *
|
---|
672 | * If the search was successful, the new buffer
|
---|
673 | * is returned and, if (ppEnd != NULL), *ppEnd points
|
---|
674 | * to the first character after the cClose character
|
---|
675 | * found in the buffer.
|
---|
676 | *
|
---|
677 | * If the search was not successful, NULL is
|
---|
678 | * returned, and *ppEnd is unchanged.
|
---|
679 | *
|
---|
680 | * If another cOpen character is found before
|
---|
681 | * cClose, matching cClose characters will be skipped.
|
---|
682 | * You can therefore nest the cOpen and cClose
|
---|
683 | * characters.
|
---|
684 | *
|
---|
685 | * This function ignores cOpen and cClose characters
|
---|
686 | * in C-style comments and strings surrounded by
|
---|
687 | * double quotes.
|
---|
688 | *
|
---|
689 | * Example:
|
---|
690 | *
|
---|
691 | + PSZ pszBuf = "KEYWORD { --blah-- } next",
|
---|
692 | + pEnd;
|
---|
693 | + strhExtract(pszBuf,
|
---|
694 | + '{', '}',
|
---|
695 | + &pEnd)
|
---|
696 | *
|
---|
697 | * would return a new buffer containing " --blah-- ",
|
---|
698 | * and ppEnd would afterwards point to the space
|
---|
699 | * before "next" in the static buffer.
|
---|
700 | *
|
---|
701 | *@@added V0.9.0 [umoeller]
|
---|
702 | */
|
---|
703 |
|
---|
704 | PSZ strhExtract(PCSZ pszBuf, // in: search buffer
|
---|
705 | CHAR cOpen, // in: opening char
|
---|
706 | CHAR cClose, // in: closing char
|
---|
707 | PCSZ *ppEnd) // out: if != NULL, receives first character after closing char
|
---|
708 | {
|
---|
709 | PSZ pszReturn = NULL;
|
---|
710 | PCSZ pOpen;
|
---|
711 | if ( (pszBuf)
|
---|
712 | && (pOpen = strchr(pszBuf, cOpen))
|
---|
713 | )
|
---|
714 | {
|
---|
715 | // opening char found:
|
---|
716 | // now go thru the whole rest of the buffer
|
---|
717 | PCSZ p = pOpen + 1;
|
---|
718 | LONG lLevel = 1; // if this goes 0, we're done
|
---|
719 | while (*p)
|
---|
720 | {
|
---|
721 | if (*p == cOpen)
|
---|
722 | lLevel++;
|
---|
723 | else if (*p == cClose)
|
---|
724 | {
|
---|
725 | lLevel--;
|
---|
726 | if (lLevel <= 0)
|
---|
727 | {
|
---|
728 | // matching closing bracket found:
|
---|
729 | // extract string
|
---|
730 | pszReturn = strhSubstr(pOpen + 1, // after cOpen
|
---|
731 | p); // excluding cClose
|
---|
732 | if (ppEnd)
|
---|
733 | *ppEnd = p + 1;
|
---|
734 | break; // while (*p)
|
---|
735 | }
|
---|
736 | }
|
---|
737 | else if (*p == '\"')
|
---|
738 | {
|
---|
739 | // beginning of string:
|
---|
740 | PCSZ p2 = p+1;
|
---|
741 | // find end of string
|
---|
742 | while ((*p2) && (*p2 != '\"'))
|
---|
743 | p2++;
|
---|
744 |
|
---|
745 | if (*p2 == '\"')
|
---|
746 | // closing quote found:
|
---|
747 | // search on after that
|
---|
748 | p = p2; // raised below
|
---|
749 | else
|
---|
750 | break; // while (*p)
|
---|
751 | }
|
---|
752 |
|
---|
753 | p++;
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | return pszReturn;
|
---|
758 | }
|
---|
759 |
|
---|
760 | /*
|
---|
761 | *@@ strhQuote:
|
---|
762 | * similar to strhExtract, except that
|
---|
763 | * opening and closing chars are the same,
|
---|
764 | * and therefore no nesting is possible.
|
---|
765 | * Useful for extracting stuff between
|
---|
766 | * quotes.
|
---|
767 | *
|
---|
768 | *@@added V0.9.0 [umoeller]
|
---|
769 | */
|
---|
770 |
|
---|
771 | PSZ strhQuote(PSZ pszBuf,
|
---|
772 | CHAR cQuote,
|
---|
773 | PSZ *ppEnd)
|
---|
774 | {
|
---|
775 | PSZ pszReturn = NULL,
|
---|
776 | p1 = NULL;
|
---|
777 | if ((p1 = strchr(pszBuf, cQuote)))
|
---|
778 | {
|
---|
779 | PSZ p2;
|
---|
780 | if (p2 = strchr(p1+1, cQuote))
|
---|
781 | {
|
---|
782 | pszReturn = strhSubstr(p1+1, p2);
|
---|
783 | if (ppEnd)
|
---|
784 | // store closing char
|
---|
785 | *ppEnd = p2 + 1;
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | return pszReturn;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /*
|
---|
793 | *@@ strhStrip:
|
---|
794 | * removes all double spaces.
|
---|
795 | * This copies within the "psz" buffer.
|
---|
796 | * If any double spaces are found, the
|
---|
797 | * string will be shorter than before,
|
---|
798 | * but the buffer is _not_ reallocated,
|
---|
799 | * so there will be unused bytes at the
|
---|
800 | * end.
|
---|
801 | *
|
---|
802 | * Returns the number of spaces removed.
|
---|
803 | *
|
---|
804 | *@@added V0.9.0 [umoeller]
|
---|
805 | */
|
---|
806 |
|
---|
807 | ULONG strhStrip(PSZ psz) // in/out: string
|
---|
808 | {
|
---|
809 | PSZ p;
|
---|
810 | ULONG cb = strlen(psz),
|
---|
811 | ulrc = 0;
|
---|
812 |
|
---|
813 | for (p = psz; p < psz+cb; p++)
|
---|
814 | {
|
---|
815 | if ((*p == ' ') && (*(p+1) == ' '))
|
---|
816 | {
|
---|
817 | PSZ p2 = p;
|
---|
818 | while (*p2)
|
---|
819 | {
|
---|
820 | *p2 = *(p2+1);
|
---|
821 | p2++;
|
---|
822 | }
|
---|
823 | cb--;
|
---|
824 | p--;
|
---|
825 | ulrc++;
|
---|
826 | }
|
---|
827 | }
|
---|
828 | return ulrc;
|
---|
829 | }
|
---|
830 |
|
---|
831 | /*
|
---|
832 | *@@ strhins:
|
---|
833 | * this inserts one string into another.
|
---|
834 | *
|
---|
835 | * pszInsert is inserted into pszBuffer at offset
|
---|
836 | * ulInsertOfs (which counts from 0).
|
---|
837 | *
|
---|
838 | * A newly allocated string is returned. pszBuffer is
|
---|
839 | * not changed. The new string should be free()'d after
|
---|
840 | * use.
|
---|
841 | *
|
---|
842 | * Upon errors, NULL is returned.
|
---|
843 | *
|
---|
844 | *@@changed V0.9.0 [umoeller]: completely rewritten.
|
---|
845 | */
|
---|
846 |
|
---|
847 | PSZ strhins(PCSZ pcszBuffer,
|
---|
848 | ULONG ulInsertOfs,
|
---|
849 | PCSZ pcszInsert)
|
---|
850 | {
|
---|
851 | PSZ pszNew = NULL;
|
---|
852 |
|
---|
853 | if ((pcszBuffer) && (pcszInsert))
|
---|
854 | {
|
---|
855 | do {
|
---|
856 | ULONG cbBuffer = strlen(pcszBuffer);
|
---|
857 | ULONG cbInsert = strlen(pcszInsert);
|
---|
858 |
|
---|
859 | // check string length
|
---|
860 | if (ulInsertOfs > cbBuffer + 1)
|
---|
861 | break; // do
|
---|
862 |
|
---|
863 | // OK, let's go.
|
---|
864 | pszNew = (PSZ)malloc(cbBuffer + cbInsert + 1); // additional null terminator
|
---|
865 |
|
---|
866 | // copy stuff before pInsertPos
|
---|
867 | memcpy(pszNew,
|
---|
868 | pcszBuffer,
|
---|
869 | ulInsertOfs);
|
---|
870 | // copy string to be inserted
|
---|
871 | memcpy(pszNew + ulInsertOfs,
|
---|
872 | pcszInsert,
|
---|
873 | cbInsert);
|
---|
874 | // copy stuff after pInsertPos
|
---|
875 | strcpy(pszNew + ulInsertOfs + cbInsert,
|
---|
876 | pcszBuffer + ulInsertOfs);
|
---|
877 | } while (FALSE);
|
---|
878 | }
|
---|
879 |
|
---|
880 | return pszNew;
|
---|
881 | }
|
---|
882 |
|
---|
883 | /*
|
---|
884 | *@@ strhFindReplace:
|
---|
885 | * wrapper around xstrFindReplace to work with C strings.
|
---|
886 | * Note that *ppszBuf can get reallocated and must
|
---|
887 | * be free()'able.
|
---|
888 | *
|
---|
889 | * Repetitive use of this wrapper is not recommended
|
---|
890 | * because it is considerably slower than xstrFindReplace.
|
---|
891 | *
|
---|
892 | *@@added V0.9.6 (2000-11-01) [umoeller]
|
---|
893 | *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from strhrpl
|
---|
894 | */
|
---|
895 |
|
---|
896 | ULONG strhFindReplace(PSZ *ppszBuf, // in/out: string
|
---|
897 | PULONG pulOfs, // in: where to begin search (0 = start);
|
---|
898 | // out: ofs of first char after replacement string
|
---|
899 | PCSZ pcszSearch, // in: search string; cannot be NULL
|
---|
900 | PCSZ pcszReplace) // in: replacement string; cannot be NULL
|
---|
901 | {
|
---|
902 | ULONG ulrc = 0;
|
---|
903 | XSTRING xstrBuf,
|
---|
904 | xstrFind,
|
---|
905 | xstrReplace;
|
---|
906 | size_t ShiftTable[256];
|
---|
907 | BOOL fRepeat = FALSE;
|
---|
908 | xstrInitSet(&xstrBuf, *ppszBuf);
|
---|
909 | // reallocated and returned, so we're safe
|
---|
910 | xstrInitSet(&xstrFind, (PSZ)pcszSearch);
|
---|
911 | xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
|
---|
912 | // these two are never freed, so we're safe too
|
---|
913 |
|
---|
914 | if ((ulrc = xstrFindReplace(&xstrBuf,
|
---|
915 | pulOfs,
|
---|
916 | &xstrFind,
|
---|
917 | &xstrReplace,
|
---|
918 | ShiftTable,
|
---|
919 | &fRepeat)))
|
---|
920 | // replaced:
|
---|
921 | *ppszBuf = xstrBuf.psz;
|
---|
922 |
|
---|
923 | return ulrc;
|
---|
924 | }
|
---|
925 |
|
---|
926 | /*
|
---|
927 | * strhWords:
|
---|
928 | * returns the no. of words in "psz".
|
---|
929 | * A string is considered a "word" if
|
---|
930 | * it is surrounded by spaces only.
|
---|
931 | *
|
---|
932 | *@@added V0.9.0 [umoeller]
|
---|
933 | */
|
---|
934 |
|
---|
935 | ULONG strhWords(PSZ psz)
|
---|
936 | {
|
---|
937 | PSZ p;
|
---|
938 | ULONG cb = strlen(psz),
|
---|
939 | ulWords = 0;
|
---|
940 | if (cb > 1)
|
---|
941 | {
|
---|
942 | ulWords = 1;
|
---|
943 | for (p = psz; p < psz+cb; p++)
|
---|
944 | if (*p == ' ')
|
---|
945 | ulWords++;
|
---|
946 | }
|
---|
947 | return ulWords;
|
---|
948 | }
|
---|
949 |
|
---|
950 | /*
|
---|
951 | *@@ strhGetWord:
|
---|
952 | * finds word boundaries.
|
---|
953 | *
|
---|
954 | * *ppszStart is used as the beginning of the
|
---|
955 | * search.
|
---|
956 | *
|
---|
957 | * If a word is found, *ppszStart is set to
|
---|
958 | * the first character of the word which was
|
---|
959 | * found and *ppszEnd receives the address
|
---|
960 | * of the first character _after_ the word,
|
---|
961 | * which is probably a space or a \n or \r char.
|
---|
962 | * We then return TRUE.
|
---|
963 | *
|
---|
964 | * The search is stopped if a null character
|
---|
965 | * is found or pLimit is reached. In that case,
|
---|
966 | * FALSE is returned.
|
---|
967 | *
|
---|
968 | *@@added V0.9.1 (2000-02-13) [umoeller]
|
---|
969 | */
|
---|
970 |
|
---|
971 | BOOL strhGetWord(PSZ *ppszStart, // in: start of search,
|
---|
972 | // out: start of word (if TRUE is returned)
|
---|
973 | PCSZ pLimit, // in: ptr to last char after *ppszStart to be
|
---|
974 | // searched; if the word does not end before
|
---|
975 | // or with this char, FALSE is returned
|
---|
976 | PCSZ pcszBeginChars, // stringh.h defines STRH_BEGIN_CHARS
|
---|
977 | PCSZ pcszEndChars, // stringh.h defines STRH_END_CHARS
|
---|
978 | PSZ *ppszEnd) // out: first char _after_ word
|
---|
979 | // (if TRUE is returned)
|
---|
980 | {
|
---|
981 | // characters after which a word can be started
|
---|
982 | // PCSZ pcszBeginChars = "\x0d\x0a ";
|
---|
983 | // PCSZ pcszEndChars = "\x0d\x0a /-";
|
---|
984 |
|
---|
985 | PSZ pStart = *ppszStart;
|
---|
986 |
|
---|
987 | // find start of word
|
---|
988 | while ( (pStart < (PSZ)pLimit)
|
---|
989 | && (strchr(pcszBeginChars, *pStart))
|
---|
990 | )
|
---|
991 | // if char is a "before word" char: go for next
|
---|
992 | pStart++;
|
---|
993 |
|
---|
994 | if (pStart < (PSZ)pLimit)
|
---|
995 | {
|
---|
996 | // found a valid "word start" character
|
---|
997 | // (which is not in pcszBeginChars):
|
---|
998 |
|
---|
999 | // find end of word
|
---|
1000 | PSZ pEndOfWord = pStart;
|
---|
1001 | while ( (pEndOfWord <= (PSZ)pLimit)
|
---|
1002 | && (strchr(pcszEndChars, *pEndOfWord) == 0)
|
---|
1003 | )
|
---|
1004 | // if char is not an "end word" char: go for next
|
---|
1005 | pEndOfWord++;
|
---|
1006 |
|
---|
1007 | if (pEndOfWord <= (PSZ)pLimit)
|
---|
1008 | {
|
---|
1009 | // whoa, got a word:
|
---|
1010 | *ppszStart = pStart;
|
---|
1011 | *ppszEnd = pEndOfWord;
|
---|
1012 | return TRUE;
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | return FALSE;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | *@@ strhIsWord:
|
---|
1021 | * returns TRUE if p points to a "word"
|
---|
1022 | * in pcszBuf.
|
---|
1023 | *
|
---|
1024 | * p is considered a word if the character _before_
|
---|
1025 | * it is in pcszBeginChars and the char _after_
|
---|
1026 | * it (i.e. *(p+cbSearch)) is in pcszEndChars.
|
---|
1027 | *
|
---|
1028 | *@@added V0.9.6 (2000-11-12) [umoeller]
|
---|
1029 | *@@changed V0.9.18 (2002-02-23) [umoeller]: fixed end char check
|
---|
1030 | */
|
---|
1031 |
|
---|
1032 | BOOL strhIsWord(PCSZ pcszBuf,
|
---|
1033 | PCSZ p, // in: start of word
|
---|
1034 | ULONG cbSearch, // in: length of word
|
---|
1035 | PCSZ pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
|
---|
1036 | PCSZ pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
|
---|
1037 | {
|
---|
1038 | // check previous char
|
---|
1039 | if ( (p == pcszBuf)
|
---|
1040 | || (strchr(pcszBeginChars, *(p-1)))
|
---|
1041 | )
|
---|
1042 | {
|
---|
1043 | // OK, valid begin char:
|
---|
1044 | // check end char
|
---|
1045 | CHAR cNextChar;
|
---|
1046 | if (!(cNextChar = p[cbSearch]))
|
---|
1047 | // null terminator:
|
---|
1048 | return TRUE;
|
---|
1049 | else
|
---|
1050 | {
|
---|
1051 | // not null terminator: check if char is
|
---|
1052 | // in the list of valid end chars
|
---|
1053 | if (strchr(pcszEndChars, cNextChar))
|
---|
1054 | {
|
---|
1055 | // OK, is end char: avoid doubles of that char,
|
---|
1056 | // but allow spaces
|
---|
1057 | // fixed V0.9.18 (2002-02-23) [umoeller]
|
---|
1058 | CHAR cNextNext = p[cbSearch + 1];
|
---|
1059 | if ( (cNextNext != cNextChar)
|
---|
1060 | || (cNextNext == ' ')
|
---|
1061 | || (cNextNext == 0)
|
---|
1062 | )
|
---|
1063 | return TRUE;
|
---|
1064 | }
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | return FALSE;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /*
|
---|
1072 | *@@ strhFindWord:
|
---|
1073 | * searches for pszSearch in pszBuf, which is
|
---|
1074 | * returned if found (or NULL if not).
|
---|
1075 | *
|
---|
1076 | * As opposed to strstr, this finds pszSearch
|
---|
1077 | * only if it is a "word". A search string is
|
---|
1078 | * considered a word if the character _before_
|
---|
1079 | * it is in pcszBeginChars and the char _after_
|
---|
1080 | * it is in pcszEndChars.
|
---|
1081 | *
|
---|
1082 | * Example:
|
---|
1083 | + strhFindWord("This is an example.", "is");
|
---|
1084 | + returns ...........^ this, but not the "is" in "This".
|
---|
1085 | *
|
---|
1086 | * The algorithm here uses strstr to find pszSearch in pszBuf
|
---|
1087 | * and performs additional "is-word" checks for each item found
|
---|
1088 | * (by calling strhIsWord).
|
---|
1089 | *
|
---|
1090 | * Note that this function is fairly slow compared to xstrFindWord.
|
---|
1091 | *
|
---|
1092 | *@@added V0.9.0 (99-11-08) [umoeller]
|
---|
1093 | *@@changed V0.9.0 (99-11-10) [umoeller]: tried second algorithm, reverted to original...
|
---|
1094 | */
|
---|
1095 |
|
---|
1096 | PSZ strhFindWord(PCSZ pszBuf,
|
---|
1097 | PCSZ pszSearch,
|
---|
1098 | PCSZ pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
|
---|
1099 | PCSZ pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
|
---|
1100 | {
|
---|
1101 | PSZ pszReturn = 0;
|
---|
1102 | ULONG cbBuf = strlen(pszBuf),
|
---|
1103 | cbSearch = strlen(pszSearch);
|
---|
1104 |
|
---|
1105 | if ((cbBuf) && (cbSearch))
|
---|
1106 | {
|
---|
1107 | PCSZ p = pszBuf;
|
---|
1108 |
|
---|
1109 | do // while p
|
---|
1110 | {
|
---|
1111 | p = strstr(p, pszSearch);
|
---|
1112 | if (p)
|
---|
1113 | {
|
---|
1114 | // string found:
|
---|
1115 | // check if that's a word
|
---|
1116 |
|
---|
1117 | if (strhIsWord(pszBuf,
|
---|
1118 | p,
|
---|
1119 | cbSearch,
|
---|
1120 | pcszBeginChars,
|
---|
1121 | pcszEndChars))
|
---|
1122 | {
|
---|
1123 | // valid end char:
|
---|
1124 | pszReturn = (PSZ)p;
|
---|
1125 | break;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | p += cbSearch;
|
---|
1129 | }
|
---|
1130 | } while (p);
|
---|
1131 |
|
---|
1132 | }
|
---|
1133 | return pszReturn;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | /*
|
---|
1137 | *@@ strhFindEOL:
|
---|
1138 | * returns a pointer to the next \r, \n or null character
|
---|
1139 | * following pszSearchIn. Stores the offset in *pulOffset.
|
---|
1140 | *
|
---|
1141 | * This should never return NULL because at some point,
|
---|
1142 | * there will be a null byte in your string.
|
---|
1143 | *
|
---|
1144 | *@@added V0.9.4 (2000-07-01) [umoeller]
|
---|
1145 | */
|
---|
1146 |
|
---|
1147 | PSZ strhFindEOL(PCSZ pcszSearchIn, // in: where to search
|
---|
1148 | PULONG pulOffset) // out: offset (ptr can be NULL)
|
---|
1149 | {
|
---|
1150 | PCSZ p = pcszSearchIn,
|
---|
1151 | prc = 0;
|
---|
1152 | while (TRUE)
|
---|
1153 | {
|
---|
1154 | if ( (*p == '\r') || (*p == '\n') || (*p == 0) )
|
---|
1155 | {
|
---|
1156 | prc = p;
|
---|
1157 | break;
|
---|
1158 | }
|
---|
1159 | p++;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | if ((pulOffset) && (prc))
|
---|
1163 | *pulOffset = prc - pcszSearchIn;
|
---|
1164 |
|
---|
1165 | return (PSZ)prc;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | /*
|
---|
1169 | *@@ strhFindNextLine:
|
---|
1170 | * like strhFindEOL, but this returns the character
|
---|
1171 | * _after_ \r or \n. Note that this might return
|
---|
1172 | * a pointer to terminating NULL character also.
|
---|
1173 | */
|
---|
1174 |
|
---|
1175 | PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset)
|
---|
1176 | {
|
---|
1177 | PSZ pEOL = strhFindEOL(pszSearchIn, NULL);
|
---|
1178 | // pEOL now points to the \r char or the terminating 0 byte;
|
---|
1179 | // if not null byte, advance pointer
|
---|
1180 | PSZ pNextLine = pEOL;
|
---|
1181 | if (*pNextLine == '\r')
|
---|
1182 | pNextLine++;
|
---|
1183 | if (*pNextLine == '\n')
|
---|
1184 | pNextLine++;
|
---|
1185 | if (pulOffset)
|
---|
1186 | *pulOffset = pNextLine - pszSearchIn;
|
---|
1187 | return pNextLine;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /*
|
---|
1191 | *@@ strhBeautifyTitle:
|
---|
1192 | * replaces all line breaks (0xd, 0xa) with spaces.
|
---|
1193 | * Returns the new length of the string or 0 on
|
---|
1194 | * errors.
|
---|
1195 | *
|
---|
1196 | *@@changed V0.9.12 (2001-05-17) [pr]: multiple line break chars. end up as only 1 space
|
---|
1197 | *@@changed V0.9.19 (2002-06-18) [umoeller]: now returning length
|
---|
1198 | */
|
---|
1199 |
|
---|
1200 | ULONG strhBeautifyTitle(PSZ psz)
|
---|
1201 | {
|
---|
1202 | ULONG ulrc;
|
---|
1203 | PSZ p = psz;
|
---|
1204 |
|
---|
1205 | while (*p)
|
---|
1206 | {
|
---|
1207 | if ( (*p == '\r')
|
---|
1208 | || (*p == '\n')
|
---|
1209 | )
|
---|
1210 | {
|
---|
1211 | if ( (p != psz)
|
---|
1212 | && (p[-1] == ' ')
|
---|
1213 | )
|
---|
1214 | memmove(p, p + 1, strlen(p));
|
---|
1215 | else
|
---|
1216 | *p++ = ' ';
|
---|
1217 | }
|
---|
1218 | else
|
---|
1219 | p++;
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | return (p - psz);
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | /*
|
---|
1226 | *@@ strhBeautifyTitle:
|
---|
1227 | * like strhBeautifyTitle, but copies into
|
---|
1228 | * a new buffer. More efficient.
|
---|
1229 | *
|
---|
1230 | *@@added V0.9.19 (2002-06-18) [umoeller]
|
---|
1231 | */
|
---|
1232 |
|
---|
1233 | ULONG strhBeautifyTitle2(PSZ pszTarget, // out: beautified string
|
---|
1234 | PCSZ pcszSource) // in: string to be beautified (can be NULL)
|
---|
1235 | {
|
---|
1236 | ULONG ulrc;
|
---|
1237 | PCSZ pSource = pcszSource;
|
---|
1238 | PSZ pTarget = pszTarget;
|
---|
1239 | CHAR c;
|
---|
1240 | if (!pcszSource)
|
---|
1241 | {
|
---|
1242 | *pszTarget = '\0';
|
---|
1243 | return 0;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | while (c = *pSource++)
|
---|
1247 | {
|
---|
1248 | if ( (c == '\r')
|
---|
1249 | || (c == '\n')
|
---|
1250 | )
|
---|
1251 | {
|
---|
1252 | if ( (pTarget == pszTarget)
|
---|
1253 | || (pTarget[-1] != ' ')
|
---|
1254 | )
|
---|
1255 | *pTarget++ = ' ';
|
---|
1256 | }
|
---|
1257 | else
|
---|
1258 | *pTarget++ = c;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | // null-terminate
|
---|
1262 | *pTarget = '\0';
|
---|
1263 |
|
---|
1264 | return (pTarget - pszTarget);
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | /*
|
---|
1268 | *@@ strhKillChar:
|
---|
1269 | * removes the first occurence of c in psz
|
---|
1270 | * by overwriting it with the following characters.
|
---|
1271 | *
|
---|
1272 | * For this to work, you _must_ pass in strlen(psz)
|
---|
1273 | * in the ULONG pointed to by ulLength. If
|
---|
1274 | *
|
---|
1275 | * Returns TRUE only if c was actually found. In
|
---|
1276 | * that case, *pulLength is decremented.
|
---|
1277 | *
|
---|
1278 | *@@added V1.0.1 (2003-01-30) [umoeller]
|
---|
1279 | */
|
---|
1280 |
|
---|
1281 | BOOL strhKillChar(PSZ psz,
|
---|
1282 | CHAR c,
|
---|
1283 | PULONG pulLength)
|
---|
1284 | {
|
---|
1285 | PSZ p;
|
---|
1286 | if (p = strchr(psz, c))
|
---|
1287 | {
|
---|
1288 | // "string~rest"
|
---|
1289 | // ÀÄÄÄÄÄÙ 6 chars (p - pszBuf)
|
---|
1290 | // ÀÄÄÄÄÄÄÄÄÄÄÙ 11 chars (ulLen)
|
---|
1291 | // ^ p (pszBuf + 6)
|
---|
1292 | // ^ pszBuf
|
---|
1293 | memmove(p, // pszBuf + 6
|
---|
1294 | p + 1, // pszBuf + 7
|
---|
1295 | // include null byte
|
---|
1296 | *pulLength // 11
|
---|
1297 | - (p - psz)); // - 6 = 5
|
---|
1298 | --(*pulLength);
|
---|
1299 |
|
---|
1300 | return TRUE;
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | return FALSE;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | /*
|
---|
1307 | * strhFindAttribValue:
|
---|
1308 | * searches for pszAttrib in pszSearchIn; if found,
|
---|
1309 | * returns the first character after the "=" char.
|
---|
1310 | * If "=" is not found, a space, \r, and \n are
|
---|
1311 | * also accepted. This function searches without
|
---|
1312 | * respecting case.
|
---|
1313 | *
|
---|
1314 | * <B>Example:</B>
|
---|
1315 | + strhFindAttribValue("<PAGE BLAH=\"data\">", "BLAH")
|
---|
1316 | +
|
---|
1317 | + returns ....................... ^ this address.
|
---|
1318 | *
|
---|
1319 | *@@added V0.9.0 [umoeller]
|
---|
1320 | *@@changed V0.9.3 (2000-05-19) [umoeller]: some speed optimizations
|
---|
1321 | *@@changed V0.9.12 (2001-05-22) [umoeller]: fixed space bug, thanks Yuri Dario
|
---|
1322 | *@@changed WarpIN V1.0.11 (2006-08-29) [pr]: handle attrib names in quoted strings @@fixes 718
|
---|
1323 | *@@changed WarpIN V1.0.12 (2006-09-07) [pr]: fix attrib handling again @@fixes 718 @@fixes 836
|
---|
1324 | *@@changed WarpIN V1.0.23 (2016-09-23) [pr]: fix single quote handling and allow escaping @@fixes 1244
|
---|
1325 | */
|
---|
1326 |
|
---|
1327 | PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
|
---|
1328 | {
|
---|
1329 | PSZ prc = 0;
|
---|
1330 | PSZ pszSearchIn2, p, pszStart, pszName, pszValue;
|
---|
1331 | ULONG cbAttrib = strlen(pszAttrib),
|
---|
1332 | ulLength = strlen(pszSearchIn);
|
---|
1333 | BOOL fInQuote = FALSE;
|
---|
1334 | CHAR cQuote = '\0', cPrev = '\0';
|
---|
1335 |
|
---|
1336 | // use alloca(), so memory is freed on function exit
|
---|
1337 | pszSearchIn2 = (PSZ)alloca(ulLength + 1);
|
---|
1338 | memcpy(pszSearchIn2, pszSearchIn, ulLength + 1);
|
---|
1339 |
|
---|
1340 | // V1.0.12 (2006-09-07) [pr]: filter leading " and ' left over from the previous pass
|
---|
1341 | for (p = pszSearchIn2; *p == '\'' || *p == '"' || *p == ' '
|
---|
1342 | || *p == '\n' || *p == '\r' || *p == '\t'; p++);
|
---|
1343 | for (pszStart = p; *p; cPrev = *(p++))
|
---|
1344 | {
|
---|
1345 | if (fInQuote)
|
---|
1346 | {
|
---|
1347 | // V1.0.12 (2006-09-07) [pr]: allow end of line to terminate a (broken) quote
|
---|
1348 | if ((*p == cQuote && cPrev != '\\') || *p == '\n' || *p == '\r')
|
---|
1349 | fInQuote = FALSE;
|
---|
1350 | }
|
---|
1351 | else
|
---|
1352 | {
|
---|
1353 | if (*p == '"' || *p == '\'')
|
---|
1354 | {
|
---|
1355 | fInQuote = TRUE;
|
---|
1356 | cQuote = *p;
|
---|
1357 | }
|
---|
1358 | else
|
---|
1359 | {
|
---|
1360 | if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
|
---|
1361 | {
|
---|
1362 | *p = '\0';
|
---|
1363 | pszName = strtok(pszStart, "=>");
|
---|
1364 | pszStart = p + 1;
|
---|
1365 | if (pszName && !stricmp(pszName, pszAttrib))
|
---|
1366 | {
|
---|
1367 | pszValue = strtok(NULL, "");
|
---|
1368 | if (pszValue)
|
---|
1369 | prc = (PSZ)pszSearchIn + (pszValue - pszSearchIn2);
|
---|
1370 | else
|
---|
1371 | prc = (PSZ)pszSearchIn + (pszName - pszSearchIn2) + cbAttrib;
|
---|
1372 |
|
---|
1373 | return(prc);
|
---|
1374 | }
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (pszStart != p)
|
---|
1381 | {
|
---|
1382 | pszName = strtok(pszStart, "=>");
|
---|
1383 | if (pszName && !stricmp(pszName, pszAttrib))
|
---|
1384 | {
|
---|
1385 | pszValue = strtok(NULL, "");
|
---|
1386 | if (pszValue)
|
---|
1387 | prc = (PSZ)pszSearchIn + (pszValue - pszSearchIn2);
|
---|
1388 | else
|
---|
1389 | prc = (PSZ)pszSearchIn + (pszName - pszSearchIn2) + cbAttrib;
|
---|
1390 | }
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | return prc;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | /*
|
---|
1397 | * strhGetNumAttribValue:
|
---|
1398 | * stores the numerical parameter value of an HTML-style
|
---|
1399 | * tag in *pl.
|
---|
1400 | *
|
---|
1401 | * Returns the address of the tag parameter in the
|
---|
1402 | * search buffer, if found, or NULL.
|
---|
1403 | *
|
---|
1404 | * <B>Example:</B>
|
---|
1405 | + strhGetNumAttribValue("<PAGE BLAH=123>, "BLAH", &l);
|
---|
1406 | *
|
---|
1407 | * stores 123 in the "l" variable.
|
---|
1408 | *
|
---|
1409 | *@@added V0.9.0 [umoeller]
|
---|
1410 | *@@changed V0.9.9 (2001-04-04) [umoeller]: this failed on "123" strings in quotes, fixed
|
---|
1411 | */
|
---|
1412 |
|
---|
1413 | PSZ strhGetNumAttribValue(const char *pszSearchIn, // in: where to search
|
---|
1414 | const char *pszTag, // e.g. "INDEX"
|
---|
1415 | PLONG pl) // out: numerical value
|
---|
1416 | {
|
---|
1417 | PSZ pParam;
|
---|
1418 | if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
|
---|
1419 | {
|
---|
1420 | if ( (*pParam == '\"')
|
---|
1421 | || (*pParam == '\'')
|
---|
1422 | )
|
---|
1423 | pParam++; // V0.9.9 (2001-04-04) [umoeller]
|
---|
1424 |
|
---|
1425 | sscanf(pParam, "%ld", pl);
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | return pParam;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | /*
|
---|
1432 | * strhGetTextAttr:
|
---|
1433 | * retrieves the attribute value of a textual HTML-style tag
|
---|
1434 | * in a newly allocated buffer, which is returned,
|
---|
1435 | * or NULL if attribute not found.
|
---|
1436 | * If an attribute value is to contain spaces, it
|
---|
1437 | * must be enclosed in quotes.
|
---|
1438 | *
|
---|
1439 | * The offset of the attribute data in pszSearchIn is
|
---|
1440 | * returned in *pulOffset so that you can do multiple
|
---|
1441 | * searches.
|
---|
1442 | *
|
---|
1443 | * This returns a new buffer, which should be free()'d after use.
|
---|
1444 | *
|
---|
1445 | * <B>Example:</B>
|
---|
1446 | + ULONG ulOfs = 0;
|
---|
1447 | + strhGetTextAttr("<PAGE BLAH="blublub">, "BLAH", &ulOfs)
|
---|
1448 | + ............^ ulOfs
|
---|
1449 | *
|
---|
1450 | * returns a new string with the value "blublub" (without
|
---|
1451 | * quotes) and sets ulOfs to 12.
|
---|
1452 | *
|
---|
1453 | *@@added V0.9.0 [umoeller]
|
---|
1454 | *@@changed V1.0.13 (2006-09-10) [pr]: improved parsing
|
---|
1455 | *@@changed WarpIN V1.0.23 (2016-09-23) [pr]: allow escaping @@fixes 1244
|
---|
1456 | */
|
---|
1457 |
|
---|
1458 | PSZ strhGetTextAttr(const char *pszSearchIn,
|
---|
1459 | const char *pszTag,
|
---|
1460 | PULONG pulOffset) // out: offset where found
|
---|
1461 | {
|
---|
1462 | PSZ pParam,
|
---|
1463 | pParam2,
|
---|
1464 | prc = NULL;
|
---|
1465 | ULONG ulCount = 0;
|
---|
1466 | LONG lNestingLevel = 0;
|
---|
1467 |
|
---|
1468 | if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
|
---|
1469 | {
|
---|
1470 | // determine end character to search for: a space
|
---|
1471 | CHAR cEnd = ' ', cPrev = '\0';
|
---|
1472 | // V1.0.3 (2004-11-10) [pr]: @@fixes 461
|
---|
1473 | // V1.0.13 (2006-09-10) [pr]: optimized
|
---|
1474 | if ((*pParam == '"') || (*pParam == '\''))
|
---|
1475 | {
|
---|
1476 | // or, if the data is enclosed in quotes, a quote or single quote
|
---|
1477 | cEnd = *pParam;
|
---|
1478 | pParam++;
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | if (pulOffset)
|
---|
1482 | // store the offset
|
---|
1483 | (*pulOffset) = pParam - (PSZ)pszSearchIn;
|
---|
1484 |
|
---|
1485 | // now find end of attribute
|
---|
1486 | pParam2 = pParam;
|
---|
1487 | while (*pParam)
|
---|
1488 | {
|
---|
1489 | // V1.0.13 (2006-09-10) [pr]: line end terminates non-quoted attribute
|
---|
1490 | if ( ( (cEnd == ' ')
|
---|
1491 | && ((*pParam == ' ') || (*pParam == '\r') || (*pParam == '\n')))
|
---|
1492 | || ((*pParam == cEnd) && (cPrev != '\\'))
|
---|
1493 | )
|
---|
1494 | // end character found
|
---|
1495 | break;
|
---|
1496 | else if (*pParam == '<')
|
---|
1497 | // yet another opening tag found:
|
---|
1498 | // this is probably some "<" in the attributes
|
---|
1499 | lNestingLevel++;
|
---|
1500 | else if (*pParam == '>')
|
---|
1501 | {
|
---|
1502 | lNestingLevel--;
|
---|
1503 | if (lNestingLevel < 0)
|
---|
1504 | // end of tag found:
|
---|
1505 | break;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | ulCount++;
|
---|
1509 | cPrev = *(pParam++);
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | // copy attribute to new buffer, de-escaping if necessary
|
---|
1513 | if (ulCount && (prc = (PSZ) malloc(ulCount+1)))
|
---|
1514 | {
|
---|
1515 | ULONG i = 0, j = 0;
|
---|
1516 |
|
---|
1517 | for(cPrev = '\0'; i < ulCount; ++i, ++j)
|
---|
1518 | {
|
---|
1519 | if (cEnd != ' ' && pParam2[i] == cEnd && cPrev == '\\')
|
---|
1520 | --j;
|
---|
1521 |
|
---|
1522 | cPrev = prc[j] = pParam2[i];
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | prc[j] = '\0';
|
---|
1526 | }
|
---|
1527 | }
|
---|
1528 | return prc;
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | /*
|
---|
1532 | * strhFindEndOfTag:
|
---|
1533 | * returns a pointer to the ">" char
|
---|
1534 | * which seems to terminate the tag beginning
|
---|
1535 | * after pszBeginOfTag.
|
---|
1536 | *
|
---|
1537 | * If additional "<" chars are found, we look
|
---|
1538 | * for additional ">" characters too.
|
---|
1539 | *
|
---|
1540 | * Note: You must pass the address of the opening
|
---|
1541 | * '<' character to this function.
|
---|
1542 | *
|
---|
1543 | * Example:
|
---|
1544 | + PSZ pszTest = "<BODY ATTR=\"<BODY>\">";
|
---|
1545 | + strhFindEndOfTag(pszTest)
|
---|
1546 | + returns.................................^ this.
|
---|
1547 | *
|
---|
1548 | *@@added V0.9.0 [umoeller]
|
---|
1549 | */
|
---|
1550 |
|
---|
1551 | PSZ strhFindEndOfTag(const char *pszBeginOfTag)
|
---|
1552 | {
|
---|
1553 | PSZ p = (PSZ)pszBeginOfTag,
|
---|
1554 | prc = NULL;
|
---|
1555 | LONG lNestingLevel = 0;
|
---|
1556 |
|
---|
1557 | while (*p)
|
---|
1558 | {
|
---|
1559 | if (*p == '<')
|
---|
1560 | // another opening tag found:
|
---|
1561 | lNestingLevel++;
|
---|
1562 | else if (*p == '>')
|
---|
1563 | {
|
---|
1564 | // closing tag found:
|
---|
1565 | lNestingLevel--;
|
---|
1566 | if (lNestingLevel < 1)
|
---|
1567 | {
|
---|
1568 | // corresponding: return this
|
---|
1569 | prc = p;
|
---|
1570 | break;
|
---|
1571 | }
|
---|
1572 | }
|
---|
1573 | p++;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | return prc;
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | /*
|
---|
1580 | * strhGetBlock:
|
---|
1581 | * this complex function searches the given string
|
---|
1582 | * for a pair of opening/closing HTML-style tags.
|
---|
1583 | *
|
---|
1584 | * If found, this routine returns TRUE and does
|
---|
1585 | * the following:
|
---|
1586 | *
|
---|
1587 | * 1) allocate a new buffer, copy the text
|
---|
1588 | * enclosed by the opening/closing tags
|
---|
1589 | * into it and set *ppszBlock to that
|
---|
1590 | * buffer;
|
---|
1591 | *
|
---|
1592 | * 2) if the opening tag has any attributes,
|
---|
1593 | * allocate another buffer, copy the
|
---|
1594 | * attributes into it and set *ppszAttrs
|
---|
1595 | * to that buffer; if no attributes are
|
---|
1596 | * found, *ppszAttrs will be NULL;
|
---|
1597 | *
|
---|
1598 | * 3) set *pulOffset to the offset from the
|
---|
1599 | * beginning of *ppszSearchIn where the
|
---|
1600 | * opening tag was found;
|
---|
1601 | *
|
---|
1602 | * 4) advance *ppszSearchIn to after the
|
---|
1603 | * closing tag, so that you can do
|
---|
1604 | * multiple searches without finding the
|
---|
1605 | * same tags twice.
|
---|
1606 | *
|
---|
1607 | * All buffers should be freed using free().
|
---|
1608 | *
|
---|
1609 | * This returns the following:
|
---|
1610 | * -- 0: no error
|
---|
1611 | * -- 1: tag not found at all (doesn't have to be an error)
|
---|
1612 | * -- 2: begin tag found, but no corresponding end tag found. This
|
---|
1613 | * is a real error.
|
---|
1614 | * -- 3: begin tag is not terminated by ">" (e.g. "<BEGINTAG whatever")
|
---|
1615 | *
|
---|
1616 | * <B>Example:</B>
|
---|
1617 | + PSZ pSearch = "<PAGE INDEX=1>This is page 1.</PAGE>More text."
|
---|
1618 | + PSZ pszBlock, pszAttrs;
|
---|
1619 | + ULONG ulOfs;
|
---|
1620 | + strhGetBlock(&pSearch, "PAGE", &pszBlock, &pszAttrs, &ulOfs)
|
---|
1621 | *
|
---|
1622 | * would do the following:
|
---|
1623 | *
|
---|
1624 | * 1) set pszBlock to a new string containing "This is page 1."
|
---|
1625 | * without quotes;
|
---|
1626 | *
|
---|
1627 | * 2) set pszAttrs to a new string containing "<PAGE INDEX=1>";
|
---|
1628 | *
|
---|
1629 | * 3) set ulOfs to 0, because "<PAGE" was found at the beginning;
|
---|
1630 | *
|
---|
1631 | * 4) pSearch would be advanced to point to the "More text"
|
---|
1632 | * string in the original buffer.
|
---|
1633 | *
|
---|
1634 | * Hey-hey. A one-shot function, fairly complicated, but indispensable
|
---|
1635 | * for HTML parsing.
|
---|
1636 | *
|
---|
1637 | *@@added V0.9.0 [umoeller]
|
---|
1638 | *@@changed V0.9.1 (2000-01-03) [umoeller]: fixed heap overwrites (thanks to string debugging)
|
---|
1639 | *@@changed V0.9.1 (2000-01-06) [umoeller]: changed prototype
|
---|
1640 | *@@changed V0.9.3 (2000-05-06) [umoeller]: NULL string check was missing
|
---|
1641 | */
|
---|
1642 |
|
---|
1643 | ULONG strhGetBlock(const char *pszSearchIn, // in: buffer to search
|
---|
1644 | PULONG pulSearchOffset, // in/out: offset where to start search (0 for beginning)
|
---|
1645 | const char *pszTag,
|
---|
1646 | PSZ *ppszBlock, // out: block enclosed by the tags
|
---|
1647 | PSZ *ppszAttribs, // out: attributes of the opening tag
|
---|
1648 | PULONG pulOfsBeginTag, // out: offset from pszSearchIn where opening tag was found
|
---|
1649 | PULONG pulOfsBeginBlock) // out: offset from pszSearchIn where beginning of block was found
|
---|
1650 | {
|
---|
1651 | ULONG ulrc = 1;
|
---|
1652 | PSZ pszBeginTag = (PSZ)pszSearchIn + *pulSearchOffset,
|
---|
1653 | pszSearch2 = pszBeginTag,
|
---|
1654 | pszClosingTag;
|
---|
1655 | ULONG cbTag = strlen(pszTag);
|
---|
1656 |
|
---|
1657 | // go thru the block and check all tags if it's the
|
---|
1658 | // begin tag we're looking for
|
---|
1659 | while ((pszBeginTag = strchr(pszBeginTag, '<')))
|
---|
1660 | {
|
---|
1661 | if (memicmp(pszBeginTag+1, (void*)pszTag, strlen(pszTag)) == 0)
|
---|
1662 | // yes: stop
|
---|
1663 | break;
|
---|
1664 | else
|
---|
1665 | pszBeginTag++;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | if (pszBeginTag)
|
---|
1669 | {
|
---|
1670 | // we found <TAG>:
|
---|
1671 | ULONG ulNestingLevel = 0;
|
---|
1672 |
|
---|
1673 | PSZ pszEndOfBeginTag = strhFindEndOfTag(pszBeginTag);
|
---|
1674 | // strchr(pszBeginTag, '>');
|
---|
1675 | if (pszEndOfBeginTag)
|
---|
1676 | {
|
---|
1677 | // does the caller want the attributes?
|
---|
1678 | if (ppszAttribs)
|
---|
1679 | {
|
---|
1680 | // yes: then copy them
|
---|
1681 | ULONG ulAttrLen = pszEndOfBeginTag - pszBeginTag;
|
---|
1682 | PSZ pszAttrs = (PSZ)malloc(ulAttrLen + 1);
|
---|
1683 | strncpy(pszAttrs, pszBeginTag, ulAttrLen);
|
---|
1684 | // add terminating 0
|
---|
1685 | *(pszAttrs + ulAttrLen) = 0;
|
---|
1686 |
|
---|
1687 | *ppszAttribs = pszAttrs;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | // output offset of where we found the begin tag
|
---|
1691 | if (pulOfsBeginTag)
|
---|
1692 | *pulOfsBeginTag = pszBeginTag - (PSZ)pszSearchIn;
|
---|
1693 |
|
---|
1694 | // now find corresponding closing tag (e.g. "</BODY>"
|
---|
1695 | pszBeginTag = pszEndOfBeginTag+1;
|
---|
1696 | // now we're behind the '>' char of the opening tag
|
---|
1697 | // increase offset of that too
|
---|
1698 | if (pulOfsBeginBlock)
|
---|
1699 | *pulOfsBeginBlock = pszBeginTag - (PSZ)pszSearchIn;
|
---|
1700 |
|
---|
1701 | // find next closing tag;
|
---|
1702 | // for the first run, pszSearch2 points to right
|
---|
1703 | // after the '>' char of the opening tag
|
---|
1704 | pszSearch2 = pszBeginTag;
|
---|
1705 | while ( (pszSearch2) // fixed V0.9.3 (2000-05-06) [umoeller]
|
---|
1706 | && (pszClosingTag = strstr(pszSearch2, "<"))
|
---|
1707 | )
|
---|
1708 | {
|
---|
1709 | // if we have another opening tag before our closing
|
---|
1710 | // tag, we need to have several closing tags before
|
---|
1711 | // we're done
|
---|
1712 | if (memicmp(pszClosingTag+1, (void*)pszTag, cbTag) == 0)
|
---|
1713 | ulNestingLevel++;
|
---|
1714 | else
|
---|
1715 | {
|
---|
1716 | // is this ours?
|
---|
1717 | if ( (*(pszClosingTag+1) == '/')
|
---|
1718 | && (memicmp(pszClosingTag+2, (void*)pszTag, cbTag) == 0)
|
---|
1719 | )
|
---|
1720 | {
|
---|
1721 | // we've found a matching closing tag; is
|
---|
1722 | // it ours?
|
---|
1723 | if (ulNestingLevel == 0)
|
---|
1724 | {
|
---|
1725 | // our closing tag found:
|
---|
1726 | // allocate mem for a new buffer
|
---|
1727 | // and extract all the text between
|
---|
1728 | // open and closing tags to it
|
---|
1729 | ULONG ulLen = pszClosingTag - pszBeginTag;
|
---|
1730 | if (ppszBlock)
|
---|
1731 | {
|
---|
1732 | PSZ pNew = (PSZ)malloc(ulLen + 1);
|
---|
1733 | strhncpy0(pNew, pszBeginTag, ulLen);
|
---|
1734 | *ppszBlock = pNew;
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | // raise search offset to after the closing tag
|
---|
1738 | *pulSearchOffset = (pszClosingTag + cbTag + 1) - (PSZ)pszSearchIn;
|
---|
1739 |
|
---|
1740 | ulrc = 0;
|
---|
1741 |
|
---|
1742 | break;
|
---|
1743 | } else
|
---|
1744 | // not our closing tag:
|
---|
1745 | ulNestingLevel--;
|
---|
1746 | }
|
---|
1747 | }
|
---|
1748 | // no matching closing tag: search on after that
|
---|
1749 | pszSearch2 = strhFindEndOfTag(pszClosingTag);
|
---|
1750 | } // end while (pszClosingTag = strstr(pszSearch2, "<"))
|
---|
1751 |
|
---|
1752 | if (!pszClosingTag)
|
---|
1753 | // no matching closing tag found:
|
---|
1754 | // return 2 (closing tag not found)
|
---|
1755 | ulrc = 2;
|
---|
1756 | } // end if (pszBeginTag)
|
---|
1757 | else
|
---|
1758 | // no matching ">" for opening tag found:
|
---|
1759 | ulrc = 3;
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | return ulrc;
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | /* ******************************************************************
|
---|
1766 | *
|
---|
1767 | * Miscellaneous
|
---|
1768 | *
|
---|
1769 | ********************************************************************/
|
---|
1770 |
|
---|
1771 | /*
|
---|
1772 | *@@ strhArrayAppend:
|
---|
1773 | * this appends a string to a "string array".
|
---|
1774 | *
|
---|
1775 | * A string array is considered a sequence of
|
---|
1776 | * zero-terminated strings in memory. That is,
|
---|
1777 | * after each string's null-byte, the next
|
---|
1778 | * string comes up.
|
---|
1779 | *
|
---|
1780 | * This is useful for composing a single block
|
---|
1781 | * of memory from, say, list box entries, which
|
---|
1782 | * can then be written to OS2.INI in one flush.
|
---|
1783 | *
|
---|
1784 | * To append strings to such an array, call this
|
---|
1785 | * function for each string you wish to append.
|
---|
1786 | * This will re-allocate *ppszRoot with each call,
|
---|
1787 | * and update *pcbRoot, which then contains the
|
---|
1788 | * total size of all strings (including all null
|
---|
1789 | * terminators).
|
---|
1790 | *
|
---|
1791 | * Pass *pcbRoot to PrfSaveProfileData to have the
|
---|
1792 | * block saved.
|
---|
1793 | *
|
---|
1794 | * Note: On the first call, *ppszRoot and *pcbRoot
|
---|
1795 | * _must_ be both NULL, or this crashes.
|
---|
1796 | *
|
---|
1797 | *@@changed V0.9.13 (2001-06-21) [umoeller]: added cbNew
|
---|
1798 | */
|
---|
1799 |
|
---|
1800 | VOID strhArrayAppend(PSZ *ppszRoot, // in: root of array
|
---|
1801 | const char *pcszNew, // in: string to append
|
---|
1802 | ULONG cbNew, // in: size of that string or 0 to run strlen() here
|
---|
1803 | PULONG pcbRoot) // in/out: size of array
|
---|
1804 | {
|
---|
1805 | PSZ pszTemp;
|
---|
1806 |
|
---|
1807 | if (!cbNew) // V0.9.13 (2001-06-21) [umoeller]
|
---|
1808 | cbNew = strlen(pcszNew);
|
---|
1809 |
|
---|
1810 | pszTemp = (PSZ)malloc(*pcbRoot
|
---|
1811 | + cbNew
|
---|
1812 | + 1); // two null bytes
|
---|
1813 | if (*ppszRoot)
|
---|
1814 | {
|
---|
1815 | // not first loop: copy old stuff
|
---|
1816 | memcpy(pszTemp,
|
---|
1817 | *ppszRoot,
|
---|
1818 | *pcbRoot);
|
---|
1819 | free(*ppszRoot);
|
---|
1820 | }
|
---|
1821 | // append new string
|
---|
1822 | strcpy(pszTemp + *pcbRoot,
|
---|
1823 | pcszNew);
|
---|
1824 | // update root
|
---|
1825 | *ppszRoot = pszTemp;
|
---|
1826 | // update length
|
---|
1827 | *pcbRoot += cbNew + 1;
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | /*
|
---|
1831 | *@@ strhCreateDump:
|
---|
1832 | * this dumps a memory block into a string
|
---|
1833 | * and returns that string in a new buffer.
|
---|
1834 | *
|
---|
1835 | * You must free() the returned PSZ after use.
|
---|
1836 | *
|
---|
1837 | * The output looks like the following:
|
---|
1838 | *
|
---|
1839 | + 0000: FE FF 0E 02 90 00 00 00 ........
|
---|
1840 | + 0008: FD 01 00 00 57 50 46 6F ....WPFo
|
---|
1841 | + 0010: 6C 64 65 72 00 78 01 34 lder.x.4
|
---|
1842 | *
|
---|
1843 | * Each line is terminated with a newline (\n)
|
---|
1844 | * character only.
|
---|
1845 | *
|
---|
1846 | *@@added V0.9.1 (2000-01-22) [umoeller]
|
---|
1847 | */
|
---|
1848 |
|
---|
1849 | PSZ strhCreateDump(PBYTE pb, // in: start address of buffer
|
---|
1850 | ULONG ulSize, // in: size of buffer
|
---|
1851 | ULONG ulIndent) // in: indentation of every line
|
---|
1852 | {
|
---|
1853 | PSZ pszReturn = 0;
|
---|
1854 | XSTRING strReturn;
|
---|
1855 | CHAR szTemp[1000];
|
---|
1856 |
|
---|
1857 | PBYTE pbCurrent = pb; // current byte
|
---|
1858 | ULONG ulCount = 0,
|
---|
1859 | ulCharsInLine = 0; // if this grows > 7, a new line is started
|
---|
1860 | CHAR szLine[400] = "",
|
---|
1861 | szAscii[30] = " "; // ASCII representation; filled for every line
|
---|
1862 | PSZ pszLine = szLine,
|
---|
1863 | pszAscii = szAscii;
|
---|
1864 |
|
---|
1865 | xstrInit(&strReturn, (ulSize * 30) + ulIndent);
|
---|
1866 |
|
---|
1867 | for (pbCurrent = pb;
|
---|
1868 | ulCount < ulSize;
|
---|
1869 | pbCurrent++, ulCount++)
|
---|
1870 | {
|
---|
1871 | if (ulCharsInLine == 0)
|
---|
1872 | {
|
---|
1873 | memset(szLine, ' ', ulIndent);
|
---|
1874 | pszLine += ulIndent;
|
---|
1875 | }
|
---|
1876 | pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent);
|
---|
1877 |
|
---|
1878 | if ( (*pbCurrent > 31) && (*pbCurrent < 127) )
|
---|
1879 | // printable character:
|
---|
1880 | *pszAscii = *pbCurrent;
|
---|
1881 | else
|
---|
1882 | *pszAscii = '.';
|
---|
1883 | pszAscii++;
|
---|
1884 |
|
---|
1885 | ulCharsInLine++;
|
---|
1886 | if ( (ulCharsInLine > 7) // 8 bytes added?
|
---|
1887 | || (ulCount == ulSize-1) // end of buffer reached?
|
---|
1888 | )
|
---|
1889 | {
|
---|
1890 | // if we haven't had eight bytes yet,
|
---|
1891 | // fill buffer up to eight bytes with spaces
|
---|
1892 | ULONG ul2;
|
---|
1893 | for (ul2 = ulCharsInLine;
|
---|
1894 | ul2 < 8;
|
---|
1895 | ul2++)
|
---|
1896 | pszLine += sprintf(pszLine, " ");
|
---|
1897 |
|
---|
1898 | sprintf(szTemp, "%04lX: %s %s\n",
|
---|
1899 | (ulCount & 0xFFFFFFF8), // offset in hex
|
---|
1900 | szLine, // bytes string
|
---|
1901 | szAscii); // ASCII string
|
---|
1902 | xstrcat(&strReturn, szTemp, 0);
|
---|
1903 |
|
---|
1904 | // restart line buffer
|
---|
1905 | pszLine = szLine;
|
---|
1906 |
|
---|
1907 | // clear ASCII buffer
|
---|
1908 | strcpy(szAscii, " ");
|
---|
1909 | pszAscii = szAscii;
|
---|
1910 |
|
---|
1911 | // reset line counter
|
---|
1912 | ulCharsInLine = 0;
|
---|
1913 | }
|
---|
1914 | }
|
---|
1915 |
|
---|
1916 | if (strReturn.cbAllocated)
|
---|
1917 | pszReturn = strReturn.psz;
|
---|
1918 |
|
---|
1919 | return pszReturn;
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 | /* ******************************************************************
|
---|
1923 | *
|
---|
1924 | * Fast string searches
|
---|
1925 | *
|
---|
1926 | ********************************************************************/
|
---|
1927 |
|
---|
1928 | #define ASSERT(a)
|
---|
1929 |
|
---|
1930 | /*
|
---|
1931 | * The following code has been taken from the "Standard
|
---|
1932 | * Function Library", file sflfind.c, and only slightly
|
---|
1933 | * modified to conform to the rest of this file.
|
---|
1934 | *
|
---|
1935 | * Written: 96/04/24 iMatix SFL project team <sfl@imatix.com>
|
---|
1936 | * Revised: 98/05/04
|
---|
1937 | *
|
---|
1938 | * Copyright: Copyright (c) 1991-99 iMatix Corporation.
|
---|
1939 | *
|
---|
1940 | * The SFL Licence allows incorporating SFL code into other
|
---|
1941 | * programs, as long as the copyright is reprinted and the
|
---|
1942 | * code is marked as modified, so this is what we do.
|
---|
1943 | */
|
---|
1944 |
|
---|
1945 | /*
|
---|
1946 | *@@ strhmemfind:
|
---|
1947 | * searches for a pattern in a block of memory using the
|
---|
1948 | * Boyer-Moore-Horspool-Sunday algorithm.
|
---|
1949 | *
|
---|
1950 | * The block and pattern may contain any values; you must
|
---|
1951 | * explicitly provide their lengths. If you search for strings,
|
---|
1952 | * use strlen() on the buffers.
|
---|
1953 | *
|
---|
1954 | * Returns a pointer to the pattern if found within the block,
|
---|
1955 | * or NULL if the pattern was not found.
|
---|
1956 | *
|
---|
1957 | * This algorithm needs a "shift table" to cache data for the
|
---|
1958 | * search pattern. This table can be reused when performing
|
---|
1959 | * several searches with the same pattern.
|
---|
1960 | *
|
---|
1961 | * "shift" must point to an array big enough to hold 256 (8**2)
|
---|
1962 | * "size_t" values.
|
---|
1963 | *
|
---|
1964 | * If (*repeat_find == FALSE), the shift table is initialized.
|
---|
1965 | * So on the first search with a given pattern, *repeat_find
|
---|
1966 | * should be FALSE. This function sets it to TRUE after the
|
---|
1967 | * shift table is initialised, allowing the initialisation
|
---|
1968 | * phase to be skipped on subsequent searches.
|
---|
1969 | *
|
---|
1970 | * This function is most effective when repeated searches are
|
---|
1971 | * made for the same pattern in one or more large buffers.
|
---|
1972 | *
|
---|
1973 | * Example:
|
---|
1974 | *
|
---|
1975 | + PSZ pszHaystack = "This is a sample string.",
|
---|
1976 | + pszNeedle = "string";
|
---|
1977 | + size_t shift[256];
|
---|
1978 | + BOOL fRepeat = FALSE;
|
---|
1979 | +
|
---|
1980 | + PSZ pFound = strhmemfind(pszHaystack,
|
---|
1981 | + strlen(pszHaystack), // block size
|
---|
1982 | + pszNeedle,
|
---|
1983 | + strlen(pszNeedle), // pattern size
|
---|
1984 | + shift,
|
---|
1985 | + &fRepeat);
|
---|
1986 | *
|
---|
1987 | * Taken from the "Standard Function Library", file sflfind.c.
|
---|
1988 | * Copyright: Copyright (c) 1991-99 iMatix Corporation.
|
---|
1989 | * Slightly modified by umoeller.
|
---|
1990 | *
|
---|
1991 | *@@added V0.9.3 (2000-05-08) [umoeller]
|
---|
1992 | */
|
---|
1993 |
|
---|
1994 | void* strhmemfind(const void *in_block, // in: block containing data
|
---|
1995 | size_t block_size, // in: size of block in bytes
|
---|
1996 | const void *in_pattern, // in: pattern to search for
|
---|
1997 | size_t pattern_size, // in: size of pattern block
|
---|
1998 | size_t *shift, // in/out: shift table (search buffer)
|
---|
1999 | BOOL *repeat_find) // in/out: if TRUE, *shift is already initialized
|
---|
2000 | {
|
---|
2001 | size_t byte_nbr, // Distance through block
|
---|
2002 | match_size; // Size of matched part
|
---|
2003 | const unsigned char
|
---|
2004 | *match_base = NULL, // Base of match of pattern
|
---|
2005 | *match_ptr = NULL, // Point within current match
|
---|
2006 | *limit = NULL; // Last potiental match point
|
---|
2007 | const unsigned char
|
---|
2008 | *block = (unsigned char *) in_block, // Concrete pointer to block data
|
---|
2009 | *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value
|
---|
2010 |
|
---|
2011 | if ( (block == NULL)
|
---|
2012 | || (pattern == NULL)
|
---|
2013 | || (shift == NULL)
|
---|
2014 | )
|
---|
2015 | return NULL;
|
---|
2016 |
|
---|
2017 | // Pattern must be smaller or equal in size to string
|
---|
2018 | if (block_size < pattern_size)
|
---|
2019 | return NULL; // Otherwise it's not found
|
---|
2020 |
|
---|
2021 | if (pattern_size == 0) // Empty patterns match at start
|
---|
2022 | return (void*)block;
|
---|
2023 |
|
---|
2024 | // Build the shift table unless we're continuing a previous search
|
---|
2025 |
|
---|
2026 | // The shift table determines how far to shift before trying to match
|
---|
2027 | // again, if a match at this point fails. If the byte after where the
|
---|
2028 | // end of our pattern falls is not in our pattern, then we start to
|
---|
2029 | // match again after that byte; otherwise we line up the last occurence
|
---|
2030 | // of that byte in our pattern under that byte, and try match again.
|
---|
2031 |
|
---|
2032 | if (!repeat_find || !*repeat_find)
|
---|
2033 | {
|
---|
2034 | for (byte_nbr = 0;
|
---|
2035 | byte_nbr < 256;
|
---|
2036 | byte_nbr++)
|
---|
2037 | shift[byte_nbr] = pattern_size + 1;
|
---|
2038 | for (byte_nbr = 0;
|
---|
2039 | byte_nbr < pattern_size;
|
---|
2040 | byte_nbr++)
|
---|
2041 | shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr;
|
---|
2042 |
|
---|
2043 | if (repeat_find)
|
---|
2044 | *repeat_find = TRUE;
|
---|
2045 | }
|
---|
2046 |
|
---|
2047 | // Search for the block, each time jumping up by the amount
|
---|
2048 | // computed in the shift table
|
---|
2049 |
|
---|
2050 | limit = block + (block_size - pattern_size + 1);
|
---|
2051 | ASSERT (limit > block);
|
---|
2052 |
|
---|
2053 | for (match_base = block;
|
---|
2054 | match_base < limit;
|
---|
2055 | match_base += shift[*(match_base + pattern_size)])
|
---|
2056 | {
|
---|
2057 | match_ptr = match_base;
|
---|
2058 | match_size = 0;
|
---|
2059 |
|
---|
2060 | // Compare pattern until it all matches, or we find a difference
|
---|
2061 | while (*match_ptr++ == pattern[match_size++])
|
---|
2062 | {
|
---|
2063 | ASSERT (match_size <= pattern_size &&
|
---|
2064 | match_ptr == (match_base + match_size));
|
---|
2065 |
|
---|
2066 | // If we found a match, return the start address
|
---|
2067 | if (match_size >= pattern_size)
|
---|
2068 | return (void*)match_base;
|
---|
2069 |
|
---|
2070 | }
|
---|
2071 | }
|
---|
2072 | return NULL; // Found nothing
|
---|
2073 | }
|
---|
2074 |
|
---|
2075 | /*
|
---|
2076 | *@@ strhtxtfind:
|
---|
2077 | * searches for a case-insensitive text pattern in a string
|
---|
2078 | * using the Boyer-Moore-Horspool-Sunday algorithm. The string and
|
---|
2079 | * pattern are null-terminated strings. Returns a pointer to the pattern
|
---|
2080 | * if found within the string, or NULL if the pattern was not found.
|
---|
2081 | * Will match strings irrespective of case. To match exact strings, use
|
---|
2082 | * strhfind(). Will not work on multibyte characters.
|
---|
2083 | *
|
---|
2084 | * Examples:
|
---|
2085 | + char *result;
|
---|
2086 | +
|
---|
2087 | + result = strhtxtfind ("AbracaDabra", "cad");
|
---|
2088 | + if (result)
|
---|
2089 | + puts (result);
|
---|
2090 | +
|
---|
2091 | * Taken from the "Standard Function Library", file sflfind.c.
|
---|
2092 | * Copyright: Copyright (c) 1991-99 iMatix Corporation.
|
---|
2093 | * Slightly modified.
|
---|
2094 | *
|
---|
2095 | *@@added V0.9.3 (2000-05-08) [umoeller]
|
---|
2096 | */
|
---|
2097 |
|
---|
2098 | char* strhtxtfind (const char *string, // String containing data
|
---|
2099 | const char *pattern) // Pattern to search for
|
---|
2100 | {
|
---|
2101 | size_t
|
---|
2102 | shift [256]; // Shift distance for each value
|
---|
2103 | size_t
|
---|
2104 | string_size,
|
---|
2105 | pattern_size,
|
---|
2106 | byte_nbr, // Index into byte array
|
---|
2107 | match_size; // Size of matched part
|
---|
2108 | const char
|
---|
2109 | *match_base = NULL, // Base of match of pattern
|
---|
2110 | *match_ptr = NULL, // Point within current match
|
---|
2111 | *limit = NULL; // Last potiental match point
|
---|
2112 |
|
---|
2113 | ASSERT (string); // Expect non-NULL pointers, but
|
---|
2114 | ASSERT (pattern); // fail gracefully if not debugging
|
---|
2115 | if (string == NULL || pattern == NULL)
|
---|
2116 | return NULL;
|
---|
2117 |
|
---|
2118 | string_size = strlen (string);
|
---|
2119 | pattern_size = strlen (pattern);
|
---|
2120 |
|
---|
2121 | // Pattern must be smaller or equal in size to string
|
---|
2122 | if (string_size < pattern_size)
|
---|
2123 | return NULL; // Otherwise it cannot be found
|
---|
2124 |
|
---|
2125 | if (pattern_size == 0) // Empty string matches at start
|
---|
2126 | return (char*)string;
|
---|
2127 |
|
---|
2128 | // Build the shift table
|
---|
2129 |
|
---|
2130 | // The shift table determines how far to shift before trying to match
|
---|
2131 | // again, if a match at this point fails. If the byte after where the
|
---|
2132 | // end of our pattern falls is not in our pattern, then we start to
|
---|
2133 | // match again after that byte; otherwise we line up the last occurence
|
---|
2134 | // of that byte in our pattern under that byte, and try match again.
|
---|
2135 |
|
---|
2136 | for (byte_nbr = 0; byte_nbr < 256; byte_nbr++)
|
---|
2137 | shift [byte_nbr] = pattern_size + 1;
|
---|
2138 |
|
---|
2139 | for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++)
|
---|
2140 | shift [(unsigned char) tolower (pattern [byte_nbr])] = pattern_size - byte_nbr;
|
---|
2141 |
|
---|
2142 | // Search for the string. If we don't find a match, move up by the
|
---|
2143 | // amount we computed in the shift table above, to find location of
|
---|
2144 | // the next potiental match.
|
---|
2145 |
|
---|
2146 | limit = string + (string_size - pattern_size + 1);
|
---|
2147 | ASSERT (limit > string);
|
---|
2148 |
|
---|
2149 | for (match_base = string;
|
---|
2150 | match_base < limit;
|
---|
2151 | match_base += shift [(unsigned char) tolower (*(match_base + pattern_size))])
|
---|
2152 | {
|
---|
2153 | match_ptr = match_base;
|
---|
2154 | match_size = 0;
|
---|
2155 |
|
---|
2156 | // Compare pattern until it all matches, or we find a difference
|
---|
2157 | while (tolower (*match_ptr++) == tolower (pattern [match_size++]))
|
---|
2158 | {
|
---|
2159 | ASSERT (match_size <= pattern_size &&
|
---|
2160 | match_ptr == (match_base + match_size));
|
---|
2161 |
|
---|
2162 | // If we found a match, return the start address
|
---|
2163 | if (match_size >= pattern_size)
|
---|
2164 | return (char*)match_base;
|
---|
2165 | }
|
---|
2166 | }
|
---|
2167 |
|
---|
2168 | return NULL; // Found nothing
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 |
|
---|