标签: boost-iostreams

如何使用boost :: iostreams将bash脚本转换为C++

我正在尝试使用boost :: iostreams将以下bash代码转换为C++:

#!/usr/bin/bash
(
    gzip -cd file1.ext.gz
    cat file2.ext
) | grep '^regex' # or sed 's/search/replace/'
Run Code Online (Sandbox Code Playgroud)

我可以打开一个文件并解压缩它:

std::ifstream s("file.ext.gz", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_istreambuf in;
in.push(boost::iostreams::gzip_decompressor());
in.push(s);
Run Code Online (Sandbox Code Playgroud)

然后打开一个未压缩的文件:

std::ifstream s2("file.ext", std::ios_base::in | std::ios_base::binary);
Run Code Online (Sandbox Code Playgroud)

现在我有点卡住了,所以这是我的问题:

1)什么是boost :: iostreams解决方案来连接两个流?

2)如何通过正则表达式过滤器输出结果来模拟grep/sed?

因此,我想要一个可以复制到cout的istream:

boost::iostream::copy(result, std::cout);
Run Code Online (Sandbox Code Playgroud)

使用Hamigaki的连接更新完整解决方案:

/*
 * convert the following bash script into C++
 *
 * #!/bin/bash
 * (
 *     gzip -cd file1.ext.gz
 *     cat file2.ext
 * ) | grep '^filter' | 'sed s/search/replace/g'
 *
 */

#include <iostream>
#include …
Run Code Online (Sandbox Code Playgroud)

c++ bash boost boost-iostreams boost-regex

9
推荐指数
1
解决办法
2702
查看次数

使用boost :: iostreams :: tee_device?

有人能帮我吗?

我想尝试做以下事情:

#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <sstream>  
#include <cassert>  

namespace io = boost::iostreams;
typedef io::stream<io::tee_device<std::stringstream, std::stringstream> > Tee;
std::stringstream ss1, ss2;
Tee my_split(ss1, ss2); // redirects to both streams
my_split << "Testing";
assert(ss1.str() == "Testing" && ss1.str() == ss2.str());
Run Code Online (Sandbox Code Playgroud)

但它不会在VC9中编译:

c:\lib\boost_current_version\boost\iostreams\stream.hpp(131) : error C2665: 'boost::iostreams::tee_device<Sink1,Sink2>::tee_device' : none of the 2 overloads could convert all the argument types
Run Code Online (Sandbox Code Playgroud)

有没有人得到这个工作?我知道我可以自己上课去做,但我想知道我做错了什么.

谢谢

c++ iostream stream boost-iostreams

7
推荐指数
1
解决办法
3426
查看次数

有人可以提供使用boost iostreams搜索,读取和写入> 4GB文件的示例

我已经读过,升压iostreams据称支持64位访问大文件的半便携式方式.他们的常见问题解答提到64位偏移函数,但没有关于如何使用它们的例子.有没有人用这个库来处理大文件?打开两个文件,寻找中间文件,将一个文件复制到另一个文件的简单示例将非常有用.

谢谢.

c++ large-files boost-iostreams

7
推荐指数
1
解决办法
8060
查看次数

为什么std :: istream不承担对streambuf的所有权?

我正在为CRI Middleware的ROFS(见维基百科)等视频游戏编写某种虚拟文件系统库.我对图书馆的意图是提供访问我开发的游戏资源的自然方法,它存储可执行文件中嵌入的一些数据,一些存储在媒体上,一些存储在本地用户的硬盘上(首选项,保存游戏文件等) .

访问这些资源应该像打电话一样简单

std::auto_ptr<std::istream> defaultConfigIStream(
    fslib.inputStream("self://defaultConfig.ini"));
std::auto_ptr<std::ostream> defaultConfigOStream(
    fslib.outputStream("localappdata://config.ini"));

// Copies default configuration to local user's appdata folder
defaultConfigIStream >> defaultConfigOStream;
Run Code Online (Sandbox Code Playgroud)

实际的做事方式实际上是不同的,另一个抽象层用于后台加载,但这在这里并不重要.

我想知道的是,我怎么能返回那个auto_ptr<>(或者unique_ptr<>你选择),因为当它被破坏时,与它std::streambuf<>相关联的东西std::[i/o]stream<>不被它删除.

我正在考虑std::[i/o]stream<>不建议在构建时传递给它的streambuf的所有权,因为构造函数不提供所有权语义的转移,Apache的STDCXX引用没有提到所有权的转换(我找不到任何stdlib引用)在网上).

我有什么替代品?我不妨返回一个共享指针并继续观察它,直到FSlib管理器保留共享指针的唯一副本,在这种情况下它会破坏它的唯一副本以及streambuf.考虑到图书馆的组织模式,这是切实可行的,但对于这个问题,这不是很优雅也不高效.

