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

  1. /***
  2. *isctype.c - support is* ctype functions/macros for two-byte multibyte chars
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines _isctype.c - support is* ctype functions/macros for
  8. *       two-byte multibyte chars.
  9. *
  10. *******************************************************************************/
  11.  
  12. #ifndef _MAC
  13. #include <stdio.h>
  14. #include <cruntime.h>
  15. #include <ctype.h>
  16. #include <locale.h>
  17. #include <setlocal.h>
  18. #include <mtdll.h>
  19. #include <awint.h>
  20. #endif  /* _MAC */
  21.  
  22. #if !defined (_MAC)
  23.  
  24. /*
  25.  *  Use GetCharType() API so check that character type masks agree between
  26.  *  ctype.h and winnls.h
  27.  */
  28. #if _UPPER   != C1_UPPER || \
  29.         _LOWER   != C1_LOWER || \
  30.         _DIGIT   != C1_DIGIT || \
  31.         _SPACE   != C1_SPACE || \
  32.         _PUNCT   != C1_PUNCT || \
  33.         _CONTROL != C1_CNTRL
  34. #error Character type masks do not agree in ctype and winnls
  35. #endif  /* _UPPER   != C1_UPPER || \ */
  36.  
  37. /***
  38. *_isctype - support is* ctype functions/macros for two-byte multibyte chars
  39. *
  40. *Purpose:
  41. *       This function is called by the is* ctype functions/macros
  42. *       (e.g. isalpha()) when their argument is a two-byte multibyte char.
  43. *       Returns true or false depending on whether the argument satisfies
  44. *       the character class property encoded by the mask.
  45. *
  46. *Entry:
  47. *       int c - the multibyte character whose type is to be tested
  48. *       unsigned int mask - the mask used by the is* functions/macros
  49. *                  corresponding to each character class property
  50. *
  51. *       The leadbyte and the trailbyte should be packed into the int c as:
  52. *
  53. *       H.......|.......|.......|.......L
  54. *           0       0   leadbyte trailbyte
  55. *
  56. *Exit:
  57. *       Returns non-zero if c is of the character class.
  58. *       Returns 0 if c is not of the character class.
  59. *
  60. *Exceptions:
  61. *       Returns 0 on any error.
  62. *
  63. *******************************************************************************/
  64.  
  65. int __cdecl _isctype (
  66.         int c,
  67.         int mask
  68.         )
  69. {
  70.         int size;
  71.         unsigned short chartype;
  72.         char buffer[3];
  73.  
  74.         /* c valid between -1 and 255 */
  75.         if (((unsigned)(c + 1)) <= 256)
  76.             return _pctype[c] & mask;
  77.  
  78.         if (isleadbyte(c >> 8 & 0xff))
  79.         {
  80.             buffer[0] = (c >> 8 & 0xff); /* put lead-byte at start of str */
  81.             buffer[1] = (char)c;
  82.             buffer[2] = 0;
  83.             size = 2;
  84.         } else {
  85.             buffer[0] = (char)c;
  86.             buffer[1] = 0;
  87.             size = 1;
  88.         }
  89.  
  90.         if (0 == __crtGetStringTypeA(CT_CTYPE1, buffer, size, &chartype, 0, 0, TRUE))
  91.         {
  92.             return 0;
  93.         }
  94.  
  95.         return (int)(chartype & mask);
  96. }
  97.  
  98. #else  /* !defined (_MAC) */
  99.  
  100. int __cdecl _isctype (
  101.         int c,
  102.         int mask
  103.         )
  104. {
  105.         return 0;
  106. }
  107.  
  108. #endif  /* !defined (_MAC) */
  109.