home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / AROS / dos / fgetc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1978-03-06  |  3.4 KB  |  137 lines

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