// This is part of the iostream library, providing input/output for C++.
// Copyright (C) 1991 Per Bothner.
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This library 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
// Library General Public License for more details.
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
streambuf* parsebuf::setbuf(char*, int)
int parsebuf::tell_in_line()
int parsebuf::pbackfail(int c)
if (seekoff(-1, ios::cur) == EOF)
char* parsebuf::current_line() { return NULL; }
streampos parsebuf::seekoff(streamoff offset, _seek_dir dir, int)
// Make offset relative to line start.
offset -= pos_at_line_start;
offset += tell_in_line();
if (offset > _line_length + 1)
return seek_in_line(offset) + pos_at_line_start;
// string_parsebuf invariants:
// The reserve ares (base() .. ebuf()) is always the entire string.
// The get area (eback() .. egptr()) is the extended current line
// (i.e. with the '\n' at either end, if these exist).
string_parsebuf::string_parsebuf(char *buf, int len,
int delete_at_close /* = 0*/)
setb(buf, buf+len, delete_at_close);
register char *ptr = buf;
while (ptr < ebuf() && *ptr != '\n') ptr++;
_line_length = ptr - buf;
int string_parsebuf::underflow()
register char* ptr = egptr(); // Point to end of current_line
ptr++; i--; // Skip '\n'.
while (ptr < right() && *ptr == '\n') ptr++;
setg(line_start-1, line_start, ptr + (ptr < right()));
pos_at_line_start = line_start - left();
_line_length = ptr - line_start;
char* string_parsebuf::current_line()
ptr++; // Skip '\n' at end of previous line.
int string_parsebuf::tell_in_line()
int offset = gptr() - eback();
int string_parsebuf::seek_in_line(int i)
int delta = i - tell_in_line();
gbump(delta); // FIXME: Needs error (bounds) checking!
static const char NewLine[1] = { '\n' };
general_parsebuf::general_parsebuf(streambuf *buf, int delete_arg_buf)
delete_buf = delete_arg_buf;
char* buffer = ALLOC_BUF(buf_size);
setb(buffer, buffer+buf_size, 1);
// setg(buffer, buffer, buffer);
general_parsebuf::~general_parsebuf()
int general_parsebuf::underflow()
register char *ptr = base();
int has_newline = eback() < gptr() && gptr()[-1] == '\n';
register streambuf *sb = sbuf;
int old_size = ebuf() - base();
char *new_buffer = new char[old_size * 2];
memcpy(new_buffer, base(), old_size);
setb(new_buffer, new_buffer + 2 * old_size, 1);
ptr = new_buffer + old_size;
char *cur_pos = base() + has_newline;
pos_at_line_start += _line_length + 1;
_line_length = ptr - cur_pos;
if (ch != EOF || _line_length > 0)
setg(base(), cur_pos, ptr);
return ptr == cur_pos ? EOF : cur_pos[0];
char* general_parsebuf::current_line()
ret++; // Move past '\n' from end of previous line.
int general_parsebuf::tell_in_line()
int off = gptr() - base();
off--; // Subtract 1 for '\n' from end of previous line.
int general_parsebuf::seek_in_line(int i)
(void)general_parsebuf::underflow();
i++; // Add 1 for '\n' from end of previous line.
int len = egptr() - eback();
setg(base(), base() + i, egptr());
func_parsebuf::func_parsebuf(CharReader func, void *argm) : parsebuf()
setb((char*)NewLine, (char*)NewLine+1, 0);
setg((char*)NewLine, (char*)NewLine+1, (char*)NewLine+1);
backed_up_to_newline = 0;
int func_parsebuf::tell_in_line()
if (egptr() != (char*)NewLine+1)
// Get buffer was line buffer.
return gptr() - buf_start;
if (backed_up_to_newline)
return -1; // Get buffer is '\n' preceding current line.
// Get buffer is '\n' following current line.
return (buf_end - buf_start) + (gptr() - (char*)NewLine);
char* func_parsebuf::current_line()
int func_parsebuf::seek_in_line(int i)
// Back up to preceding '\n'.
backed_up_to_newline = 1;
setg((char*)NewLine, (char*)NewLine+(i+1), (char*)NewLine+1);
backed_up_to_newline = 0;
int line_length = buf_end-buf_start;
setg(buf_start, buf_start+i, buf_end);
setg((char*)NewLine, (char*)NewLine+i, (char*)NewLine+1);
int func_parsebuf::underflow()
if (gptr() != (char*)NewLine+1) {
// Get buffer was line buffer. Move to following '\n'.
setg((char*)NewLine, (char*)NewLine, (char*)NewLine+1);
if (backed_up_to_newline)
// Get buffer was '\n' preceding current line. Move to current line.
backed_up_to_newline = 0;
// Get buffer was '\n' following current line. Read new line.
if (buf_start) free(buf_start);
char *str = (*read_func)(arg);
// Initially, _line_length == -1, so pos_at_line_start becomes 0.
pos_at_line_start += _line_length + 1;
_line_length = strlen(str);
buf_end = str + _line_length;
setg(buf_start, buf_start, buf_end);
size_t parsebuf::line_length()
if (current_line_length == (size_t)(-1)) // Initial value;
return current_line_length;
int parsebuf::seek_in_line(int i)
return 0; // Suppress warning.
size_t len = line_length();
if ((unsigned)i > len) i = len;
int new_pos = seekoff(pos_at_line_start + i, ios::beg);
else return new_pos - pos_at_line_start;