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