2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
8 #include <exec/memory.h>
9 #include <proto/exec.h>
11 #include <dos/stdio.h>
12 #include <dos/dosextens.h>
14 #include "dos_intern.h"
17 /*****************************************************************************
20 #include <proto/dos.h>
25 AROS_LHA(BPTR
, file
, D1
),
28 struct DosLibrary
*, DOSBase
, 51, Dos
)
31 Get a character from a buffered file. Buffered I/O is more efficient
32 for small amounts of data but less for big chunks. You have to
33 use Flush() between buffered and non-buffered I/O or you'll
34 clutter your I/O stream.
40 The character read or EOF if the file ended or an error happened.
41 IoErr() gives additional information in that case.
54 *****************************************************************************/
58 /* Get pointer to filehandle */
59 struct FileHandle
*fh
= (struct FileHandle
*)BADDR(file
);
68 /* If the file is in write mode... */
69 if(fh
->fh_Flags
& FHF_WRITE
)
71 /* write the buffer (in many pieces if the first one isn't enough). */
72 UBYTE
*pos
= fh
->fh_Buf
;
74 while(pos
!= fh
->fh_Pos
)
76 size
= Write(file
, pos
, fh
->fh_Pos
- pos
);
78 /* An error happened? Return it. */
87 /* Reinit filehandle. */
88 fh
->fh_Flags
&= ~FHF_WRITE
;
89 fh
->fh_Pos
= fh
->fh_End
= fh
->fh_Buf
;
92 /* No normal characters left. */
93 if(fh
->fh_Pos
>= fh
->fh_End
)
95 /* Check for a pushed back EOF. */
96 if(fh
->fh_Pos
> fh
->fh_End
)
98 /* Reinit filehandle and return EOF. */
99 fh
->fh_Pos
= fh
->fh_End
;
105 /* Is there a buffer? */
106 if(fh
->fh_Buf
== NULL
)
108 if (NULL
== vbuf_alloc(fh
, IOBUFSIZE
, DOSBase
))
114 /* Fill the buffer. */
115 size
= Read(file
, fh
->fh_Buf
, fh
->fh_Size
);
117 /* Prepare filehandle for data. */
121 fh
->fh_Pos
= fh
->fh_Buf
;
122 fh
->fh_End
= fh
->fh_Buf
+ size
;
124 /* No data read? Return EOF. */
131 /* All OK. Get data. */
132 return *fh
->fh_Pos
++;