11 class fbuf final :
public std::streambuf{
19 std::streambuf *setbuf(char_type *
s, std::streamsize
n)
override;
21 int_type underflow()
override;
24 class cfstream final :
public std::istream{
32 FILE *
f = popen(cmdline.c_str(),
"r");
35 return std::unique_ptr<std::istream>(
new cfstream(f));
40 :
file(
f), buf(
new char[bufsz]){
41 this->setg(this->buf, this->buf+bufsz, this->buf+bufsz);
49 std::streambuf *fbuf::setbuf(char_type *
s, std::streamsize
n){
54 if(fflush(this->
file) != 0)
59 fbuf::int_type fbuf::underflow(){
60 if(this->gptr() < this->egptr()){
61 char c = *this->gptr();
62 this->setg(this->eback(), this->gptr()+1, this->egptr());
63 return traits_type::to_int_type(c);
65 size_t n = fread(this->buf, 1,
sizeof(this->buf), this->
file);
67 return traits_type::eof();
68 this->setg(this->buf, this->buf, this->buf+n);
69 return traits_type::to_int_type(*this->gptr());
72 cfstream::cfstream(FILE *
f)
73 : std::istream(&buf), buf(
f){
std::unique_ptr< std::istream > popenCPP(const std::string &cmdline)