我试过看看Boost.Iostreams,但看起来事情对我来说更糟糕,因为流本身的设备类型强烈附加到它们的类型(流的设备必须在其模板参数中定义) ).这个问题似乎使得我的库使用Boost.Iostreams是不可行的,因为它需要抽象出流的具体"源/接收器"实现,以便流可以无缝地用于打开位于可执行文件本身内的文件,例如,在系统文件系统的文件内或存档类型文件中.

我可以写一个处理这些问题的容器类,但我宁愿更干净地做(也就是刚刚返回流;这就是它应该需要的全部!;).

建议?

c++ iostream virtualfilesystem boost-iostreams

6
推荐指数
1
解决办法
1270
查看次数

BOOST.IOstreams:写入bzip2的麻烦

您好我想使用Boost.IOstreams将我的数据存储到bzip2文件中.

void test_bzip()
{
namespace BI = boost::iostreams;
{
string fname="test.bz2";
  {
    BI::filtering_stream<BI::bidirectional> my_filter; 
    my_filter.push(BI::combine(BI::bzip2_decompressor(), BI::bzip2_compressor())) ; 
    my_filter.push(std::fstream(fname.c_str(), std::ios::binary|std::ios::out)) ; 
    my_filter << "test" ; 

    }//when my_filter is destroyed it is trowing an assertion.
}
};
Run Code Online (Sandbox Code Playgroud)

我做错了什么?我正在使用boost 1.42.0.

亲切的问候阿曼.

编辑 如果我删除双向选项代码正在运行:

#include <fstream> 
#include <iostream> 
#include <boost/iostreams/copy.hpp> 
#include <boost/iostreams/filter/bzip2.hpp> 
#include <boost/iostreams/device/file.hpp> 
#include <boost/iostreams/filtering_stream.hpp>
#include <string>



