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