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

Last change on this file since 240 was 240, checked in by umoeller, 23 years ago

Misc fixes.

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