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

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

Misc minor fixes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 58.1 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 *@@changed V0.9.18 (2002-02-23) [umoeller]: fixed end char check
876 */
877
878BOOL strhIsWord(PCSZ pcszBuf,
879 PCSZ p, // in: start of word
880 ULONG cbSearch, // in: length of word
881 PCSZ pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
882 PCSZ pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
883{
884 // check previous char
885 if ( (p == pcszBuf)
886 || (strchr(pcszBeginChars, *(p-1)))
887 )
888 {
889 // OK, valid begin char:
890 // check end char
891 CHAR cNextChar;
892 if (!(cNextChar = p[cbSearch]))
893 // null terminator:
894 return TRUE;
895 else
896 {
897 // not null terminator: check if char is
898 // in the list of valid end chars
899 if (strchr(pcszEndChars, cNextChar))
900 {
901 // OK, is end char: avoid doubles of that char,
902 // but allow spaces
903 // fixed V0.9.18 (2002-02-23) [umoeller]
904 CHAR cNextNext = p[cbSearch + 1];
905 if ( (cNextNext != cNextChar)
906 || (cNextNext == ' ')
907 || (cNextNext == 0)
908 )
909 return TRUE;
910 }
911 }
912 }
913
914 return FALSE;
915}
916
917/*
918 *@@ strhFindWord:
919 * searches for pszSearch in pszBuf, which is
920 * returned if found (or NULL if not).
921 *
922 * As opposed to strstr, this finds pszSearch
923 * only if it is a "word". A search string is
924 * considered a word if the character _before_
925 * it is in pcszBeginChars and the char _after_
926 * it is in pcszEndChars.
927 *
928 * Example:
929 + strhFindWord("This is an example.", "is");
930 + returns ...........^ this, but not the "is" in "This".
931 *
932 * The algorithm here uses strstr to find pszSearch in pszBuf
933 * and performs additional "is-word" checks for each item found
934 * (by calling strhIsWord).
935 *
936 * Note that this function is fairly slow compared to xstrFindWord.
937 *
938 *@@added V0.9.0 (99-11-08) [umoeller]
939 *@@changed V0.9.0 (99-11-10) [umoeller]: tried second algorithm, reverted to original...
940 */
941
942PSZ strhFindWord(PCSZ pszBuf,
943 PCSZ pszSearch,
944 PCSZ pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
945 PCSZ pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
946{
947 PSZ pszReturn = 0;
948 ULONG cbBuf = strlen(pszBuf),
949 cbSearch = strlen(pszSearch);
950
951 if ((cbBuf) && (cbSearch))
952 {
953 PCSZ p = pszBuf;
954
955 do // while p
956 {
957 p = strstr(p, pszSearch);
958 if (p)
959 {
960 // string found:
961 // check if that's a word
962
963 if (strhIsWord(pszBuf,
964 p,
965 cbSearch,
966 pcszBeginChars,
967 pcszEndChars))
968 {
969 // valid end char:
970 pszReturn = (PSZ)p;
971 break;
972 }
973
974 p += cbSearch;
975 }
976 } while (p);
977
978 }
979 return (pszReturn);
980}
981
982/*
983 *@@ strhFindEOL:
984 * returns a pointer to the next \r, \n or null character
985 * following pszSearchIn. Stores the offset in *pulOffset.
986 *
987 * This should never return NULL because at some point,
988 * there will be a null byte in your string.
989 *
990 *@@added V0.9.4 (2000-07-01) [umoeller]
991 */
992
993PSZ strhFindEOL(PCSZ pcszSearchIn, // in: where to search
994 PULONG pulOffset) // out: offset (ptr can be NULL)
995{
996 PCSZ p = pcszSearchIn,
997 prc = 0;
998 while (TRUE)
999 {
1000 if ( (*p == '\r') || (*p == '\n') || (*p == 0) )
1001 {
1002 prc = p;
1003 break;
1004 }
1005 p++;
1006 }
1007
1008 if ((pulOffset) && (prc))
1009 *pulOffset = prc - pcszSearchIn;
1010
1011 return ((PSZ)prc);
1012}
1013
1014/*
1015 *@@ strhFindNextLine:
1016 * like strhFindEOL, but this returns the character
1017 * _after_ \r or \n. Note that this might return
1018 * a pointer to terminating NULL character also.
1019 */
1020
1021PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset)
1022{
1023 PSZ pEOL = strhFindEOL(pszSearchIn, NULL);
1024 // pEOL now points to the \r char or the terminating 0 byte;
1025 // if not null byte, advance pointer
1026 PSZ pNextLine = pEOL;
1027 if (*pNextLine == '\r')
1028 pNextLine++;
1029 if (*pNextLine == '\n')
1030 pNextLine++;
1031 if (pulOffset)
1032 *pulOffset = pNextLine - pszSearchIn;
1033 return (pNextLine);
1034}
1035
1036/*
1037 *@@ strhBeautifyTitle:
1038 * replaces all line breaks (0xd, 0xa) with spaces.
1039 *
1040 *@@changed V0.9.12 (2001-05-17) [pr]: multiple line break chars. end up as only 1 space
1041 */
1042
1043BOOL strhBeautifyTitle(PSZ psz)
1044{
1045 BOOL rc = FALSE;
1046 CHAR *p = psz;
1047
1048 while(*p)
1049 if ( (*p == '\r')
1050 || (*p == '\n')
1051 )
1052 {
1053 rc = TRUE;
1054 if ( (p != psz)
1055 && (p[-1] == ' ')
1056 )
1057 memmove(p, p + 1, strlen(p));
1058 else
1059 *p++ = ' ';
1060 }
1061 else
1062 p++;
1063
1064 return (rc);
1065}
1066
1067/*
1068 * strhFindAttribValue:
1069 * searches for pszAttrib in pszSearchIn; if found,
1070 * returns the first character after the "=" char.
1071 * If "=" is not found, a space, \r, and \n are
1072 * also accepted. This function searches without
1073 * respecting case.
1074 *
1075 * <B>Example:</B>
1076 + strhFindAttribValue("<PAGE BLAH=\"data\">", "BLAH")
1077 +
1078 + returns ....................... ^ this address.
1079 *
1080 *@@added V0.9.0 [umoeller]
1081 *@@changed V0.9.3 (2000-05-19) [umoeller]: some speed optimizations
1082 *@@changed V0.9.12 (2001-05-22) [umoeller]: fixed space bug, thanks Yuri Dario
1083 */
1084
1085PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
1086{
1087 PSZ prc = 0;
1088 PSZ pszSearchIn2, p;
1089 ULONG cbAttrib = strlen(pszAttrib),
1090 ulLength = strlen(pszSearchIn);
1091
1092 // use alloca(), so memory is freed on function exit
1093 pszSearchIn2 = (PSZ)alloca(ulLength + 1);
1094 memcpy(pszSearchIn2, pszSearchIn, ulLength + 1);
1095
1096 // 1) find token, (space char, \n, \r, \t)
1097 p = strtok(pszSearchIn2, " \n\r\t");
1098 while (p)
1099 {
1100 CHAR c2;
1101 PSZ pOrig;
1102
1103 // check tag name
1104 if (!strnicmp(p, pszAttrib, cbAttrib))
1105 {
1106 // position in original string
1107 pOrig = (PSZ)pszSearchIn + (p - pszSearchIn2);
1108
1109 // yes:
1110 prc = pOrig + cbAttrib;
1111 c2 = *prc;
1112 while ( ( (c2 == ' ')
1113 || (c2 == '=')
1114 || (c2 == '\n')
1115 || (c2 == '\r')
1116 )
1117 && (c2 != 0)
1118 )
1119 c2 = *++prc;
1120
1121 break;
1122 }
1123
1124 p = strtok(NULL, " \n\r\t");
1125 }
1126
1127 return (prc);
1128}
1129
1130/* PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
1131{
1132 PSZ prc = 0;
1133 PSZ pszSearchIn2 = (PSZ)pszSearchIn,
1134 p,
1135 p2;
1136 ULONG cbAttrib = strlen(pszAttrib);
1137
1138 // 1) find space char
1139 while ((p = strchr(pszSearchIn2, ' ')))
1140 {
1141 CHAR c;
1142 p++;
1143 if (strlen(p) >= cbAttrib) // V0.9.9 (2001-03-27) [umoeller]
1144 {
1145 c = *(p+cbAttrib); // V0.9.3 (2000-05-19) [umoeller]
1146 // now check whether the p+strlen(pszAttrib)
1147 // is a valid end-of-tag character
1148 if ( (memicmp(p, (PVOID)pszAttrib, cbAttrib) == 0)
1149 && ( (c == ' ')
1150 || (c == '>')
1151 || (c == '=')
1152 || (c == '\r')
1153 || (c == '\n')
1154 || (c == 0)
1155 )
1156 )
1157 {
1158 // yes:
1159 CHAR c2;
1160 p2 = p + cbAttrib;
1161 c2 = *p2;
1162 while ( ( (c2 == ' ')
1163 || (c2 == '=')
1164 || (c2 == '\n')
1165 || (c2 == '\r')
1166 )
1167 && (c2 != 0)
1168 )
1169 c2 = *++p2;
1170
1171 prc = p2;
1172 break; // first while
1173 }
1174 }
1175 else
1176 break;
1177
1178 pszSearchIn2++;
1179 }
1180 return (prc);
1181} */
1182
1183/*
1184 * strhGetNumAttribValue:
1185 * stores the numerical parameter value of an HTML-style
1186 * tag in *pl.
1187 *
1188 * Returns the address of the tag parameter in the
1189 * search buffer, if found, or NULL.
1190 *
1191 * <B>Example:</B>
1192 + strhGetNumAttribValue("<PAGE BLAH=123>, "BLAH", &l);
1193 *
1194 * stores 123 in the "l" variable.
1195 *
1196 *@@added V0.9.0 [umoeller]
1197 *@@changed V0.9.9 (2001-04-04) [umoeller]: this failed on "123" strings in quotes, fixed
1198 */
1199
1200PSZ strhGetNumAttribValue(const char *pszSearchIn, // in: where to search
1201 const char *pszTag, // e.g. "INDEX"
1202 PLONG pl) // out: numerical value
1203{
1204 PSZ pParam;
1205 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
1206 {
1207 if ( (*pParam == '\"')
1208 || (*pParam == '\'')
1209 )
1210 pParam++; // V0.9.9 (2001-04-04) [umoeller]
1211
1212 sscanf(pParam, "%ld", pl);
1213 }
1214
1215 return (pParam);
1216}
1217
1218/*
1219 * strhGetTextAttr:
1220 * retrieves the attribute value of a textual HTML-style tag
1221 * in a newly allocated buffer, which is returned,
1222 * or NULL if attribute not found.
1223 * If an attribute value is to contain spaces, it
1224 * must be enclosed in quotes.
1225 *
1226 * The offset of the attribute data in pszSearchIn is
1227 * returned in *pulOffset so that you can do multiple
1228 * searches.
1229 *
1230 * This returns a new buffer, which should be free()'d after use.
1231 *
1232 * <B>Example:</B>
1233 + ULONG ulOfs = 0;
1234 + strhGetTextAttr("<PAGE BLAH="blublub">, "BLAH", &ulOfs)
1235 + ............^ ulOfs
1236 *
1237 * returns a new string with the value "blublub" (without
1238 * quotes) and sets ulOfs to 12.
1239 *
1240 *@@added V0.9.0 [umoeller]
1241 */
1242
1243PSZ strhGetTextAttr(const char *pszSearchIn,
1244 const char *pszTag,
1245 PULONG pulOffset) // out: offset where found
1246{
1247 PSZ pParam,
1248 pParam2,
1249 prc = NULL;
1250 ULONG ulCount = 0;
1251 LONG lNestingLevel = 0;
1252
1253 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
1254 {
1255 // determine end character to search for: a space
1256 CHAR cEnd = ' ';
1257 if (*pParam == '\"')
1258 {
1259 // or, if the data is enclosed in quotes, a quote
1260 cEnd = '\"';
1261 pParam++;
1262 }
1263
1264 if (pulOffset)
1265 // store the offset
1266 (*pulOffset) = pParam - (PSZ)pszSearchIn;
1267
1268 // now find end of attribute
1269 pParam2 = pParam;
1270 while (*pParam)
1271 {
1272 if (*pParam == cEnd)
1273 // end character found
1274 break;
1275 else if (*pParam == '<')
1276 // yet another opening tag found:
1277 // this is probably some "<" in the attributes
1278 lNestingLevel++;
1279 else if (*pParam == '>')
1280 {
1281 lNestingLevel--;
1282 if (lNestingLevel < 0)
1283 // end of tag found:
1284 break;
1285 }
1286 ulCount++;
1287 pParam++;
1288 }
1289
1290 // copy attribute to new buffer
1291 if (ulCount)
1292 {
1293 prc = (PSZ)malloc(ulCount+1);
1294 memcpy(prc, pParam2, ulCount);
1295 *(prc+ulCount) = 0;
1296 }
1297 }
1298 return (prc);
1299}
1300
1301/*
1302 * strhFindEndOfTag:
1303 * returns a pointer to the ">" char
1304 * which seems to terminate the tag beginning
1305 * after pszBeginOfTag.
1306 *
1307 * If additional "<" chars are found, we look
1308 * for additional ">" characters too.
1309 *
1310 * Note: You must pass the address of the opening
1311 * '<' character to this function.
1312 *
1313 * Example:
1314 + PSZ pszTest = "<BODY ATTR=\"<BODY>\">";
1315 + strhFindEndOfTag(pszTest)
1316 + returns.................................^ this.
1317 *
1318 *@@added V0.9.0 [umoeller]
1319 */
1320
1321PSZ strhFindEndOfTag(const char *pszBeginOfTag)
1322{
1323 PSZ p = (PSZ)pszBeginOfTag,
1324 prc = NULL;
1325 LONG lNestingLevel = 0;
1326
1327 while (*p)
1328 {
1329 if (*p == '<')
1330 // another opening tag found:
1331 lNestingLevel++;
1332 else if (*p == '>')
1333 {
1334 // closing tag found:
1335 lNestingLevel--;
1336 if (lNestingLevel < 1)
1337 {
1338 // corresponding: return this
1339 prc = p;
1340 break;
1341 }
1342 }
1343 p++;
1344 }
1345
1346 return (prc);
1347}
1348
1349/*
1350 * strhGetBlock:
1351 * this complex function searches the given string
1352 * for a pair of opening/closing HTML-style tags.
1353 *
1354 * If found, this routine returns TRUE and does
1355 * the following:
1356 *
1357 * 1) allocate a new buffer, copy the text
1358 * enclosed by the opening/closing tags
1359 * into it and set *ppszBlock to that
1360 * buffer;
1361 *
1362 * 2) if the opening tag has any attributes,
1363 * allocate another buffer, copy the
1364 * attributes into it and set *ppszAttrs
1365 * to that buffer; if no attributes are
1366 * found, *ppszAttrs will be NULL;
1367 *
1368 * 3) set *pulOffset to the offset from the
1369 * beginning of *ppszSearchIn where the
1370 * opening tag was found;
1371 *
1372 * 4) advance *ppszSearchIn to after the
1373 * closing tag, so that you can do
1374 * multiple searches without finding the
1375 * same tags twice.
1376 *
1377 * All buffers should be freed using free().
1378 *
1379 * This returns the following:
1380 * -- 0: no error
1381 * -- 1: tag not found at all (doesn't have to be an error)
1382 * -- 2: begin tag found, but no corresponding end tag found. This
1383 * is a real error.
1384 * -- 3: begin tag is not terminated by "&gt;" (e.g. "&lt;BEGINTAG whatever")
1385 *
1386 * <B>Example:</B>
1387 + PSZ pSearch = "&lt;PAGE INDEX=1&gt;This is page 1.&lt;/PAGE&gt;More text."
1388 + PSZ pszBlock, pszAttrs;
1389 + ULONG ulOfs;
1390 + strhGetBlock(&pSearch, "PAGE", &pszBlock, &pszAttrs, &ulOfs)
1391 *
1392 * would do the following:
1393 *
1394 * 1) set pszBlock to a new string containing "This is page 1."
1395 * without quotes;
1396 *
1397 * 2) set pszAttrs to a new string containing "&lt;PAGE INDEX=1&gt;";
1398 *
1399 * 3) set ulOfs to 0, because "&lt;PAGE" was found at the beginning;
1400 *
1401 * 4) pSearch would be advanced to point to the "More text"
1402 * string in the original buffer.
1403 *
1404 * Hey-hey. A one-shot function, fairly complicated, but indispensable
1405 * for HTML parsing.
1406 *
1407 *@@added V0.9.0 [umoeller]
1408 *@@changed V0.9.1 (2000-01-03) [umoeller]: fixed heap overwrites (thanks to string debugging)
1409 *@@changed V0.9.1 (2000-01-06) [umoeller]: changed prototype
1410 *@@changed V0.9.3 (2000-05-06) [umoeller]: NULL string check was missing
1411 */
1412
1413ULONG strhGetBlock(const char *pszSearchIn, // in: buffer to search
1414 PULONG pulSearchOffset, // in/out: offset where to start search (0 for beginning)
1415 PSZ pszTag,
1416 PSZ *ppszBlock, // out: block enclosed by the tags
1417 PSZ *ppszAttribs, // out: attributes of the opening tag
1418 PULONG pulOfsBeginTag, // out: offset from pszSearchIn where opening tag was found
1419 PULONG pulOfsBeginBlock) // out: offset from pszSearchIn where beginning of block was found
1420{
1421 ULONG ulrc = 1;
1422 PSZ pszBeginTag = (PSZ)pszSearchIn + *pulSearchOffset,
1423 pszSearch2 = pszBeginTag,
1424 pszClosingTag;
1425 ULONG cbTag = strlen(pszTag);
1426
1427 // go thru the block and check all tags if it's the
1428 // begin tag we're looking for
1429 while ((pszBeginTag = strchr(pszBeginTag, '<')))
1430 {
1431 if (memicmp(pszBeginTag+1, pszTag, strlen(pszTag)) == 0)
1432 // yes: stop
1433 break;
1434 else
1435 pszBeginTag++;
1436 }
1437
1438 if (pszBeginTag)
1439 {
1440 // we found <TAG>:
1441 ULONG ulNestingLevel = 0;
1442
1443 PSZ pszEndOfBeginTag = strhFindEndOfTag(pszBeginTag);
1444 // strchr(pszBeginTag, '>');
1445 if (pszEndOfBeginTag)
1446 {
1447 // does the caller want the attributes?
1448 if (ppszAttribs)
1449 {
1450 // yes: then copy them
1451 ULONG ulAttrLen = pszEndOfBeginTag - pszBeginTag;
1452 PSZ pszAttrs = (PSZ)malloc(ulAttrLen + 1);
1453 strncpy(pszAttrs, pszBeginTag, ulAttrLen);
1454 // add terminating 0
1455 *(pszAttrs + ulAttrLen) = 0;
1456
1457 *ppszAttribs = pszAttrs;
1458 }
1459
1460 // output offset of where we found the begin tag
1461 if (pulOfsBeginTag)
1462 *pulOfsBeginTag = pszBeginTag - (PSZ)pszSearchIn;
1463
1464 // now find corresponding closing tag (e.g. "</BODY>"
1465 pszBeginTag = pszEndOfBeginTag+1;
1466 // now we're behind the '>' char of the opening tag
1467 // increase offset of that too
1468 if (pulOfsBeginBlock)
1469 *pulOfsBeginBlock = pszBeginTag - (PSZ)pszSearchIn;
1470
1471 // find next closing tag;
1472 // for the first run, pszSearch2 points to right
1473 // after the '>' char of the opening tag
1474 pszSearch2 = pszBeginTag;
1475 while ( (pszSearch2) // fixed V0.9.3 (2000-05-06) [umoeller]
1476 && (pszClosingTag = strstr(pszSearch2, "<"))
1477 )
1478 {
1479 // if we have another opening tag before our closing
1480 // tag, we need to have several closing tags before
1481 // we're done
1482 if (memicmp(pszClosingTag+1, pszTag, cbTag) == 0)
1483 ulNestingLevel++;
1484 else
1485 {
1486 // is this ours?
1487 if ( (*(pszClosingTag+1) == '/')
1488 && (memicmp(pszClosingTag+2, pszTag, cbTag) == 0)
1489 )
1490 {
1491 // we've found a matching closing tag; is
1492 // it ours?
1493 if (ulNestingLevel == 0)
1494 {
1495 // our closing tag found:
1496 // allocate mem for a new buffer
1497 // and extract all the text between
1498 // open and closing tags to it
1499 ULONG ulLen = pszClosingTag - pszBeginTag;
1500 if (ppszBlock)
1501 {
1502 PSZ pNew = (PSZ)malloc(ulLen + 1);
1503 strhncpy0(pNew, pszBeginTag, ulLen);
1504 *ppszBlock = pNew;
1505 }
1506
1507 // raise search offset to after the closing tag
1508 *pulSearchOffset = (pszClosingTag + cbTag + 1) - (PSZ)pszSearchIn;
1509
1510 ulrc = 0;
1511
1512 break;
1513 } else
1514 // not our closing tag:
1515 ulNestingLevel--;
1516 }
1517 }
1518 // no matching closing tag: search on after that
1519 pszSearch2 = strhFindEndOfTag(pszClosingTag);
1520 } // end while (pszClosingTag = strstr(pszSearch2, "<"))
1521
1522 if (!pszClosingTag)
1523 // no matching closing tag found:
1524 // return 2 (closing tag not found)
1525 ulrc = 2;
1526 } // end if (pszBeginTag)
1527 else
1528 // no matching ">" for opening tag found:
1529 ulrc = 3;
1530 }
1531
1532 return (ulrc);
1533}
1534
1535/* ******************************************************************
1536 *
1537 * Miscellaneous
1538 *
1539 ********************************************************************/
1540
1541/*
1542 *@@ strhArrayAppend:
1543 * this appends a string to a "string array".
1544 *
1545 * A string array is considered a sequence of
1546 * zero-terminated strings in memory. That is,
1547 * after each string's null-byte, the next
1548 * string comes up.
1549 *
1550 * This is useful for composing a single block
1551 * of memory from, say, list box entries, which
1552 * can then be written to OS2.INI in one flush.
1553 *
1554 * To append strings to such an array, call this
1555 * function for each string you wish to append.
1556 * This will re-allocate *ppszRoot with each call,
1557 * and update *pcbRoot, which then contains the
1558 * total size of all strings (including all null
1559 * terminators).
1560 *
1561 * Pass *pcbRoot to PrfSaveProfileData to have the
1562 * block saved.
1563 *
1564 * Note: On the first call, *ppszRoot and *pcbRoot
1565 * _must_ be both NULL, or this crashes.
1566 *
1567 *@@changed V0.9.13 (2001-06-21) [umoeller]: added cbNew
1568 */
1569
1570VOID strhArrayAppend(PSZ *ppszRoot, // in: root of array
1571 const char *pcszNew, // in: string to append
1572 ULONG cbNew, // in: size of that string or 0 to run strlen() here
1573 PULONG pcbRoot) // in/out: size of array
1574{
1575 PSZ pszTemp;
1576
1577 if (!cbNew) // V0.9.13 (2001-06-21) [umoeller]
1578 cbNew = strlen(pcszNew);
1579
1580 pszTemp = (PSZ)malloc(*pcbRoot
1581 + cbNew
1582 + 1); // two null bytes
1583 if (*ppszRoot)
1584 {
1585 // not first loop: copy old stuff
1586 memcpy(pszTemp,
1587 *ppszRoot,
1588 *pcbRoot);
1589 free(*ppszRoot);
1590 }
1591 // append new string
1592 strcpy(pszTemp + *pcbRoot,
1593 pcszNew);
1594 // update root
1595 *ppszRoot = pszTemp;
1596 // update length
1597 *pcbRoot += cbNew + 1;
1598}
1599
1600/*
1601 *@@ strhCreateDump:
1602 * this dumps a memory block into a string
1603 * and returns that string in a new buffer.
1604 *
1605 * You must free() the returned PSZ after use.
1606 *
1607 * The output looks like the following:
1608 *
1609 + 0000: FE FF 0E 02 90 00 00 00 ........
1610 + 0008: FD 01 00 00 57 50 46 6F ....WPFo
1611 + 0010: 6C 64 65 72 00 78 01 34 lder.x.4
1612 *
1613 * Each line is terminated with a newline (\n)
1614 * character only.
1615 *
1616 *@@added V0.9.1 (2000-01-22) [umoeller]
1617 */
1618
1619PSZ strhCreateDump(PBYTE pb, // in: start address of buffer
1620 ULONG ulSize, // in: size of buffer
1621 ULONG ulIndent) // in: indentation of every line
1622{
1623 PSZ pszReturn = 0;
1624 XSTRING strReturn;
1625 CHAR szTemp[1000];
1626
1627 PBYTE pbCurrent = pb; // current byte
1628 ULONG ulCount = 0,
1629 ulCharsInLine = 0; // if this grows > 7, a new line is started
1630 CHAR szLine[400] = "",
1631 szAscii[30] = " "; // ASCII representation; filled for every line
1632 PSZ pszLine = szLine,
1633 pszAscii = szAscii;
1634
1635 xstrInit(&strReturn, (ulSize * 30) + ulIndent);
1636
1637 for (pbCurrent = pb;
1638 ulCount < ulSize;
1639 pbCurrent++, ulCount++)
1640 {
1641 if (ulCharsInLine == 0)
1642 {
1643 memset(szLine, ' ', ulIndent);
1644 pszLine += ulIndent;
1645 }
1646 pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent);
1647
1648 if ( (*pbCurrent > 31) && (*pbCurrent < 127) )
1649 // printable character:
1650 *pszAscii = *pbCurrent;
1651 else
1652 *pszAscii = '.';
1653 pszAscii++;
1654
1655 ulCharsInLine++;
1656 if ( (ulCharsInLine > 7) // 8 bytes added?
1657 || (ulCount == ulSize-1) // end of buffer reached?
1658 )
1659 {
1660 // if we haven't had eight bytes yet,
1661 // fill buffer up to eight bytes with spaces
1662 ULONG ul2;
1663 for (ul2 = ulCharsInLine;
1664 ul2 < 8;
1665 ul2++)
1666 pszLine += sprintf(pszLine, " ");
1667
1668 sprintf(szTemp, "%04lX: %s %s\n",
1669 (ulCount & 0xFFFFFFF8), // offset in hex
1670 szLine, // bytes string
1671 szAscii); // ASCII string
1672 xstrcat(&strReturn, szTemp, 0);
1673
1674 // restart line buffer
1675 pszLine = szLine;
1676
1677 // clear ASCII buffer
1678 strcpy(szAscii, " ");
1679 pszAscii = szAscii;
1680
1681 // reset line counter
1682 ulCharsInLine = 0;
1683 }
1684 }
1685
1686 if (strReturn.cbAllocated)
1687 pszReturn = strReturn.psz;
1688
1689 return (pszReturn);
1690}
1691
1692/* ******************************************************************
1693 *
1694 * Fast string searches
1695 *
1696 ********************************************************************/
1697
1698#define ASSERT(a)
1699
1700/*
1701 * The following code has been taken from the "Standard
1702 * Function Library", file sflfind.c, and only slightly
1703 * modified to conform to the rest of this file.
1704 *
1705 * Written: 96/04/24 iMatix SFL project team <sfl@imatix.com>
1706 * Revised: 98/05/04
1707 *
1708 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
1709 *
1710 * The SFL Licence allows incorporating SFL code into other
1711 * programs, as long as the copyright is reprinted and the
1712 * code is marked as modified, so this is what we do.
1713 */
1714
1715/*
1716 *@@ strhmemfind:
1717 * searches for a pattern in a block of memory using the
1718 * Boyer-Moore-Horspool-Sunday algorithm.
1719 *
1720 * The block and pattern may contain any values; you must
1721 * explicitly provide their lengths. If you search for strings,
1722 * use strlen() on the buffers.
1723 *
1724 * Returns a pointer to the pattern if found within the block,
1725 * or NULL if the pattern was not found.
1726 *
1727 * This algorithm needs a "shift table" to cache data for the
1728 * search pattern. This table can be reused when performing
1729 * several searches with the same pattern.
1730 *
1731 * "shift" must point to an array big enough to hold 256 (8**2)
1732 * "size_t" values.
1733 *
1734 * If (*repeat_find == FALSE), the shift table is initialized.
1735 * So on the first search with a given pattern, *repeat_find
1736 * should be FALSE. This function sets it to TRUE after the
1737 * shift table is initialised, allowing the initialisation
1738 * phase to be skipped on subsequent searches.
1739 *
1740 * This function is most effective when repeated searches are
1741 * made for the same pattern in one or more large buffers.
1742 *
1743 * Example:
1744 *
1745 + PSZ pszHaystack = "This is a sample string.",
1746 + pszNeedle = "string";
1747 + size_t shift[256];
1748 + BOOL fRepeat = FALSE;
1749 +
1750 + PSZ pFound = strhmemfind(pszHaystack,
1751 + strlen(pszHaystack), // block size
1752 + pszNeedle,
1753 + strlen(pszNeedle), // pattern size
1754 + shift,
1755 + &fRepeat);
1756 *
1757 * Taken from the "Standard Function Library", file sflfind.c.
1758 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
1759 * Slightly modified by umoeller.
1760 *
1761 *@@added V0.9.3 (2000-05-08) [umoeller]
1762 */
1763
1764void* strhmemfind(const void *in_block, // in: block containing data
1765 size_t block_size, // in: size of block in bytes
1766 const void *in_pattern, // in: pattern to search for
1767 size_t pattern_size, // in: size of pattern block
1768 size_t *shift, // in/out: shift table (search buffer)
1769 BOOL *repeat_find) // in/out: if TRUE, *shift is already initialized
1770{
1771 size_t byte_nbr, // Distance through block
1772 match_size; // Size of matched part
1773 const unsigned char
1774 *match_base = NULL, // Base of match of pattern
1775 *match_ptr = NULL, // Point within current match
1776 *limit = NULL; // Last potiental match point
1777 const unsigned char
1778 *block = (unsigned char *) in_block, // Concrete pointer to block data
1779 *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value
1780
1781 if ( (block == NULL)
1782 || (pattern == NULL)
1783 || (shift == NULL)
1784 )
1785 return (NULL);
1786
1787 // Pattern must be smaller or equal in size to string
1788 if (block_size < pattern_size)
1789 return (NULL); // Otherwise it's not found
1790
1791 if (pattern_size == 0) // Empty patterns match at start
1792 return ((void *)block);
1793
1794 // Build the shift table unless we're continuing a previous search
1795
1796 // The shift table determines how far to shift before trying to match
1797 // again, if a match at this point fails. If the byte after where the
1798 // end of our pattern falls is not in our pattern, then we start to
1799 // match again after that byte; otherwise we line up the last occurence
1800 // of that byte in our pattern under that byte, and try match again.
1801
1802 if (!repeat_find || !*repeat_find)
1803 {
1804 for (byte_nbr = 0;
1805 byte_nbr < 256;
1806 byte_nbr++)
1807 shift[byte_nbr] = pattern_size + 1;
1808 for (byte_nbr = 0;
1809 byte_nbr < pattern_size;
1810 byte_nbr++)
1811 shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr;
1812
1813 if (repeat_find)
1814 *repeat_find = TRUE;
1815 }
1816
1817 // Search for the block, each time jumping up by the amount
1818 // computed in the shift table
1819
1820 limit = block + (block_size - pattern_size + 1);
1821 ASSERT (limit > block);
1822
1823 for (match_base = block;
1824 match_base < limit;
1825 match_base += shift[*(match_base + pattern_size)])
1826 {
1827 match_ptr = match_base;
1828 match_size = 0;
1829
1830 // Compare pattern until it all matches, or we find a difference
1831 while (*match_ptr++ == pattern[match_size++])
1832 {
1833 ASSERT (match_size <= pattern_size &&
1834 match_ptr == (match_base + match_size));
1835
1836 // If we found a match, return the start address
1837 if (match_size >= pattern_size)
1838 return ((void*)(match_base));
1839
1840 }
1841 }
1842 return (NULL); // Found nothing
1843}
1844
1845/*
1846 *@@ strhtxtfind:
1847 * searches for a case-insensitive text pattern in a string
1848 * using the Boyer-Moore-Horspool-Sunday algorithm. The string and
1849 * pattern are null-terminated strings. Returns a pointer to the pattern
1850 * if found within the string, or NULL if the pattern was not found.
1851 * Will match strings irrespective of case. To match exact strings, use
1852 * strhfind(). Will not work on multibyte characters.
1853 *
1854 * Examples:
1855 + char *result;
1856 +
1857 + result = strhtxtfind ("AbracaDabra", "cad");
1858 + if (result)
1859 + puts (result);
1860 +
1861 * Taken from the "Standard Function Library", file sflfind.c.
1862 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
1863 * Slightly modified.
1864 *
1865 *@@added V0.9.3 (2000-05-08) [umoeller]
1866 */
1867
1868char* strhtxtfind (const char *string, // String containing data
1869 const char *pattern) // Pattern to search for
1870{
1871 size_t
1872 shift [256]; // Shift distance for each value
1873 size_t
1874 string_size,
1875 pattern_size,
1876 byte_nbr, // Index into byte array
1877 match_size; // Size of matched part
1878 const char
1879 *match_base = NULL, // Base of match of pattern
1880 *match_ptr = NULL, // Point within current match
1881 *limit = NULL; // Last potiental match point
1882
1883 ASSERT (string); // Expect non-NULL pointers, but
1884 ASSERT (pattern); // fail gracefully if not debugging
1885 if (string == NULL || pattern == NULL)
1886 return (NULL);
1887
1888 string_size = strlen (string);
1889 pattern_size = strlen (pattern);
1890
1891 // Pattern must be smaller or equal in size to string
1892 if (string_size < pattern_size)
1893 return (NULL); // Otherwise it cannot be found
1894
1895 if (pattern_size == 0) // Empty string matches at start
1896 return (char *) string;
1897
1898 // Build the shift table
1899
1900 // The shift table determines how far to shift before trying to match
1901 // again, if a match at this point fails. If the byte after where the
1902 // end of our pattern falls is not in our pattern, then we start to
1903 // match again after that byte; otherwise we line up the last occurence
1904 // of that byte in our pattern under that byte, and try match again.
1905
1906 for (byte_nbr = 0; byte_nbr < 256; byte_nbr++)
1907 shift [byte_nbr] = pattern_size + 1;
1908
1909 for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++)
1910 shift [(unsigned char) tolower (pattern [byte_nbr])] = pattern_size - byte_nbr;
1911
1912 // Search for the string. If we don't find a match, move up by the
1913 // amount we computed in the shift table above, to find location of
1914 // the next potiental match.
1915
1916 limit = string + (string_size - pattern_size + 1);
1917 ASSERT (limit > string);
1918
1919 for (match_base = string;
1920 match_base < limit;
1921 match_base += shift [(unsigned char) tolower (*(match_base + pattern_size))])
1922 {
1923 match_ptr = match_base;
1924 match_size = 0;
1925
1926 // Compare pattern until it all matches, or we find a difference
1927 while (tolower (*match_ptr++) == tolower (pattern [match_size++]))
1928 {
1929 ASSERT (match_size <= pattern_size &&
1930 match_ptr == (match_base + match_size));
1931
1932 // If we found a match, return the start address
1933 if (match_size >= pattern_size)
1934 return ((char *)(match_base));
1935 }
1936 }
1937 return (NULL); // Found nothing
1938}
1939
Note: See TracBrowser for help on using the repository browser.