2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
9 #include <aros/debug.h>
10 #include <exec/memory.h>
11 #include <proto/exec.h>
13 #include <dos/stdio.h>
14 #include <dos/dosextens.h>
16 #include "dos_intern.h"
19 /*****************************************************************************
22 #include <proto/dos.h>
27 AROS_LHA(BPTR
, file
, D1
),
30 struct DosLibrary
*, DOSBase
, 51, Dos
)
33 Get a character from a buffered file. Buffered I/O is more efficient
34 for small amounts of data but less for big chunks. You have to
35 use Flush() between buffered and non-buffered I/O or you'll
36 clutter your I/O stream.
42 The character read or EOF if the file ended or an error happened.
43 IoErr() gives additional information in that case.
56 *****************************************************************************/
60 /* Get pointer to filehandle */
61 struct FileHandle
*fh
= (struct FileHandle
*)BADDR(file
);
71 /* If the file is in write mode... */
72 if(fh
->fh_Flags
& FHF_WRITE
)
74 /* write the buffer (in many pieces if the first one isn't enough). */
77 while(pos
!= fh
->fh_Pos
)
79 size
= Write(file
, BADDR(fh
->fh_Buf
) + pos
, fh
->fh_Pos
- pos
);
81 /* An error happened? Return it. */
90 /* Reinit filehandle. */
91 fh
->fh_Flags
&= ~FHF_WRITE
;
92 fh
->fh_Pos
= fh
->fh_End
= 0;
95 /* No normal characters left. */
96 if(fh
->fh_Pos
>= fh
->fh_End
)
98 /* Check for a pushed back EOF. */
99 if(fh
->fh_Pos
> fh
->fh_End
)
101 D(bug("FGetC: Weird pos: fh_Pos (%d) > fh_End (%d)\n", fh
->fh_Pos
, fh
->fh_End
));
106 /* Is there a buffer? */
107 if(fh
->fh_Buf
== BNULL
)
109 if (NULL
== vbuf_alloc(fh
, NULL
, IOBUFSIZE
))
111 D(bug("FGetC: Can't allocate buffer\n"));
116 /* Fill the buffer. */
117 if (fh
->fh_Buf
!= fh
->fh_OrigBuf
) {
118 D(bug("FGetC: Can't trust fh_BufSize. Using 208 as the buffer size.\n"));
121 bufsize
= fh
->fh_BufSize
;
123 size
= Read(file
, BADDR(fh
->fh_Buf
), bufsize
);
125 /* Prepare filehandle for data. */
132 /* No data read? Return EOF. */
135 D(bug("FGetC: Tried to Read() to a %d byte buffer, got 0)\n", bufsize
));
140 /* If fh_End == 0, simulate an EOF */
141 if (fh
->fh_End
== 0) {
142 D(bug("FGetC: Got an EOF via fh_End == 0\n"));
146 /* All OK. Get data. */
147 return ((UBYTE
*)BADDR(fh
->fh_Buf
))[fh
->fh_Pos
++];