home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / dos / fgetc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  3.6 KB  |  145 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fgetc.c,v 1.6 1997/01/27 00:36:19 ldp Exp $
  4.     $Log: fgetc.c,v $
  5.     Revision 1.6  1997/01/27 00:36:19  ldp
  6.     Polish
  7.  
  8.     Revision 1.5  1996/12/09 13:53:27  aros
  9.     Added empty templates for all missing functions
  10.  
  11.     Moved #include's into first column
  12.  
  13.     Revision 1.4  1996/10/24 15:50:27  aros
  14.     Use the official AROS macros over the __AROS versions.
  15.  
  16.     Revision 1.3  1996/08/13 13:52:46  digulla
  17.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  18.     Replaced AROS_LA by AROS_LHA
  19.  
  20.     Revision 1.2  1996/08/01 17:40:50  digulla
  21.     Added standard header for all files
  22.  
  23.     Desc:
  24.     Lang: english
  25. */
  26. #include <exec/memory.h>
  27. #include <proto/exec.h>
  28. #include <dos/dosextens.h>
  29. #include "dos_intern.h"
  30.  
  31. /*****************************************************************************
  32.  
  33.     NAME */
  34. #include <proto/dos.h>
  35.  
  36.     AROS_LH1(LONG, FGetC,
  37.  
  38. /*  SYNOPSIS */
  39.     AROS_LHA(BPTR, file, D1),
  40.  
  41. /*  LOCATION */
  42.     struct DosLibrary *, DOSBase, 51, Dos)
  43.  
  44. /*  FUNCTION
  45.     Get a character from a buffered file. Buffered I/O is more efficient
  46.     for small amounts of data but less for big chunks. You have to
  47.     use Flush() between buffered and non-buffered I/O or you'll
  48.     clutter your I/O stream.
  49.  
  50.     INPUTS
  51.     file   - filehandle
  52.  
  53.     RESULT
  54.     The character read or EOF if the file ended or an error happened.
  55.     IoErr() gives additional information in that case.
  56.  
  57.     NOTES
  58.  
  59.     EXAMPLE
  60.  
  61.     BUGS
  62.  
  63.     SEE ALSO
  64.     IoErr(), Flush()
  65.  
  66.     INTERNALS
  67.  
  68.     HISTORY
  69.     29-10-95    digulla automatically created from
  70.                 dos_lib.fd and clib/dos_protos.h
  71.  
  72. *****************************************************************************/
  73. {
  74.     AROS_LIBFUNC_INIT
  75.     AROS_LIBBASE_EXT_DECL(struct DosLibrary *,DOSBase)
  76.  
  77.     /* Get pointer to filehandle */
  78.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  79.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  80.     LONG size;
  81.  
  82.     /* If the file is in write mode... */
  83.     if(fh->fh_Flags&FHF_WRITE)
  84.     {
  85.         /* write the buffer. (In many pieces if the first one isn't enough). */
  86.         UBYTE *pos=fh->fh_Buf;
  87.         while(pos!=fh->fh_Pos)
  88.         {
  89.             size=Write(file,pos,fh->fh_Pos-pos);
  90.             /* An error happened? Return it. */
  91.             if(size<0)
  92.                 return EOF;
  93.             pos+=size;
  94.         }
  95.         /* Reinit filehandle. */
  96.         fh->fh_Flags&=~FHF_WRITE;
  97.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  98.     }
  99.     /* No normal characters left. */
  100.     if(fh->fh_Pos>=fh->fh_End)
  101.     {
  102.         /* Check for a pushed back EOF. */
  103.         if(fh->fh_Pos>fh->fh_End)
  104.         {
  105.             /* Reinit filehandle and return EOF. */
  106.             fh->fh_Pos=fh->fh_End;
  107.         *result=0;
  108.             return EOF;
  109.         }
  110.         
  111.         /* Is there a buffer? */
  112.         if(fh->fh_Buf==NULL)
  113.         {
  114.             /* No. Get one. */
  115.             fh->fh_Buf=AllocMem(IOBUFSIZE,MEMF_ANY);
  116.             if(fh->fh_Buf==NULL)
  117.             {
  118.                 /* Couldn't get buffer. Return error. */
  119.                 *result=ERROR_NO_FREE_STORE;
  120.                 return EOF;
  121.             }
  122.             /* Got it. Use it. */
  123.             fh->fh_Flags|=FHF_BUF;
  124.             fh->fh_Size=IOBUFSIZE;
  125.         }
  126.         
  127.         /* Fill the buffer. */
  128.         size=Read(file,fh->fh_Buf,fh->fh_Size);
  129.         
  130.         /* Prepare filehandle for data. */
  131.         if(size<=0)
  132.             size=0;
  133.         fh->fh_Pos=fh->fh_Buf;
  134.         fh->fh_End=fh->fh_Buf+size;
  135.         
  136.         /* No data read? Return EOF. */
  137.         if(!size)
  138.             return EOF;
  139.     }
  140.     
  141.     /* All OK. Get data. */
  142.     return *fh->fh_Pos++;
  143.     AROS_LIBFUNC_EXIT
  144. } /* FGetC */
  145.