-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkers.cpp
More file actions
145 lines (119 loc) · 4.01 KB
/
workers.cpp
File metadata and controls
145 lines (119 loc) · 4.01 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// workers.cpp
// Workflow
//
// Created by Кирилл on 20.11.17.
// Copyright © 2017 Кирилл. All rights reserved.
//
#include <algorithm>
#include <fstream>
#include <vector>
#include "workers.h"
namespace workers {
const wkfw::Worker* constructWorker(const size_t ident,
const std::string& name,
const std::vector<std::string>& args) {
if (name == "readfile") {
if (args.size() == 1)
return new ReadFile(ident, args[0]);
} else if (name == "writefile") {
if (args.size() == 1)
return new WriteFile(ident, args[0]);
} else if (name == "grep") {
if (args.size() == 1)
return new Grep(ident, args[0]);
} else if (name == "sort") {
if (args.size() == 0)
return new Sort(ident);
} else if (name == "replace") {
if (args.size() == 2)
return new Replace(ident, args[0], args[1]);
} else if (name == "dump") {
if (args.size() == 1)
return new Dump(ident, args[0]);
}
return nullptr;
}
const wkfw::WorkerResult ReadFile::execute(const wkfw::WorkerResult& previous)
const throw(wkfw::WorkerExecuteException) {
std::ifstream input;
std::vector<std::string> list;
input.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
input.open(filename);
std::string line;
while (!input.eof() && std::getline(input, line))
list.push_back(line);
input.close();
} catch (std::ifstream::failure& e) {
if (!input.eof())
throw wkfw::WorkerExecuteException("Cannot read lines from file \"" +
filename + "\"");
}
return wkfw::WorkerResult(list);
}
const wkfw::WorkerResult WriteFile::execute(const wkfw::WorkerResult& previous)
const throw(wkfw::WorkerExecuteException) {
std::ofstream output;
output.exceptions(std::ofstream::failbit | std::ofstream::badbit);
try {
output.open(filename);
for (auto const& line : previous.getValue())
output << line << std::endl;
output.close();
} catch (std::ofstream::failure& e) {
throw wkfw::WorkerExecuteException("Cannot write lines to file \"" +
filename + "\"");
}
return wkfw::WorkerResult();
}
const wkfw::WorkerResult Grep::execute(const wkfw::WorkerResult& previous) const
throw(wkfw::WorkerExecuteException) {
std::vector<std::string> list;
for (auto const& line : previous.getValue())
if (line.find(pattern) != -1)
list.push_back(line);
return wkfw::WorkerResult(list);
}
const wkfw::WorkerResult Sort::execute(const wkfw::WorkerResult& previous) const
throw(wkfw::WorkerExecuteException) {
std::vector<std::string> list = previous.getValue();
std::sort(list.begin(), list.end());
return wkfw::WorkerResult(list);
}
/**
* Заменяет подстроки в с троке.
*
* @param str Исходная строка
* @param pattern Паттерн для замены
* @param substitution Замена для паттерна
*
* @return Строка с примененными заменами.
*/
static std::string replace(const std::string& str,
const std::string& pattern,
const std::string& substitution) {
std::string result(str);
size_t index = 0;
while (true) {
index = str.find(pattern, index);
if (index == std::string::npos)
break;
result.replace(index, pattern.size(), substitution);
index += substitution.size();
}
return result;
}
const wkfw::WorkerResult Replace::execute(const wkfw::WorkerResult& previous)
const throw(wkfw::WorkerExecuteException) {
std::vector<std::string> list;
for (auto const& line : previous.getValue())
list.push_back(replace(line, pattern, substitution));
return wkfw::WorkerResult(list);
}
const wkfw::WorkerResult Dump::execute(const wkfw::WorkerResult& previous) const
throw(wkfw::WorkerExecuteException) {
WriteFile::execute(previous);
return wkfw::WorkerResult(previous.getValue());
}
} // namespace workers