Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Platform independent fgetc() implementation.
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "widechar.h"
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #ifndef __UNIX__
  37.     #include <conio.h>
  38. #endif
  39. #include "fileacc.h"
  40. #include <errno.h>
  41. #include "rtdata.h"
  42. #include "seterrno.h"
  43. #ifdef __WIDECHAR__
  44.     #include <mbstring.h>
  45.     #include <wchar.h>
  46. #endif
  47. #include "qread.h"
  48. #include "orient.h"
  49. #include "flush.h"
  50. #include "streamio.h"
  51.  
  52.  
  53. #define DOS_EOF_CHAR        0x1a
  54.  
  55. #ifdef __WIDECHAR__
  56.     #define CHARMASK        0xffff
  57. #else
  58.     #define CHARMASK        0xff
  59. #endif
  60.  
  61. int __F_NAME(__fill_buffer,__wfill_buffer)( FILE *fp )
  62. {
  63.     if( _FP_BASE(fp) == NULL ) {
  64.         __ioalloc( fp );
  65.     }
  66.     if( fp->_flag & _ISTTY ) {                      /* 20-aug-90 */
  67.         if( fp->_flag & (_IONBF | _IOLBF) ) {
  68.             __flushall( _ISTTY );           /* flush all TTY output */
  69.         }
  70.     }
  71.     fp->_flag &= ~_UNGET;                           /* 10-mar-90 */
  72.     fp->_ptr = _FP_BASE(fp);
  73. #ifdef __UNIX__
  74.     fp->_cnt = __qread( fileno( fp ), fp->_ptr,
  75.         (fp->_flag & _IONBF) ? CHARSIZE : fp->_bufsize );
  76. #else
  77.     if(( fp->_flag & (_IONBF | _ISTTY)) == (_IONBF | _ISTTY) &&
  78.        ( fileno( fp ) == STDIN_FILENO ))
  79.     {
  80.         int c;                      /* JBS 31-may-91 */
  81.  
  82.         fp->_cnt = 0;
  83.       //  c = getche();
  84.         if( c != EOF ) {
  85.             *(CHAR_TYPE *)fp->_ptr = c;
  86.             fp->_cnt = CHARSIZE;
  87.         }
  88.     } else {
  89.         fp->_cnt = __qread( fileno( fp ), fp->_ptr,
  90.             (fp->_flag & _IONBF) ? CHARSIZE : fp->_bufsize );
  91.     }
  92. #endif
  93.     if( fp->_cnt <= 0 ) {
  94.         if( fp->_cnt == 0 ) {
  95.             fp->_flag |= _EOF;
  96.         } else {
  97.             fp->_flag |= _SFERR;
  98.             fp->_cnt = 0;
  99.         }
  100.     }
  101.     return( fp->_cnt );
  102. }
  103.  
  104. int __F_NAME(__filbuf,__wfilbuf)( FILE *fp )
  105. {
  106.     if( __F_NAME(__fill_buffer,__wfill_buffer)( fp ) == 0 ) {
  107.         return( EOF );
  108.     }
  109.     else {
  110.         fp->_cnt -= CHARSIZE;
  111.         fp->_ptr += CHARSIZE;
  112.         return( *(CHAR_TYPE *)(fp->_ptr - CHARSIZE) & CHARMASK );
  113.     }
  114. }
  115.  
  116.  
  117. #ifndef __WIDECHAR__
  118.  
  119. _WCRTLINK int fgetc( FILE *fp )
  120. {
  121.     int c;
  122.  
  123.     _ValidFile( fp, EOF );
  124.     _AccessFile( fp );
  125.  
  126.     /*** Deal with stream orientation ***/
  127.     ORIENT_STREAM(fp,EOF);
  128.  
  129.     if( (fp->_flag & _READ) == 0 ) {
  130.         __set_errno( EBADF );
  131.         fp->_flag |= _SFERR;
  132.         c = EOF;
  133.     } else {
  134.         fp->_cnt--;
  135.         // it is important that this remain a relative comparison
  136.         // to ensure that the getc() macro works properly
  137.         if( fp->_cnt < 0 ) {
  138.             c = __F_NAME(__filbuf,__wfilbuf)( fp );
  139.         } else {
  140.             c = *(char *)fp->_ptr;
  141.             fp->_ptr++;
  142.         }
  143.     }
  144. #ifndef __UNIX__
  145.     if( !(fp->_flag & _BINARY) ) {
  146.         if( c == '\r' ) {
  147.             fp->_cnt--;
  148.             // it is important that this remain a relative comparison
  149.             // to ensure that the getc() macro works properly
  150.             if( fp->_cnt < 0 ) {
  151.                 c = __F_NAME(__filbuf,__wfilbuf)( fp );
  152.             } else {
  153.                 c = *(CHAR_TYPE*)fp->_ptr & CHARMASK;
  154.                 fp->_ptr += CHARSIZE;
  155.             }
  156.         }
  157.         if( c == DOS_EOF_CHAR ) {
  158.             fp->_flag |= _EOF;
  159.             c = EOF;
  160.         }
  161.     }
  162. #endif
  163.     _ReleaseFile( fp );
  164.     return( c );
  165. }
  166.  
  167. #else
  168.  
  169. static int __read_wide_char( FILE *fp, wchar_t *wc )
  170. /**************************************************/
  171. {
  172.     if( fp->_flag & _BINARY ) {
  173.         /*** Read a wide character ***/
  174.         return( fread( wc, sizeof( wchar_t ), 1, fp ) );
  175.     } else {
  176.         char            mbc[MB_CUR_MAX];
  177.         wchar_t         wcTemp;
  178.         int             rc;
  179.  
  180.         /*** Read the multibyte character ***/
  181.         if( !fread( &mbc[0], 1, 1, fp ) )
  182.             return( 0 );
  183.  
  184.         if( _ismbblead( mbc[0] ) ) {
  185.             if( !fread( &mbc[1], 1, 1, fp ) )
  186.                 return( 0 );
  187.         }
  188.  
  189.         /*** Convert it to wide form ***/
  190.         rc = mbtowc( &wcTemp, mbc, MB_CUR_MAX );
  191.         if( rc >= 0 ) {
  192.             *wc = wcTemp;
  193.             return( 1 );
  194.         } else {
  195.             __set_errno( EILSEQ );
  196.             return( 0 );
  197.         }
  198.     }
  199. }
  200.  
  201. _WCRTLINK wint_t fgetwc( FILE *fp )
  202. {
  203.     wchar_t             c;
  204.  
  205.     _ValidFile( fp, WEOF );
  206.     _AccessFile( fp );
  207.  
  208.     /*** Deal with stream orientation ***/
  209.     ORIENT_STREAM(fp,WEOF);
  210.  
  211.     /*** Read the character ***/
  212.     if( !__read_wide_char( fp, &c ) ) {
  213.         _ReleaseFile( fp );
  214.         return( WEOF );
  215.     }
  216.     if( !(fp->_flag & _BINARY) && (c == L'\r') ) {
  217.         if( !__read_wide_char( fp, &c ) ) {
  218.             _ReleaseFile( fp );
  219.             return( WEOF );
  220.         }
  221.     }
  222.  
  223.     _ReleaseFile( fp );
  224.     return( (wint_t)c );
  225. }
  226.  
  227. #endif
  228.