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

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

Misc fixes.

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