4

を使用してzlib_decompressorを介してデータを解凍しようとしていistreambuf_iteratorます。入力イテレータをストリームへの入力として使用する組み込みの方法が見つからなかったので (方法が既に存在する場合は指摘してください)、このソースを書きました。

template <class cha_type, class iterator_type>
class IteratorSource {
    public:
        typedef cha_type char_type;
        typedef boost::iostreams::source_tag category;
        iterator_type& i;
        iterator_type eof;

        IteratorSource(iterator_type& it, iterator_type end) : i(it), eof(end) {
        }

        std::streamsize read(char* s, std::streamsize n) {
            for(int j = 0; j < n; j++) {
                if(i == eof) {
                    std::cout << "Reached eof after " << j << " bytes\n";
                    return -1;
                }
                char next = *i++;
                std::cout << "Reading " << next << "\n";
                *s++ = next;
            }
            return n;
        }
};

そして、次のように使用しました:

int main() {       
    std::vector<char> data_back = {'\x78', '\x9c', '\x73', '\x04', '\x00', '\x00', '\x42', '\x00', '\x42'};
    auto start = data_back.begin();
    IteratorSource<char, decltype(data_back)::iterator> data(start, data_back.end());

    boost::iostreams::filtering_istreambuf def;
    def.push(boost::iostreams::zlib_decompressor());
    def.push(data);
    boost::iostreams::copy(def, std::cout);
    return 0;
}

この出力を与えるには:

Reading x
Reading �
Reading s
Reading 
Reading 
Reading 
Reading B
Reading 
Reading B
Reached eof after 9 bytes
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::iostreams::zlib_error> >'
  what():  zlib error
Aborted (core dumped)

ファイルからの読み込みは正常に機能するため、これがエラーを生成する理由はわかりません。

4

1 に答える 1

4
于 2013-11-04T23:58:23.367 に答える