1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
4 // Public Domain C Library - http://pdclib.sourceforge.net
5 // This code is Public Domain. Use, modify, and redistribute at will.
6 // ----------------------------------------------------------------------------
8 size_t fread( void * restrict ptr, size_t size, size_t nelem, FILE * restrict stream ) { /* TODO */ };
10 /* PDPC code - unreviewed, verbatim.
11 Read the note in fopen.c.
35 toread = size * nmemb;
37 if (toread < stream->szfbuf)
41 if (!stream->quickBin)
45 freadSlowT(ptr, stream, toread, &actualRead);
49 if (toread <= (stream->endbuf - stream->upto))
51 memcpy(ptr, stream->upto, toread);
53 stream->upto += toread;
57 freadSlowB(ptr, stream, toread, &actualRead);
62 if (actualRead == size)
73 elemRead = actualRead;
77 elemRead = actualRead / size;
84 rc = DosRead(stream->hfile, ptr, toread, &tempRead);
93 actualRead = tempRead;
97 tempRead = __read(stream->hfile, ptr, toread, &errind);
102 stream->errorInd = 1;
106 actualRead = tempRead;
111 if (actualRead == size)
123 elemRead = actualRead;
124 if (nmemb != actualRead)
131 elemRead = actualRead / size;
132 if (toread != actualRead)
137 stream->bufStartR += actualRead;
144 while toread has not been satisfied
146 scan stuff out of buffer, replenishing buffer as required
150 static void freadSlowT(void *ptr,
172 if (stream->upto == stream->endbuf)
174 stream->bufStartR += (stream->upto - stream->fbuf);
176 rc = DosRead(stream->hfile,
183 stream->errorInd = 1;
188 tempRead = __read(stream->hfile,
196 stream->errorInd = 1;
204 stream->endbuf = stream->fbuf + tempRead;
205 *stream->endbuf = '\n';
206 stream->upto = stream->fbuf;
208 avail = (size_t)(stream->endbuf - stream->upto) + 1;
209 need = toread - *actualRead;
210 p = memchr(stream->upto, '\n', avail);
211 got = (size_t)(p - stream->upto);
214 memcpy((char *)ptr + *actualRead, stream->upto, need);
215 stream->upto += need;
220 memcpy((char *)ptr + *actualRead, stream->upto, got);
223 if (p != stream->endbuf)
225 if (*(stream->upto - 1) == '\r')
227 *((char *)ptr + *actualRead - 1) = '\n';
231 *((char *)ptr + *actualRead) = '\n';
238 if (*(stream->upto - 1) == '\r')
244 if (*actualRead == toread)
252 static void freadSlowB(void *ptr,
267 avail = (size_t)(stream->endbuf - stream->upto);
268 memcpy(ptr, stream->upto, avail);
270 stream->bufStartR += (stream->endbuf - stream->fbuf);
271 if (toread >= stream->szfbuf)
273 stream->upto = stream->endbuf;
274 stream->quickBin = 1;
276 rc = DosRead(stream->hfile,
277 (char *)ptr + *actualRead,
278 toread - *actualRead,
283 stream->errorInd = 1;
288 tempRead = __read(stream->hfile,
289 (char *)ptr + *actualRead,
290 toread - *actualRead,
296 stream->errorInd = 1;
299 else if (tempRead != (toread - *actualRead))
303 *actualRead += tempRead;
304 stream->bufStartR += tempRead;
310 stream->upto = stream->fbuf;
312 rc = DosRead(stream->hfile,
316 left = toread - *actualRead;
320 stream->errorInd = 1;
325 tempRead = __read(stream->hfile,
329 left = toread - *actualRead;
334 stream->errorInd = 1;
337 else if (tempRead < left)
341 stream->endbuf = stream->fbuf + tempRead;
342 *stream->endbuf = '\n';
343 avail = (size_t)(stream->endbuf - stream->upto);
348 memcpy((char *)ptr + *actualRead,
351 stream->upto += avail;
352 *actualRead += avail;