void test_bzip()
{
        namespace BI = boost::iostreams;
        {
                std::string fname="test.bz2";
                {
                        std::fstream myfile(fname.c_str(), std::ios::binary|std::ios::out); 
                        BI::filtering_stream<BI::output> my_filter; 
                        my_filter.push(BI::bzip2_compressor()) ; 
                        //my_filter.push(std::fstream(fname.c_str(), std::ios::binary|std::ios::out)) ; //this line will work on …
Run Code Online (Sandbox Code Playgroud)

c++ boost-iostreams bzip2

6
推荐指数
1
解决办法
2036
查看次数

使用从Windows上的源代码构建的bzip2来增强iostream

最近,我花了一些时间来研究在Windows上构建的升级版本1.44.0,两个iostream都支持zlib和bzip2压缩过滤器.出于各种原因,决定允许boost从源代码构建zlib和bzip2库.对于它的价值,但我认为不重要,我使用的MSVC版本是VC9(VS2008).另请注意,从我的肤浅检查来看,这个问题应该适用于在Windows上针对bzip2构建的iostreams的任何版本的boost.

通过指定-sZLIB_SOURCE=<PATH>和完全提升构建-sBZIP2_SOURCE=<PATH>.但是,有人注意到boost_iostreams*.dll库取决于libbz2.dll(而不是boost_bzip2*.dll),它不存在.然而,Boost成功构建了boost_bzip2*.dll.请注意,我使用通配符作为所有构建变体信息的占位符.

问题:某处存在对libbz2.dll的硬编码依赖.

c++ windows boost boost-iostreams bzip2

6
推荐指数
1
解决办法
2021
查看次数

可以增强iostreams读取和压缩gzip文件吗?

我正在使用boost iostreams读取一个gzip压缩文件:以下工作正常:

 namespace io = boost::iostreams;
  io::filtering_istream in;
  in.push(boost::iostreams::basic_gzip_decompressor<>());
  in.push(io::file_source("test.gz"));
  stringstream ss;
  copy(in, ss);
Run Code Online (Sandbox Code Playgroud)

但是,我不想把整个gzip压缩文件读入内存.我希望能够逐步读取文件.

例如,如果我有一个从istream初始化自己的数据结构X,

X x;
x.read(in);
Run Code Online (Sandbox Code Playgroud)

失败.据推测,这是因为如果我们正在进行部分流,我们可能不得不将字符放回到流中.任何想法是否提升iostreams支持这个?

c++ gzip boost-iostreams

6
推荐指数
1
解决办法
2785
查看次数

使用boost :: iostreams时找不到警告消息RTTI符号

我使用Boost :: iostreams同时写入我的控制台和文件.当我使用eclipse进行调试时(当然使用gdb),我会收到一条警告,说明我在Boost :: iostreams中使用的某个类没有找到RTTI符号.

这是重现问题的最小代码.

#ifndef BOOST_IO_STREAM_H_
#define BOOST_IO_STREAM_H_

#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
using boost::iostreams::tee_device;
using boost::iostreams::stream;

typedef tee_device<std::ostream, std::ofstream> TeeDevice;
typedef stream<TeeDevice> TeeStream;

#endif /* BOOST_IO_STREAM_H_ */

int
main()
{

  /* A config file to output experiment details */
  std::string self_filename = "./experimentconfig.txt";
  std::ofstream fconfig(self_filename.c_str());
  TeeDevice my_tee(std::cout, fconfig);
  TeeStream cool_cout(my_tee);

  cool_cout << "Output to file and console during experiment run" << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我TeeStream cool_cout(my_tee);在调试期间越线时,我收到以下警告:

warning: RTTI symbol not found for class …
Run Code Online (Sandbox Code Playgroud)

gdb eclipse-cdt rtti debug-symbols boost-iostreams

6
推荐指数
1
解决办法
1万
查看次数

用 boost 过滤流替换 std::ofstream 的参数语法

关于 boostfiltering_streams 的一些基本问题。我有几十个函数接受 std::ofstream& 的参数

void foo(std::ofstream& outStream)
{
    // lots of operations, like this:
    outStream << "various bits of text";
}

void StreamSomeTextToFile(char* fileName)
{
    ofstream myFileStream(fileName, ios::out | ios::app | ios::binary);
    foo(myFileStream);
    myFileStream.close();
}
Run Code Online (Sandbox Code Playgroud)

现在我想使用 boost filtering_stream 输出到压缩的 ZIP 文件。用于打包和解包的常用boost filtering_streams 测试代码已编译、链接并非常适合我。我想替换filtering_stream:

void StreamSomeCompressedTextToFile(char* fileName)
{
    ofstream myFileStream(destPath, std::ios_base::out | std::ios_base::app | std::ios_base::binary);
    boost::iostreams::filtering_streambuf<boost::iostreams::output> myCompressedFileStream;
    myCompressedFileStream.push(boost::iostreams::zlib_compressor());
    myCompressedFileStream.push(myFileStream);

    foo(myCompressedFileStream);    // I can't just pass myCompressedFileStream to foo(std::ofstream&), right?
    myFileStream.close();
}
Run Code Online (Sandbox Code Playgroud)

三个问题:

1) 我之前接受 std::ofstream& outStream 的所有函数现在都需要接受 boost::iostreams::filtering_streambuf& 类型的参数吗?或者是否有适当的参数类型,以便那些众多(“foo”)函数可以与任何一种类型的流类型一起使用?

2) …

c++ boost zlib boost-iostreams

6
推荐指数
1
解决办法
1909
查看次数

如何使用zlib支持构建增强功能?

我正在尝试使用zlib支持在Windows上构建Boost C++库版本1.65.1.我正在使用Conan包中的zlib库和我之前构建的zlib.我正在尝试了解使用zlib支持构建boost的正确步骤,以使用它们来创建Conan配方以进行提升.我试图按照官方增强文档中的说明进行操作.我设置环境变量ZLIB_LIBRARY_PATH,ZLIB_NAME以及ZLIB_INCLUDE通过以下方式:

set ZLIB_LIBRARY_PATH=C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\igsoft\stable\package\63da998e3642b50bee33f4449826b2d623661505\lib
set ZLIB_NAME=zlibstat
set ZLIB_INCLUDE=C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\igsoft\stable\package\63da998e3642b50bee33f4449826b2d623661505\include
Run Code Online (Sandbox Code Playgroud)

并且构建命令是:

.\b2.exe -j8 --prefix="C:\work\test_builds\boost\install\x64_shared_release" --build-dir="C:\work\test_builds\boost\build\x64_shared_release" --layout=system architecture=x86 address-model=64 toolset=msvc variant=release debug-symbols=on link=shared threading=multi runtime-link=shared install
Run Code Online (Sandbox Code Playgroud)

结果是:

 - zlib                     : no  (cached)
Run Code Online (Sandbox Code Playgroud)

我还尝试在运行之间清除boost build cash.

在此之后,我尝试直接从b2build命令设置环境变量:

.\b2.exe -j8 -sZLIB_LIBRARY_PATH="C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\igsoft\stable\package\63da998e3642b50bee33f4449826b2d623661505\lib"-sZLIB_NAME="zlibstat" -sZLIB_INCLUDE="C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\igsoft\stable\package\63da998e3642b50bee33f4449826b2d623661505\include" --prefix="C:\work\test_builds\boost\install\x64_shared_release" --build-dir="C:\work\test_builds\boost\build\x64_shared_release" --layout=system architecture=x86 address-model=64 toolset=msvc variant=release debug-symbols=on link=shared threading=multi runtime-link=shared install
Run Code Online (Sandbox Code Playgroud)

结果是一样的.

最后,我尝试通过project-config.jam以下方式将选项添加到文件中:

using zlib : 1.2.11 : <search>C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\igsoft\stable\package\63da998e3642b50bee33f4449826b2d623661505\lib <name>zlibstat <include>C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\igsoft\stable\package\63da998e3642b50bee33f4449826b2d623661505\include ;
Run Code Online (Sandbox Code Playgroud)

我再一次没有成功.

在zlib include文件夹中有子文件夹zlib.我还尝试了上面的3个变体,将路径设置为"../include/zlib".再没有成功.

如何使用zlib预先构建的Conan包使用zlib支持构建Boost?

c++ boost zlib boost-iostreams

6
推荐指数
1
解决办法
2179
查看次数