简体   繁体   English

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

[英]how to convert bash script to C++ using boost::iostreams

I'm trying to convert the following bash code into C++ using boost::iostreams: 我正在尝试使用boost :: iostreams将以下bash代码转换为C ++:

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

I can open a file and decompress it: 我可以打开一个文件并解压缩它:

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);

Then open an uncompressed file: 然后打开一个未压缩的文件:

std::ifstream s2("file.ext", std::ios_base::in | std::ios_base::binary);

Now I'm a bit stuck, so here are my questions: 现在我有点卡住了,所以这是我的问题:

1) What's the boost::iostreams solution to concat the two streams? 1)什么是boost :: iostreams解决方案来连接两个流?

2) How to output the result through a regex filter to emulate grep/sed? 2)如何通过正则表达式过滤器输出结果来模拟grep / sed?

As a result I'd like to have a an istream that i can copy to cout: 因此,我想要一个可以复制到cout的istream:

boost::iostream::copy(result, std::cout);

UPDATE complete solution using Hamigaki's concatenate : 使用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;
}

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 1)我不知道是否内置了boost,但这个类似乎正是你想要的: 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. 这里的问题是它希望CopyConstructible设备能够连接,而Chains似乎不是CopyConstructible。 However, we can easily work around that using boost::ref. 但是,我们可以使用boost :: ref轻松解决这个问题。 This code does (almost) what I understood you're asking: 这段代码(几乎)实现了我所理解的:

int main(int argc, char const* argv[])
{
  boost::iostreams::filtering_istreambuf in;
  boost::regex regex("search");
  boost::iostreams::regex_filter rf(regex, "replace");
  in.push(rf);

  boost::iostreams::file_source file1(argv[1]);
  in.push(file1);

  boost::iostreams::file_source file2(argv[2]);
  boost::iostreams::copy(hamigaki::iostreams::concatenate(boost::ref(in), file2), std::cout);

  return 0;
}

I just used the regex filter instead of gzip, for testing. 我只是使用正则表达式过滤器而不是gzip进行测试。

2) boost::iostreams has a regex filter: http://www.boost.org/doc/libs/1_45_0/libs/iostreams/doc/classes/regex_filter.html 2)boost :: iostreams有一个正则表达式过滤器: 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) Not available in boost 1)在提升中不可用

Hamigakis's concatenate sounds interesting, but I couldn't figure out how to use it to combine two boost::iostreams::chain s. Hamigakis的连接听起来很有趣,但我无法弄清楚如何使用它来组合两个boost :: iostreams :: chain 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): 2a)grep行为(过滤器):

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/grep.hpp>

boost::iostreams::filtering_istreambuf in;
boost::regex regex("^search")
boost::iostreams::grep_filter grep(regex);
in.push(grep);

2b) sed behavior (search/replace): 2b)sed行为(搜索/替换):

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/regex.hpp>

struct formatter {
    std::string operator()(const boost::match_results<const char*>& match)
    {
        return str(boost::format("%s | %s") % match[2] % match[1]);
    }
};
boost::iostreams::filtering_istreambuf in;
boost::regex regex("^([a-z]+) ([0-9]+)");
boost::iostreams::regex_filter sed(regex, formatter());
in.push(sed);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM