home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / GETC.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  5.9 KB  |  221 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - getc.cas
  3.  *
  4.  * function(s)
  5.  *    FlushOutStreams - flushes output streams
  6.  *    _ffill      - fill up the read-ahead buffer
  7.  *    fgetc       - gets character from stream
  8.  *    _fgetc      - gets character from stream
  9.  *    fgetchar    - gets character from stream
  10.  *-----------------------------------------------------------------------*/
  11.  
  12. /*
  13.  *      C/C++ Run Time Library - Version 5.0
  14.  *
  15.  *      Copyright (c) 1987, 1992 by Borland International
  16.  *      All Rights Reserved.
  17.  *
  18.  */
  19.  
  20.  
  21. #pragma inline
  22. #include <stdio.h>
  23. #include <io.h>
  24. #include <_io.h>
  25. #include <asmrules.h>
  26. #include <RtlData.h>
  27.  
  28. /*---------------------------------------------------------------------*
  29.  
  30. Name            FlushOutStreams - flushes output streams
  31.  
  32. Usage           static  void pascal near FlushOutStreams(void);
  33.  
  34. Description     flushes all open output streams
  35.  
  36. Return value    nothing
  37.  
  38. *---------------------------------------------------------------------*/
  39. static void pascal near FlushOutStreams( void )
  40. {
  41.     register FILE *fp;
  42.     register int  Ndx;
  43.  
  44.     for( Ndx = FOPEN_MAX, fp = _RTLInstanceData(_streams); Ndx--; fp++ )
  45.         {
  46.         if( (fp->flags & (_F_TERM | _F_OUT)) == (_F_TERM | _F_OUT) )
  47.             fflush( fp );
  48.         }
  49. }
  50.  
  51.  
  52. /*---------------------------------------------------------------------*
  53.  
  54. Name            _ffill - fill up the read-ahead buffer
  55.  
  56. Usage           static  int near pascal _ffill  (FILE *fp)
  57.  
  58. Description     Fill up the read-ahead buffer.  Trusts caller to have checked
  59.                 permission to read before calling.  However, we do check
  60.                 for invalid pointers here, since there is no noticeable
  61.                 performance cost and it catches trouble before it causes too
  62.                 much damage.
  63.  
  64. Return value    -1 <carry set> if error,
  65.                 zero <no carry> if correct.
  66.  
  67. *---------------------------------------------------------------------*/
  68. static int near pascal _ffill( FILE *fp )
  69. {
  70.     if( fp->flags & _F_TERM )
  71.         FlushOutStreams();
  72.  
  73.     if( (fp->level = __read( fp->fd, (fp->curp = fp->buffer), fp->bsize) ) > 0)
  74.         {
  75.         fp->flags &= ~_F_EOF;
  76.         return 0;
  77.         }
  78.     else
  79.         {
  80.         if( !fp->level )
  81.             {
  82.             fp->flags = (fp->flags & ~(_F_IN | _F_OUT)) | _F_EOF;
  83.             }
  84.         else
  85.             {
  86.             fp->level = 0;
  87.             fp->flags |= _F_ERR;
  88.             }
  89.         }
  90.  
  91.     return -1;
  92. }
  93.  
  94.  
  95. /*---------------------------------------------------------------------*
  96.  
  97. Name            _fgetc - gets character from stream
  98.  
  99. Usage           int _fgetc(FILE *stream);
  100.  
  101. Prototype in    stdio.h
  102.  
  103. Description     this function is only called by the getc() macro. The
  104.                 only purpose for this is to increment the level
  105.                 indicator before calling fgetc().
  106.  
  107. Return value    the character read, after converting it to an int
  108.                 without sign extension.  On end-of-file or error,
  109.                 fgetchar returns EOF
  110.  
  111. *---------------------------------------------------------------------*/
  112. int _FARFUNC _fgetc( register FILE *fp )
  113. {
  114.     ++fp->level;
  115.  
  116.     return( fgetc( fp ) );
  117. }
  118.  
  119.  
  120. /*---------------------------------------------------------------------*
  121.  
  122. Name            fgetc - gets character from stream
  123.  
  124. Usage           int fgetc(FILE *stream);
  125.  
  126. Prototype in    stdio.h
  127.  
  128. Description     behaves exactly like getc, except that it is a true
  129.                 function while getc is a macro.
  130.  
  131. Return value    the character read, after converting it to an int
  132.                 without sign extension.  On end-of-file or error,
  133.                 fgetchar returns EOF
  134.  
  135. *---------------------------------------------------------------------*/
  136. asm _TEXT   segment
  137. asm         public __Nfgetc
  138. asm __Nfgetc    label near
  139. #if LPROG
  140. asm     pop ax
  141. asm     push    cs
  142. asm     push    ax
  143. #endif
  144. asm     ends
  145.  
  146. int _FARFUNC fgetc( register FILE  *fp )
  147. {
  148.     static unsigned char    c;
  149.  
  150.     if( fp == NULL)
  151.         return( EOF );
  152.  
  153.     if( fp->level > 0 )
  154.         {
  155.         --fp->level;
  156.         return( (unsigned char)(*fp->curp++) );
  157.         }
  158.  
  159.     if( fp->level < 0 || fp->flags & (_F_OUT | _F_ERR) || !(fp->flags & _F_READ) )
  160.         {
  161.         fp->flags |= _F_ERR;
  162.         return( EOF );          /* file is in writing mode  */
  163.         }
  164.  
  165.     fp->flags |= _F_IN;
  166.  
  167.     if( fp->bsize != 0 )        /* is the stream buffered ? */
  168.         {
  169.         if( _ffill( fp ) )
  170.             return( EOF );
  171.         --fp->level;
  172.         return( (unsigned char)(*fp->curp++) );
  173.         }
  174.     else                /* an unbuffered stream */
  175.         {
  176.         do
  177.             {
  178.             if( fp->flags & _F_TERM )
  179.                 FlushOutStreams();
  180.  
  181.             if( !__read( fp->fd, &c, 1 ) )
  182.                 {
  183.                 if( eof( fp->fd ) != 1 )
  184.                     {
  185.                     fp->flags |= _F_ERR;
  186.                     }
  187.                 else
  188.                     {
  189.                     fp->flags = (fp->flags & ~(_F_IN | _F_OUT)) | _F_EOF;
  190.                     }
  191.  
  192.                 return( EOF );
  193.                 }
  194.             }
  195.         while( c == '\r' && !(fp->flags & _F_BIN) );
  196.  
  197.         fp->flags &= ~_F_EOF;
  198.         return( (unsigned char) c);
  199.     }
  200. }
  201.  
  202. /*---------------------------------------------------------------------*
  203.  
  204. Name            fgetchar - gets character from stream
  205.  
  206. Usage           int fgetchar  (void);
  207.  
  208. Prototype in    stdio.h
  209.  
  210. Description     the same as fgetc(stdin).
  211.  
  212. return value    the character read, after converting it to an int
  213.                 without sign extension.  On end-of-file or error,
  214.                 fgetchar returns EOF
  215.  
  216. *---------------------------------------------------------------------*/
  217. int _FARFUNC fgetchar( void )
  218. {
  219.     return( fgetc( stdin ) );
  220. }
  221.