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

  1. /***
  2. *mbsncat.c - concatenate string2 onto string1, max length n
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines mbsncat() - concatenate maximum of n characters
  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. * _mbsncat - concatenate max cnt characters onto dst
  25. *
  26. *Purpose:
  27. *       Concatenates src onto dst, with a maximum of cnt characters 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 characters 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 _mbsncat(
  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 (_ismbslead(start, dst))
  67.             --dst;
  68.  
  69.         /* copy over the characters */
  70.  
  71.         while (cnt--) {
  72.  
  73.             if (_ISLEADBYTE(*src)) {
  74.                 *dst++ = *src++;
  75.                 if ((*dst++ = *src++) == '\0') {
  76.                     dst[-2] = '\0';
  77.                     break;
  78.                 }
  79.             }
  80.  
  81.             else if ((*dst++ = *src++) == '\0')
  82.                 break;
  83.  
  84.         }
  85.  
  86.         /* enter final nul, if necessary */
  87.  
  88.         if (_MBSBTYPE(start, (int) ((dst - start) - 1)) == _MBC_LEAD)
  89.             dst[-1] = '\0';
  90.         else
  91.             *dst = '\0';
  92.  
  93.  
  94.         _munlock(_MB_CP_LOCK);
  95.         return(start);
  96. }
  97.  
  98. #endif  /* _MBCS */
  99.