如何将 bash 脚本转换为 C++使用 boost::iostreams
我正在尝试使用 boost::iostreams 将以下 bash 代码转换为 C++:
#!/usr/bin/bash
(
gzip -cd file1.ext.gz
cat file2.ext
) | grep '^regex' # or sed 's/search/replace/'
我可以打开一个文件并解压缩它:
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);
然后打开一个未压缩的文件:
std::ifstream s2("file.ext", std::ios_base::in | std::ios_base::binary);
现在我有点卡住了,所以这里是我的问题:
1)什么是连接两个流的 boost::iostreams 解决方案?
2)如何通过正则表达式过滤器输出结果来模拟grep/sed?
因此,我想要一个可以复制到 cout 的 istream:
boost::iostream::copy(result, std::cout);
使用 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 <boost/bind.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filter/regex.hpp>
#include <boost/iostreams/filter/grep.hpp>
#include <boost/iostreams/copy.hpp>
// http://hamigaki.sourceforge.jp/hamigaki/iostreams/concatenate.hpp
#include "concatenate.hpp"
namespace io = boost::iostreams;
int main(int argc, char const* argv[])
{
io::file_source file1("file1.ext.gz");
io::file_source file2("file2.ext");
io::gzip_decompressor gzip;
io::regex_filter sed(boost::regex("search"), "replace");
io::grep_filter grep(boost::regex("^filter"));
io::filtering_istreambuf in1(gzip | file1);
io::filtering_istreambuf in2(file2);
io::filtering_istreambuf combined(sed | grep |
hamigaki::iostreams::concatenate(
boost::ref(in1),
boost::ref(in2)
)
);
io::copy(combined, std::cout);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)我不知道boost中是否内置了任何东西,但这个类似乎正是你想要的: http://hamigaki.sourceforge.jp/hamigaki/iostreams/concatenate.hpp
这里的问题是它期望可复制构造的设备连接,而链似乎不是可复制构造的。然而,我们可以使用 boost::ref 轻松解决这个问题。这段代码(几乎)做了我所理解的你要问的事情:
我只是使用正则表达式过滤器而不是 gzip 进行测试。
2) boost::iostreams 有一个正则表达式过滤器: http://www.boost.org/doc/libs/1_45_0/libs/iostreams/doc/classes/regex_filter.html
编辑:你现在似乎可以正常工作了。
1) I don't know if there's anything built into boost, but this class seems to be exactly what you want: http://hamigaki.sourceforge.jp/hamigaki/iostreams/concatenate.hpp
The catch here is that it expects CopyConstructible devices to concatenate and Chains seem to not be CopyConstructible. However, we can easily work around that using boost::ref. This code does (almost) what I understood you're asking:
I just used the regex filter instead of gzip, for testing.
2) boost::iostreams has a regex filter: http://www.boost.org/doc/libs/1_45_0/libs/iostreams/doc/classes/regex_filter.html
EDIT: You seem to have this working, now.
1) 在 boost 中不可用
Hamigakis 的串联 听起来很有趣,但我不知道如何使用它来组合两个 boost::iostreams::chain。该代码提到它用于“设备串联”,因此它可能不适用于链。如果我错了,请纠正我。编辑:用完整的解决方案更新了我的问题。
2a) grep 行为(过滤器):
2b) sed 行为(搜索/替换):
1) Not available in boost
Hamigakis's concatenate sounds interesting, but I couldn't figure out how to use it to combine two boost::iostreams::chains. The code mentions it's meant for "concatenation of devices", so it might not be usable for chains. Please correct me if I'm wrong.EDIT: updated my question with the complete solution.
2a) grep behavior (filter):
2b) sed behavior (search/replace):