-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.hpp
More file actions
126 lines (113 loc) · 2.71 KB
/
shell.hpp
File metadata and controls
126 lines (113 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (C) 2013, Aldrin D'Souza.
// License: http://opensource.org/licenses/BSD-2-Clause
#include <set>
#include <regex>
#include <string>
#include <fstream>
#include <iostream>
#include <boost/tokenizer.hpp>
#include <boost/filesystem.hpp>
#include <coro/pipeline.hpp>
typedef basic_pipeline<std::string> shell;
shell::link cat(const std::string& filename)
{
return [filename](shell::in&, shell::out & yield) {
std::string line;
std::ifstream input(filename);
while(std::getline(input, line)) {
yield(line);
}
};
}
shell::link cat()
{
return [](shell::in & source, shell::out & yield) {
std::string line;
for(; source; source()) {
std::ifstream input(source.get());
while(std::getline(input, line)) {
yield(line);
}
}
};
}
shell::link ls(const std::string& dir)
{
namespace fs = boost::filesystem;
return [dir](shell::in & source, shell::out & yield) {
fs::path p(dir);
if(fs::exists(p) && fs::is_directory(p)) {
for(auto f = fs::directory_iterator(p); f != fs::directory_iterator(); ++f) {
if(fs::is_regular_file(f->path())) {
yield(f->path().string());
}
}
}
};
}
shell::link grep(const std::string& pattern)
{
return [pattern](shell::in & source, shell::out & yield) {
const std::regex regex(pattern);
for(; source; source()) {
const std::string& line(source.get());
if(std::regex_search(line, regex)) {
yield(line);
}
}
};
}
shell::link substr(const std::string& in)
{
return [in](shell::in & source, shell::out & yield) {
for(; source; source()) {
const std::string& line(source.get());
if(line.find(in) != std::string::npos) {
yield(line);
}
}
};
}
shell::link uniq()
{
return [](shell::in & source, shell::out & yield) {
std::set<std::string> unique;
for(; source; source()) {
unique.insert(source.get());
}
for(const auto & s : unique) {
yield(s);
}
};
}
shell::link uniq(std::set<std::string>& unique)
{
return [&unique](shell::in & source, shell::out&) {
for(; source; source()) {
unique.insert(source.get());
}
};
}
shell::link cut(const char *delim, int field)
{
return [delim, field](shell::in & source, shell::out & yield) {
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
for(; source; source()) {
int i = 1;
for(auto & t : tokenizer(source.get(), boost::char_separator<char>(delim))) {
if(i++ == field) {
yield(t);
break;
}
}
}
};
}
shell::link echo()
{
return [](shell::in & source, shell::out&) {
for(; source; source()) {
std::cout << source.get() << std::endl;
}
};
}