-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSed.cpp
More file actions
71 lines (59 loc) · 2.18 KB
/
Copy pathSed.cpp
File metadata and controls
71 lines (59 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Sed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpoveda- <me@izenynn.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/15 12:16:29 by dpoveda- #+# #+# */
/* Updated: 2022/02/15 13:49:52 by dpoveda- ### ########.fr */
/* */
/* ************************************************************************** */
#include "Sed.hpp"
# include <iostream>
# include <fstream>
Sed::Sed() {}
Sed::~Sed() {}
int Sed::replace(std::string const filename, std::string const s1, std::string const s2) {
if (filename.empty() || s1.empty() || s2.empty()) {
std::cerr << "Error: empty argument" << std::endl;
return 1;
}
std::ofstream output;
std::ifstream input;
input.open(filename.c_str(), std::fstream::out);
if (input.is_open() == false) {
std::cerr << "Error: could not open \"" << filename << "\"." << std::endl;
return 1;
}
output.open((filename + ".replace").c_str(), std::fstream::in | std::ifstream::trunc);
if (output.is_open() == false) {
std::cerr << "Error: could not open \"" << filename << "\"." << std::endl;
input.close();
return 1;
}
std::string line;
while (std::getline(input, line, '\n').good()) {
output << Sed::_strReplace(line, s1, s2);
if (input.eof() == false) {
output << '\n';
}
}
input.close();
output.close();
return 0;
}
std::string Sed::_strReplace(std::string line, std::string const s1, std::string const s2) {
std::string out;
size_t index = 0;
size_t end = 0;
while (true) {
end = line.find(s1, index);
if (end == std::string::npos) break;
out.append(line, index, end - index);
index = end + s1.size();
out.append(s2);
}
out.append(line, index);
return out;
}