home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / FREAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.8 KB  |  147 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - fread.c
  3.  *
  4.  * function(s)
  5.  *        fread  - reads data from a stream
  6.  *        _fgetn - reads data from a stream
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <io.h>
  21. #include <asmrules.h>
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name            _fgetn - reads data from a stream
  26.  
  27. Usage           static unsigned pascal near
  28.                         _fgetn (register void *ptr, size_t n, FILE *fp);
  29.  
  30. Prototype in    local to this module
  31.  
  32. Description     _fgetn reads n bytes of data from the named input stream
  33.                 into a block pointed to by ptr.
  34.  
  35. Return value    success : 0
  36.                 else    : non-zero
  37.  
  38. *---------------------------------------------------------------------*/
  39. static unsigned pascal near _fgetn( register void *ptr, size_t n, FILE *fp )
  40.   {
  41.   register int Byte, Temp;
  42.  
  43.   while( n )
  44.     {
  45.     n++;
  46.  
  47.     Temp = min( n, fp->bsize );
  48.  
  49.     if( (fp->flags & _F_BIN) && fp->bsize && (n > fp->bsize) && (fp->level == 0))
  50.       {
  51.       n--;
  52.       Temp = 0;
  53.  
  54.       while( n >= fp->bsize )
  55.         {
  56.         Temp += fp->bsize;
  57.         n -= fp->bsize;
  58.         }
  59.  
  60.       Byte = _read(fp->fd, ptr, Temp);
  61.  
  62.       (char *) ptr += Byte;
  63.  
  64.       if( Byte != Temp )
  65.         {
  66.         n += (Temp - Byte);
  67.         fp->flags |= _F_EOF;
  68.         break;
  69.         }
  70.       }
  71.     else
  72.       {
  73.       while (--n  && --Temp && ( Byte = getc (fp)) != EOF )
  74.         {
  75.         *((char *) ptr)++ = Byte;
  76.         }
  77.  
  78.       if( Byte == EOF )
  79.         {
  80.         fp->flags |= _F_EOF;
  81.         break;
  82.         }
  83.       }
  84.     }
  85.  
  86.   return n;
  87.   }
  88.  
  89.  
  90. /*---------------------------------------------------------------------*
  91.  
  92. Name            fread - reads data from a stream
  93.  
  94. Usage           #include <stdio.h>
  95.                 size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
  96.  
  97. Related
  98. functions usage size_t fwrite(void *ptr, size_t size, size_t nitems, FILE *stream);
  99.  
  100. Prototype in    stdio.h
  101.  
  102. Description     fread reads nitems of data, each of length size
  103.                 bytes, from the named input stream into a block pointed to by
  104.                 ptr.
  105.  
  106.                 fwrite appends nitems of data, each of length size bytes, to
  107.                 the named output stream. The data appended begins at ptr.
  108.  
  109.                 For both functions, the total number of bytes read is (nitems
  110.                 * size).
  111.  
  112.                 ptr in the declarations is a pointer to any object.
  113.                 size is the size of the object ptr points to. The
  114.                 expression sizeof *ptr will produce the proper value.
  115.  
  116. Return value    On successful completion, each function returns the
  117.                 number of items (not bytes) actually read or written. fread
  118.                 returns a short count (possibly 0) on end-of-file or error.
  119.                 fwrite returns a short count on error.
  120.  
  121. *---------------------------------------------------------------------*/
  122. size_t _FARFUNC fread( void *ptr, register size_t psize, size_t nitems, FILE *fp )
  123.   {
  124. #if (LDATA)
  125.   register size_t n;
  126. #endif
  127.   unsigned long temp;
  128.  
  129.   if( !psize )  return( 0 );
  130.  
  131.   if( (temp = (unsigned long)psize * (unsigned long)nitems) < 0x10000L)
  132.         return( ((unsigned)temp - _fgetn( ptr, (unsigned)temp, fp )) / psize );
  133.  
  134. #if (!LDATA)
  135.   return 0;
  136. #else
  137.   n = nitems+1;
  138.  
  139.   while( --n && 0 == _fgetn( ptr, psize, fp ))
  140.     {
  141.     ptr = (void *) (((char huge *) ptr) + psize);
  142.     }
  143.  
  144.   return( nitems - n );
  145. #endif
  146.   }
  147.