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

  1. /***
  2. *fread.c - read from a stream
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Read from the specified stream into the user's buffer.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <mtdll.h>
  14. #include <io.h>
  15. #include <string.h>
  16. #include <file2.h>
  17.  
  18. /***
  19. *size_t fread(void *buffer, size_t size, size_t count, FILE *stream) -
  20. *       read from specified stream into the specified buffer.
  21. *
  22. *Purpose:
  23. *       Read 'count' items of size 'size' from the specified stream into
  24. *       the specified buffer. Return when 'count' items have been read in
  25. *       or no more items can be read from the stream.
  26. *
  27. *Entry:
  28. *       buffer  - pointer to user's buffer
  29. *       size    - size of the item to read in
  30. *       count   - number of items to read
  31. *       stream  - stream to read from
  32. *
  33. *Exit:
  34. *       Returns the number of (whole) items that were read into the buffer.
  35. *       This may be less than 'count' if an error or eof occurred. In this
  36. *       case, ferror() or feof() should be used to distinguish between the
  37. *       two conditions.
  38. *
  39. *Notes:
  40. *       fread will attempt to buffer the stream (side effect of the _filbuf
  41. *       call) if necessary.
  42. *
  43. *       No more than 0xFFFE bytes may be read in at a time by a call to
  44. *       read(). Further, read() does not handle huge buffers. Therefore,
  45. *       in large data models, the read request is broken down into chunks
  46. *       that do not violate these considerations. Each of these chunks is
  47. *       processed much like an fread() call in a small data model (by a
  48. *       call to _nfread()).
  49. *
  50. *       MTHREAD/DLL - Handled in three layers. fread() handles the locking
  51. *       and DS saving/loading/restoring (if required) and calls _fread_lk()
  52. *       to do the work. _fread_lk() is the same as the single-thread,
  53. *       large data model version of fread(). It breaks up the read request
  54. *       into digestible chunks and calls _nfread() to do the actual work.
  55. *
  56. *       386/MTHREAD/DLL - Handled in just the two layers since it is small
  57. *       data model. The outer layer, fread(), takes care of the stream locking
  58. *       and calls _fread_lk() to do the actual work. _fread_lk() is the same
  59. *       as the single-thread version of fread().
  60. *
  61. *******************************************************************************/
  62.  
  63.  
  64. #ifdef _MT
  65. /* define locking/unlocking version */
  66. size_t __cdecl fread (
  67.         void *buffer,
  68.         size_t size,
  69.         size_t count,
  70.         FILE *stream
  71.         )
  72. {
  73.         size_t retval;
  74.  
  75.         _lock_str(stream);                                /* lock stream */
  76.         retval = _fread_lk(buffer, size, count, stream);  /* do the read */
  77.         _unlock_str(stream);                              /* unlock stream */
  78.         return retval;
  79. }
  80. #endif  /* _MT */
  81.  
  82. /* define the normal version */
  83. #ifdef _MT
  84. size_t __cdecl _fread_lk (
  85. #else  /* _MT */
  86. size_t __cdecl fread (
  87. #endif  /* _MT */
  88.         void *buffer,
  89.         size_t size,
  90.         size_t num,
  91.         FILE *stream
  92.         )
  93. {
  94.         char *data;                     /* point to where should be read next */
  95.         unsigned total;                 /* total bytes to read */
  96.         unsigned count;                 /* num bytes left to read */
  97.         unsigned bufsize;               /* size of stream buffer */
  98.         unsigned nbytes;                /* how much to read now */
  99.         unsigned nread;                 /* how much we did read */
  100.         int c;                          /* a temp char */
  101.  
  102.         /* initialize local vars */
  103.         data = buffer;
  104.  
  105.         if ( (count = total = size * num) == 0 )
  106.                 return 0;
  107.  
  108.         if (anybuf(stream))
  109.                 /* already has buffer, use its size */
  110.                 bufsize = stream->_bufsiz;
  111.         else
  112. #if defined (_M_M68K) || defined (_M_MPPC)
  113.                 /* assume will get BUFSIZ buffer */
  114.                 bufsize = BUFSIZ;
  115. #else  /* defined (_M_M68K) || defined (_M_MPPC) */
  116.                 /* assume will get _INTERNAL_BUFSIZ buffer */
  117.                 bufsize = _INTERNAL_BUFSIZ;
  118. #endif  /* defined (_M_M68K) || defined (_M_MPPC) */
  119.  
  120.         /* here is the main loop -- we go through here until we're done */
  121.         while (count != 0) {
  122.                 /* if the buffer exists and has characters, copy them to user
  123.                    buffer */
  124.                 if (anybuf(stream) && stream->_cnt != 0) {
  125.                         /* how much do we want? */
  126.                         nbytes = (count < (unsigned)stream->_cnt) ? count : stream->_cnt;
  127.                         memcpy(data, stream->_ptr, nbytes);
  128.  
  129.                         /* update stream and amt of data read */
  130.                         count -= nbytes;
  131.                         stream->_cnt -= nbytes;
  132.                         stream->_ptr += nbytes;
  133.                         data += nbytes;
  134.                 }
  135.                 else if (count >= bufsize) {
  136.                         /* If we have more than bufsize chars to read, get data
  137.                            by calling read with an integral number of bufsiz
  138.                            blocks.  Note that if the stream is text mode, read
  139.                            will return less chars than we ordered. */
  140.  
  141.                         /* calc chars to read -- (count/bufsize) * bufsize */
  142.                         nbytes = ( bufsize ? (count - count % bufsize) :
  143.                                    count );
  144.  
  145.                         nread = _read(_fileno(stream), data, nbytes);
  146.                         if (nread == 0) {
  147.                                 /* end of file -- out of here */
  148.                                 stream->_flag |= _IOEOF;
  149.                                 return (total - count) / size;
  150.                         }
  151.                         else if (nread == (unsigned)-1) {
  152.                                 /* error -- out of here */
  153.                                 stream->_flag |= _IOERR;
  154.                                 return (total - count) / size;
  155.                         }
  156.  
  157.                         /* update count and data to reflect read */
  158.                         count -= nread;
  159.                         data += nread;
  160.                 }
  161.                 else {
  162.                         /* less than bufsize chars to read, so call _filbuf to
  163.                            fill buffer */
  164.                         if ((c = _filbuf(stream)) == EOF) {
  165.                                 /* error or eof, stream flags set by _filbuf */
  166.                                 return (total - count) / size;
  167.                         }
  168.  
  169.                         /* _filbuf returned a char -- store it */
  170.                         *data++ = (char) c;
  171.                         --count;
  172.  
  173.                         /* update buffer size */
  174.                         bufsize = stream->_bufsiz;
  175.                 }
  176.         }
  177.  
  178.         /* we finished successfully, so just return num */
  179.         return num;
  180. }
  181.