DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T r

⟦0e0992640⟧ TextFile

    Length: 3096 (0xc18)
    Types: TextFile
    Names: »read-line.cc«

Derivation

└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89
    └─⟦cc8755de2⟧ »./libg++-1.36.1.tar.Z« 
        └─⟦23757c458⟧ 
            └─⟦this⟧ »libg++/gperf/src/read-line.cc« 

TextFile

/* Correctly reads an arbitrarily size string.

   Copyright (C) 1989 Free Software Foundation, Inc.
   written by Douglas C. Schmidt (schmidt@ics.uci.edu)

This file is part of GNU GPERF.

GNU GPERF is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.

GNU GPERF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU GPERF; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <std.h>
#include "std-err.h"
#include "read-line.h"

/* Provide an abstraction that cuts down on the number of
   calls to NEW by buffering the memory pool from which
   strings are allocated. */

static inline void *
operator new (long size)
{
  char *temp;
  static char *buf_start = 0;          /* Large array used to reduce calls to NEW. */
  static char *buf_end = 0;            /* Indicates end of BUF_START. */
  static int   buf_size = 4 * BUFSIZ; /* Size of buffer pointed to by BUF_START. */

  /* If we are about to overflow our buffer we'll just grab another
     chunk of memory.  Since we never free the original memory it
     doesn't matter that no one points to the beginning of that
     chunk. */
  
  if (buf_start + size >= buf_end)
    {
      if (buf_start = malloc (buf_size))
        buf_end = buf_start + buf_size;
      else
        Std_Err::report_error ("virtual memory failed at %s, %s%a\n", __FILE__, __LINE__);
    }

  temp = buf_start;
  buf_start += size;
  return temp;
}

/* Read_Line constructor. */

Read_Line::Read_Line (FILE *stream, int size)
{
  fp         = stream;
  chunk_size = size;
}

/* Recursively fills up the buffer. */

char *
Read_Line::readln_aux(int chunks) 
{
  char buf[chunk_size];
  char *bufptr = buf;
  char *ptr;
  int c;

  while ((c = getc (fp)) != EOF && c != '\n') /* fill the current buffer */
    {
      *bufptr++ = c;
      if (bufptr - buf >= chunk_size) /* prepend remainder to ptr buffer */
        {
          if (ptr = readln_aux (chunks + 1))

            for (; bufptr != buf; *--ptr = *--bufptr);

          return ptr;
        }
    }
  if (c == EOF && bufptr == buf)
    return 0;

  c   = chunks * chunk_size + bufptr - buf + 1;
  ptr = new char[c];

  for (*(ptr += (c - 1)) = '\0'; bufptr != buf; *--ptr = *--bufptr)
    ;

  return ptr;
}

#ifndef __OPTIMIZE__

/* Returns the ``next'' line, ignoring comments beginning with '#'. */

char *
Read_Line::get_line (void) 
{
  int c;

  if ((c = getc (fp)) == '#')
    {
      while ((c = getc (fp)) != '\n' && c != EOF)
        ;

      return c != EOF ? get_line () : NULL;
    }
  else
    {
      ungetc (c, stdin);
      return readln_aux (0);
    }
}

/* Returns length of ``current'' line. */
#endif