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

  1. /***
  2. *mbsncpy.c - Copy one string to another, n chars only (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Copy one string to another, n chars only (MBCS)
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <mtdll.h>
  14. #include <cruntime.h>
  15. #include <string.h>
  16. #include <mbdata.h>
  17. #include <mbctype.h>
  18. #include <mbstring.h>
  19.  
  20.  
  21. /***
  22. * _mbsncpy - Copy one string to another, n chars only (MBCS)
  23. *
  24. *Purpose:
  25. *       Copies exactly cnt character from src to dst.  If strlen(src) < cnt, the
  26. *       remaining character are padded with null bytes.  If strlen >= cnt, no
  27. *       terminating null byte is added.  2-byte MBCS characters are handled
  28. *       correctly.
  29. *
  30. *Entry:
  31. *       unsigned char *dst = destination for copy
  32. *       unsigned char *src = source for copy
  33. *       int cnt = number of characters to copy
  34. *
  35. *Exit:
  36. *       returns dst = destination of copy
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. unsigned char * __cdecl _mbsncpy(
  43.         unsigned char *dst,
  44.         const unsigned char *src,
  45.         size_t cnt
  46.         )
  47. {
  48.  
  49.         unsigned char *start = dst;
  50.  
  51.         if ( _ISNOTMBCP )
  52.             return strncpy(dst, src, cnt);
  53.  
  54.         _mlock(_MB_CP_LOCK);
  55.  
  56.         while (cnt) {
  57.  
  58.                 cnt--;
  59.                 if (_ISLEADBYTE(*src)) {
  60.                         *dst++ = *src++;
  61.                         if ((*dst++ = *src++) == '\0') {
  62.                                 dst[-2] = '\0';
  63.                                 break;
  64.                         }
  65.                 }
  66.  
  67.                 else
  68.                         if ((*dst++ = *src++) == '\0')
  69.                                 break;
  70.  
  71.         }
  72.  
  73.         /* pad with nulls as needed */
  74.  
  75.         while (cnt--)
  76.                 *dst++ = '\0';
  77.  
  78.         _munlock(_MB_CP_LOCK);
  79.         return start;
  80. }
  81.  
  82. #endif  /* _MBCS */
  83.