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

  1. /***
  2. *mbsnbcat.c - concatenate string2 onto string1, max length n bytes
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines mbsnbcat() - concatenate maximum of n bytes
  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. #define _MBSBTYPE(str,len)      _mbsbtype(str,len)
  22.  
  23. /***
  24. * _mbsnbcat - concatenate max cnt bytes onto dst
  25. *
  26. *Purpose:
  27. *       Concatenates src onto dst, with a maximum of cnt bytes copied.
  28. *       Handles 2-byte MBCS characters correctly.
  29. *
  30. *Entry:
  31. *       unsigned char *dst - string to concatenate onto
  32. *       unsigned char *src - string to concatenate from
  33. *       int cnt - number of bytes to copy
  34. *
  35. *Exit:
  36. *       returns dst, with src (at least part) concatenated on
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. unsigned char * __cdecl _mbsnbcat(
  43.         unsigned char *dst,
  44.         const unsigned char *src,
  45.         size_t cnt
  46.         )
  47. {
  48.         unsigned char *start;
  49.  
  50.         if (!cnt)
  51.                 return(dst);
  52.  
  53.         if ( _ISNOTMBCP )
  54.             return strncat(dst, src, cnt);
  55.  
  56.         _mlock(_MB_CP_LOCK);
  57.  
  58.         start = dst;
  59.         while (*dst++)
  60.                 ;
  61.         --dst;          // dst now points to end of dst string
  62.  
  63.  
  64.         /* if last char in string is a lead byte, back up pointer */
  65.  
  66.         if (_MBSBTYPE(start, (int) ((dst - start) - 1)) == _MBC_LEAD)
  67.                 --dst;
  68.  
  69.         /* copy over the characters */
  70.  
  71.         while (cnt--) {
  72.  
  73.                 if (_ISLEADBYTE(*src)) {
  74.                         *dst++ = *src++;
  75.                         if (!cnt--) {   /* write nul if cnt exhausted */
  76.                                 dst[-1] = '\0';
  77.                                 break;
  78.                         }
  79.                         if ((*dst++ = *src++)=='\0') { /* or if no trail byte */
  80.                                 dst[-2] = '\0';
  81.                                 break;
  82.                         }
  83.                 }
  84.  
  85.                 else if ((*dst++ = *src++) == '\0')
  86.                         break;
  87.  
  88.         }
  89.  
  90.         /* enter final nul, if necessary */
  91.  
  92.         if (_MBSBTYPE(start, (int) ((dst - start) - 1)) == _MBC_LEAD)
  93.             dst[-1] = '\0';
  94.         else
  95.             *dst = '\0';
  96.  
  97.  
  98.         _munlock(_MB_CP_LOCK);
  99.         return(start);
  100. }
  101.  
  102. #endif  /* _MBCS */
  103.