home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / mbsstr.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  84 lines

  1. /***
  2. * mbsstr.c - Search for one MBCS string inside another
  3. *
  4. *       Copyright (c) 1988-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Search for one MBCS string inside another
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <cruntime.h>
  14. #include <mbdata.h>
  15. #include <mbctype.h>
  16. #include <mbstring.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19.  
  20. #define _BYTELEN(s)     (strlen(s))
  21. #define _MBSINC(s)      (_mbsinc(s))
  22. #define PCHAR           char *
  23.  
  24. /***
  25. * _mbsstr - Search for one MBCS string inside another
  26. *
  27. *Purpose:
  28. *       Find the first occurrence of str2 in str1.
  29. *
  30. *Entry:
  31. *       unsigned char *str1 = beginning of string
  32. *       unsigned char *str2 = string to search for
  33. *
  34. *Exit:
  35. *       Returns a pointer to the first occurrence of str2 in
  36. *       str1, or NULL if str2 does not occur in str1
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. unsigned char * __cdecl _mbsstr(
  43.         const unsigned char *str1,
  44.         const unsigned char *str2
  45.         )
  46. {
  47.         unsigned char *cp, *s1, *s2, *endp;
  48.  
  49.         if ( _ISNOTMBCP )
  50.                 return strstr(str1, str2);
  51.  
  52.         cp = (unsigned char *) str1;
  53.         endp = (unsigned PCHAR) (str1 + (_BYTELEN(str1) - _BYTELEN(str2)));
  54.  
  55.         while (*cp && (cp <= endp))
  56.         {
  57.                 s1 = cp;
  58.                 s2 = (PCHAR) str2;
  59.  
  60.                 /*
  61.                  * MBCS: ok to ++ since doing equality comparison.
  62.                  * [This depends on MBCS strings being "legal".]
  63.                  */
  64.  
  65.                 while ( *s1 && *s2 && (*s1 == *s2) )
  66.                         s1++, s2++;
  67.  
  68.                 if (!(*s2))
  69.                         return(cp);     /* success! */
  70.  
  71.                 /*
  72.                  * bump pointer to next char
  73.                  */
  74.  
  75.                 cp = _MBSINC(cp);
  76.  
  77.         }
  78.  
  79.         return(NULL);
  80.  
  81. }
  82.  
  83. #endif  /* _MBCS */
  84.