Ignore:
Timestamp:
Nov 23, 2000, 7:36:41 PM (25 years ago)
Author:
umoeller
Message:

Updates for V0.9.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/stringh.c

    r12 r13  
    33 *@@sourcefile stringh.c:
    44 *      contains string/text helper functions. These are good for
    5  *      parsing/splitting strings and other stuff used throughout XWorkplace.
     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.
    612 *
    713 *      Usage: All OS/2 programs.
     
    180186 *      this creates a new PSZ containing the string
    181187 *      from pBegin to pEnd, excluding the pEnd character.
    182  *      The new string is null-terminated.
     188 *      The new string is null-terminated. The caller
     189 *      must free() the new string after use.
    183190 *
    184191 *      Example:
     
    424431 *      be free()'able.
    425432 *
    426  *      Use of this wrapper is not recommended because
    427  *      it is considerably slower than xstrrpl.
     433 *      Repetitive use of this wrapper is not recommended
     434 *      because it is considerably slower than xstrrpl.
    428435 *
    429436 *@@added V0.9.6 (2000-11-01) [umoeller]
     
    431438
    432439ULONG strhrpl(PSZ *ppszBuf,                // in/out: string
    433               ULONG ulOfs,                  // in: where to begin search (0 = start)
    434               const char *pcszSearch,    // in: search string; cannot be NULL
    435               const char *pcszReplace,   // in: replacement string; cannot be NULL
    436               PULONG pulAfterOfs)           // out: offset where found (ptr can be NULL)
     440              PULONG pulOfs,               // in: where to begin search (0 = start);
     441                                           // out: ofs of first char after replacement string
     442              const char *pcszSearch,      // in: search string; cannot be NULL
     443              const char *pcszReplace)     // in: replacement string; cannot be NULL
    437444{
    438445    ULONG   ulrc = 0;
     
    440447            xstrFind,
    441448            xstrReplace;
     449    size_t  ShiftTable[256];
     450    BOOL    fRepeat = FALSE;
    442451    xstrInit(&xstrBuf, 0);
    443452    xstrset(&xstrBuf, *ppszBuf);
     
    447456    xstrset(&xstrReplace, (PSZ)pcszReplace);
    448457
    449     if (ulrc = xstrrpl(&xstrBuf, ulOfs, &xstrFind, &xstrReplace, pulAfterOfs))
     458    if ((ulrc = xstrrpl(&xstrBuf,
     459                        pulOfs,
     460                        &xstrFind,
     461                        &xstrReplace,
     462                        ShiftTable,
     463                        &fRepeat)))
    450464        // replaced:
    451465        *ppszBuf = xstrBuf.psz;
     
    482496 *      converts a ULONG into a decimal string, while
    483497 *      inserting thousands separators into it. Specify
    484  *      the separator char in cThousands.
     498 *      the separator character in cThousands.
     499 *
    485500 *      Returns pszTarget so you can use it directly
    486501 *      with sprintf and the "%s" flag.
     502 *
    487503 *      For cThousands, you should use the data in
    488504 *      OS2.INI ("PM_National" application), which is
    489505 *      always set according to the "Country" object.
     506 *      You can use prfhQueryCountrySettings to
     507 *      retrieve this setting.
     508 *
    490509 *      Use strhThousandsDouble for "double" values.
    491510 */
     
    497516    USHORT ust, uss, usc;
    498517    CHAR   szTemp[40];
    499     sprintf(szTemp, "%d", ul);
     518    sprintf(szTemp, "%lu", ul);
    500519
    501520    ust = 0;
     
    548567
    549568/*
     569 *@@ strhVariableDouble:
     570 *      like strhThousandsULong, but for a "double" value, and
     571 *      with a variable number of decimal places depending on the
     572 *      size of the quantity.
     573 *
     574 *@@added V0.9.6 (2000-11-12) [pr]
     575 */
     576
     577PSZ strhVariableDouble(PSZ pszTarget,
     578                       double dbl,
     579                       PSZ pszUnits,
     580                       CHAR cThousands)
     581{
     582    if (dbl < 100.0)
     583        sprintf(pszTarget, "%.2f%s", dbl, pszUnits);
     584    else
     585        if (dbl < 1000.0)
     586            sprintf(pszTarget, "%.1f%s", dbl, pszUnits);
     587        else
     588            strcat(strhThousandsDouble(pszTarget, dbl, cThousands),
     589                   pszUnits);
     590
     591    return(pszTarget);
     592}
     593
     594/*
    550595 *@@ strhFileDate:
    551596 *      converts file date data to a string (to pszBuf).
     
    566611 *      cDateSep is used as a date separator (e.g. '.').
    567612 *      This can be queried using:
    568  +              prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/');
     613 +          prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/');
    569614 *
    570615 *      Alternatively, you can query all the country settings
    571  *      at once using prfhQueryCountrySettings (prfh.c, new with V0.9.0).
     616 *      at once using prfhQueryCountrySettings (prfh.c).
    572617 *
    573618 *@@changed (99-11-07) [umoeller]: now calling strhDateTime
     
    611656 *
    612657 *      Alternatively, you can query all the country settings
    613  *      at once using prfhQueryCountrySettings (prfh.c, new with V0.9.0).
     658 *      at once using prfhQueryCountrySettings (prfh.c).
    614659 *
    615660 *@@changed 99-03-15 fixed 12-hour crash
     
    637682/*
    638683 *@@ strhDateTime:
    639  *      converts Control Programe DATETIME info
    640  *      to two strings. See strhFileDate and strhFileTime
     684 *      converts Control Program DATETIME info
     685 *      into two strings. See strhFileDate and strhFileTime
    641686 *      for more detailed parameter descriptions.
    642687 *
     
    821866
    822867/*
     868 *@@ strhIsWord:
     869 *      returns TRUE if p points to a "word"
     870 *      in pcszBuf.
     871 *
     872 *      p is considered a word if the character _before_
     873 *      it is in pcszBeginChars and the char _after_
     874 *      it (i.e. *(p+cbSearch)) is in pcszEndChars.
     875 *
     876 *@@added V0.9.6 (2000-11-12) [umoeller]
     877 */
     878
     879BOOL strhIsWord(const char *pcszBuf,
     880                const char *p,                 // in: start of word
     881                ULONG cbSearch,                // in: length of word
     882                const char *pcszBeginChars,    // suggestion: "\x0d\x0a ()/\\-,."
     883                const char *pcszEndChars)      // suggestion: "\x0d\x0a ()/\\-,.:;"
     884{
     885    BOOL    fEndOK = FALSE;
     886
     887    // check previous char
     888    if (    (p == pcszBuf)
     889         || (strchr(pcszBeginChars, *(p-1)))
     890       )
     891    {
     892        // OK, valid begin char:
     893        // check end char
     894        CHAR    cNextChar = *(p + cbSearch);
     895        if (cNextChar == 0)
     896            fEndOK = TRUE;
     897        else
     898        {
     899            char *pc = strchr(pcszEndChars, cNextChar);
     900            if (pc)
     901                // OK, is end char: avoid doubles of that char,
     902                // but allow spaces
     903                if (    (cNextChar+1 != *pc)
     904                     || (cNextChar+1 == ' ')
     905                     || (cNextChar+1 == 0)
     906                   )
     907                    fEndOK = TRUE;
     908        }
     909    }
     910
     911    return (fEndOK);
     912}
     913
     914/*
    823915 *@@ strhFindWord:
    824916 *      searches for pszSearch in pszBuf, which is
     
    836928 *
    837929 *      The algorithm here uses strstr to find pszSearch in pszBuf
    838  *      and performs additional "is-word" checks for each item found.
    839  *      With VAC++ 3.0, this is still much faster than searching
    840  *      words first and then comparing each word with pszSearch.
    841  *      I've tried it that way too, and that took nearly double as
    842  *      long. Apparently, the VAC++ runtime library uses some
    843  *      optimized search algorithm here, so we better use that one.
     930 *      and performs additional "is-word" checks for each item found
     931 *      (by calling strhIsWord).
     932 *
     933 *      Note that this function is fairly slow compared to xstrFindWord.
    844934 *
    845935 *@@added V0.9.0 (99-11-08) [umoeller]
     
    859949    {
    860950        const char *p = pszBuf;
    861 
    862         /*  // go thru all characters
    863         while (*p)
    864         {
    865             // check if current character is either the
    866             // very first or a "begin word" character
    867             if (    (p == pszBuf)
    868                  || (strchr(pcszBeginChars, *p) == 0)
    869                )
    870             {
    871                 // yes: go for next
    872                 if (*(++p))
    873                 {
    874                     // compare with search string
    875                     if (strcmp(p, pszSearch) == 0)
    876                     {
    877                         // is the same:
    878                         // check if still in buffer
    879                         if (p < pszBuf + cbBuf)
    880                         {
    881                             CHAR    cAfterEndOfWord = *(p + cbSearch);
    882                             if (cAfterEndOfWord == 0)
    883                             {
    884                                 // end of string:
    885                                 // that's ok
    886                                 pszReturn = (PSZ)p;
    887                                 break;
    888                             }
    889                             else
    890                             {
    891                                 // check if in "end of word" list
    892                                 char *pc2 = strchr(pcszEndChars, cAfterEndOfWord);
    893                                 if (pc2)
    894                                     // OK, is end char: avoid doubles of that char,
    895                                     // but allow spaces
    896                                     if (    (cAfterEndOfWord+1 != *pc2)
    897                                          || (cAfterEndOfWord+1 == ' ')
    898                                          || (cAfterEndOfWord+1 == 0)
    899                                        )
    900                                     {
    901                                         // end of string:
    902                                         // that's ok
    903                                         pszReturn = (PSZ)p;
    904                                         break;
    905                                     }
    906                             }
    907                         }
    908                     }
    909                 }
    910                 else
    911                     // end of string:
    912                     break;
    913             }
    914 
    915             ++p;
    916         } // end while
    917         */
    918951
    919952        do  // while p
     
    925958                // check if that's a word
    926959
    927                 // check previous char
    928                 if (    (p == pszBuf)
    929                      || (strchr(pcszBeginChars, *(p-1)))
    930                    )
     960                if (strhIsWord(pszBuf,
     961                               p,
     962                               cbSearch,
     963                               pcszBeginChars,
     964                               pcszEndChars))
    931965                {
    932                     // OK, valid begin char:
    933                     BOOL    fEndOK = FALSE;
    934                     // check end char
    935                     CHAR    cNextChar = *(p + cbSearch);
    936                     if (cNextChar == 0)
    937                         fEndOK = TRUE;
    938                     else
    939                     {
    940                         char *pc = strchr(pcszEndChars, cNextChar);
    941                         if (pc)
    942                             // OK, is end char: avoid doubles of that char,
    943                             // but allow spaces
    944                             if (    (cNextChar+1 != *pc)
    945                                  || (cNextChar+1 == ' ')
    946                                  || (cNextChar+1 == 0)
    947                                )
    948                                 fEndOK = TRUE;
    949                     }
    950 
    951                     if (fEndOK)
    952                     {
    953                         // valid end char:
    954                         pszReturn = (PSZ)p;
    955                         break;
    956                     }
     966                    // valid end char:
     967                    pszReturn = (PSZ)p;
     968                    break;
    957969                }
     970
    958971                p += cbSearch;
    959972            }
     
    11861199            if (pEOL)
    11871200            {
    1188                 XSTRING strBuf,
    1189                         strFind,
    1190                         strReplace;
     1201                XSTRING strBuf;
     1202                ULONG   ulOfs = 0;
    11911203
    11921204                PSZ pszOldCopy = (PSZ)malloc(cbOldParam+1);
     
    11961208                xstrInit(&strBuf, 0);
    11971209                xstrset(&strBuf, *ppszBuf);         // this must not be freed!
    1198                 xstrInit(&strFind, 0);
     1210                /* xstrInit(&strFind, 0);
    11991211                xstrset(&strFind, pszOldCopy);      // this must not be freed!
    12001212                xstrInit(&strReplace, 0);
    12011213                xstrset(&strReplace, pszNewParam);  // this must not be freed!
     1214                   */
    12021215
    12031216                // check for upper case desired?
     
    12061219                        strupr(pszNewParam);
    12071220
    1208                 xstrrpl(&strBuf, 0, &strFind, &strReplace, NULL);
     1221                xstrcrpl(&strBuf, &ulOfs, pszOldCopy, pszNewParam);
    12091222
    12101223                free(pszOldCopy);
     
    13731386    PSZ pParam;
    13741387    if ((pParam = strhFindAttribValue(pszSearchIn, pszTag)))
    1375         sscanf(pParam, "%d", pl);
     1388        sscanf(pParam, "%ld", pl);
    13761389
    13771390    return (pParam);
     
    17991812            pszLine += ulIndent;
    18001813        }
    1801         pszLine += sprintf(pszLine, "%02lX ", *pbCurrent);
     1814        pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent);
    18021815
    18031816        if ( (*pbCurrent > 31) && (*pbCurrent < 127) )
     
    18691882
    18701883/*
    1871  *@@ skip_comp_os2:
     1884 * skip_comp_os2:
    18721885 *      Return a pointer to the next component of the path SRC, for OS/2
    18731886 *      and DOS styles.  When the end of the string is reached, a pointer
     
    24672480
    24682481/*
    2469  *@@ strhfind:
    2470  *      searches for a pattern in a string using the Boyer-Moore-
    2471  *      Horspool-Sunday algorithm.  The string and pattern are null-terminated
    2472  *      strings.  Returns a pointer to the pattern if found within the string,
    2473  *      or NULL if the pattern was not found.  If you repeatedly scan for the
    2474  *      same pattern, use the repeat_find argument.  If this is TRUE, the
    2475  *      function does not re-parse the pattern.  You must of course call the
    2476  *      function with repeat_find equal to FALSE the first time.  This function
    2477  *      is meant to handle  character data, and is most effective when you work
    2478  *      with large strings.  To search binary data use strhmemfind().  Will not work
    2479  *      on multibyte characters.
    2480  *
    2481  *      Examples:
    2482  +      char *result;
     2482 *@@ strhmemfind:
     2483 *      searches for a pattern in a block of memory using the
     2484 *      Boyer-Moore-Horspool-Sunday algorithm.
     2485 *
     2486 *      The block and pattern may contain any values; you must
     2487 *      explicitly provide their lengths. If you search for strings,
     2488 *      use strlen() on the buffers.
     2489 *
     2490 *      Returns a pointer to the pattern if found within the block,
     2491 *      or NULL if the pattern was not found.
     2492 *
     2493 *      This algorithm needs a "shift table" to cache data for the
     2494 *      search pattern. This table can be reused when performing
     2495 *      several searches with the same pattern.
     2496 *
     2497 *      "shift" must point to an array big enough to hold 256 (8**2)
     2498 *      "size_t" values.
     2499 *
     2500 *      If (*repeat_find == FALSE), the shift table is initialized.
     2501 *      So on the first search with a given pattern, *repeat_find
     2502 *      should be FALSE. This function sets it to TRUE after the
     2503 *      shift table is initialised, allowing the initialisation
     2504 *      phase to be skipped on subsequent searches.
     2505 *
     2506 *      This function is most effective when repeated searches are
     2507 *      made for the same pattern in one or more large buffers.
     2508 *
     2509 *      Example:
     2510 *
     2511 +          PSZ     pszHaystack = "This is a sample string.",
     2512 +                  pszNeedle = "string";
     2513 +          size_t  shift[256];
     2514 +          BOOL    fRepeat = FALSE;
    24832515 +
    2484  +      result = strhfind ("abracadabra", "cad", FALSE);
    2485  +      if (result)
    2486  +          puts (result);
    2487  +
     2516 +          PSZ     pFound = strhmemfind(pszHaystack,
     2517 +                                       strlen(pszHaystack),   // block size
     2518 +                                       pszNeedle,
     2519 +                                       strlen(pszNeedle),     // pattern size
     2520 +                                       shift,
     2521 +                                       &fRepeat);
     2522 *
    24882523 *      Taken from the "Standard Function Library", file sflfind.c.
    24892524 *      Copyright:  Copyright (c) 1991-99 iMatix Corporation.
    2490  *      Slightly modified.
     2525 *      Slightly modified by umoeller.
    24912526 *
    24922527 *@@added V0.9.3 (2000-05-08) [umoeller]
    24932528 */
    24942529
    2495 char* strhfind (const char *string,            //  String containing data
    2496                 const char *pattern,           //  Pattern to search for
    2497                 BOOL repeat_find)              //  Same pattern as last time
    2498 {
    2499     static size_t
    2500         searchbuf [256];                //  Fixed search buffer
    2501 
    2502     ASSERT (string);                    //  Expect non-NULL pointers, but
    2503     ASSERT (pattern);                   //  fall through if not debugging
    2504 
    2505     return (char *) strhmemfind_rb (string,    strlen (string),
    2506                                 pattern,   strlen (pattern),
    2507                                 searchbuf, &repeat_find);
    2508 }
    2509 
    2510 /*
    2511  *@@ strhfind_r:
    2512  *      searches for a pattern in a string using the Boyer-Moore-
    2513  *      Horspool-Sunday algorithm.  The string and pattern are null-terminated
    2514  *      strings.  Returns a pointer to the pattern if found within the string,
    2515  *      or NULL if the pattern was not found.  This function is meant to handle
    2516  *      character data, and is most effective when you work with large strings.
    2517  *      To search binary data use strhmemfind().  Will not work on multibyte
    2518  *      characters.  Reentrant.
    2519  *
    2520  *      Examples:
    2521  +      char *result;
    2522  +
    2523  +      result = strhfind_r ("abracadabra", "cad");
    2524  +      if (result)
    2525  +          puts (result);
    2526  *
    2527  *      Taken from the "Standard Function Library", file sflfind.c.
    2528  *      Copyright:  Copyright (c) 1991-99 iMatix Corporation.
    2529  *      Slightly modified.
    2530  *
    2531  *@@added V0.9.3 (2000-05-08) [umoeller]
    2532  */
    2533 
    2534 char* strhfind_r (const char *string,          //  String containing data
    2535                   const char *pattern)         //  Pattern to search for
    2536 {
    2537     size_t
    2538         searchbuf [256];                //  One-time search buffer
    2539     BOOL
    2540         secondtime = FALSE;             //  Search buffer init needed
    2541 
    2542     ASSERT (string);                    //  Expect non-NULL pointers, but
    2543     ASSERT (pattern);                   //  fall through if not debugging
    2544 
    2545     return (char *) strhmemfind_rb (string,    strlen (string),
    2546                                     pattern,   strlen (pattern),
    2547                                     searchbuf, &secondtime);
    2548 }
    2549 
    2550 /*
    2551  *@@ strhfind_rb:
    2552  *      searches for a pattern in a string using the Boyer-Moore-
    2553  *      Horspool-Sunday algorithm.  The string and pattern are null-terminated
    2554  *      strings.  Returns a pointer to the pattern if found within the string,
    2555  *      or NULL if the pattern was not found.  Supports more efficient repeat
    2556  *      searches (for the same pattern), through a supplied search buffer.  The
    2557  *      search buffer must be long enough to contain 256 (2**8) size_t entries.
    2558  *      On the first call repeat_find must be set to FALSE.  After the search
    2559  *      buffer has been initialised, repeat_find will be set to TRUE by the
    2560  *      function, avoiding the search buffer initialisation on later calls.
    2561  *
    2562  *      This function is most effective when repeated searches are made for
    2563  *      the same pattern in one or more strings.  This function is meant to
    2564  *      handle  character data, and is most effective when you work with
    2565  *      large strings.  To search binary data use strhmemfind().  Will not work
    2566  *      on multibyte characters.  Reentrant.
    2567  *
    2568  *      Examples:
    2569  +      char   *result;
    2570  +      BOOL   repeat_search = FALSE;
    2571  +      size_t searchbuf[256];
    2572  +
    2573  +      result = strhfind_rb ("abracadabra", "cad", searchbuf, &repeat_search);
    2574  +      if (result)
    2575  +        {
    2576  +          puts (result);
    2577  +          result = strhfind_rb ("cad/cam", "cad", searchbuf, &repeat_search);
    2578  +          if (result)
    2579  +              puts (result);
    2580  +        }
    2581  *
    2582  *      Taken from the "Standard Function Library", file sflfind.c.
    2583  *      Copyright:  Copyright (c) 1991-99 iMatix Corporation.
    2584  *      Slightly modified.
    2585  *
    2586  *@@added V0.9.3 (2000-05-08) [umoeller]
    2587  */
    2588 
    2589 char* strhfind_rb (const char *string,         //  String containing data
    2590                    const char *pattern,        //  Pattern to search for
    2591                    size_t     *shift,          //  Working buffer between searches
    2592                    BOOL       *repeat_find)    //  Flag for first/later search
    2593 {
    2594     ASSERT (string);                    //  Expect non-NULL pointers, but
    2595     ASSERT (pattern);                   //  fall through if not debugging
    2596     ASSERT (shift);
    2597     ASSERT (repeat_find);
    2598 
    2599     return (char *) strhmemfind_rb (string,    strlen (string),
    2600                                     pattern,   strlen (pattern),
    2601                                     shift,     repeat_find);
    2602 }
    2603 
    2604 /*
    2605  *@@ strhmemfind:
    2606  *      searches for a pattern in a block of memory using the Boyer-
    2607  *      Moore-Horspool-Sunday algorithm.  The block and pattern may contain any
    2608  *      values; you must explicitly provide their lengths.  Returns a pointer to
    2609  *      the pattern if found within the block, or NULL if the pattern was not
    2610  *      found.  If you repeatedly scan for the same pattern, use the repeat_find
    2611  *      argument.  If this is TRUE, the function does not re-parse the pattern.
    2612  *      This function is meant to handle binary data.  If you need to search
    2613  *      strings, use the strhfind_r or strhfind_rb() functions.  Non-Reentrant.
    2614  *
    2615  *      Taken from the "Standard Function Library", file sflfind.c.
    2616  *      Copyright:  Copyright (c) 1991-99 iMatix Corporation.
    2617  *      Slightly modified.
    2618  *
    2619  *@@added V0.9.3 (2000-05-08) [umoeller]
    2620  */
    2621 
    2622 void* strhmemfind (const void  *block,            //  Block containing data
    2623                    size_t       block_size,       //  Size of block in bytes
    2624                    const void  *pattern,          //  Pattern to search for
    2625                    size_t       pattern_size,     //  Size of pattern block
    2626                    BOOL         repeat_find)      //  Same pattern as last time
    2627 {
    2628     static size_t
    2629         searchbuf [256];                //  Static shared search buffer
    2630 
    2631     ASSERT (block);                     //  Expect non-NULL pointers, but
    2632     ASSERT (pattern);                   //  full through if not debugging
    2633 
    2634     return strhmemfind_rb (block, block_size, pattern, pattern_size,
    2635                        searchbuf, &repeat_find);
    2636 }
    2637 
    2638 /*
    2639  *@@ strhmemfind_r:
    2640  *      searches for a pattern in a block of memory using the Boyer-
    2641  *      Moore-Horspool-Sunday algorithm.  The block and pattern may contain any
    2642  *      values; you must explicitly provide their lengths.  Returns a pointer to
    2643  *      the pattern if found within the block, or NULL if the pattern was not
    2644  *      found.
    2645  *
    2646  *      This function is meant to handle binary data, for a single search for
    2647  *      a given pattern.  If you need to search strings, use the strhfind_r()
    2648  *      or strhfind_rb() functions.  If you want to do efficient repeated searches
    2649  *      for one pattern, use strhmemfind_rb().  Reentrant.
    2650  *
    2651  *      Taken from the "Standard Function Library", file sflfind.c.
    2652  *      Copyright:  Copyright (c) 1991-99 iMatix Corporation.
    2653  *      Slightly modified.
    2654  *
    2655  *@@added V0.9.3 (2000-05-08) [umoeller]
    2656  */
    2657 
    2658 void* strhmemfind_r (const void  *block,          //  Block containing data
    2659                      size_t       block_size,     //  Size of block in bytes
    2660                      const void  *pattern,        //  Pattern to search for
    2661                      size_t       pattern_size)   //  Size of pattern block
    2662 {
    2663     size_t
    2664         searchbuf [256];                //  One-time search buffer
    2665     BOOL
    2666         secondtime  = FALSE;
    2667 
    2668     ASSERT (block);                     //  Expect non-NULL pointers, but
    2669     ASSERT (pattern);                   //  full through if not debugging
    2670 
    2671     return strhmemfind_rb (block, block_size, pattern, pattern_size,
    2672                            searchbuf, &secondtime);
    2673 }
    2674 
    2675 /*
    2676  *@@ strhmemfind_rb:
    2677  *      searches for a pattern in a block of memory using the Boyer-
    2678  *      Moore-Horspool-Sunday algorithm.  The block and pattern may contain any
    2679  *      values; you must explicitly provide their lengths.  Returns a pointer to
    2680  *      the pattern if found within the block, or NULL if the pattern was not
    2681  *      found.  On the first search with a given pattern, *repeat_find should
    2682  *      be FALSE.  It will be set to TRUE after the shift table is initialised,
    2683  *      allowing the initialisation phase to be skipped on subsequent searches.
    2684  *      shift must point to an array big enough to hold 256 (8**2) size_t values.
    2685  *
    2686  *      This function is meant to handle binary data, for repeated searches
    2687  *      for the same pattern.  If you need to search strings, use the
    2688  *      strhfind_r() or strhfind_rb() functions.  If you wish to search for a
    2689  *      pattern only once consider using strhmemfind_r(). Reentrant.
    2690  *
    2691  *      Taken from the "Standard Function Library", file sflfind.c.
    2692  *      Copyright:  Copyright (c) 1991-99 iMatix Corporation.
    2693  *      Slightly modified.
    2694  *
    2695  *@@added V0.9.3 (2000-05-08) [umoeller]
    2696  */
    2697 
    2698 void* strhmemfind_rb (const void  *in_block,      //  Block containing data
    2699                       size_t       block_size,    //  Size of block in bytes
    2700                       const void  *in_pattern,    //  Pattern to search for
    2701                       size_t       pattern_size,  //  Size of pattern block
    2702                       size_t      *shift,         //  Shift table (search buffer)
    2703                       BOOL        *repeat_find)   //  TRUE: search buffer already init
    2704 {
    2705     size_t
    2706         byte_nbr,                       //  Distance through block
    2707         match_size;                     //  Size of matched part
     2530void* strhmemfind(const void *in_block,     // in: block containing data
     2531                  size_t block_size,        // in: size of block in bytes
     2532                  const void *in_pattern,   // in: pattern to search for
     2533                  size_t pattern_size,      // in: size of pattern block
     2534                  size_t *shift,            // in/out: shift table (search buffer)
     2535                  BOOL *repeat_find)        // in/out: if TRUE, *shift is already initialized
     2536{
     2537    size_t      byte_nbr,                       //  Distance through block
     2538                match_size;                     //  Size of matched part
    27082539    const unsigned char
    2709         *match_base = NULL,             //  Base of match of pattern
    2710         *match_ptr  = NULL,             //  Point within current match
    2711         *limit      = NULL;             //  Last potiental match point
     2540                *match_base = NULL,             //  Base of match of pattern
     2541                *match_ptr  = NULL,             //  Point within current match
     2542                *limit      = NULL;             //  Last potiental match point
    27122543    const unsigned char
    2713         *block   = (unsigned char *) in_block,   //  Concrete pointer to block data
    2714         *pattern = (unsigned char *) in_pattern; //  Concrete pointer to search value
    2715 
    2716     ASSERT (block);                     //  Expect non-NULL pointers, but
    2717     ASSERT (pattern);                   //  fail gracefully if not debugging
    2718     ASSERT (shift);                     //  NULL repeat_find => is false
    2719     if (block == NULL || pattern == NULL || shift == NULL)
     2544                *block   = (unsigned char *) in_block,   //  Concrete pointer to block data
     2545                *pattern = (unsigned char *) in_pattern; //  Concrete pointer to search value
     2546
     2547    if (    (block == NULL)
     2548         || (pattern == NULL)
     2549         || (shift == NULL)
     2550       )
    27202551        return (NULL);
    27212552
     
    27362567
    27372568    if (!repeat_find || !*repeat_find)
    2738       {
    2739         for (byte_nbr = 0; byte_nbr < 256; byte_nbr++)
    2740             shift [byte_nbr] = pattern_size + 1;
    2741         for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++)
    2742             shift [(unsigned char) pattern [byte_nbr]] = pattern_size - byte_nbr;
     2569    {
     2570        for (byte_nbr = 0;
     2571             byte_nbr < 256;
     2572             byte_nbr++)
     2573            shift[byte_nbr] = pattern_size + 1;
     2574        for (byte_nbr = 0;
     2575             byte_nbr < pattern_size;
     2576             byte_nbr++)
     2577            shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr;
    27432578
    27442579        if (repeat_find)
    27452580            *repeat_find = TRUE;
    2746       }
     2581    }
    27472582
    27482583    //  Search for the block, each time jumping up by the amount
     
    27542589    for (match_base = block;
    27552590         match_base < limit;
    2756          match_base += shift [*(match_base + pattern_size)])
    2757       {
     2591         match_base += shift[*(match_base + pattern_size)])
     2592    {
    27582593        match_ptr  = match_base;
    27592594        match_size = 0;
    27602595
    27612596        //  Compare pattern until it all matches, or we find a difference
    2762         while (*match_ptr++ == pattern [match_size++])
    2763           {
     2597        while (*match_ptr++ == pattern[match_size++])
     2598        {
    27642599            ASSERT (match_size <= pattern_size &&
    27652600                    match_ptr == (match_base + match_size));
    27662601
    2767             //  If we found a match, return the start address
     2602            // If we found a match, return the start address
    27682603            if (match_size >= pattern_size)
    2769               return ((void*)(match_base));
    2770 
    2771           }
    2772       }
     2604                return ((void*)(match_base));
     2605
     2606        }
     2607    }
    27732608    return (NULL);                      //  Found nothing
    27742609}
Note: See TracChangeset for help on using the changeset viewer.