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