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

Last change on this file since 127 was 127, checked in by umoeller, 24 years ago

Tons of updates for turbo folders and replacement icons.

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