从功能返回提升streambuf

时间:2018-06-11 13:25:00

标签: c++ boost streambuf

我正在尝试将读取gz文件的代码包装成函数,源代码取自https://techoverflow.net/2013/11/03/c-iterating-lines-in-a-gz-file-using-boostiostreams/

我的尝试

boost::iostreams::filtering_streambuf<boost::iostreams::input> func(std::string filename);
boost::iostreams::filtering_streambuf<boost::iostreams::input> func(std::string filename)
{
  std::ifstream file(filename, std::ios_base::in | std::ios_base::binary);
  boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
  inbuf.push(boost::iostreams::gzip_decompressor());
  inbuf.push(file);
  return inbuf;

}
void mymainfunc(std::string filename)
{

  //Convert streambuf to istream
  std::istream ifstrm( func( filename));
  std::string line;
  while(std::getline(ifstrm, line)) {
        std::cout << line << std::endl;
    }
}

如果没有通过该函数运行代码运行正常,我认为在返回类型中我做错了。 错误:https://pastebin.com/kFpjYG0M

1 个答案:

答案 0 :(得分:0)

Streams不可复制。事实上,这个过滤器甚至不能移动。

因此,在这种情况下,您需要通过智能指针动态分配和返回。但是,即使只返回过滤streambuf也行不通,因为它会保存对ifstream的引用。这是一个本地人。

所以,也许你需要打包它:

<强> Live On Coliru

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <fstream>

namespace bio = boost::iostreams;

struct MySource {
    using fisb = bio::filtering_istreambuf;

    struct State {
        State(std::string filename) : ifs(filename, std::ios::binary) {
            buf.push(bio::gzip_decompressor());
            buf.push(ifs);
        }

        fisb buf;
        std::ifstream ifs;
        std::istream is { &buf };
    };

    std::unique_ptr<State> _state;

    operator std::istream&() const { return _state->is; }
};

MySource func(std::string filename) {
    auto inbuf = std::make_unique<MySource::State>(filename);
    return {std::move(inbuf)};
}

#include <iostream>
void mymainfunc(std::string filename)
{
    auto source = func(filename);
    std::istream& is = source;

    std::string line;
    while(std::getline(is, line)) {
        std::cout << line << std::endl;
    }
}

int main(){
    mymainfunc("test.cpp.gz");
}

备选方案#1

您可以简化:

<强> Live On Coliru

struct MySource {
    struct State {
        State(std::string filename) : ifs(filename, std::ios::binary) {
            is.push(bio::gzip_decompressor());
            is.push(ifs);
        }

        std::ifstream ifs;
        bio::filtering_istream is;
    };

    std::unique_ptr<State> _state;

    operator std::istream&() const { return _state->is; }
};

不单独处理streambuffer会使其更简单。

备选方案#2

不复制整件事有其优雅:

<强> Live On Coliru

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <fstream>
#include <iostream>

void mymainfunc(std::istream& is) {
    std::string line;
    while(std::getline(is, line)) {
        std::cout << line << std::endl;
    }
}

namespace bio = boost::iostreams;

int main(){
    std::ifstream ifs("test.cpp.gz", std::ios::binary);

    bio::filtering_istream is;
    is.push(bio::gzip_decompressor());
    is.push(ifs);

    mymainfunc(is);
}