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