source: trunk/src/helpers/stringh.c@ 332

Last change on this file since 332 was 332, checked in by pr, 19 years ago

Fixes bug 718/836

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