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

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