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