#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(); }
isfx();
_IO_cleanup_region_end (0);
}
else
ch = EOF;
buf[_gcount] = '\0';
if (ch == (unsigned char)delim)
_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;
}
#define CHUNK_SIZE 512
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++; 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;
}
istream& istream::gets(char **s, char delim )
{
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;
}