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

  1. /***
  2. *fgetwc.c - get a wide character from a stream
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines fgetwc() - read a wide character from a stream
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #include <cruntime.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <dbgint.h>
  16. #include <file2.h>
  17. #include <internal.h>
  18. #include <mtdll.h>
  19. #include <msdos.h>
  20. #include <errno.h>
  21. #include <wchar.h>
  22. #include <tchar.h>
  23. #include <setlocal.h>
  24.  
  25. #ifdef _MT
  26.  
  27. /***
  28. *wint_t fgetwc(stream) - read a wide character from a stream
  29. *
  30. *Purpose:
  31. *       reads a wide character from the given stream
  32. *
  33. *Entry:
  34. *       FILE *stream - stream to read wide character from
  35. *
  36. *Exit:
  37. *       returns the wide character read
  38. *       returns WEOF if at end of file or error occurred
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. wint_t __cdecl fgetwc (
  45.         REG1 FILE *stream
  46.         )
  47. {
  48.         wint_t retval;
  49.  
  50.         _ASSERTE(stream != NULL);
  51.  
  52.         _lock_str(stream);
  53.         retval = _getwc_lk(stream);
  54.         _unlock_str(stream);
  55.  
  56.         return(retval);
  57. }
  58.  
  59. /***
  60. *_getwc_lk() -  getwc() core routine (locked version)
  61. *
  62. *Purpose:
  63. *       Core getwc() routine; assumes stream is already locked.
  64. *
  65. *       [See getwc() above for more info.]
  66. *
  67. *Entry: [See getwc()]
  68. *
  69. *Exit:  [See getwc()]
  70. *
  71. *Exceptions:
  72. *
  73. *******************************************************************************/
  74.  
  75. wint_t __cdecl _getwc_lk (
  76.         REG1 FILE *stream
  77.         )
  78. {
  79.  
  80. #else  /* _MT */
  81.  
  82. wint_t __cdecl fgetwc (
  83.         REG1 FILE *stream
  84.         )
  85. {
  86.  
  87. #endif  /* _MT */
  88.  
  89.         if (!(stream->_flag & _IOSTRG) && (_osfile_safe(_fileno(stream)) &
  90.               FTEXT))
  91.         {
  92.                 int size = 1;
  93.                 int ch;
  94.                 char mbc[4];
  95.                 wchar_t wch;
  96.  
  97.                 /* text (multi-byte) mode */
  98.                 if ((ch = _getc_lk(stream)) == EOF)
  99.                         return WEOF;
  100.  
  101.                 mbc[0] = (char)ch;
  102.  
  103.                 if (isleadbyte((unsigned char)mbc[0]))
  104.                 {
  105.                         if ((ch = _getc_lk(stream)) == EOF)
  106.                         {
  107.                                 ungetc(mbc[0], stream);
  108.                                 return WEOF;
  109.                         }
  110.                         mbc[1] = (char)ch;
  111.                         size = 2;
  112.                 }
  113.                 if (mbtowc(&wch, mbc, size) == -1)
  114.                 {
  115.                         /*
  116.                          * Conversion failed! Set errno and return
  117.                          * failure.
  118.                          */
  119.                         errno = EILSEQ;
  120.                         return WEOF;
  121.                 }
  122.                 return wch;
  123.         }
  124.         /* binary (Unicode) mode */
  125.         if ((stream->_cnt -= sizeof(wchar_t)) >= 0)
  126.                 return *((wchar_t *)(stream->_ptr))++;
  127.         else
  128.                 return (wint_t) _filwbuf(stream);
  129. }
  130.  
  131. #undef getwc
  132.  
  133. wint_t __cdecl getwc (
  134.         FILE *stream
  135.         )
  136. {
  137.         return fgetwc(stream);
  138. }
  139.  
  140.  
  141.