boost :: iostreams :: filtering_istream是否允许搜索?

时间:2014-08-20 20:06:45

标签: c++ boost

我试图写一个支持搜索的Boost iostreams过滤器。我的过滤器似乎工作正常(好吧,它编译),但当它试图寻找基础文件并得到一个"没有随机访问"时,我得到了一个令人讨厌的惊喜。异常。

果然,它似乎不适用于测试用例(Ubuntu 14.04; boost 1.54):

#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>

namespace io = boost::iostreams;

main()
{
    std::ifstream input_file("hi");
    io::filtering_istream instream;

    instream.push(input_file);

    input_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
    instream.exceptions(std::ifstream::failbit | std::ifstream::badbit);

    // this works                                                                                                                  
    input_file.seekg(0);

    // this doesn't                                                                                                                
    instream.seekg(0);
}

touch hi并使用g++ -o test_seek test_seek.cc

进行编译

有关如何使用基础可搜索文件寻找filtering_istream的任何想法吗?

1 个答案:

答案 0 :(得分:0)

要支持搜索,需要通过标记 input_seekable 来定义底层设备的功能。然后所有过滤器和设备必须支持寻求否则代码无法编译。在以下示例中,YourFilter必须是可搜索的并且已定义标记 seekable_filter_tag 才能进行编译。

namespace io = boost::iostreams;    
io::filtering_stream<io::input_seekable> input(YourFilter() | boost::ref(input_file));