fixed and improved single buffer version, added seeking as well

This commit is contained in:
Bob Polis
2020-04-25 16:40:42 +02:00
parent a3f0de4582
commit de371a94a5
2 changed files with 52 additions and 16 deletions

View File

@ -37,6 +37,8 @@ std::streambuf::int_type sc::io::fdiobuf::overflow(std::streambuf::int_type c) {
std::cerr << "write result = " << res << ", errno = " << errno << '\n'; std::cerr << "write result = " << res << ", errno = " << errno << '\n';
} }
} }
_ch = c;
setp(&_ch, &_ch + 1);
return c; return c;
} }
@ -48,3 +50,28 @@ std::streamsize sc::io::fdiobuf::xsputn(const char* s, std::streamsize num) {
} }
return res; return res;
} }
std::streambuf::pos_type sc::io::fdiobuf::seekoff(std::streambuf::off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode /*which*/) {
int whence;
switch (dir) {
case std::ios_base::beg: whence = SEEK_SET; break;
case std::ios_base::end: whence = SEEK_END; break;
case std::ios_base::cur: whence = SEEK_CUR; break;
default: throw std::runtime_error("invalid seekdir");
}
return ::lseek(_fd, off, whence);
}
std::streambuf::pos_type sc::io::fdiobuf::seekpos(std::streambuf::pos_type pos,
std::ios_base::openmode /*which*/) {
return ::lseek(_fd, pos, SEEK_SET);
}
std::streambuf::int_type sc::io::fdiobuf::pbackfail(std::streambuf::int_type /*c*/) {
if (::lseek(_fd, -1, SEEK_CUR) == -1) {
return EOF;
}
return 0;
}

View File

@ -40,6 +40,15 @@ namespace sc {
// output // output
int_type overflow(int_type c) override; int_type overflow(int_type c) override;
std::streamsize xsputn(const char* s, std::streamsize num) override; std::streamsize xsputn(const char* s, std::streamsize num) override;
// stream offset positioning
pos_type seekoff(off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode which
= std::ios_base::in | std::ios_base::out) override;
pos_type seekpos(pos_type pos, std::ios_base::openmode which
= std::ios_base::in | std::ios_base::out) override;
int_type pbackfail(int_type c = EOF) override;
}; };
class fdostream : public std::ostream { class fdostream : public std::ostream {