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

  1. /***
  2. *mbsnset.c - Sets first n charcaters of string to given character (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Sets first n charcaters of string to given character (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. * _mbsnset - Sets first n charcaters of string to given character (MBCS)
  23. *
  24. *Purpose:
  25. *       Sets the first n characters of string to the supplied
  26. *       character value.  If the length of string is less than n,
  27. *       the length of string is used in place of n.  Handles
  28. *       MBCS chars correctly.
  29. *
  30. *       There are several factors that make this routine complicated:
  31. *               (1) The fill value may be 1 or 2 bytes long.
  32. *               (2) The fill operation may end by hitting the count value
  33. *               or by hitting the end of the string.
  34. *               (3) A null terminating char is NOT placed at the end of
  35. *               the string.
  36. *
  37. *       Cases to be careful of (both of these can occur at once):
  38. *               (1) Leaving an "orphaned" trail byte in the string (e.g.,
  39. *               overwriting a lead byte but not the corresponding trail byte).
  40. *               (2) Writing only the 1st byte of a 2-byte fill value because the
  41. *               end of string was encountered.
  42. *
  43. *Entry:
  44. *       unsigned char *string = string to modify
  45. *       unsigned int val = value to fill string with
  46. *       size_t count = count of characters to fill
  47. *
  48. *
  49. *Exit:
  50. *       Returns string = now filled with char val
  51. *
  52. *Uses:
  53. *
  54. *Exceptions:
  55. *
  56. *******************************************************************************/
  57.  
  58. unsigned char * __cdecl _mbsnset(
  59.         unsigned char *string,
  60.         unsigned int val,
  61.         size_t count
  62.         )
  63. {
  64.         unsigned char  *start = string;
  65.         unsigned int leadbyte = 0;
  66.         unsigned char highval, lowval;
  67.  
  68.         /*
  69.          * leadbyte flag indicates if the last byte we overwrote was
  70.          * a lead byte or not.
  71.          */
  72.  
  73.         if ( _ISNOTMBCP )
  74.                 return _strnset(string, val, count);
  75.  
  76.         _mlock(_MB_CP_LOCK);
  77.  
  78.         if (highval = (unsigned char)(val>>8)) {
  79.  
  80.                 /* double byte value */
  81.  
  82.                 lowval = (unsigned char)(val & 0x00ff);
  83.  
  84.                 while (count-- && *string) {
  85.  
  86.                         leadbyte = _ismbbtruelead(leadbyte, *string);
  87.                         *string++ = highval;
  88.  
  89.                         if (*string) {
  90.                                 leadbyte = _ismbbtruelead(leadbyte, *string);
  91.                                 *string++ = lowval;
  92.                         }
  93.                         else
  94.                                 /* overwrite orphaned highval byte */
  95.                                 *(string-1) = ' ';
  96.                 }
  97.         }
  98.  
  99.         else {
  100.                 /* single byte value */
  101.  
  102.                 while (count-- && *string) {
  103.                         leadbyte = _ismbbtruelead(leadbyte, *string);
  104.                         *string++ = (unsigned char)val;
  105.                 }
  106.  
  107.         }
  108.  
  109.         /* overwrite orphaned trailing byte, if necessary */
  110.         if(leadbyte && *string)
  111.                 *string = ' ';
  112.  
  113.         _munlock(_MB_CP_LOCK);
  114.         return( start );
  115. }
  116.  
  117. #endif  /* _MBCS */
  118.