home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / libg++ / gperf / src / read-line.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  2.2 KB  |  98 lines

  1. /* Correctly reads an arbitrarily size string.
  2.  
  3.    Copyright (C) 1989 Free Software Foundation, Inc.
  4.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  5.  
  6. This file is part of GNU GPERF.
  7.  
  8. GNU GPERF is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU GPERF is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU GPERF; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <std.h>
  23. #include "std-err.h"
  24. #include "read-line.h"
  25.  
  26. /* Recursively fills up the buffer. */
  27.  
  28. char *
  29. Read_Line::readln_aux (int chunks)
  30. {
  31.   T (Trace t ("Read_Line::readln_aux");)
  32. #ifdef __GNUC__
  33.   char buf[CHUNK_SIZE];
  34. #else
  35.   // Note: we don't use new, because that invokes a custom operator new.
  36.   char *buf = (char*)malloc(CHUNK_SIZE);
  37.   if (buf == NULL)
  38.     abort ();
  39. #endif
  40.   char *bufptr = buf;
  41.   char *ptr;
  42.   int c;
  43.  
  44.   while ((c = getc (fp)) != EOF && c != '\n') /* fill the current buffer */
  45.     {
  46.       *bufptr++ = c;
  47.       if (bufptr - buf >= CHUNK_SIZE) /* prepend remainder to ptr buffer */
  48.         {
  49.           if (ptr = readln_aux (chunks + 1))
  50.  
  51.             for (; bufptr != buf; *--ptr = *--bufptr);
  52.  
  53.           goto done;
  54.         }
  55.     }
  56.   if (c == EOF && bufptr == buf)
  57.     ptr = NULL;
  58.   else
  59.     {
  60.       c   = chunks * CHUNK_SIZE + bufptr - buf + 1;
  61.       ptr = new char[c];
  62.  
  63.       for (*(ptr += (c - 1)) = '\0'; bufptr != buf; *--ptr = *--bufptr)
  64.     ;
  65.     }
  66.  done:
  67. #ifndef __GNUC__
  68.   free (buf);
  69. #endif
  70.  
  71.   return ptr;
  72. }
  73.  
  74. #ifndef __OPTIMIZE__
  75.  
  76. /* Returns the ``next'' line, ignoring comments beginning with '#'. */
  77.  
  78. char *
  79. Read_Line::get_line (void) 
  80. {
  81.   T (Trace t ("Read_Line::get_line");)
  82.   int c;
  83.  
  84.   if ((c = getc (fp)) == '#')
  85.     {
  86.       while ((c = getc (fp)) != '\n' && c != EOF)
  87.         ;
  88.  
  89.       return c != EOF ? get_line () : 0;
  90.     }
  91.   else
  92.     {
  93.       ungetc (c, stdin);
  94.       return readln_aux (0);
  95.     }
  96. }
  97. #endif
  98.