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

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

Sources as of 0.9.18.

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