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

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