home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / aros / readstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  1.8 KB  |  105 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: readstring.c,v 1.5 1997/01/27 00:16:38 ldp Exp $
  4.  
  5.     Desc: Read one C string from a file
  6.     Lang: english
  7. */
  8. #include <proto/dos.h>
  9. #include <exec/memory.h>
  10. #include <proto/exec.h>
  11.  
  12. /******************************************************************************
  13.  
  14.     NAME */
  15. #include <stdio.h>
  16. #include <proto/alib.h>
  17.  
  18.     BOOL ReadString (
  19.  
  20. /*  SYNOPSIS */
  21.     BPTR     fh,
  22.     STRPTR * dataptr)
  23.  
  24. /*  FUNCTION
  25.     Reads one C string from a file.
  26.  
  27.     INPUTS
  28.     fh - Read from this file
  29.     dataptr - Put the data here. If you don't need the string anymore,
  30.         call FreeVec() to free it.
  31.  
  32.     RESULT
  33.     The function returns TRUE on success. On success, the string
  34.     read is written into dataptr. On failure, FALSE is returned and the
  35.     contents of dataptr are not changed. The string must be freed with
  36.     FreeVec().
  37.  
  38.     NOTES
  39.     This function reads big endian values from a file even on little
  40.     endian machines.
  41.  
  42.     EXAMPLE
  43.  
  44.     BUGS
  45.  
  46.     SEE ALSO
  47.     Open(), Close(), ReadByte(), ReadWord(), ReadLong(), ReadFloat(),
  48.     ReadDouble(), WriteByte(), WriteWord(), WriteLong(),
  49.     WriteFloat(), WriteDouble(), WriteString()
  50.  
  51.     HISTORY
  52.     14.09.93    ada created
  53.  
  54. ******************************************************************************/
  55. {
  56.     STRPTR buffer;
  57.     LONG   size, maxsize;
  58.     LONG   c;
  59.  
  60.     size = maxsize = 0;
  61.     buffer = NULL;
  62.  
  63.     for (;;)
  64.     {
  65.     c = FGetC (fh);
  66.  
  67.     if (c == EOF)
  68.     {
  69.         FreeVec (buffer);
  70.         return FALSE;
  71.     }
  72.  
  73.     if (size == maxsize)
  74.     {
  75.         STRPTR tmp;
  76.  
  77.         tmp = AllocVec (maxsize + 16, MEMF_ANY);
  78.  
  79.         if (!tmp)
  80.         {
  81.         FreeVec (buffer);
  82.         return FALSE;
  83.         }
  84.  
  85.         if (buffer)
  86.         {
  87.         strcpy (tmp, buffer);
  88.         FreeVec (buffer);
  89.         }
  90.  
  91.         buffer = tmp;
  92.     }
  93.  
  94.     buffer[size ++] = c;
  95.  
  96.     if (!c)
  97.         break;
  98.     }
  99.  
  100.     *dataptr = buffer;
  101.  
  102.     return TRUE;
  103. } /* ReadString */
  104.  
  105.