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 this library; see the file COPYING. If not, write to the Free 17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 (len <= 0) 33 { 34 set(ios::failbit); 35 return *this; 36 } 37 int ch; 38 if (ipfx1()) 39 { 40 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, 41 _strbuf); 42 streambuf *sb = rdbuf(); 43 _gcount = _IO_getline_info(sb, buf, len - 1, delim, -1, &ch); 44 if (ch != EOF) 45 ch = sb->sbumpc(); 46 if (ch == EOF) 47 set (_gcount == 0 ? (ios::failbit|ios::eofbit) : ios::eofbit); 48 else if (ch != (unsigned char) delim) 49 { 50 set(ios::failbit); 51 sb->sungetc(); // Leave delimiter unread. 52 } 53 isfx(); 54 _IO_cleanup_region_end (0); 55 } 56 else 57 ch = EOF; 58 buf[_gcount] = '\0'; 59 if (ch == (unsigned char)delim) 60 _gcount++; // The delimiter is counted in the gcount(). 61 return *this; 62 } 63 64 istream& istream::get(char* buf, int len, char delim) 65 { 66 _gcount = 0; 67 if (len <= 0) 68 { 69 set(ios::failbit); 70 return *this; 71 } 72 if (ipfx1()) 73 { 74 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, 75 _strbuf); 76 streambuf *sbuf = rdbuf(); 77 int ch; 78 _gcount = _IO_getline_info(sbuf, buf, len - 1, delim, -1, &ch); 79 if (_gcount == 0 && ch == EOF) 80 set(ios::failbit|ios::eofbit); 81 isfx(); 82 _IO_cleanup_region_end (0); 83 } 84 buf[_gcount] = '\0'; 85 return *this; 86 } 87 88 89 // from Doug Schmidt 90 91 #define CHUNK_SIZE 512 92 93 /* Reads an arbitrarily long input line terminated by a user-specified 94 TERMINATOR. Super-nifty trick using recursion avoids unnecessary calls 95 to NEW! */ 96 97 char *_sb_readline (streambuf *sb, long& total, char terminator) 98 { 99 char buf[CHUNK_SIZE]; 100 char *ptr; 101 int ch; 102 103 _IO_size_t count = _IO_getline_info(sb, buf, CHUNK_SIZE, terminator, 104 -1, &ch); 105 if (ch != EOF) 106 ch = sb->sbumpc(); 107 long old_total = total; 108 total += count; 109 if (ch != EOF && ch != terminator) { 110 total++; // Include ch in total. 111 ptr = _sb_readline(sb, total, terminator); 112 if (ptr) { 113 memcpy(ptr + old_total, buf, count); 114 ptr[old_total+count] = ch; 115 } 116 return ptr; 117 } 118 119 ptr = new char[total+1]; 120 if (ptr != NULL) { 121 ptr[total] = '\0'; 122 memcpy(ptr + total - count, buf, count); 123 } 124 return ptr; 125 } 126 127 /* Reads an arbitrarily long input line terminated by TERMINATOR. 128 This routine allocates its own memory, so the user should 129 only supply the address of a (char *). */ 130 131 istream& istream::gets(char **s, char delim /* = '\n' */) 132 { 133 if (ipfx1()) { 134 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, 135 _strbuf); 136 long size = 0; 137 streambuf *sb = rdbuf(); 138 *s = _sb_readline (sb, size, delim); 139 _gcount = *s ? size : 0; 140 if (sb->_flags & _IO_EOF_SEEN) { 141 set(ios::eofbit); 142 if (_gcount == 0) 143 set(ios::failbit); 144 } 145 isfx(); 146 _IO_cleanup_region_end (0); 147 } 148 else { 149 _gcount = 0; 150 *s = NULL; 151 } 152 return *this; 153 } 154