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

  1. /***
  2. *fputwc.c - write a wide character to an output stream
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines fputwc() - writes a wide character to 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 fputwc(ch, stream) - write a wide character to a stream
  29. *
  30. *Purpose:
  31. *       Writes a wide character to a stream.  Function version of putwc().
  32. *
  33. *Entry:
  34. *       wint_t ch - wide character to write
  35. *       FILE *stream - stream to write to
  36. *
  37. *Exit:
  38. *       returns the wide character if successful
  39. *       returns WEOF if fails
  40. *
  41. *Exceptions:
  42. *
  43. *******************************************************************************/
  44.  
  45. wint_t __cdecl fputwc (
  46.         wint_t ch,
  47.         FILE *str
  48.         )
  49. {
  50.         REG1 FILE *stream;
  51.         REG2 wint_t retval;
  52.  
  53.         _ASSERTE(str != NULL);
  54.  
  55.         /* Init stream pointer */
  56.         stream = str;
  57.  
  58.         _lock_str(stream);
  59.         retval = _putwc_lk(ch,stream);
  60.         _unlock_str(stream);
  61.  
  62.         return(retval);
  63. }
  64.  
  65. /***
  66. *_putwc_lk() -  putwc() core routine (locked version)
  67. *
  68. *Purpose:
  69. *       Core putwc() routine; assumes stream is already locked.
  70. *
  71. *       [See putwc() above for more info.]
  72. *
  73. *Entry: [See putwc()]
  74. *
  75. *Exit:  [See putwc()]
  76. *
  77. *Exceptions:
  78. *
  79. *******************************************************************************/
  80.  
  81. wint_t __cdecl _putwc_lk (
  82.         wint_t ch,
  83.         FILE *str
  84.         )
  85. {
  86.  
  87. #else  /* _MT */
  88.  
  89. wint_t __cdecl fputwc (
  90.         wint_t ch,
  91.         FILE *str
  92.         )
  93. {
  94.  
  95. #endif  /* _MT */
  96.  
  97.         if (!(str->_flag & _IOSTRG) && (_osfile_safe(_fileno(str)) & FTEXT))
  98.         {
  99.                 int size;
  100.                 char mbc[4];
  101.  
  102.                 /* text (multi-byte) mode */
  103.                 if ((size = wctomb(mbc, ch)) == -1)
  104.                 {
  105.                         /*
  106.                          * Conversion failed! Set errno and return
  107.                          * failure.
  108.                          */
  109.                         errno = EILSEQ;
  110.                         return WEOF;
  111.                 }
  112.                 else if ( size == 1 )
  113.                 {
  114.                         if ( _putc_lk(mbc[0], str) == EOF )
  115.                                 return WEOF;
  116.                         return (wint_t)(0xffff & ch);
  117.                 }
  118.                 else { /* size == 2 */
  119.                         if ( (_putc_lk(mbc[0], str) == EOF) ||
  120.                              (_putc_lk(mbc[1], str) == EOF) )
  121.                                 return WEOF;
  122.                         return (wint_t)(0xffff & ch);
  123.                 }
  124.         }
  125.         /* binary (Unicode) mode */
  126.         if ( (str->_cnt -= sizeof(wchar_t)) >= 0 )
  127.                 return (wint_t) (0xffff & (*((wchar_t *)(str->_ptr))++ = (wchar_t)ch));
  128.         else
  129.                 return (wint_t) _flswbuf(ch, str);
  130. }
  131.  
  132. #undef putwc
  133.  
  134. wint_t __cdecl putwc (
  135.         wint_t ch,
  136.         FILE *str
  137.         )
  138. {
  139.         return fputwc(ch, str);
  140. }
  141.  
  142.