|
|
1.1 ! root 1: /* This is part of libio/iostream, providing -*- C++ -*- input/output. ! 2: Copyright (C) 1993 Free Software Foundation ! 3: ! 4: This file is part of the GNU IO Library. This library is free ! 5: software; you can redistribute it and/or modify it under the ! 6: terms of the GNU General Public License as published by the ! 7: Free Software Foundation; either version 2, or (at your option) ! 8: any later version. ! 9: ! 10: This library is distributed in the hope that it will be useful, ! 11: but WITHOUT ANY WARRANTY; without even the implied warranty of ! 12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! 13: GNU General Public License for more details. ! 14: ! 15: You should have received a copy of the GNU General Public License ! 16: along with GNU CC; see the file COPYING. If not, write to ! 17: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. ! 18: ! 19: As a special exception, if you link this library with files ! 20: compiled with a GNU compiler to produce an executable, this does not cause ! 21: the resulting executable to be covered by the GNU General Public License. ! 22: This exception does not however invalidate any other reasons why ! 23: the executable file might be covered by the GNU General Public License. */ ! 24: ! 25: #include <libioP.h> ! 26: #include "iostream.h" ! 27: #include <string.h> ! 28: ! 29: istream& istream::getline(char* buf, int len, char delim) ! 30: { ! 31: _gcount = 0; ! 32: if (ipfx1()) ! 33: { ! 34: streambuf *sb = rdbuf(); ! 35: long count = _IO_getline(sb, buf, len, delim, -1); ! 36: if (count == len-1) ! 37: set(ios::failbit); ! 38: else { ! 39: int ch = sb->sbumpc(); ! 40: if (ch == EOF) ! 41: set(ios::failbit|ios::eofbit); ! 42: else if (ch == (unsigned char)delim) ! 43: count++; ! 44: else ! 45: sb->sungetc(); // Leave delimiter unread. ! 46: } ! 47: _gcount = count; ! 48: } ! 49: return *this; ! 50: } ! 51: ! 52: istream& istream::get(char* buf, int len, char delim) ! 53: { ! 54: _gcount = 0; ! 55: if (ipfx1()) ! 56: { ! 57: streambuf *sbuf = rdbuf(); ! 58: long count = _IO_getline(sbuf, buf, len, delim, -1); ! 59: if (count < 0 || (count == 0 && sbuf->sgetc() == EOF)) ! 60: set(ios::failbit|ios::eofbit); ! 61: else ! 62: _gcount = count; ! 63: } ! 64: return *this; ! 65: } ! 66: ! 67: ! 68: // from Doug Schmidt ! 69: ! 70: #define CHUNK_SIZE 512 ! 71: ! 72: /* Reads an arbitrarily long input line terminated by a user-specified ! 73: TERMINATOR. Super-nifty trick using recursion avoids unnecessary calls ! 74: to NEW! */ ! 75: ! 76: char *_sb_readline (streambuf *sb, long& total, char terminator) ! 77: { ! 78: char buf[CHUNK_SIZE+1]; ! 79: char *ptr; ! 80: int ch; ! 81: ! 82: long count = sb->sgetline(buf, CHUNK_SIZE+1, terminator, -1); ! 83: if (count == EOF) ! 84: return NULL; ! 85: ch = sb->sbumpc(); ! 86: long old_total = total; ! 87: total += count; ! 88: if (ch != EOF && ch != terminator) { ! 89: total++; // Include ch in total. ! 90: ptr = _sb_readline(sb, total, terminator); ! 91: if (ptr) { ! 92: memcpy(ptr + old_total, buf, count); ! 93: ptr[old_total+count] = ch; ! 94: } ! 95: return ptr; ! 96: } ! 97: ! 98: if (ptr = new char[total+1]) { ! 99: ptr[total] = '\0'; ! 100: memcpy(ptr + total - count, buf, count); ! 101: return ptr; ! 102: } ! 103: else ! 104: return NULL; ! 105: } ! 106: ! 107: /* Reads an arbitrarily long input line terminated by TERMINATOR. ! 108: This routine allocates its own memory, so the user should ! 109: only supply the address of a (char *). */ ! 110: ! 111: istream& istream::gets(char **s, char delim /* = '\n' */) ! 112: { ! 113: if (ipfx1()) { ! 114: long size = 0; ! 115: streambuf *sb = rdbuf(); ! 116: *s = _sb_readline (sb, size, delim); ! 117: _gcount = *s ? size : 0; ! 118: if (sb->_flags & _IO_EOF_SEEN) { ! 119: set(ios::eofbit); ! 120: if (_gcount == 0) ! 121: set(ios::failbit); ! 122: } ! 123: } ! 124: else { ! 125: _gcount = 0; ! 126: *s = NULL; ! 127: } ! 128: return *this; ! 129: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.