home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FMEMMEM.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  127 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FMEMMEM.C - A strstr() work-alike for large non-text buffers
  5. **
  6. **  public domain by Bob Stout
  7. */
  8.  
  9. #include "snip_str.h"
  10. #include "snpdosys.h"                     /* For addptr() & farnormal() */
  11.  
  12. #if defined(__cplusplus) && __cplusplus
  13.  extern "C" {
  14. #endif
  15.  
  16. void FAR *fmemmem(const void FAR *buf,
  17.                   const void FAR *pattern,
  18.                   long            buflen,
  19.                   long            len)
  20. {
  21.       long i, j;
  22.       char FAR *bf = (char FAR *)buf, FAR *pt = (char FAR *)pattern;
  23.  
  24.       if (len > buflen)
  25.             return (void FAR *)NULL;
  26.  
  27.       for (i = 0L; i <= (buflen - len); ++i)
  28.       {
  29.             for (j = 0L; j < len; ++j)
  30.             {
  31.                   /*
  32.                   ** Not all compilers support huge pointers the same way
  33.                   ** (or at all!), so add the offsets to the pointers,
  34.                   ** normalize them, then dereference them.
  35.                   */
  36.  
  37.                   char FAR *pp = addptr(pt, j);       /* &pt[j]        */
  38.                   char FAR *bb = addptr(bf, i + j);   /* &bf[i + j]     */
  39.  
  40.                   if (*pp != *bb)                     /* pt[j]==bf[i+j] */
  41.                         break;
  42.             }
  43.             if (j == len)
  44.                   return addptr(bf, i);
  45.       }
  46.       return (void FAR *)NULL;
  47. }
  48.  
  49. #if defined(__cplusplus) && __cplusplus
  50.  }
  51. #endif
  52.  
  53. #ifdef TEST
  54.  
  55. #include <stdio.h>
  56.  
  57. main()
  58. {
  59.       char FAR buf[13] = "\0""12344567890\x1b";
  60.       char FAR a[3] = "456";
  61.       char FAR b[3] = "\0""12";
  62.       char FAR c[3] = "90\x1b";
  63.       char FAR d[3] = "ABC";
  64.       char FAR e[3] = "0\x1b""\0";
  65.       char FAR f[1] = "\x1b";
  66.       char FAR *ptr;
  67.       long lp, lb;
  68.  
  69.       if (NULL == (ptr = fmemmem(buf, a, 13L, 3L)))
  70.             puts("a not found in buf");
  71.       else
  72.       {
  73.             lp = (long)farnormal(ptr);
  74.             lb = (long)farnormal(buf);
  75.             printf("a found in buf at posn %ld\n", lp - lb);
  76.       }
  77.  
  78.       if (NULL == (ptr = fmemmem(buf, b, 13L, 3L)))
  79.             puts("b not found in buf");
  80.       else
  81.       {
  82.             lp = (long)farnormal(ptr);
  83.             lb = (long)farnormal(buf);
  84.             printf("b found in buf at posn %ld\n", lp - lb);
  85.       }
  86.  
  87.       if (NULL == (ptr = fmemmem(buf, c, 13L, 3L)))
  88.             puts("c not found in buf");
  89.       else
  90.       {
  91.             lp = (long)farnormal(ptr);
  92.             lb = (long)farnormal(buf);
  93.             printf("c found in buf at posn %ld\n", lp - lb);
  94.       }
  95.  
  96.       if (NULL == (ptr = fmemmem(buf, d, 13L, 3L)))
  97.             puts("d not found in buf");
  98.       else
  99.       {
  100.             lp = (long)farnormal(ptr);
  101.             lb = (long)farnormal(buf);
  102.             printf("d found in buf at posn %ld\n", lp - lb);
  103.       }
  104.  
  105.       if (NULL == (ptr = fmemmem(buf, e, 13L, 3L)))
  106.             puts("e not found in buf");
  107.       else
  108.       {
  109.             lp = (long)farnormal(ptr);
  110.             lb = (long)farnormal(buf);
  111.             printf("e found in buf at posn %ld\n", lp - lb);
  112.       }
  113.  
  114.       if (NULL == (ptr = fmemmem(buf, f, 13L, 1L)))
  115.             puts("f not found in buf");
  116.       else
  117.       {
  118.             lp = (long)farnormal(ptr);
  119.             lb = (long)farnormal(buf);
  120.             printf("f found in buf at posn %ld\n", lp - lb);
  121.       }
  122.       
  123.       return 0;
  124. }
  125.  
  126. #endif /* TEST */
  127.