aboutsummaryrefslogtreecommitdiff
path: root/libio/isgetline.cc
blob: 5e48a000c09ab9b35a0091bdf1f56a026f523849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This is part of libio/iostream, providing -*- C++ -*- input/output.
Copyright (C) 1993 Free Software Foundation

This file is part of the GNU IO Library.  This library 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 2, 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

As a special exception, if you link this library with files
compiled with a GNU compiler to produce an executable, this does not cause
the resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License. */

#include <libioP.h>
#include "iostream.h"
#include <string.h>

istream& istream::getline(char* buf, int len, char delim)
{
  _gcount = 0;
  if (len <= 0)
    {
      set(ios::failbit);
      return *this;
    }
  int ch;
  if (ipfx1())
    {
      _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile,
				_strbuf);
      streambuf *sb = rdbuf();
      _gcount = _IO_getline_info(sb, buf, len - 1, delim, -1, &ch);
      if (ch != EOF)
	ch = sb->sbumpc();
      if (ch == EOF)
	set (_gcount == 0 ? (ios::failbit|ios::eofbit) : ios::eofbit);
      else if (ch != (unsigned char) delim)
	{
	  set(ios::failbit);
	  sb->sungetc(); // Leave delimiter unread.
	}
      isfx();
      _IO_cleanup_region_end (0);
    }
  else
    ch = EOF;
  buf[_gcount] = '\0';
  if (ch == (unsigned char)delim)
    _gcount++; // The delimiter is counted in the gcount().
  return *this;
}

istream& istream::get(char* buf, int len, char delim)
{
  _gcount = 0;
  if (len <= 0)
    {
      set(ios::failbit);
      return *this;
    }
  if (ipfx1())
    {
      _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile,
				_strbuf);
      streambuf *sbuf = rdbuf();
      int ch;
      _gcount = _IO_getline_info(sbuf, buf, len - 1, delim, -1, &ch);
      if (_gcount == 0 && ch == EOF)
	set(ios::failbit|ios::eofbit);
      isfx();
      _IO_cleanup_region_end (0);
    }
  buf[_gcount] = '\0';
  return *this;
}


// from Doug Schmidt

#define CHUNK_SIZE 512

/* Reads an arbitrarily long input line terminated by a user-specified
   TERMINATOR.  Super-nifty trick using recursion avoids unnecessary calls
   to NEW! */

char *_sb_readline (streambuf *sb, long& total, char terminator) 
{
    char buf[CHUNK_SIZE];
    char *ptr;
    int ch;
    
    _IO_size_t count = _IO_getline_info(sb, buf, CHUNK_SIZE, terminator,
				       -1, &ch);
    if (ch != EOF)
      ch = sb->sbumpc();
    long old_total = total;
    total += count;
    if (ch != EOF && ch != terminator) {
	total++; // Include ch in total.
	ptr = _sb_readline(sb, total, terminator);
	if (ptr) {
	    memcpy(ptr + old_total, buf, count);
	    ptr[old_total+count] = ch;
	}
	return ptr;
    }
    
    ptr = new char[total+1];
    if (ptr != NULL) {
	ptr[total] = '\0';
	memcpy(ptr + total - count, buf, count);
    } 
    return ptr;
}

/* Reads an arbitrarily long input line terminated by TERMINATOR.
   This routine allocates its own memory, so the user should
   only supply the address of a (char *). */

istream& istream::gets(char **s, char delim /* = '\n' */)
{
    if (ipfx1()) {
	_IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile,
				  _strbuf);
	long size = 0;
	streambuf *sb = rdbuf();
	*s = _sb_readline (sb, size, delim);
	_gcount = *s ? size : 0;
	if (sb->_flags & _IO_EOF_SEEN) {
	    set(ios::eofbit);
	    if (_gcount == 0)
		set(ios::failbit);
	}
	isfx();
	_IO_cleanup_region_end (0);
    }
    else {
	_gcount = 0;
	*s = NULL;
    }
    return *this;
}