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

Last change on this file since 48 was 38, checked in by umoeller, 25 years ago

Updates to XML.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 77.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#include <os2.h>
45
46#include <stdlib.h>
47#include <stdio.h>
48#include <string.h>
49#include <ctype.h>
50#include <math.h>
51
52#include "setup.h" // code generation and debugging options
53
54#include "helpers\stringh.h"
55#include "helpers\xstring.h" // extended string helpers
56
57#pragma hdrstop
58
59/*
60 *@@category: Helpers\C helpers\String management
61 * See stringh.c and xstring.c.
62 */
63
64/*
65 *@@category: Helpers\C helpers\String management\C string helpers
66 * See stringh.c.
67 */
68
69/*
70 *@@ strhdup:
71 * like strdup, but this one
72 * doesn't crash if pszSource is NULL,
73 * but returns NULL also.
74 *
75 *@@added V0.9.0 [umoeller]
76 */
77
78PSZ strhdup(const char *pszSource)
79{
80 if (pszSource)
81 return (strdup(pszSource));
82 else
83 return (0);
84}
85
86/*
87 *@@ strhcmp:
88 * better strcmp. This doesn't crash if any of the
89 * string pointers are NULL, but returns a proper
90 * value then.
91 *
92 * Besides, this is guaranteed to only return -1, 0,
93 * or +1, while strcmp can return any positive or
94 * negative value.
95 *
96 *@@added V0.9.9 (2001-02-16) [umoeller]
97 */
98
99int strhcmp(const char *p1, const char *p2)
100{
101 if (p1 && p2)
102 {
103 int i = strcmp(p1, p2);
104 if (i < 0) return (-1);
105 if (i > 0) return (+1);
106 }
107 else if (p1)
108 // but p2 is NULL: p1 greater than p2 then
109 return (+1);
110 else if (p2)
111 // but p1 is NULL: p1 less than p2 then
112 return (-1);
113
114 // return 0 if strcmp returned 0 above or both strings are NULL
115 return (0);
116}
117
118/*
119 *@@ strhistr:
120 * like strstr, but case-insensitive.
121 *
122 *@@changed V0.9.0 [umoeller]: crashed if null pointers were passed, thanks Rdiger Ihle
123 */
124
125PSZ strhistr(const char *string1, const char *string2)
126{
127 PSZ prc = NULL;
128
129 if ((string1) && (string2))
130 {
131 PSZ pszSrchIn = strdup(string1);
132 PSZ pszSrchFor = strdup(string2);
133
134 if ((pszSrchIn) && (pszSrchFor))
135 {
136 strupr(pszSrchIn);
137 strupr(pszSrchFor);
138
139 prc = strstr(pszSrchIn, pszSrchFor);
140 if (prc)
141 {
142 // prc now has the first occurence of the string,
143 // but in pszSrchIn; we need to map this
144 // return value to the original string
145 prc = (prc-pszSrchIn) // offset in pszSrchIn
146 + (PSZ)string1;
147 }
148 }
149 if (pszSrchFor)
150 free(pszSrchFor);
151 if (pszSrchIn)
152 free(pszSrchIn);
153 }
154 return (prc);
155}
156
157/*
158 *@@ strhncpy0:
159 * like strncpy, but always appends a 0 character.
160 */
161
162ULONG strhncpy0(PSZ pszTarget,
163 const char *pszSource,
164 ULONG cbSource)
165{
166 ULONG ul = 0;
167 PSZ pTarget = pszTarget,
168 pSource = (PSZ)pszSource;
169
170 for (ul = 0; ul < cbSource; ul++)
171 if (*pSource)
172 *pTarget++ = *pSource++;
173 else
174 break;
175 *pTarget = 0;
176
177 return (ul);
178}
179
180/*
181 * strhCount:
182 * this counts the occurences of c in pszSearch.
183 */
184
185ULONG strhCount(const char *pszSearch,
186 CHAR c)
187{
188 PSZ p = (PSZ)pszSearch;
189 ULONG ulCount = 0;
190 while (TRUE)
191 {
192 p = strchr(p, c);
193 if (p)
194 {
195 ulCount++;
196 p++;
197 }
198 else
199 break;
200 }
201 return (ulCount);
202}
203
204/*
205 *@@ strhIsDecimal:
206 * returns TRUE if psz consists of decimal digits only.
207 */
208
209BOOL strhIsDecimal(PSZ psz)
210{
211 PSZ p = psz;
212 while (*p != 0)
213 {
214 if (isdigit(*p) == 0)
215 return (FALSE);
216 p++;
217 }
218
219 return (TRUE);
220}
221
222/*
223 *@@ strhSubstr:
224 * this creates a new PSZ containing the string
225 * from pBegin to pEnd, excluding the pEnd character.
226 * The new string is null-terminated. The caller
227 * must free() the new string after use.
228 *
229 * Example:
230 + "1234567890"
231 + ^ ^
232 + p1 p2
233 + strhSubstr(p1, p2)
234 * would return a new string containing "2345678".
235 */
236
237PSZ strhSubstr(const char *pBegin, const char *pEnd)
238{
239 ULONG cbSubstr = (pEnd - pBegin);
240 PSZ pszSubstr = (PSZ)malloc(cbSubstr + 1);
241 strhncpy0(pszSubstr, pBegin, cbSubstr);
242 return (pszSubstr);
243}
244
245/*
246 *@@ strhExtract:
247 * searches pszBuf for the cOpen character and returns
248 * the data in between cOpen and cClose, excluding
249 * those two characters, in a newly allocated buffer
250 * which you must free() afterwards.
251 *
252 * Spaces and newlines/linefeeds are skipped.
253 *
254 * If the search was successful, the new buffer
255 * is returned and, if (ppEnd != NULL), *ppEnd points
256 * to the first character after the cClose character
257 * found in the buffer.
258 *
259 * If the search was not successful, NULL is
260 * returned, and *ppEnd is unchanged.
261 *
262 * If another cOpen character is found before
263 * cClose, matching cClose characters will be skipped.
264 * You can therefore nest the cOpen and cClose
265 * characters.
266 *
267 * This function ignores cOpen and cClose characters
268 * in C-style comments and strings surrounded by
269 * double quotes.
270 *
271 * Example:
272 + PSZ pszBuf = "KEYWORD { --blah-- } next",
273 + pEnd;
274 + strhExtract(pszBuf,
275 + '{', '}',
276 + &pEnd)
277 * would return a new buffer containing " --blah-- ",
278 * and ppEnd would afterwards point to the space
279 * before "next" in the static buffer.
280 *
281 *@@added V0.9.0 [umoeller]
282 */
283
284PSZ strhExtract(PSZ pszBuf, // in: search buffer
285 CHAR cOpen, // in: opening char
286 CHAR cClose, // in: closing char
287 PSZ *ppEnd) // out: if != NULL, receives first character after closing char
288{
289 PSZ pszReturn = NULL;
290
291 if (pszBuf)
292 {
293 PSZ pOpen = strchr(pszBuf, cOpen);
294 if (pOpen)
295 {
296 // opening char found:
297 // now go thru the whole rest of the buffer
298 PSZ p = pOpen+1;
299 LONG lLevel = 1; // if this goes 0, we're done
300 while (*p)
301 {
302 if (*p == cOpen)
303 lLevel++;
304 else if (*p == cClose)
305 {
306 lLevel--;
307 if (lLevel <= 0)
308 {
309 // matching closing bracket found:
310 // extract string
311 pszReturn = strhSubstr(pOpen+1, // after cOpen
312 p); // excluding cClose
313 if (ppEnd)
314 *ppEnd = p+1;
315 break; // while (*p)
316 }
317 }
318 else if (*p == '\"')
319 {
320 // beginning of string:
321 PSZ p2 = p+1;
322 // find end of string
323 while ((*p2) && (*p2 != '\"'))
324 p2++;
325
326 if (*p2 == '\"')
327 // closing quote found:
328 // search on after that
329 p = p2; // raised below
330 else
331 break; // while (*p)
332 }
333
334 p++;
335 }
336 }
337 }
338
339 return (pszReturn);
340}
341
342/*
343 *@@ strhQuote:
344 * similar to strhExtract, except that
345 * opening and closing chars are the same,
346 * and therefore no nesting is possible.
347 * Useful for extracting stuff between
348 * quotes.
349 *
350 *@@added V0.9.0 [umoeller]
351 */
352
353PSZ strhQuote(PSZ pszBuf,
354 CHAR cQuote,
355 PSZ *ppEnd)
356{
357 PSZ pszReturn = NULL,
358 p1 = NULL;
359 if ((p1 = strchr(pszBuf, cQuote)))
360 {
361 PSZ p2 = strchr(p1+1, cQuote);
362 if (p2)
363 {
364 pszReturn = strhSubstr(p1+1, p2);
365 if (ppEnd)
366 // store closing char
367 *ppEnd = p2 + 1;
368 }
369 }
370
371 return (pszReturn);
372}
373
374/*
375 *@@ strhStrip:
376 * removes all double spaces.
377 * This copies within the "psz" buffer.
378 * If any double spaces are found, the
379 * string will be shorter than before,
380 * but the buffer is _not_ reallocated,
381 * so there will be unused bytes at the
382 * end.
383 *
384 * Returns the number of spaces removed.
385 *
386 *@@added V0.9.0 [umoeller]
387 */
388
389ULONG strhStrip(PSZ psz) // in/out: string
390{
391 PSZ p;
392 ULONG cb = strlen(psz),
393 ulrc = 0;
394
395 for (p = psz; p < psz+cb; p++)
396 {
397 if ((*p == ' ') && (*(p+1) == ' '))
398 {
399 PSZ p2 = p;
400 while (*p2)
401 {
402 *p2 = *(p2+1);
403 p2++;
404 }
405 cb--;
406 p--;
407 ulrc++;
408 }
409 }
410 return (ulrc);
411}
412
413/*
414 *@@ strhins:
415 * this inserts one string into another.
416 *
417 * pszInsert is inserted into pszBuffer at offset
418 * ulInsertOfs (which counts from 0).
419 *
420 * A newly allocated string is returned. pszBuffer is
421 * not changed. The new string should be free()'d after
422 * use.
423 *
424 * Upon errors, NULL is returned.
425 *
426 *@@changed V0.9.0 [umoeller]: completely rewritten.
427 */
428
429PSZ strhins(const char *pcszBuffer,
430 ULONG ulInsertOfs,
431 const char *pcszInsert)
432{
433 PSZ pszNew = NULL;
434
435 if ((pcszBuffer) && (pcszInsert))
436 {
437 do {
438 ULONG cbBuffer = strlen(pcszBuffer);
439 ULONG cbInsert = strlen(pcszInsert);
440
441 // check string length
442 if (ulInsertOfs > cbBuffer + 1)
443 break; // do
444
445 // OK, let's go.
446 pszNew = (PSZ)malloc(cbBuffer + cbInsert + 1); // additional null terminator
447
448 // copy stuff before pInsertPos
449 memcpy(pszNew,
450 pcszBuffer,
451 ulInsertOfs);
452 // copy string to be inserted
453 memcpy(pszNew + ulInsertOfs,
454 pcszInsert,
455 cbInsert);
456 // copy stuff after pInsertPos
457 strcpy(pszNew + ulInsertOfs + cbInsert,
458 pcszBuffer + ulInsertOfs);
459 } while (FALSE);
460 }
461
462 return (pszNew);
463}
464
465/*
466 *@@ strhFindReplace:
467 * wrapper around xstrFindReplace to work with C strings.
468 * Note that *ppszBuf can get reallocated and must
469 * be free()'able.
470 *
471 * Repetitive use of this wrapper is not recommended
472 * because it is considerably slower than xstrFindReplace.
473 *
474 *@@added V0.9.6 (2000-11-01) [umoeller]
475 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from strhrpl
476 */
477
478ULONG strhFindReplace(PSZ *ppszBuf, // in/out: string
479 PULONG pulOfs, // in: where to begin search (0 = start);
480 // out: ofs of first char after replacement string
481 const char *pcszSearch, // in: search string; cannot be NULL
482 const char *pcszReplace) // in: replacement string; cannot be NULL
483{
484 ULONG ulrc = 0;
485 XSTRING xstrBuf,
486 xstrFind,
487 xstrReplace;
488 size_t ShiftTable[256];
489 BOOL fRepeat = FALSE;
490 xstrInitSet(&xstrBuf, *ppszBuf);
491 // reallocated and returned, so we're safe
492 xstrInitSet(&xstrFind, (PSZ)pcszSearch);
493 xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
494 // these two are never freed, so we're safe too
495
496 if ((ulrc = xstrFindReplace(&xstrBuf,
497 pulOfs,
498 &xstrFind,
499 &xstrReplace,
500 ShiftTable,
501 &fRepeat)))
502 // replaced:
503 *ppszBuf = xstrBuf.psz;
504
505 return (ulrc);
506}
507
508/*
509 * strhWords:
510 * returns the no. of words in "psz".
511 * A string is considered a "word" if
512 * it is surrounded by spaces only.
513 *
514 *@@added V0.9.0 [umoeller]
515 */
516
517ULONG strhWords(PSZ psz)
518{
519 PSZ p;
520 ULONG cb = strlen(psz),
521 ulWords = 0;
522 if (cb > 1)
523 {
524 ulWords = 1;
525 for (p = psz; p < psz+cb; p++)
526 if (*p == ' ')
527 ulWords++;
528 }
529 return (ulWords);
530}
531
532/*
533 *@@ strhThousandsULong:
534 * converts a ULONG into a decimal string, while
535 * inserting thousands separators into it. Specify
536 * the separator character in cThousands.
537 *
538 * Returns pszTarget so you can use it directly
539 * with sprintf and the "%s" flag.
540 *
541 * For cThousands, you should use the data in
542 * OS2.INI ("PM_National" application), which is
543 * always set according to the "Country" object.
544 * You can use prfhQueryCountrySettings to
545 * retrieve this setting.
546 *
547 * Use strhThousandsDouble for "double" values.
548 */
549
550PSZ strhThousandsULong(PSZ pszTarget, // out: decimal as string
551 ULONG ul, // in: decimal to convert
552 CHAR cThousands) // in: separator char (e.g. '.')
553{
554 USHORT ust, uss, usc;
555 CHAR szTemp[40];
556 sprintf(szTemp, "%lu", ul);
557
558 ust = 0;
559 usc = strlen(szTemp);
560 for (uss = 0; uss < usc; uss++)
561 {
562 if (uss)
563 if (((usc - uss) % 3) == 0)
564 {
565 pszTarget[ust] = cThousands;
566 ust++;
567 }
568 pszTarget[ust] = szTemp[uss];
569 ust++;
570 }
571 pszTarget[ust] = '\0';
572
573 return (pszTarget);
574}
575
576/*
577 *@@ strhThousandsDouble:
578 * like strhThousandsULong, but for a "double"
579 * value. Note that after-comma values are truncated.
580 */
581
582PSZ strhThousandsDouble(PSZ pszTarget, double dbl, CHAR cThousands)
583{
584 USHORT ust, uss, usc;
585 CHAR szTemp[40];
586 sprintf(szTemp, "%.0f", floor(dbl));
587
588 ust = 0;
589 usc = strlen(szTemp);
590 for (uss = 0; uss < usc; uss++)
591 {
592 if (uss)
593 if (((usc - uss) % 3) == 0)
594 {
595 pszTarget[ust] = cThousands;
596 ust++;
597 }
598 pszTarget[ust] = szTemp[uss];
599 ust++;
600 }
601 pszTarget[ust] = '\0';
602
603 return (pszTarget);
604}
605
606/*
607 *@@ strhVariableDouble:
608 * like strhThousandsULong, but for a "double" value, and
609 * with a variable number of decimal places depending on the
610 * size of the quantity.
611 *
612 *@@added V0.9.6 (2000-11-12) [pr]
613 */
614
615PSZ strhVariableDouble(PSZ pszTarget,
616 double dbl,
617 PSZ pszUnits,
618 CHAR cThousands)
619{
620 if (dbl < 100.0)
621 sprintf(pszTarget, "%.2f%s", dbl, pszUnits);
622 else
623 if (dbl < 1000.0)
624 sprintf(pszTarget, "%.1f%s", dbl, pszUnits);
625 else
626 strcat(strhThousandsDouble(pszTarget, dbl, cThousands),
627 pszUnits);
628
629 return(pszTarget);
630}
631
632/*
633 *@@ strhFileDate:
634 * converts file date data to a string (to pszBuf).
635 * You can pass any FDATE structure to this function,
636 * which are returned in those FILEFINDBUF* or
637 * FILESTATUS* structs by the Dos* functions.
638 *
639 * ulDateFormat is the PM setting for the date format,
640 * as set in the "Country" object, and can be queried using
641 + PrfQueryProfileInt(HINI_USER, "PM_National", "iDate", 0);
642 *
643 * meaning:
644 * -- 0 mm.dd.yyyy (English)
645 * -- 1 dd.mm.yyyy (e.g. German)
646 * -- 2 yyyy.mm.dd (Japanese, ISO)
647 * -- 3 yyyy.dd.mm
648 *
649 * cDateSep is used as a date separator (e.g. '.').
650 * This can be queried using:
651 + prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/');
652 *
653 * Alternatively, you can query all the country settings
654 * at once using prfhQueryCountrySettings (prfh.c).
655 *
656 *@@changed (99-11-07) [umoeller]: now calling strhDateTime
657 */
658
659VOID strhFileDate(PSZ pszBuf, // out: string returned
660 FDATE *pfDate, // in: date information
661 ULONG ulDateFormat, // in: date format (0-3)
662 CHAR cDateSep) // in: date separator (e.g. '.')
663{
664 DATETIME dt;
665 dt.day = pfDate->day;
666 dt.month = pfDate->month;
667 dt.year = pfDate->year + 1980;
668
669 strhDateTime(pszBuf,
670 NULL, // no time
671 &dt,
672 ulDateFormat,
673 cDateSep,
674 0, 0); // no time
675}
676
677/*
678 *@@ strhFileTime:
679 * converts file time data to a string (to pszBuf).
680 * You can pass any FTIME structure to this function,
681 * which are returned in those FILEFINDBUF* or
682 * FILESTATUS* structs by the Dos* functions.
683 *
684 * ulTimeFormat is the PM setting for the time format,
685 * as set in the "Country" object, and can be queried using
686 + PrfQueryProfileInt(HINI_USER, "PM_National", "iTime", 0);
687 * meaning:
688 * -- 0 12-hour clock
689 * -- >0 24-hour clock
690 *
691 * cDateSep is used as a time separator (e.g. ':').
692 * This can be queried using:
693 + prfhQueryProfileChar(HINI_USER, "PM_National", "sTime", ':');
694 *
695 * Alternatively, you can query all the country settings
696 * at once using prfhQueryCountrySettings (prfh.c).
697 *
698 *@@changed 99-03-15 fixed 12-hour crash
699 *@@changed (99-11-07) [umoeller]: now calling strhDateTime
700 */
701
702VOID strhFileTime(PSZ pszBuf, // out: string returned
703 FTIME *pfTime, // in: time information
704 ULONG ulTimeFormat, // in: 24-hour time format (0 or 1)
705 CHAR cTimeSep) // in: time separator (e.g. ':')
706{
707 DATETIME dt;
708 dt.hours = pfTime->hours;
709 dt.minutes = pfTime->minutes;
710 dt.seconds = pfTime->twosecs * 2;
711
712 strhDateTime(NULL, // no date
713 pszBuf,
714 &dt,
715 0, 0, // no date
716 ulTimeFormat,
717 cTimeSep);
718}
719
720/*
721 *@@ strhDateTime:
722 * converts Control Program DATETIME info
723 * into two strings. See strhFileDate and strhFileTime
724 * for more detailed parameter descriptions.
725 *
726 *@@added V0.9.0 (99-11-07) [umoeller]
727 */
728
729VOID strhDateTime(PSZ pszDate, // out: date string returned (can be NULL)
730 PSZ pszTime, // out: time string returned (can be NULL)
731 DATETIME *pDateTime, // in: date/time information
732 ULONG ulDateFormat, // in: date format (0-3); see strhFileDate
733 CHAR cDateSep, // in: date separator (e.g. '.')
734 ULONG ulTimeFormat, // in: 24-hour time format (0 or 1); see strhFileTime
735 CHAR cTimeSep) // in: time separator (e.g. ':')
736{
737 if (pszDate)
738 {
739 switch (ulDateFormat)
740 {
741 case 0: // mm.dd.yyyy (English)
742 sprintf(pszDate, "%02d%c%02d%c%04d",
743 pDateTime->month,
744 cDateSep,
745 pDateTime->day,
746 cDateSep,
747 pDateTime->year);
748 break;
749
750 case 1: // dd.mm.yyyy (e.g. German)
751 sprintf(pszDate, "%02d%c%02d%c%04d",
752 pDateTime->day,
753 cDateSep,
754 pDateTime->month,
755 cDateSep,
756 pDateTime->year);
757 break;
758
759 case 2: // yyyy.mm.dd (Japanese)
760 sprintf(pszDate, "%04d%c%02d%c%02d",
761 pDateTime->year,
762 cDateSep,
763 pDateTime->month,
764 cDateSep,
765 pDateTime->day);
766 break;
767
768 default: // yyyy.dd.mm
769 sprintf(pszDate, "%04d%c%02d%c%02d",
770 pDateTime->year,
771 cDateSep,
772 pDateTime->day,
773 cDateSep,
774 pDateTime->month);
775 break;
776 }
777 }
778
779 if (pszTime)
780 {
781 if (ulTimeFormat == 0)
782 {
783 // for 12-hour clock, we need additional INI data
784 CHAR szAMPM[10] = "err";
785
786 if (pDateTime->hours > 12)
787 {
788 // > 12h: PM.
789
790 // Note: 12:xx noon is 12 AM, not PM (even though
791 // AM stands for "ante meridiam", but English is just
792 // not logical), so that's handled below.
793
794 PrfQueryProfileString(HINI_USER,
795 "PM_National",
796 "s2359", // key
797 "PM", // default
798 szAMPM, sizeof(szAMPM)-1);
799 sprintf(pszTime, "%02d%c%02d%c%02d %s",
800 // leave 12 == 12 (not 0)
801 pDateTime->hours % 12,
802 cTimeSep,
803 pDateTime->minutes,
804 cTimeSep,
805 pDateTime->seconds,
806 szAMPM);
807 }
808 else
809 {
810 // <= 12h: AM
811 PrfQueryProfileString(HINI_USER,
812 "PM_National",
813 "s1159", // key
814 "AM", // default
815 szAMPM, sizeof(szAMPM)-1);
816 sprintf(pszTime, "%02d%c%02d%c%02d %s",
817 pDateTime->hours,
818 cTimeSep,
819 pDateTime->minutes,
820 cTimeSep,
821 pDateTime->seconds,
822 szAMPM);
823 }
824 }
825 else
826 // 24-hour clock
827 sprintf(pszTime, "%02d%c%02d%c%02d",
828 pDateTime->hours,
829 cTimeSep,
830 pDateTime->minutes,
831 cTimeSep,
832 pDateTime->seconds);
833 }
834}
835
836/*
837 *@@ strhGetWord:
838 * finds word boundaries.
839 *
840 * *ppszStart is used as the beginning of the
841 * search.
842 *
843 * If a word is found, *ppszStart is set to
844 * the first character of the word which was
845 * found and *ppszEnd receives the address
846 * of the first character _after_ the word,
847 * which is probably a space or a \n or \r char.
848 * We then return TRUE.
849 *
850 * The search is stopped if a null character
851 * is found or pLimit is reached. In that case,
852 * FALSE is returned.
853 *
854 *@@added V0.9.1 (2000-02-13) [umoeller]
855 */
856
857BOOL strhGetWord(PSZ *ppszStart, // in: start of search,
858 // out: start of word (if TRUE is returned)
859 const char *pLimit, // in: ptr to last char after *ppszStart to be
860 // searched; if the word does not end before
861 // or with this char, FALSE is returned
862 const char *pcszBeginChars, // stringh.h defines STRH_BEGIN_CHARS
863 const char *pcszEndChars, // stringh.h defines STRH_END_CHARS
864 PSZ *ppszEnd) // out: first char _after_ word
865 // (if TRUE is returned)
866{
867 // characters after which a word can be started
868 // const char *pcszBeginChars = "\x0d\x0a ";
869 // const char *pcszEndChars = "\x0d\x0a /-";
870
871 PSZ pStart = *ppszStart;
872
873 // find start of word
874 while ( (pStart < (PSZ)pLimit)
875 && (strchr(pcszBeginChars, *pStart))
876 )
877 // if char is a "before word" char: go for next
878 pStart++;
879
880 if (pStart < (PSZ)pLimit)
881 {
882 // found a valid "word start" character
883 // (which is not in pcszBeginChars):
884
885 // find end of word
886 PSZ pEndOfWord = pStart;
887 while ( (pEndOfWord <= (PSZ)pLimit)
888 && (strchr(pcszEndChars, *pEndOfWord) == 0)
889 )
890 // if char is not an "end word" char: go for next
891 pEndOfWord++;
892
893 if (pEndOfWord <= (PSZ)pLimit)
894 {
895 // whoa, got a word:
896 *ppszStart = pStart;
897 *ppszEnd = pEndOfWord;
898 return (TRUE);
899 }
900 }
901
902 return (FALSE);
903}
904
905/*
906 *@@ strhIsWord:
907 * returns TRUE if p points to a "word"
908 * in pcszBuf.
909 *
910 * p is considered a word if the character _before_
911 * it is in pcszBeginChars and the char _after_
912 * it (i.e. *(p+cbSearch)) is in pcszEndChars.
913 *
914 *@@added V0.9.6 (2000-11-12) [umoeller]
915 */
916
917BOOL strhIsWord(const char *pcszBuf,
918 const char *p, // in: start of word
919 ULONG cbSearch, // in: length of word
920 const char *pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
921 const char *pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
922{
923 BOOL fEndOK = FALSE;
924
925 // check previous char
926 if ( (p == pcszBuf)
927 || (strchr(pcszBeginChars, *(p-1)))
928 )
929 {
930 // OK, valid begin char:
931 // check end char
932 CHAR cNextChar = *(p + cbSearch);
933 if (cNextChar == 0)
934 fEndOK = TRUE;
935 else
936 {
937 char *pc = strchr(pcszEndChars, cNextChar);
938 if (pc)
939 // OK, is end char: avoid doubles of that char,
940 // but allow spaces
941 if ( (cNextChar+1 != *pc)
942 || (cNextChar+1 == ' ')
943 || (cNextChar+1 == 0)
944 )
945 fEndOK = TRUE;
946 }
947 }
948
949 return (fEndOK);
950}
951
952/*
953 *@@ strhFindWord:
954 * searches for pszSearch in pszBuf, which is
955 * returned if found (or NULL if not).
956 *
957 * As opposed to strstr, this finds pszSearch
958 * only if it is a "word". A search string is
959 * considered a word if the character _before_
960 * it is in pcszBeginChars and the char _after_
961 * it is in pcszEndChars.
962 *
963 * Example:
964 + strhFindWord("This is an example.", "is");
965 + returns ...........^ this, but not the "is" in "This".
966 *
967 * The algorithm here uses strstr to find pszSearch in pszBuf
968 * and performs additional "is-word" checks for each item found
969 * (by calling strhIsWord).
970 *
971 * Note that this function is fairly slow compared to xstrFindWord.
972 *
973 *@@added V0.9.0 (99-11-08) [umoeller]
974 *@@changed (99-11-10) [umoeller]: tried second algorithm, reverted to original...
975 */
976
977PSZ strhFindWord(const char *pszBuf,
978 const char *pszSearch,
979 const char *pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
980 const char *pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
981{
982 PSZ pszReturn = 0;
983 ULONG cbBuf = strlen(pszBuf),
984 cbSearch = strlen(pszSearch);
985
986 if ((cbBuf) && (cbSearch))
987 {
988 const char *p = pszBuf;
989
990 do // while p
991 {
992 p = strstr(p, pszSearch);
993 if (p)
994 {
995 // string found:
996 // check if that's a word
997
998 if (strhIsWord(pszBuf,
999 p,
1000 cbSearch,
1001 pcszBeginChars,
1002 pcszEndChars))
1003 {
1004 // valid end char:
1005 pszReturn = (PSZ)p;
1006 break;
1007 }
1008
1009 p += cbSearch;
1010 }
1011 } while (p);
1012
1013 }
1014 return (pszReturn);
1015}
1016
1017/*
1018 *@@ strhFindEOL:
1019 * returns a pointer to the next \r, \n or null character
1020 * following pszSearchIn. Stores the offset in *pulOffset.
1021 *
1022 * This should never return NULL because at some point,
1023 * there will be a null byte in your string.
1024 *
1025 *@@added V0.9.4 (2000-07-01) [umoeller]
1026 */
1027
1028PSZ strhFindEOL(const char *pcszSearchIn, // in: where to search
1029 PULONG pulOffset) // out: offset (ptr can be NULL)
1030{
1031 const char *p = pcszSearchIn,
1032 *prc = 0;
1033 while (TRUE)
1034 {
1035 if ( (*p == '\r') || (*p == '\n') || (*p == 0) )
1036 {
1037 prc = p;
1038 break;
1039 }
1040 p++;
1041 }
1042
1043 if ((pulOffset) && (prc))
1044 *pulOffset = prc - pcszSearchIn;
1045
1046 return ((PSZ)prc);
1047}
1048
1049/*
1050 *@@ strhFindNextLine:
1051 * like strhFindEOL, but this returns the character
1052 * _after_ \r or \n. Note that this might return
1053 * a pointer to terminating NULL character also.
1054 */
1055
1056PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset)
1057{
1058 PSZ pEOL = strhFindEOL(pszSearchIn, NULL);
1059 // pEOL now points to the \r char or the terminating 0 byte;
1060 // if not null byte, advance pointer
1061 PSZ pNextLine = pEOL;
1062 if (*pNextLine == '\r')
1063 pNextLine++;
1064 if (*pNextLine == '\n')
1065 pNextLine++;
1066 if (pulOffset)
1067 *pulOffset = pNextLine - pszSearchIn;
1068 return (pNextLine);
1069}
1070
1071/*
1072 *@@ strhBeautifyTitle:
1073 * replaces all line breaks (0xd, 0xa) with spaces.
1074 */
1075
1076BOOL strhBeautifyTitle(PSZ psz)
1077{
1078 BOOL rc = FALSE;
1079 CHAR *p;
1080 while ((p = strchr(psz, 0xa)))
1081 {
1082 *p = ' ';
1083 rc = TRUE;
1084 }
1085 while ((p = strchr(psz, 0xd)))
1086 {
1087 *p = ' ';
1088 rc = TRUE;
1089 }
1090 return (rc);
1091}
1092
1093/*
1094 * strhFindAttribValue:
1095 * searches for pszAttrib in pszSearchIn; if found,
1096 * returns the first character after the "=" char.
1097 * If "=" is not found, a space, \r, and \n are
1098 * also accepted. This function searches without
1099 * respecting case.
1100 *
1101 * <B>Example:</B>
1102 + strhFindAttribValue("<PAGE BLAH="data">, "BLAH")
1103 +
1104 + returns ....................... ^ this address.
1105 *
1106 *@@added V0.9.0 [umoeller]
1107 *@@changed V0.9.3 (2000-05-19) [umoeller]: some speed optimizations
1108 */
1109
1110PSZ strhFindAttribValue(const char *pszSearchIn, const char *pszAttrib)
1111{
1112 PSZ prc = 0;
1113 PSZ pszSearchIn2 = (PSZ)pszSearchIn,
1114 p,
1115 p2;
1116 ULONG cbAttrib = strlen(pszAttrib);
1117
1118 // 1) find space char
1119 while ((p = strchr(pszSearchIn2, ' ')))
1120 {
1121 CHAR c;
1122 p++;
1123 c = *(p+cbAttrib); // V0.9.3 (2000-05-19) [umoeller]
1124 // now check whether the p+strlen(pszAttrib)
1125 // is a valid end-of-tag character
1126 if ( (memicmp(p, (PVOID)pszAttrib, cbAttrib) == 0)
1127 && ( (c == ' ')
1128 || (c == '>')
1129 || (c == '=')
1130 || (c == '\r')
1131 || (c == '\n')
1132 || (c == 0)
1133 )
1134 )
1135 {
1136 // yes:
1137 CHAR c2;
1138 p2 = p + cbAttrib;
1139 c2 = *p2;
1140 while ( ( (c2 == ' ')
1141 || (c2 == '=')
1142 || (c2 == '\n')
1143 || (c2 == '\r')
1144 )
1145 && (c2 != 0)
1146 )
1147 c2 = *++p2;
1148 prc = p2;
1149 break; // first while
1150 }
1151 pszSearchIn2++;
1152 }
1153 return (prc);
1154}
1155
1156/*
1157 * strhGetNumAttribValue:
1158 * stores the numerical parameter value of an HTML-style
1159 * tag in *pl.
1160 *
1161 * Returns the address of the tag parameter in the
1162 * search buffer, if found, or NULL.
1163 *
1164 * <B>Example:</B>
1165 + strhGetNumAttribValue("<PAGE BLAH=123>, "BLAH", &l);
1166 *
1167 * stores 123 in the "l" variable.
1168 *
1169 *@@added V0.9.0 [umoeller]
1170 */
1171
1172PSZ strhGetNumAttribValue(const char *pszSearchIn, // in: where to search
1173 const char *pszTag, // e.g. "INDEX"
1174 PLONG pl) // out: numerical value
1175{
1176 PSZ pParam;
1177 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
1178 sscanf(pParam, "%ld", pl);
1179
1180 return (pParam);
1181}
1182
1183/*
1184 * strhGetTextAttr:
1185 * retrieves the attribute value of a textual HTML-style tag
1186 * in a newly allocated buffer, which is returned,
1187 * or NULL if attribute not found.
1188 * If an attribute value is to contain spaces, it
1189 * must be enclosed in quotes.
1190 *
1191 * The offset of the attribute data in pszSearchIn is
1192 * returned in *pulOffset so that you can do multiple
1193 * searches.
1194 *
1195 * This returns a new buffer, which should be free()'d after use.
1196 *
1197 * <B>Example:</B>
1198 + ULONG ulOfs = 0;
1199 + strhGetTextAttr("<PAGE BLAH="blublub">, "BLAH", &ulOfs)
1200 + ............^ ulOfs
1201 *
1202 * returns a new string with the value "blublub" (without
1203 * quotes) and sets ulOfs to 12.
1204 *
1205 *@@added V0.9.0 [umoeller]
1206 */
1207
1208PSZ strhGetTextAttr(const char *pszSearchIn,
1209 const char *pszTag,
1210 PULONG pulOffset) // out: offset where found
1211{
1212 PSZ pParam,
1213 pParam2,
1214 prc = NULL;
1215 ULONG ulCount = 0;
1216 LONG lNestingLevel = 0;
1217
1218 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
1219 {
1220 // determine end character to search for: a space
1221 CHAR cEnd = ' ';
1222 if (*pParam == '\"')
1223 {
1224 // or, if the data is enclosed in quotes, a quote
1225 cEnd = '\"';
1226 pParam++;
1227 }
1228
1229 if (pulOffset)
1230 // store the offset
1231 (*pulOffset) = pParam - (PSZ)pszSearchIn;
1232
1233 // now find end of attribute
1234 pParam2 = pParam;
1235 while (*pParam)
1236 {
1237 if (*pParam == cEnd)
1238 // end character found
1239 break;
1240 else if (*pParam == '<')
1241 // yet another opening tag found:
1242 // this is probably some "<" in the attributes
1243 lNestingLevel++;
1244 else if (*pParam == '>')
1245 {
1246 lNestingLevel--;
1247 if (lNestingLevel < 0)
1248 // end of tag found:
1249 break;
1250 }
1251 ulCount++;
1252 pParam++;
1253 }
1254
1255 // copy attribute to new buffer
1256 if (ulCount)
1257 {
1258 prc = (PSZ)malloc(ulCount+1);
1259 memcpy(prc, pParam2, ulCount);
1260 *(prc+ulCount) = 0;
1261 }
1262 }
1263 return (prc);
1264}
1265
1266/*
1267 * strhFindEndOfTag:
1268 * returns a pointer to the ">" char
1269 * which seems to terminate the tag beginning
1270 * after pszBeginOfTag.
1271 *
1272 * If additional "<" chars are found, we look
1273 * for additional ">" characters too.
1274 *
1275 * Note: You must pass the address of the opening
1276 * '<' character to this function.
1277 *
1278 * Example:
1279 + PSZ pszTest = "<BODY ATTR=\"<BODY>\">";
1280 + strhFindEndOfTag(pszTest)
1281 + returns.................................^ this.
1282 *
1283 *@@added V0.9.0 [umoeller]
1284 */
1285
1286PSZ strhFindEndOfTag(const char *pszBeginOfTag)
1287{
1288 PSZ p = (PSZ)pszBeginOfTag,
1289 prc = NULL;
1290 LONG lNestingLevel = 0;
1291
1292 while (*p)
1293 {
1294 if (*p == '<')
1295 // another opening tag found:
1296 lNestingLevel++;
1297 else if (*p == '>')
1298 {
1299 // closing tag found:
1300 lNestingLevel--;
1301 if (lNestingLevel < 1)
1302 {
1303 // corresponding: return this
1304 prc = p;
1305 break;
1306 }
1307 }
1308 p++;
1309 }
1310
1311 return (prc);
1312}
1313
1314/*
1315 * strhGetBlock:
1316 * this complex function searches the given string
1317 * for a pair of opening/closing HTML-style tags.
1318 *
1319 * If found, this routine returns TRUE and does
1320 * the following:
1321 *
1322 * 1) allocate a new buffer, copy the text
1323 * enclosed by the opening/closing tags
1324 * into it and set *ppszBlock to that
1325 * buffer;
1326 *
1327 * 2) if the opening tag has any attributes,
1328 * allocate another buffer, copy the
1329 * attributes into it and set *ppszAttrs
1330 * to that buffer; if no attributes are
1331 * found, *ppszAttrs will be NULL;
1332 *
1333 * 3) set *pulOffset to the offset from the
1334 * beginning of *ppszSearchIn where the
1335 * opening tag was found;
1336 *
1337 * 4) advance *ppszSearchIn to after the
1338 * closing tag, so that you can do
1339 * multiple searches without finding the
1340 * same tags twice.
1341 *
1342 * All buffers should be freed using free().
1343 *
1344 * This returns the following:
1345 * -- 0: no error
1346 * -- 1: tag not found at all (doesn't have to be an error)
1347 * -- 2: begin tag found, but no corresponding end tag found. This
1348 * is a real error.
1349 * -- 3: begin tag is not terminated by "&gt;" (e.g. "&lt;BEGINTAG whatever")
1350 *
1351 * <B>Example:</B>
1352 + PSZ pSearch = "&lt;PAGE INDEX=1&gt;This is page 1.&lt;/PAGE&gt;More text."
1353 + PSZ pszBlock, pszAttrs;
1354 + ULONG ulOfs;
1355 + strhGetBlock(&pSearch, "PAGE", &pszBlock, &pszAttrs, &ulOfs)
1356 *
1357 * would do the following:
1358 *
1359 * 1) set pszBlock to a new string containing "This is page 1."
1360 * without quotes;
1361 *
1362 * 2) set pszAttrs to a new string containing "&lt;PAGE INDEX=1&gt;";
1363 *
1364 * 3) set ulOfs to 0, because "&lt;PAGE" was found at the beginning;
1365 *
1366 * 4) pSearch would be advanced to point to the "More text"
1367 * string in the original buffer.
1368 *
1369 * Hey-hey. A one-shot function, fairly complicated, but indispensable
1370 * for HTML parsing.
1371 *
1372 *@@added V0.9.0 [umoeller]
1373 *@@changed V0.9.1 (2000-01-03) [umoeller]: fixed heap overwrites (thanks to string debugging)
1374 *@@changed V0.9.1 (2000-01-06) [umoeller]: changed prototype
1375 *@@changed V0.9.3 (2000-05-06) [umoeller]: NULL string check was missing
1376 */
1377
1378ULONG strhGetBlock(const char *pszSearchIn, // in: buffer to search
1379 PULONG pulSearchOffset, // in/out: offset where to start search (0 for beginning)
1380 PSZ pszTag,
1381 PSZ *ppszBlock, // out: block enclosed by the tags
1382 PSZ *ppszAttribs, // out: attributes of the opening tag
1383 PULONG pulOfsBeginTag, // out: offset from pszSearchIn where opening tag was found
1384 PULONG pulOfsBeginBlock) // out: offset from pszSearchIn where beginning of block was found
1385{
1386 ULONG ulrc = 1;
1387 PSZ pszBeginTag = (PSZ)pszSearchIn + *pulSearchOffset,
1388 pszSearch2 = pszBeginTag,
1389 pszClosingTag;
1390 ULONG cbTag = strlen(pszTag);
1391
1392 // go thru the block and check all tags if it's the
1393 // begin tag we're looking for
1394 while ((pszBeginTag = strchr(pszBeginTag, '<')))
1395 {
1396 if (memicmp(pszBeginTag+1, pszTag, strlen(pszTag)) == 0)
1397 // yes: stop
1398 break;
1399 else
1400 pszBeginTag++;
1401 }
1402
1403 if (pszBeginTag)
1404 {
1405 // we found <TAG>:
1406 ULONG ulNestingLevel = 0;
1407
1408 PSZ pszEndOfBeginTag = strhFindEndOfTag(pszBeginTag);
1409 // strchr(pszBeginTag, '>');
1410 if (pszEndOfBeginTag)
1411 {
1412 // does the caller want the attributes?
1413 if (ppszAttribs)
1414 {
1415 // yes: then copy them
1416 ULONG ulAttrLen = pszEndOfBeginTag - pszBeginTag;
1417 PSZ pszAttrs = (PSZ)malloc(ulAttrLen + 1);
1418 strncpy(pszAttrs, pszBeginTag, ulAttrLen);
1419 // add terminating 0
1420 *(pszAttrs + ulAttrLen) = 0;
1421
1422 *ppszAttribs = pszAttrs;
1423 }
1424
1425 // output offset of where we found the begin tag
1426 if (pulOfsBeginTag)
1427 *pulOfsBeginTag = pszBeginTag - (PSZ)pszSearchIn;
1428
1429 // now find corresponding closing tag (e.g. "</BODY>"
1430 pszBeginTag = pszEndOfBeginTag+1;
1431 // now we're behind the '>' char of the opening tag
1432 // increase offset of that too
1433 if (pulOfsBeginBlock)
1434 *pulOfsBeginBlock = pszBeginTag - (PSZ)pszSearchIn;
1435
1436 // find next closing tag;
1437 // for the first run, pszSearch2 points to right
1438 // after the '>' char of the opening tag
1439 pszSearch2 = pszBeginTag;
1440 while ( (pszSearch2) // fixed V0.9.3 (2000-05-06) [umoeller]
1441 && (pszClosingTag = strstr(pszSearch2, "<"))
1442 )
1443 {
1444 // if we have another opening tag before our closing
1445 // tag, we need to have several closing tags before
1446 // we're done
1447 if (memicmp(pszClosingTag+1, pszTag, cbTag) == 0)
1448 ulNestingLevel++;
1449 else
1450 {
1451 // is this ours?
1452 if ( (*(pszClosingTag+1) == '/')
1453 && (memicmp(pszClosingTag+2, pszTag, cbTag) == 0)
1454 )
1455 {
1456 // we've found a matching closing tag; is
1457 // it ours?
1458 if (ulNestingLevel == 0)
1459 {
1460 // our closing tag found:
1461 // allocate mem for a new buffer
1462 // and extract all the text between
1463 // open and closing tags to it
1464 ULONG ulLen = pszClosingTag - pszBeginTag;
1465 if (ppszBlock)
1466 {
1467 PSZ pNew = (PSZ)malloc(ulLen + 1);
1468 strhncpy0(pNew, pszBeginTag, ulLen);
1469 *ppszBlock = pNew;
1470 }
1471
1472 // raise search offset to after the closing tag
1473 *pulSearchOffset = (pszClosingTag + cbTag + 1) - (PSZ)pszSearchIn;
1474
1475 ulrc = 0;
1476
1477 break;
1478 } else
1479 // not our closing tag:
1480 ulNestingLevel--;
1481 }
1482 }
1483 // no matching closing tag: search on after that
1484 pszSearch2 = strhFindEndOfTag(pszClosingTag);
1485 } // end while (pszClosingTag = strstr(pszSearch2, "<"))
1486
1487 if (!pszClosingTag)
1488 // no matching closing tag found:
1489 // return 2 (closing tag not found)
1490 ulrc = 2;
1491 } // end if (pszBeginTag)
1492 else
1493 // no matching ">" for opening tag found:
1494 ulrc = 3;
1495 }
1496
1497 return (ulrc);
1498}
1499
1500/* ******************************************************************
1501 *
1502 * Miscellaneous
1503 *
1504 ********************************************************************/
1505
1506/*
1507 *@@ strhArrayAppend:
1508 * this appends a string to a "string array".
1509 *
1510 * A string array is considered a sequence of
1511 * zero-terminated strings in memory. That is,
1512 * after each string's null-byte, the next
1513 * string comes up.
1514 *
1515 * This is useful for composing a single block
1516 * of memory from, say, list box entries, which
1517 * can then be written to OS2.INI in one flush.
1518 *
1519 * To append strings to such an array, call this
1520 * function for each string you wish to append.
1521 * This will re-allocate *ppszRoot with each call,
1522 * and update *pcbRoot, which then contains the
1523 * total size of all strings (including all null
1524 * terminators).
1525 *
1526 * Pass *pcbRoot to PrfSaveProfileData to have the
1527 * block saved.
1528 *
1529 * Note: On the first call, *ppszRoot and *pcbRoot
1530 * _must_ be both NULL, or this crashes.
1531 */
1532
1533VOID strhArrayAppend(PSZ *ppszRoot, // in: root of array
1534 const char *pcszNew, // in: string to append
1535 PULONG pcbRoot) // in/out: size of array
1536{
1537 ULONG cbNew = strlen(pcszNew);
1538 PSZ pszTemp = (PSZ)malloc(*pcbRoot
1539 + cbNew
1540 + 1); // two null bytes
1541 if (*ppszRoot)
1542 {
1543 // not first loop: copy old stuff
1544 memcpy(pszTemp,
1545 *ppszRoot,
1546 *pcbRoot);
1547 free(*ppszRoot);
1548 }
1549 // append new string
1550 strcpy(pszTemp + *pcbRoot,
1551 pcszNew);
1552 // update root
1553 *ppszRoot = pszTemp;
1554 // update length
1555 *pcbRoot += cbNew + 1;
1556}
1557
1558/*
1559 *@@ strhCreateDump:
1560 * this dumps a memory block into a string
1561 * and returns that string in a new buffer.
1562 *
1563 * You must free() the returned PSZ after use.
1564 *
1565 * The output looks like the following:
1566 *
1567 + 0000: FE FF 0E 02 90 00 00 00 ........
1568 + 0008: FD 01 00 00 57 50 46 6F ....WPFo
1569 + 0010: 6C 64 65 72 00 78 01 34 lder.x.4
1570 *
1571 * Each line is terminated with a newline (\n)
1572 * character only.
1573 *
1574 *@@added V0.9.1 (2000-01-22) [umoeller]
1575 */
1576
1577PSZ strhCreateDump(PBYTE pb, // in: start address of buffer
1578 ULONG ulSize, // in: size of buffer
1579 ULONG ulIndent) // in: indentation of every line
1580{
1581 PSZ pszReturn = 0;
1582 XSTRING strReturn;
1583 CHAR szTemp[1000];
1584
1585 PBYTE pbCurrent = pb; // current byte
1586 ULONG ulCount = 0,
1587 ulCharsInLine = 0; // if this grows > 7, a new line is started
1588 CHAR szLine[400] = "",
1589 szAscii[30] = " "; // ASCII representation; filled for every line
1590 PSZ pszLine = szLine,
1591 pszAscii = szAscii;
1592
1593 xstrInit(&strReturn, (ulSize * 30) + ulIndent);
1594
1595 for (pbCurrent = pb;
1596 ulCount < ulSize;
1597 pbCurrent++, ulCount++)
1598 {
1599 if (ulCharsInLine == 0)
1600 {
1601 memset(szLine, ' ', ulIndent);
1602 pszLine += ulIndent;
1603 }
1604 pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent);
1605
1606 if ( (*pbCurrent > 31) && (*pbCurrent < 127) )
1607 // printable character:
1608 *pszAscii = *pbCurrent;
1609 else
1610 *pszAscii = '.';
1611 pszAscii++;
1612
1613 ulCharsInLine++;
1614 if ( (ulCharsInLine > 7) // 8 bytes added?
1615 || (ulCount == ulSize-1) // end of buffer reached?
1616 )
1617 {
1618 // if we haven't had eight bytes yet,
1619 // fill buffer up to eight bytes with spaces
1620 ULONG ul2;
1621 for (ul2 = ulCharsInLine;
1622 ul2 < 8;
1623 ul2++)
1624 pszLine += sprintf(pszLine, " ");
1625
1626 sprintf(szTemp, "%04lX: %s %s\n",
1627 (ulCount & 0xFFFFFFF8), // offset in hex
1628 szLine, // bytes string
1629 szAscii); // ASCII string
1630 xstrcat(&strReturn, szTemp, 0);
1631
1632 // restart line buffer
1633 pszLine = szLine;
1634
1635 // clear ASCII buffer
1636 strcpy(szAscii, " ");
1637 pszAscii = szAscii;
1638
1639 // reset line counter
1640 ulCharsInLine = 0;
1641 }
1642 }
1643
1644 if (strReturn.cbAllocated)
1645 pszReturn = strReturn.psz;
1646
1647 return (pszReturn);
1648}
1649
1650/* ******************************************************************
1651 *
1652 * Wildcard matching
1653 *
1654 ********************************************************************/
1655
1656/*
1657 * The following code has been taken from "fnmatch.zip".
1658 *
1659 * (c) 1994-1996 by Eberhard Mattes.
1660 */
1661
1662/* In OS/2 and DOS styles, both / and \ separate components of a path.
1663 * This macro returns true iff C is a separator. */
1664
1665#define IS_OS2_COMP_SEP(C) ((C) == '/' || (C) == '\\')
1666
1667
1668/* This macro returns true if C is at the end of a component of a
1669 * path. */
1670
1671#define IS_OS2_COMP_END(C) ((C) == 0 || IS_OS2_COMP_SEP (C))
1672
1673/*
1674 * skip_comp_os2:
1675 * Return a pointer to the next component of the path SRC, for OS/2
1676 * and DOS styles. When the end of the string is reached, a pointer
1677 * to the terminating null character is returned.
1678 *
1679 * (c) 1994-1996 by Eberhard Mattes.
1680 */
1681
1682static const unsigned char* skip_comp_os2(const unsigned char *src)
1683{
1684 /* Skip characters until hitting a separator or the end of the
1685 * string. */
1686
1687 while (!IS_OS2_COMP_END(*src))
1688 ++src;
1689
1690 /* Skip the separator if we hit a separator. */
1691
1692 if (*src != 0)
1693 ++src;
1694 return src;
1695}
1696
1697/*
1698 * has_colon:
1699 * returns true iff the path P contains a colon.
1700 *
1701 * (c) 1994-1996 by Eberhard Mattes.
1702 */
1703
1704static int has_colon(const unsigned char *p)
1705{
1706 while (*p != 0)
1707 if (*p == ':')
1708 return 1;
1709 else
1710 ++p;
1711 return 0;
1712}
1713
1714/*
1715 * match_comp_os2:
1716 * Compare a single component (directory name or file name) of the
1717 * paths, for OS/2 and DOS styles. MASK and NAME point into a
1718 * component of the wildcard and the name to be checked, respectively.
1719 * Comparing stops at the next separator. The FLAGS argument is the
1720 * same as that of fnmatch(). HAS_DOT is true if a dot is in the
1721 * current component of NAME. The number of dots is not restricted,
1722 * even in DOS style. Return FNM_MATCH iff MASK and NAME match.
1723 * Note that this function is recursive.
1724 *
1725 * (c) 1994-1996 by Eberhard Mattes.
1726 */
1727
1728static int match_comp_os2(const unsigned char *mask,
1729 const unsigned char *name,
1730 unsigned flags,
1731 int has_dot)
1732{
1733 int rc;
1734
1735 for (;;)
1736 switch (*mask)
1737 {
1738 case 0:
1739
1740 /* There must be no extra characters at the end of NAME when
1741 * reaching the end of MASK unless _FNM_PATHPREFIX is set:
1742 * in that case, NAME may point to a separator. */
1743
1744 if (*name == 0)
1745 return FNM_MATCH;
1746 if ((flags & _FNM_PATHPREFIX) && IS_OS2_COMP_SEP(*name))
1747 return FNM_MATCH;
1748 return FNM_NOMATCH;
1749
1750 case '/':
1751 case '\\':
1752
1753 /* Separators match separators. */
1754
1755 if (IS_OS2_COMP_SEP(*name))
1756 return FNM_MATCH;
1757
1758 /* If _FNM_PATHPREFIX is set, a trailing separator in MASK
1759 * is ignored at the end of NAME. */
1760
1761 if ((flags & _FNM_PATHPREFIX) && mask[1] == 0 && *name == 0)
1762 return FNM_MATCH;
1763
1764 /* Stop comparing at the separator. */
1765
1766 return FNM_NOMATCH;
1767
1768 case '?':
1769
1770 /* A question mark matches one character. It does not match
1771 * a dot. At the end of the component (and before a dot),
1772 * it also matches zero characters. */
1773
1774 if (*name != '.' && !IS_OS2_COMP_END(*name))
1775 ++name;
1776 ++mask;
1777 break;
1778
1779 case '*':
1780
1781 /* An asterisk matches zero or more characters. In DOS
1782 * mode, dots are not matched. */
1783
1784 do
1785 {
1786 ++mask;
1787 }
1788 while (*mask == '*');
1789 for (;;)
1790 {
1791 rc = match_comp_os2(mask, name, flags, has_dot);
1792 if (rc != FNM_NOMATCH)
1793 return rc;
1794 if (IS_OS2_COMP_END(*name))
1795 return FNM_NOMATCH;
1796 if (*name == '.' && (flags & _FNM_STYLE_MASK) == _FNM_DOS)
1797 return FNM_NOMATCH;
1798 ++name;
1799 }
1800
1801 case '.':
1802
1803 /* A dot matches a dot. It also matches the implicit dot at
1804 * the end of a dot-less NAME. */
1805
1806 ++mask;
1807 if (*name == '.')
1808 ++name;
1809 else if (has_dot || !IS_OS2_COMP_END(*name))
1810 return FNM_NOMATCH;
1811 break;
1812
1813 default:
1814
1815 /* All other characters match themselves. */
1816
1817 if (flags & _FNM_IGNORECASE)
1818 {
1819 if (tolower(*mask) != tolower(*name))
1820 return FNM_NOMATCH;
1821 }
1822 else
1823 {
1824 if (*mask != *name)
1825 return FNM_NOMATCH;
1826 }
1827 ++mask;
1828 ++name;
1829 break;
1830 }
1831}
1832
1833/*
1834 * match_comp:
1835 * compare a single component (directory name or file name) of the
1836 * paths, for all styles which need component-by-component matching.
1837 * MASK and NAME point to the start of a component of the wildcard and
1838 * the name to be checked, respectively. Comparing stops at the next
1839 * separator. The FLAGS argument is the same as that of fnmatch().
1840 * Return FNM_MATCH iff MASK and NAME match.
1841 *
1842 * (c) 1994-1996 by Eberhard Mattes.
1843 */
1844
1845static int match_comp(const unsigned char *mask,
1846 const unsigned char *name,
1847 unsigned flags)
1848{
1849 const unsigned char *s;
1850
1851 switch (flags & _FNM_STYLE_MASK)
1852 {
1853 case _FNM_OS2:
1854 case _FNM_DOS:
1855
1856 /* For OS/2 and DOS styles, we add an implicit dot at the end of
1857 * the component if the component doesn't include a dot. */
1858
1859 s = name;
1860 while (!IS_OS2_COMP_END(*s) && *s != '.')
1861 ++s;
1862 return match_comp_os2(mask, name, flags, *s == '.');
1863
1864 default:
1865 return FNM_ERR;
1866 }
1867}
1868
1869/* In Unix styles, / separates components of a path. This macro
1870 * returns true iff C is a separator. */
1871
1872#define IS_UNIX_COMP_SEP(C) ((C) == '/')
1873
1874
1875/* This macro returns true if C is at the end of a component of a
1876 * path. */
1877
1878#define IS_UNIX_COMP_END(C) ((C) == 0 || IS_UNIX_COMP_SEP (C))
1879
1880/*
1881 * match_unix:
1882 * match complete paths for Unix styles. The FLAGS argument is the
1883 * same as that of fnmatch(). COMP points to the start of the current
1884 * component in NAME. Return FNM_MATCH iff MASK and NAME match. The
1885 * backslash character is used for escaping ? and * unless
1886 * FNM_NOESCAPE is set.
1887 *
1888 * (c) 1994-1996 by Eberhard Mattes.
1889 */
1890
1891static int match_unix(const unsigned char *mask,
1892 const unsigned char *name,
1893 unsigned flags,
1894 const unsigned char *comp)
1895{
1896 unsigned char c1, c2;
1897 char invert, matched;
1898 const unsigned char *start;
1899 int rc;
1900
1901 for (;;)
1902 switch (*mask)
1903 {
1904 case 0:
1905
1906 /* There must be no extra characters at the end of NAME when
1907 * reaching the end of MASK unless _FNM_PATHPREFIX is set:
1908 * in that case, NAME may point to a separator. */
1909
1910 if (*name == 0)
1911 return FNM_MATCH;
1912 if ((flags & _FNM_PATHPREFIX) && IS_UNIX_COMP_SEP(*name))
1913 return FNM_MATCH;
1914 return FNM_NOMATCH;
1915
1916 case '?':
1917
1918 /* A question mark matches one character. It does not match
1919 * the component separator if FNM_PATHNAME is set. It does
1920 * not match a dot at the start of a component if FNM_PERIOD
1921 * is set. */
1922
1923 if (*name == 0)
1924 return FNM_NOMATCH;
1925 if ((flags & FNM_PATHNAME) && IS_UNIX_COMP_SEP(*name))
1926 return FNM_NOMATCH;
1927 if (*name == '.' && (flags & FNM_PERIOD) && name == comp)
1928 return FNM_NOMATCH;
1929 ++mask;
1930 ++name;
1931 break;
1932
1933 case '*':
1934
1935 /* An asterisk matches zero or more characters. It does not
1936 * match the component separator if FNM_PATHNAME is set. It
1937 * does not match a dot at the start of a component if
1938 * FNM_PERIOD is set. */
1939
1940 if (*name == '.' && (flags & FNM_PERIOD) && name == comp)
1941 return FNM_NOMATCH;
1942 do
1943 {
1944 ++mask;
1945 }
1946 while (*mask == '*');
1947 for (;;)
1948 {
1949 rc = match_unix(mask, name, flags, comp);
1950 if (rc != FNM_NOMATCH)
1951 return rc;
1952 if (*name == 0)
1953 return FNM_NOMATCH;
1954 if ((flags & FNM_PATHNAME) && IS_UNIX_COMP_SEP(*name))
1955 return FNM_NOMATCH;
1956 ++name;
1957 }
1958
1959 case '/':
1960
1961 /* Separators match only separators. If _FNM_PATHPREFIX is
1962 * set, a trailing separator in MASK is ignored at the end
1963 * of NAME. */
1964
1965 if (!(IS_UNIX_COMP_SEP(*name)
1966 || ((flags & _FNM_PATHPREFIX) && *name == 0
1967 && (mask[1] == 0
1968 || (!(flags & FNM_NOESCAPE) && mask[1] == '\\'
1969 && mask[2] == 0)))))
1970 return FNM_NOMATCH;
1971
1972 ++mask;
1973 if (*name != 0)
1974 ++name;
1975
1976 /* This is the beginning of a new component if FNM_PATHNAME
1977 * is set. */
1978
1979 if (flags & FNM_PATHNAME)
1980 comp = name;
1981 break;
1982
1983 case '[':
1984
1985 /* A set of characters. Always case-sensitive. */
1986
1987 if (*name == 0)
1988 return FNM_NOMATCH;
1989 if ((flags & FNM_PATHNAME) && IS_UNIX_COMP_SEP(*name))
1990 return FNM_NOMATCH;
1991 if (*name == '.' && (flags & FNM_PERIOD) && name == comp)
1992 return FNM_NOMATCH;
1993
1994 invert = 0;
1995 matched = 0;
1996 ++mask;
1997
1998 /* If the first character is a ! or ^, the set matches all
1999 * characters not listed in the set. */
2000
2001 if (*mask == '!' || *mask == '^')
2002 {
2003 ++mask;
2004 invert = 1;
2005 }
2006
2007 /* Loop over all the characters of the set. The loop ends
2008 * if the end of the string is reached or if a ] is
2009 * encountered unless it directly follows the initial [ or
2010 * [-. */
2011
2012 start = mask;
2013 while (!(*mask == 0 || (*mask == ']' && mask != start)))
2014 {
2015 /* Get the next character which is optionally preceded
2016 * by a backslash. */
2017
2018 c1 = *mask++;
2019 if (!(flags & FNM_NOESCAPE) && c1 == '\\')
2020 {
2021 if (*mask == 0)
2022 break;
2023 c1 = *mask++;
2024 }
2025
2026 /* Ranges of characters are written as a-z. Don't
2027 * forget to check for the end of the string and to
2028 * handle the backslash. If the character after - is a
2029 * ], it isn't a range. */
2030
2031 if (*mask == '-' && mask[1] != ']')
2032 {
2033 ++mask; /* Skip the - character */
2034 if (!(flags & FNM_NOESCAPE) && *mask == '\\')
2035 ++mask;
2036 if (*mask == 0)
2037 break;
2038 c2 = *mask++;
2039 }
2040 else
2041 c2 = c1;
2042
2043 /* Now check whether this character or range matches NAME. */
2044
2045 if (c1 <= *name && *name <= c2)
2046 matched = 1;
2047 }
2048
2049 /* If the end of the string is reached before a ] is found,
2050 * back up to the [ and compare it to NAME. */
2051
2052 if (*mask == 0)
2053 {
2054 if (*name != '[')
2055 return FNM_NOMATCH;
2056 ++name;
2057 mask = start;
2058 if (invert)
2059 --mask;
2060 }
2061 else
2062 {
2063 if (invert)
2064 matched = !matched;
2065 if (!matched)
2066 return FNM_NOMATCH;
2067 ++mask; /* Skip the ] character */
2068 if (*name != 0)
2069 ++name;
2070 }
2071 break;
2072
2073 case '\\':
2074 ++mask;
2075 if (flags & FNM_NOESCAPE)
2076 {
2077 if (*name != '\\')
2078 return FNM_NOMATCH;
2079 ++name;
2080 }
2081 else if (*mask == '*' || *mask == '?')
2082 {
2083 if (*mask != *name)
2084 return FNM_NOMATCH;
2085 ++mask;
2086 ++name;
2087 }
2088 break;
2089
2090 default:
2091
2092 /* All other characters match themselves. */
2093
2094 if (flags & _FNM_IGNORECASE)
2095 {
2096 if (tolower(*mask) != tolower(*name))
2097 return FNM_NOMATCH;
2098 }
2099 else
2100 {
2101 if (*mask != *name)
2102 return FNM_NOMATCH;
2103 }
2104 ++mask;
2105 ++name;
2106 break;
2107 }
2108}
2109
2110/*
2111 * _fnmatch_unsigned:
2112 * Check whether the path name NAME matches the wildcard MASK.
2113 *
2114 * Return:
2115 * -- 0 (FNM_MATCH) if it matches,
2116 * -- _FNM_NOMATCH if it doesn't,
2117 * -- FNM_ERR on error.
2118 *
2119 * The operation of this function is controlled by FLAGS.
2120 * This is an internal function, with unsigned arguments.
2121 *
2122 * (c) 1994-1996 by Eberhard Mattes.
2123 */
2124
2125static int _fnmatch_unsigned(const unsigned char *mask,
2126 const unsigned char *name,
2127 unsigned flags)
2128{
2129 int m_drive, n_drive,
2130 rc;
2131
2132 /* Match and skip the drive name if present. */
2133
2134 m_drive = ((isalpha(mask[0]) && mask[1] == ':') ? mask[0] : -1);
2135 n_drive = ((isalpha(name[0]) && name[1] == ':') ? name[0] : -1);
2136
2137 if (m_drive != n_drive)
2138 {
2139 if (m_drive == -1 || n_drive == -1)
2140 return FNM_NOMATCH;
2141 if (!(flags & _FNM_IGNORECASE))
2142 return FNM_NOMATCH;
2143 if (tolower(m_drive) != tolower(n_drive))
2144 return FNM_NOMATCH;
2145 }
2146
2147 if (m_drive != -1)
2148 mask += 2;
2149 if (n_drive != -1)
2150 name += 2;
2151
2152 /* Colons are not allowed in path names, except for the drive name,
2153 * which was skipped above. */
2154
2155 if (has_colon(mask) || has_colon(name))
2156 return FNM_ERR;
2157
2158 /* The name "\\server\path" should not be matched by mask
2159 * "\*\server\path". Ditto for /. */
2160
2161 switch (flags & _FNM_STYLE_MASK)
2162 {
2163 case _FNM_OS2:
2164 case _FNM_DOS:
2165
2166 if (IS_OS2_COMP_SEP(name[0]) && IS_OS2_COMP_SEP(name[1]))
2167 {
2168 if (!(IS_OS2_COMP_SEP(mask[0]) && IS_OS2_COMP_SEP(mask[1])))
2169 return FNM_NOMATCH;
2170 name += 2;
2171 mask += 2;
2172 }
2173 break;
2174
2175 case _FNM_POSIX:
2176
2177 if (name[0] == '/' && name[1] == '/')
2178 {
2179 int i;
2180
2181 name += 2;
2182 for (i = 0; i < 2; ++i)
2183 if (mask[0] == '/')
2184 ++mask;
2185 else if (mask[0] == '\\' && mask[1] == '/')
2186 mask += 2;
2187 else
2188 return FNM_NOMATCH;
2189 }
2190
2191 /* In Unix styles, treating ? and * w.r.t. components is simple.
2192 * No need to do matching component by component. */
2193
2194 return match_unix(mask, name, flags, name);
2195 }
2196
2197 /* Now compare all the components of the path name, one by one.
2198 * Note that the path separator must not be enclosed in brackets. */
2199
2200 while (*mask != 0 || *name != 0)
2201 {
2202
2203 /* If _FNM_PATHPREFIX is set, the names match if the end of MASK
2204 * is reached even if there are components left in NAME. */
2205
2206 if (*mask == 0 && (flags & _FNM_PATHPREFIX))
2207 return FNM_MATCH;
2208
2209 /* Compare a single component of the path name. */
2210
2211 rc = match_comp(mask, name, flags);
2212 if (rc != FNM_MATCH)
2213 return rc;
2214
2215 /* Skip to the next component or to the end of the path name. */
2216
2217 mask = skip_comp_os2(mask);
2218 name = skip_comp_os2(name);
2219 }
2220
2221 /* If we reached the ends of both strings, the names match. */
2222
2223 if (*mask == 0 && *name == 0)
2224 return FNM_MATCH;
2225
2226 /* The names do not match. */
2227
2228 return FNM_NOMATCH;
2229}
2230
2231/*
2232 *@@ strhMatchOS2:
2233 * this matches wildcards, similar to what DosEditName does.
2234 * However, this does not require a file to be present, but
2235 * works on strings only.
2236 */
2237
2238BOOL strhMatchOS2(const unsigned char* pcszMask, // in: mask (e.g. "*.txt")
2239 const unsigned char* pcszName) // in: string to check (e.g. "test.txt")
2240{
2241 return ((BOOL)(_fnmatch_unsigned(pcszMask,
2242 pcszName,
2243 _FNM_OS2 | _FNM_IGNORECASE)
2244 == FNM_MATCH)
2245 );
2246}
2247
2248/* ******************************************************************
2249 *
2250 * Fast string searches
2251 *
2252 ********************************************************************/
2253
2254#define ASSERT(a)
2255
2256/*
2257 * The following code has been taken from the "Standard
2258 * Function Library", file sflfind.c, and only slightly
2259 * modified to conform to the rest of this file.
2260 *
2261 * Written: 96/04/24 iMatix SFL project team <sfl@imatix.com>
2262 * Revised: 98/05/04
2263 *
2264 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
2265 *
2266 * The SFL Licence allows incorporating SFL code into other
2267 * programs, as long as the copyright is reprinted and the
2268 * code is marked as modified, so this is what we do.
2269 */
2270
2271/*
2272 *@@ strhmemfind:
2273 * searches for a pattern in a block of memory using the
2274 * Boyer-Moore-Horspool-Sunday algorithm.
2275 *
2276 * The block and pattern may contain any values; you must
2277 * explicitly provide their lengths. If you search for strings,
2278 * use strlen() on the buffers.
2279 *
2280 * Returns a pointer to the pattern if found within the block,
2281 * or NULL if the pattern was not found.
2282 *
2283 * This algorithm needs a "shift table" to cache data for the
2284 * search pattern. This table can be reused when performing
2285 * several searches with the same pattern.
2286 *
2287 * "shift" must point to an array big enough to hold 256 (8**2)
2288 * "size_t" values.
2289 *
2290 * If (*repeat_find == FALSE), the shift table is initialized.
2291 * So on the first search with a given pattern, *repeat_find
2292 * should be FALSE. This function sets it to TRUE after the
2293 * shift table is initialised, allowing the initialisation
2294 * phase to be skipped on subsequent searches.
2295 *
2296 * This function is most effective when repeated searches are
2297 * made for the same pattern in one or more large buffers.
2298 *
2299 * Example:
2300 *
2301 + PSZ pszHaystack = "This is a sample string.",
2302 + pszNeedle = "string";
2303 + size_t shift[256];
2304 + BOOL fRepeat = FALSE;
2305 +
2306 + PSZ pFound = strhmemfind(pszHaystack,
2307 + strlen(pszHaystack), // block size
2308 + pszNeedle,
2309 + strlen(pszNeedle), // pattern size
2310 + shift,
2311 + &fRepeat);
2312 *
2313 * Taken from the "Standard Function Library", file sflfind.c.
2314 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
2315 * Slightly modified by umoeller.
2316 *
2317 *@@added V0.9.3 (2000-05-08) [umoeller]
2318 */
2319
2320void* strhmemfind(const void *in_block, // in: block containing data
2321 size_t block_size, // in: size of block in bytes
2322 const void *in_pattern, // in: pattern to search for
2323 size_t pattern_size, // in: size of pattern block
2324 size_t *shift, // in/out: shift table (search buffer)
2325 BOOL *repeat_find) // in/out: if TRUE, *shift is already initialized
2326{
2327 size_t byte_nbr, // Distance through block
2328 match_size; // Size of matched part
2329 const unsigned char
2330 *match_base = NULL, // Base of match of pattern
2331 *match_ptr = NULL, // Point within current match
2332 *limit = NULL; // Last potiental match point
2333 const unsigned char
2334 *block = (unsigned char *) in_block, // Concrete pointer to block data
2335 *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value
2336
2337 if ( (block == NULL)
2338 || (pattern == NULL)
2339 || (shift == NULL)
2340 )
2341 return (NULL);
2342
2343 // Pattern must be smaller or equal in size to string
2344 if (block_size < pattern_size)
2345 return (NULL); // Otherwise it's not found
2346
2347 if (pattern_size == 0) // Empty patterns match at start
2348 return ((void *)block);
2349
2350 // Build the shift table unless we're continuing a previous search
2351
2352 // The shift table determines how far to shift before trying to match
2353 // again, if a match at this point fails. If the byte after where the
2354 // end of our pattern falls is not in our pattern, then we start to
2355 // match again after that byte; otherwise we line up the last occurence
2356 // of that byte in our pattern under that byte, and try match again.
2357
2358 if (!repeat_find || !*repeat_find)
2359 {
2360 for (byte_nbr = 0;
2361 byte_nbr < 256;
2362 byte_nbr++)
2363 shift[byte_nbr] = pattern_size + 1;
2364 for (byte_nbr = 0;
2365 byte_nbr < pattern_size;
2366 byte_nbr++)
2367 shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr;
2368
2369 if (repeat_find)
2370 *repeat_find = TRUE;
2371 }
2372
2373 // Search for the block, each time jumping up by the amount
2374 // computed in the shift table
2375
2376 limit = block + (block_size - pattern_size + 1);
2377 ASSERT (limit > block);
2378
2379 for (match_base = block;
2380 match_base < limit;
2381 match_base += shift[*(match_base + pattern_size)])
2382 {
2383 match_ptr = match_base;
2384 match_size = 0;
2385
2386 // Compare pattern until it all matches, or we find a difference
2387 while (*match_ptr++ == pattern[match_size++])
2388 {
2389 ASSERT (match_size <= pattern_size &&
2390 match_ptr == (match_base + match_size));
2391
2392 // If we found a match, return the start address
2393 if (match_size >= pattern_size)
2394 return ((void*)(match_base));
2395
2396 }
2397 }
2398 return (NULL); // Found nothing
2399}
2400
2401/*
2402 *@@ strhtxtfind:
2403 * searches for a case-insensitive text pattern in a string
2404 * using the Boyer-Moore-Horspool-Sunday algorithm. The string and
2405 * pattern are null-terminated strings. Returns a pointer to the pattern
2406 * if found within the string, or NULL if the pattern was not found.
2407 * Will match strings irrespective of case. To match exact strings, use
2408 * strhfind(). Will not work on multibyte characters.
2409 *
2410 * Examples:
2411 + char *result;
2412 +
2413 + result = strhtxtfind ("AbracaDabra", "cad");
2414 + if (result)
2415 + puts (result);
2416 +
2417 * Taken from the "Standard Function Library", file sflfind.c.
2418 * Copyright: Copyright (c) 1991-99 iMatix Corporation.
2419 * Slightly modified.
2420 *
2421 *@@added V0.9.3 (2000-05-08) [umoeller]
2422 */
2423
2424char* strhtxtfind (const char *string, // String containing data
2425 const char *pattern) // Pattern to search for
2426{
2427 size_t
2428 shift [256]; // Shift distance for each value
2429 size_t
2430 string_size,
2431 pattern_size,
2432 byte_nbr, // Index into byte array
2433 match_size; // Size of matched part
2434 const char
2435 *match_base = NULL, // Base of match of pattern
2436 *match_ptr = NULL, // Point within current match
2437 *limit = NULL; // Last potiental match point
2438
2439 ASSERT (string); // Expect non-NULL pointers, but
2440 ASSERT (pattern); // fail gracefully if not debugging
2441 if (string == NULL || pattern == NULL)
2442 return (NULL);
2443
2444 string_size = strlen (string);
2445 pattern_size = strlen (pattern);
2446
2447 // Pattern must be smaller or equal in size to string
2448 if (string_size < pattern_size)
2449 return (NULL); // Otherwise it cannot be found
2450
2451 if (pattern_size == 0) // Empty string matches at start
2452 return (char *) string;
2453
2454 // Build the shift table
2455
2456 // The shift table determines how far to shift before trying to match
2457 // again, if a match at this point fails. If the byte after where the
2458 // end of our pattern falls is not in our pattern, then we start to
2459 // match again after that byte; otherwise we line up the last occurence
2460 // of that byte in our pattern under that byte, and try match again.
2461
2462 for (byte_nbr = 0; byte_nbr < 256; byte_nbr++)
2463 shift [byte_nbr] = pattern_size + 1;
2464
2465 for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++)
2466 shift [(unsigned char) tolower (pattern [byte_nbr])] = pattern_size - byte_nbr;
2467
2468 // Search for the string. If we don't find a match, move up by the
2469 // amount we computed in the shift table above, to find location of
2470 // the next potiental match.
2471
2472 limit = string + (string_size - pattern_size + 1);
2473 ASSERT (limit > string);
2474
2475 for (match_base = string;
2476 match_base < limit;
2477 match_base += shift [(unsigned char) tolower (*(match_base + pattern_size))])
2478 {
2479 match_ptr = match_base;
2480 match_size = 0;
2481
2482 // Compare pattern until it all matches, or we find a difference
2483 while (tolower (*match_ptr++) == tolower (pattern [match_size++]))
2484 {
2485 ASSERT (match_size <= pattern_size &&
2486 match_ptr == (match_base + match_size));
2487
2488 // If we found a match, return the start address
2489 if (match_size >= pattern_size)
2490 return ((char *)(match_base));
2491 }
2492 }
2493 return (NULL); // Found nothing
2494}
2495
Note: See TracBrowser for help on using the repository browser.