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

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

Fixes bug 718

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 63.3 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 */
1324
1325PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
1326{
1327 PSZ prc = 0;
1328 PSZ pszSearchIn2, p, pszStart, pszName, pszValue;
1329 ULONG cbAttrib = strlen(pszAttrib),
1330 ulLength = strlen(pszSearchIn);
1331 BOOL fInQuote = FALSE;
1332
1333 // use alloca(), so memory is freed on function exit
1334 pszSearchIn2 = (PSZ)alloca(ulLength + 1);
1335 memcpy(pszSearchIn2, pszSearchIn, ulLength + 1);
1336
1337 for (p = pszSearchIn2; *p == ' ' || *p == '\n' || *p == '\r' || *p == '\t'; p++);
1338 for (pszStart = p; *p; p++)
1339 {
1340 if (fInQuote)
1341 {
1342 if (*p == '"')
1343 fInQuote = FALSE;
1344 }
1345 else
1346 {
1347 if (*p == '"')
1348 fInQuote = TRUE;
1349 else
1350 {
1351 if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
1352 {
1353 *p = '\0';
1354 pszName = strtok(pszStart, "=>");
1355 pszStart = p + 1;
1356 if (pszName && !stricmp(pszName, pszAttrib))
1357 {
1358 pszValue = strtok(NULL, "");
1359 if (pszValue)
1360 prc = (PSZ)pszSearchIn + (pszValue - pszSearchIn2);
1361 else
1362 prc = (PSZ)pszSearchIn + (pszName - pszSearchIn2) + cbAttrib;
1363
1364 return(prc);
1365 }
1366 }
1367 }
1368 }
1369 }
1370
1371 if (pszStart != p)
1372 {
1373 pszName = strtok(pszStart, "=>");
1374 if (pszName && !stricmp(pszName, pszAttrib))
1375 {
1376 pszValue = strtok(NULL, "");
1377 if (pszValue)
1378 prc = (PSZ)pszSearchIn + (pszValue - pszSearchIn2);
1379 else
1380 prc = (PSZ)pszSearchIn + (pszName - pszSearchIn2) + cbAttrib;
1381 }
1382 }
1383
1384 return prc;
1385}
1386
1387/*
1388 * strhGetNumAttribValue:
1389 * stores the numerical parameter value of an HTML-style
1390 * tag in *pl.
1391 *
1392 * Returns the address of the tag parameter in the
1393 * search buffer, if found, or NULL.
1394 *
1395 * <B>Example:</B>
1396 + strhGetNumAttribValue("<PAGE BLAH=123>, "BLAH", &l);
1397 *
1398 * stores 123 in the "l" variable.
1399 *
1400 *@@added V0.9.0 [umoeller]
1401 *@@changed V0.9.9 (2001-04-04) [umoeller]: this failed on "123" strings in quotes, fixed
1402 */
1403
1404PSZ strhGetNumAttribValue(const char *pszSearchIn, // in: where to search
1405 const char *pszTag, // e.g. "INDEX"
1406 PLONG pl) // out: numerical value
1407{
1408 PSZ pParam;
1409 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
1410 {
1411 if ( (*pParam == '\"')
1412 || (*pParam == '\'')
1413 )
1414 pParam++; // V0.9.9 (2001-04-04) [umoeller]
1415
1416 sscanf(pParam, "%ld", pl);
1417 }
1418
1419 return pParam;
1420}
1421
1422/*
1423 * strhGetTextAttr:
1424 * retrieves the attribute value of a textual HTML-style tag
1425 * in a newly allocated buffer, which is returned,
1426 * or NULL if attribute not found.
1427 * If an attribute value is to contain spaces, it
1428 * must be enclosed in quotes.
1429 *
1430 * The offset of the attribute data in pszSearchIn is
1431 * returned in *pulOffset so that you can do multiple
1432 * searches.
1433 *
1434 * This returns a new buffer, which should be free()'d after use.
1435 *
1436 * <B>Example:</B>
1437 + ULONG ulOfs = 0;
1438 + strhGetTextAttr("<PAGE BLAH="blublub">, "BLAH", &ulOfs)
1439 + ............^ ulOfs
1440 *
1441 * returns a new string with the value "blublub" (without
1442 * quotes) and sets ulOfs to 12.
1443 *
1444 *@@added V0.9.0 [umoeller]
1445 */
1446
1447PSZ strhGetTextAttr(const char *pszSearchIn,
1448 const char *pszTag,
1449 PULONG pulOffset) // out: offset where found
1450{
1451 PSZ pParam,
1452 pParam2,
1453 prc = NULL;
1454 ULONG ulCount = 0;
1455 LONG lNestingLevel = 0;
1456
1457 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
1458 {
1459 // determine end character to search for: a space
1460 CHAR cEnd = ' ';
1461 if (*pParam == '\"')
1462 {
1463 // or, if the data is enclosed in quotes, a quote
1464 cEnd = '\"';
1465 pParam++;
1466 }
1467
1468 // V1.0.3 (2004-11-10) [pr]: @@fixes 461
1469 if (*pParam == '\'')
1470 {
1471 // or, if the data is enclosed in single quotes, a single quote
1472 cEnd = '\'';
1473 pParam++;
1474 }
1475
1476 if (pulOffset)
1477 // store the offset
1478 (*pulOffset) = pParam - (PSZ)pszSearchIn;
1479
1480 // now find end of attribute
1481 pParam2 = pParam;
1482 while (*pParam)
1483 {
1484 if (*pParam == cEnd)
1485 // end character found
1486 break;
1487 else if (*pParam == '<')
1488 // yet another opening tag found:
1489 // this is probably some "<" in the attributes
1490 lNestingLevel++;
1491 else if (*pParam == '>')
1492 {
1493 lNestingLevel--;
1494 if (lNestingLevel < 0)
1495 // end of tag found:
1496 break;
1497 }
1498 ulCount++;
1499 pParam++;
1500 }
1501
1502 // copy attribute to new buffer
1503 if (ulCount)
1504 {
1505 prc = (PSZ)malloc(ulCount+1);
1506 memcpy(prc, pParam2, ulCount);
1507 *(prc+ulCount) = 0;
1508 }
1509 }
1510 return prc;
1511}
1512
1513/*
1514 * strhFindEndOfTag:
1515 * returns a pointer to the ">" char
1516 * which seems to terminate the tag beginning
1517 * after pszBeginOfTag.
1518 *
1519 * If additional "<" chars are found, we look
1520 * for additional ">" characters too.
1521 *
1522 * Note: You must pass the address of the opening
1523 * '<' character to this function.
1524 *
1525 * Example:
1526 + PSZ pszTest = "<BODY ATTR=\"<BODY>\">";
1527 + strhFindEndOfTag(pszTest)
1528 + returns.................................^ this.
1529 *
1530 *@@added V0.9.0 [umoeller]
1531 */
1532
1533PSZ strhFindEndOfTag(const char *pszBeginOfTag)
1534{
1535 PSZ p = (PSZ)pszBeginOfTag,
1536 prc = NULL;
1537 LONG lNestingLevel = 0;
1538
1539 while (*p)
1540 {
1541 if (*p == '<')
1542 // another opening tag found:
1543 lNestingLevel++;
1544 else if (*p == '>')
1545 {
1546 // closing tag found:
1547 lNestingLevel--;
1548 if (lNestingLevel < 1)
1549 {
1550 // corresponding: return this
1551 prc = p;
1552 break;
1553 }
1554 }
1555 p++;
1556 }
1557
1558 return prc;
1559}
1560
1561/*
1562 * strhGetBlock:
1563 * this complex function searches the given string
1564 * for a pair of opening/closing HTML-style tags.
1565 *
1566 * If found, this routine returns TRUE and does
1567 * the following:
1568 *
1569 * 1) allocate a new buffer, copy the text
1570 * enclosed by the opening/closing tags
1571 * into it and set *ppszBlock to that
1572 * buffer;
1573 *
1574 * 2) if the opening tag has any attributes,
1575 * allocate another buffer, copy the
1576 * attributes into it and set *ppszAttrs
1577 * to that buffer; if no attributes are
1578 * found, *ppszAttrs will be NULL;
1579 *
1580 * 3) set *pulOffset to the offset from the
1581 * beginning of *ppszSearchIn where the
1582 * opening tag was found;
1583 *
1584 * 4) advance *ppszSearchIn to after the
1585 * closing tag, so that you can do
1586 * multiple searches without finding the
1587 * same tags twice.
1588 *
1589 * All buffers should be freed using free().
1590 *
1591 * This returns the following:
1592 * -- 0: no error
1593 * -- 1: tag not found at all (doesn't have to be an error)
1594 * -- 2: begin tag found, but no corresponding end tag found. This
1595 * is a real error.
1596 * -- 3: begin tag is not terminated by "&gt;" (e.g. "&lt;BEGINTAG whatever")
1597 *
1598 * <B>Example:</B>
1599 + PSZ pSearch = "&lt;PAGE INDEX=1&gt;This is page 1.&lt;/PAGE&gt;More text."
1600 + PSZ pszBlock, pszAttrs;
1601 + ULONG ulOfs;
1602 + strhGetBlock(&pSearch, "PAGE", &pszBlock, &pszAttrs, &ulOfs)
1603 *
1604 * would do the following:
1605 *
1606 * 1) set pszBlock to a new string containing "This is page 1."
1607 * without quotes;
1608 *
1609 * 2) set pszAttrs to a new string containing "&lt;PAGE INDEX=1&gt;";
1610 *
1611 * 3) set ulOfs to 0, because "&lt;PAGE" was found at the beginning;
1612 *
1613 * 4) pSearch would be advanced to point to the "More text"
1614 * string in the original buffer.
1615 *
1616 * Hey-hey. A one-shot function, fairly complicated, but indispensable
1617 * for HTML parsing.
1618 *
1619 *@@added V0.9.0 [umoeller]
1620 *@@changed V0.9.1 (2000-01-03) [umoeller]: fixed heap overwrites (thanks to string debugging)
1621 *@@changed V0.9.1 (2000-01-06) [umoeller]: changed prototype
1622 *@@changed V0.9.3 (2000-05-06) [umoeller]: NULL string check was missing
1623 */
1624
1625ULONG strhGetBlock(const char *pszSearchIn, // in: buffer to search
1626 PULONG pulSearchOffset, // in/out: offset where to start search (0 for beginning)
1627 const char *pszTag,
1628 PSZ *ppszBlock, // out: block enclosed by the tags
1629 PSZ *ppszAttribs, // out: attributes of the opening tag
1630 PULONG pulOfsBeginTag, // out: offset from pszSearchIn where opening tag was found
1631 PULONG pulOfsBeginBlock) // out: offset from pszSearchIn where beginning of block was found
1632{
1633 ULONG ulrc = 1;
1634 PSZ pszBeginTag = (PSZ)pszSearchIn + *pulSearchOffset,
1635 pszSearch2 = pszBeginTag,
1636 pszClosingTag;
1637 ULONG cbTag = strlen(pszTag);
1638
1639 // go thru the block and check all tags if it's the
1640 // begin tag we're looking for
1641 while ((pszBeginTag = strchr(pszBeginTag, '<')))
1642 {
1643 if (memicmp(pszBeginTag+1, (void*)pszTag, strlen(pszTag)) == 0)
1644 // yes: stop
1645 break;
1646 else
1647 pszBeginTag++;
1648 }
1649
1650 if (pszBeginTag)
1651 {
1652 // we found <TAG>:
1653 ULONG ulNestingLevel = 0;
1654
1655 PSZ pszEndOfBeginTag = strhFindEndOfTag(pszBeginTag);
1656 // strchr(pszBeginTag, '>');
1657 if (pszEndOfBeginTag)
1658 {
1659 // does the caller want the attributes?
1660 if (ppszAttribs)
1661 {
1662 // yes: then copy them
1663 ULONG ulAttrLen = pszEndOfBeginTag - pszBeginTag;
1664 PSZ pszAttrs = (PSZ)malloc(ulAttrLen + 1);
1665 strncpy(pszAttrs, pszBeginTag, ulAttrLen);
1666 // add terminating 0
1667 *(pszAttrs + ulAttrLen) = 0;
1668
1669 *ppszAttribs = pszAttrs;
1670 }
1671
1672 // output offset of where we found the begin tag
1673 if (pulOfsBeginTag)
1674 *pulOfsBeginTag = pszBeginTag - (PSZ)pszSearchIn;
1675
1676 // now find corresponding closing tag (e.g. "</BODY>"
1677 pszBeginTag = pszEndOfBeginTag+1;
1678 // now we're behind the '>' char of the opening tag
1679 // increase offset of that too
1680 if (pulOfsBeginBlock)
1681 *pulOfsBeginBlock = pszBeginTag - (PSZ)pszSearchIn;
1682
1683 // find next closing tag;
1684 // for the first run, pszSearch2 points to right
1685 // after the '>' char of the opening tag
1686 pszSearch2 = pszBeginTag;
1687 while ( (pszSearch2) // fixed V0.9.3 (2000-05-06) [umoeller]
1688 && (pszClosingTag = strstr(pszSearch2, "<"))
1689 )
1690 {
1691 // if we have another opening tag before our closing
1692 // tag, we need to have several closing tags before
1693 // we're done
1694 if (memicmp(pszClosingTag+1, (void*)pszTag, cbTag) == 0)
1695 ulNestingLevel++;
1696 else
1697 {
1698 // is this ours?
1699 if ( (*(pszClosingTag+1) == '/')
1700 && (memicmp(pszClosingTag+2, (void*)pszTag, cbTag) == 0)
1701 )
1702 {
1703 // we've found a matching closing tag; is
1704 // it ours?
1705 if (ulNestingLevel == 0)
1706 {
1707 // our closing tag found:
1708 // allocate mem for a new buffer
1709 // and extract all the text between
1710 // open and closing tags to it
1711 ULONG ulLen = pszClosingTag - pszBeginTag;
1712 if (ppszBlock)
1713 {
1714 PSZ pNew = (PSZ)malloc(ulLen + 1);
1715 strhncpy0(pNew, pszBeginTag, ulLen);
1716 *ppszBlock = pNew;
1717 }
1718
1719 // raise search offset to after the closing tag
1720 *pulSearchOffset = (pszClosingTag + cbTag + 1) - (PSZ)pszSearchIn;
1721
1722 ulrc = 0;
1723
1724 break;
1725 } else
1726 // not our closing tag:
1727 ulNestingLevel--;
1728 }
1729 }
1730 // no matching closing tag: search on after that
1731 pszSearch2 = strhFindEndOfTag(pszClosingTag);
1732 } // end while (pszClosingTag = strstr(pszSearch2, "<"))
1733
1734 if (!pszClosingTag)
1735 // no matching closing tag found:
1736 // return 2 (closing tag not found)
1737 ulrc = 2;
1738 } // end if (pszBeginTag)
1739 else
1740 // no matching ">" for opening tag found:
1741 ulrc = 3;
1742 }
1743
1744 return ulrc;
1745}
1746
1747/* ******************************************************************
1748 *
1749 * Miscellaneous
1750 *
1751 ********************************************************************/
1752
1753/*
1754 *@@ strhArrayAppend:
1755 * this appends a string to a "string array".
1756 *
1757 * A string array is considered a sequence of
1758 * zero-terminated strings in memory. That is,
1759 * after each string's null-byte, the next
1760 * string comes up.
1761 *
1762 * This is useful for composing a single block
1763 * of memory from, say, list box entries, which
1764 * can then be written to OS2.INI in one flush.
1765 *
1766 * To append strings to such an array, call this
1767 * function for each string you wish to append.
1768 * This will re-allocate *ppszRoot with each call,
1769 * and update *pcbRoot, which then contains the
1770 * total size of all strings (including all null
1771 * terminators).
1772 *
1773 * Pass *pcbRoot to PrfSaveProfileData to have the
1774 * block saved.
1775 *
1776 * Note: On the first call, *ppszRoot and *pcbRoot
1777 * _must_ be both NULL, or this crashes.
1778 *
1779 *@@changed V0.9.13 (2001-06-21) [umoeller]: added cbNew
1780 */
1781
1782VOID strhArrayAppend(PSZ *ppszRoot, // in: root of array
1783 const char *pcszNew, // in: string to append
1784 ULONG cbNew, // in: size of that string or 0 to run strlen() here
1785 PULONG pcbRoot) // in/out: size of array
1786{
1787 PSZ pszTemp;
1788
1789 if (!cbNew) // V0.9.13 (2001-06-21) [umoeller]
1790 cbNew = strlen(pcszNew);
1791
1792 pszTemp = (PSZ)malloc(*pcbRoot
1793 + cbNew
1794 + 1); // two null bytes
1795 if (*ppszRoot)
1796 {
1797 // not first loop: copy old stuff
1798 memcpy(pszTemp,
1799 *ppszRoot,
1800 *pcbRoot);
1801 free(*ppszRoot);
1802 }
1803 // append new string
1804 strcpy(pszTemp + *pcbRoot,
1805 pcszNew);
1806 // update root
1807 *ppszRoot = pszTemp;
1808 // update length
1809 *pcbRoot += cbNew + 1;
1810}
1811
1812/*
1813 *@@ strhCreateDump:
1814 * this dumps a memory block into a string
1815 * and returns that string in a new buffer.
1816 *
1817 * You must free() the returned PSZ after use.
1818 *
1819 * The output looks like the following:
1820 *
1821 + 0000: FE FF 0E 02 90 00 00 00 ........
1822 + 0008: FD 01 00 00 57 50 46 6F ....WPFo
1823 + 0010: 6C 64 65 72 00 78 01 34 lder.x.4
1824 *
1825 * Each line is terminated with a newline (\n)
1826 * character only.
1827 *
1828 *@@added V0.9.1 (2000-01-22) [umoeller]
1829 */
1830
1831PSZ strhCreateDump(PBYTE pb, // in: start address of buffer
1832 ULONG ulSize, // in: size of buffer
1833 ULONG ulIndent) // in: indentation of every line
1834{
1835 PSZ pszReturn = 0;
1836 XSTRING strReturn;
1837 CHAR szTemp[1000];
1838
1839 PBYTE pbCurrent = pb; // current byte
1840 ULONG ulCount = 0,
1841 ulCharsInLine = 0; // if this grows > 7, a new line is started
1842 CHAR szLine[400] = "",
1843 szAscii[30] = " "; // ASCII representation; filled for every line
1844 PSZ pszLine = szLine,
1845 pszAscii = szAscii;
1846
1847 xstrInit(&strReturn, (ulSize * 30) + ulIndent);
1848
1849 for (pbCurrent = pb;
1850 ulCount < ulSize;
1851 pbCurrent++, ulCount++)
1852 {
1853 if (ulCharsInLine == 0)
1854 {
1855 memset(szLine, ' ', ulIndent);
1856 pszLine += ulIndent;
1857 }
1858 pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent);
1859
1860 if ( (*pbCurrent > 31) && (*pbCurrent < 127) )
1861 // printable character:
1862 *pszAscii = *pbCurrent;
1863 else
1864 *pszAscii = '.';
1865 pszAscii++;
1866
1867 ulCharsInLine++;
1868 if ( (ulCharsInLine > 7) // 8 bytes added?
1869 || (ulCount == ulSize-1) // end of buffer reached?
1870 )
1871 {
1872 // if we haven't had eight bytes yet,
1873 // fill buffer up to eight bytes with spaces
1874 ULONG ul2;
1875 for (ul2 = ulCharsInLine;
1876 ul2 < 8;
1877 ul2++)
1878 pszLine += sprintf(pszLine, " ");
1879
1880 sprintf(szTemp, "%04lX: %s %s\n",
1881 (ulCount & 0xFFFFFFF8), // offset in hex
1882 szLine, // bytes string
1883 szAscii); // ASCII string
1884 xstrcat(&strReturn, szTemp, 0);
1885
1886 // restart line buffer
1887 pszLine = szLine;
1888
1889 // clear ASCII buffer
1890 strcpy(szAscii, " ");
1891 pszAscii = szAscii;
1892
1893 // reset line counter
1894 ulCharsInLine = 0;
1895 }
1896 }
1897
1898 if (strReturn.cbAllocated)
1899 pszReturn = strReturn.psz;
1900
1901 return pszReturn;
1902}
1903
1904/* ******************************************************************
1905 *
1906 * Fast string searches
1907 *
1908 ********************************************************************/
1909
1910#define ASSERT(a)
1911
1912/*
1913 * The following code has been taken from the "Standard
1914 * Function Library", file sflfind.c, and only slightly
1915 * modified to conform to the rest of this file.
1916 *
1917 * Written: 96/04/24 iMatix SFL project team <sfl@imatix.com>
1918 * Revised: 98/05/04
1919 *
1920 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
1921 *
1922 * The SFL Licence allows incorporating SFL code into other
1923 * programs, as long as the copyright is reprinted and the
1924 * code is marked as modified, so this is what we do.
1925 */
1926
1927/*
1928 *@@ strhmemfind:
1929 * searches for a pattern in a block of memory using the
1930 * Boyer-Moore-Horspool-Sunday algorithm.
1931 *
1932 * The block and pattern may contain any values; you must
1933 * explicitly provide their lengths. If you search for strings,
1934 * use strlen() on the buffers.
1935 *
1936 * Returns a pointer to the pattern if found within the block,
1937 * or NULL if the pattern was not found.
1938 *
1939 * This algorithm needs a "shift table" to cache data for the
1940 * search pattern. This table can be reused when performing
1941 * several searches with the same pattern.
1942 *
1943 * "shift" must point to an array big enough to hold 256 (8**2)
1944 * "size_t" values.
1945 *
1946 * If (*repeat_find == FALSE), the shift table is initialized.
1947 * So on the first search with a given pattern, *repeat_find
1948 * should be FALSE. This function sets it to TRUE after the
1949 * shift table is initialised, allowing the initialisation
1950 * phase to be skipped on subsequent searches.
1951 *
1952 * This function is most effective when repeated searches are
1953 * made for the same pattern in one or more large buffers.
1954 *
1955 * Example:
1956 *
1957 + PSZ pszHaystack = "This is a sample string.",
1958 + pszNeedle = "string";
1959 + size_t shift[256];
1960 + BOOL fRepeat = FALSE;
1961 +
1962 + PSZ pFound = strhmemfind(pszHaystack,
1963 + strlen(pszHaystack), // block size
1964 + pszNeedle,
1965 + strlen(pszNeedle), // pattern size
1966 + shift,
1967 + &fRepeat);
1968 *
1969 * Taken from the "Standard Function Library", file sflfind.c.
1970 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
1971 * Slightly modified by umoeller.
1972 *
1973 *@@added V0.9.3 (2000-05-08) [umoeller]
1974 */
1975
1976void* strhmemfind(const void *in_block, // in: block containing data
1977 size_t block_size, // in: size of block in bytes
1978 const void *in_pattern, // in: pattern to search for
1979 size_t pattern_size, // in: size of pattern block
1980 size_t *shift, // in/out: shift table (search buffer)
1981 BOOL *repeat_find) // in/out: if TRUE, *shift is already initialized
1982{
1983 size_t byte_nbr, // Distance through block
1984 match_size; // Size of matched part
1985 const unsigned char
1986 *match_base = NULL, // Base of match of pattern
1987 *match_ptr = NULL, // Point within current match
1988 *limit = NULL; // Last potiental match point
1989 const unsigned char
1990 *block = (unsigned char *) in_block, // Concrete pointer to block data
1991 *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value
1992
1993 if ( (block == NULL)
1994 || (pattern == NULL)
1995 || (shift == NULL)
1996 )
1997 return NULL;
1998
1999 // Pattern must be smaller or equal in size to string
2000 if (block_size < pattern_size)
2001 return NULL; // Otherwise it's not found
2002
2003 if (pattern_size == 0) // Empty patterns match at start
2004 return (void*)block;
2005
2006 // Build the shift table unless we're continuing a previous search
2007
2008 // The shift table determines how far to shift before trying to match
2009 // again, if a match at this point fails. If the byte after where the
2010 // end of our pattern falls is not in our pattern, then we start to
2011 // match again after that byte; otherwise we line up the last occurence
2012 // of that byte in our pattern under that byte, and try match again.
2013
2014 if (!repeat_find || !*repeat_find)
2015 {
2016 for (byte_nbr = 0;
2017 byte_nbr < 256;
2018 byte_nbr++)
2019 shift[byte_nbr] = pattern_size + 1;
2020 for (byte_nbr = 0;
2021 byte_nbr < pattern_size;
2022 byte_nbr++)
2023 shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr;
2024
2025 if (repeat_find)
2026 *repeat_find = TRUE;
2027 }
2028
2029 // Search for the block, each time jumping up by the amount
2030 // computed in the shift table
2031
2032 limit = block + (block_size - pattern_size + 1);
2033 ASSERT (limit > block);
2034
2035 for (match_base = block;
2036 match_base < limit;
2037 match_base += shift[*(match_base + pattern_size)])
2038 {
2039 match_ptr = match_base;
2040 match_size = 0;
2041
2042 // Compare pattern until it all matches, or we find a difference
2043 while (*match_ptr++ == pattern[match_size++])
2044 {
2045 ASSERT (match_size <= pattern_size &&
2046 match_ptr == (match_base + match_size));
2047
2048 // If we found a match, return the start address
2049 if (match_size >= pattern_size)
2050 return (void*)match_base;
2051
2052 }
2053 }
2054 return NULL; // Found nothing
2055}
2056
2057/*
2058 *@@ strhtxtfind:
2059 * searches for a case-insensitive text pattern in a string
2060 * using the Boyer-Moore-Horspool-Sunday algorithm. The string and
2061 * pattern are null-terminated strings. Returns a pointer to the pattern
2062 * if found within the string, or NULL if the pattern was not found.
2063 * Will match strings irrespective of case. To match exact strings, use
2064 * strhfind(). Will not work on multibyte characters.
2065 *
2066 * Examples:
2067 + char *result;
2068 +
2069 + result = strhtxtfind ("AbracaDabra", "cad");
2070 + if (result)
2071 + puts (result);
2072 +
2073 * Taken from the "Standard Function Library", file sflfind.c.
2074 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
2075 * Slightly modified.
2076 *
2077 *@@added V0.9.3 (2000-05-08) [umoeller]
2078 */
2079
2080char* strhtxtfind (const char *string, // String containing data
2081 const char *pattern) // Pattern to search for
2082{
2083 size_t
2084 shift [256]; // Shift distance for each value
2085 size_t
2086 string_size,
2087 pattern_size,
2088 byte_nbr, // Index into byte array
2089 match_size; // Size of matched part
2090 const char
2091 *match_base = NULL, // Base of match of pattern
2092 *match_ptr = NULL, // Point within current match
2093 *limit = NULL; // Last potiental match point
2094
2095 ASSERT (string); // Expect non-NULL pointers, but
2096 ASSERT (pattern); // fail gracefully if not debugging
2097 if (string == NULL || pattern == NULL)
2098 return NULL;
2099
2100 string_size = strlen (string);
2101 pattern_size = strlen (pattern);
2102
2103 // Pattern must be smaller or equal in size to string
2104 if (string_size < pattern_size)
2105 return NULL; // Otherwise it cannot be found
2106
2107 if (pattern_size == 0) // Empty string matches at start
2108 return (char*)string;
2109
2110 // Build the shift table
2111
2112 // The shift table determines how far to shift before trying to match
2113 // again, if a match at this point fails. If the byte after where the
2114 // end of our pattern falls is not in our pattern, then we start to
2115 // match again after that byte; otherwise we line up the last occurence
2116 // of that byte in our pattern under that byte, and try match again.
2117
2118 for (byte_nbr = 0; byte_nbr < 256; byte_nbr++)
2119 shift [byte_nbr] = pattern_size + 1;
2120
2121 for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++)
2122 shift [(unsigned char) tolower (pattern [byte_nbr])] = pattern_size - byte_nbr;
2123
2124 // Search for the string. If we don't find a match, move up by the
2125 // amount we computed in the shift table above, to find location of
2126 // the next potiental match.
2127
2128 limit = string + (string_size - pattern_size + 1);
2129 ASSERT (limit > string);
2130
2131 for (match_base = string;
2132 match_base < limit;
2133 match_base += shift [(unsigned char) tolower (*(match_base + pattern_size))])
2134 {
2135 match_ptr = match_base;
2136 match_size = 0;
2137
2138 // Compare pattern until it all matches, or we find a difference
2139 while (tolower (*match_ptr++) == tolower (pattern [match_size++]))
2140 {
2141 ASSERT (match_size <= pattern_size &&
2142 match_ptr == (match_base + match_size));
2143
2144 // If we found a match, return the start address
2145 if (match_size >= pattern_size)
2146 return (char*)match_base;
2147 }
2148 }
2149
2150 return NULL; // Found nothing
2151}
2152
2153
Note: See TracBrowser for help on using the repository browser.