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

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

Tons of changes from the last weeks.

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