【问题标题】:adding boost::filtering_stream filters dynamically动态添加 boost::filtering_stream 过滤器
【发布时间】:2012-03-25 05:51:27
【问题描述】:

我正在使用boost::iostreamsfiltering_stream 来实现我的自定义存档格式。这种格式应该支持各种压缩算法,以便每个文件可以使用不同的方法进行压缩。

为此,我保留了一个用于读取(或写入)的全局流,并根据需要添加或删除过滤器。然而,目前还不清楚是否真的可以动态添加和删除过滤器。

本质上,我正在尝试做这样的事情:

void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filters )
{
    // create the filtered stream
    boost::iostreams::filtering_istream in;

    if ( filters & FILTERS_BZIP )
    {
        in.push( boost::iostreams::bzip2_decompressor() );
    }

    // add the source stream
    in.push( inputStream );

    // read file content
    in.read( &mFileContent[0], mFileSize );
}

我使用相同的 inputStream 为每个文件调用 readFromStream。但是,即使不使用任何过滤器,我也会以这种方式胡言乱语。当我直接使用 inputStream 时,文件读取正常。

我做错了什么?

【问题讨论】:

    标签: c++ boost iostream


    【解决方案1】:

    我知道这已经有一段时间了,但我只是偶然发现了这个问题。所以答案原来是把整个代码部分放在一个新的范围内:

    void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filter
    {
        // new scope
        {
            // create the filtered stream
            boost::iostreams::filtering_istream in;
    
            if ( filters & FILTERS_BZIP )
            {
              in.push( boost::iostreams::bzip2_decompressor() );
            }
    
            // add the source stream
            in.push( inputStream );
    
            // read file content
            in.read( &mFileContent[0], mFileSize );
        }
    }
    

    这可能是必要的,因为我当时使用的编译器(某些版本的 MS Visual Studio)存在一些错误。

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多