home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / textutils-1.9-src.lha / src / amiga / textutils-1.9 / lib / linebuffer.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  3KB  |  103 lines

  1. /* linebuffer.c -- read arbitrarily long lines
  2.    Copyright (C) 1986, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman. */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include "linebuffer.h"
  33.  
  34. char *xmalloc ();
  35. char *xrealloc ();
  36. void free ();
  37.  
  38. /* Initialize linebuffer LINEBUFFER for use. */
  39.  
  40. void
  41. initbuffer (linebuffer)
  42.      struct linebuffer *linebuffer;
  43. {
  44.   linebuffer->length = 0;
  45.   linebuffer->size = 200;
  46.   linebuffer->buffer = (char *) xmalloc (linebuffer->size);
  47. }
  48.  
  49. /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
  50.    Remove any newline.  Does not null terminate.
  51.    Return LINEBUFFER, except at end of file return 0.  */
  52.  
  53. struct linebuffer *
  54. readline (linebuffer, stream)
  55.      struct linebuffer *linebuffer;
  56.      FILE *stream;
  57. {
  58.   int c;
  59.   char *buffer = linebuffer->buffer;
  60.   char *p = linebuffer->buffer;
  61.   char *end = buffer + linebuffer->size; /* Sentinel. */
  62.  
  63.   if (feof (stream))
  64.     {
  65.       linebuffer->length = 0;
  66.       return 0;
  67.     }
  68.  
  69.   while (1)
  70.     {
  71.       c = getc (stream);
  72.       if (p == end)
  73.     {
  74.       linebuffer->size *= 2;
  75.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  76.       p += buffer - linebuffer->buffer;
  77.       linebuffer->buffer = buffer;
  78.       end = buffer + linebuffer->size;
  79.     }
  80.       if (c == EOF || c == '\n')
  81.     break;
  82.       *p++ = c;
  83.     }
  84.  
  85.   if (feof (stream) && p == buffer)
  86.     {
  87.       linebuffer->length = 0;
  88.       return 0;
  89.     }
  90.   linebuffer->length = p - linebuffer->buffer;
  91.   return linebuffer;
  92. }
  93.  
  94. /* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */
  95.  
  96. void
  97. freebuffer (linebuffer)
  98.      struct linebuffer *linebuffer;
  99. {
  100.   free (linebuffer->buffer);
  101.   free (linebuffer);
  102. }
  103.