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

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

PageMage fixes et al.

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