home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / txtut122.zip / textutil / lib / linebuffer.c < prev    next >
C/C++ Source or Header  |  1997-01-15  |  2KB  |  96 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. /* Written by Richard Stallman. */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23.  
  24. #include <stdio.h>
  25. #include "linebuffer.h"
  26.  
  27. char *xmalloc ();
  28. char *xrealloc ();
  29. void free ();
  30.  
  31. /* Initialize linebuffer LINEBUFFER for use. */
  32.  
  33. void
  34. initbuffer (linebuffer)
  35.      struct linebuffer *linebuffer;
  36. {
  37.   linebuffer->length = 0;
  38.   linebuffer->size = 200;
  39.   linebuffer->buffer = (char *) xmalloc (linebuffer->size);
  40. }
  41.  
  42. /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
  43.    Remove any newline.  Does not null terminate.
  44.    Return LINEBUFFER, except at end of file return 0.  */
  45.  
  46. struct linebuffer *
  47. readline (linebuffer, stream)
  48.      struct linebuffer *linebuffer;
  49.      FILE *stream;
  50. {
  51.   int c;
  52.   char *buffer = linebuffer->buffer;
  53.   char *p = linebuffer->buffer;
  54.   char *end = buffer + linebuffer->size; /* Sentinel. */
  55.  
  56.   if (feof (stream))
  57.     {
  58.       linebuffer->length = 0;
  59.       return 0;
  60.     }
  61.  
  62.   while (1)
  63.     {
  64.       c = getc (stream);
  65.       if (p == end)
  66.     {
  67.       linebuffer->size *= 2;
  68.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  69.       p += buffer - linebuffer->buffer;
  70.       linebuffer->buffer = buffer;
  71.       end = buffer + linebuffer->size;
  72.     }
  73.       if (c == EOF || c == '\n')
  74.     break;
  75.       *p++ = c;
  76.     }
  77.  
  78.   if (feof (stream) && p == buffer)
  79.     {
  80.       linebuffer->length = 0;
  81.       return 0;
  82.     }
  83.   linebuffer->length = p - linebuffer->buffer;
  84.   return linebuffer;
  85. }
  86.  
  87. /* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */
  88.  
  89. void
  90. freebuffer (linebuffer)
  91.      struct linebuffer *linebuffer;
  92. {
  93.   free (linebuffer->buffer);
  94.   free (linebuffer);
  95. }
  96.