-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplacer.cpp
109 lines (107 loc) · 3.51 KB
/
Replacer.cpp
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
#include <string>
#include <regex>
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream tempfile;
string argv[6];
try{
tempfile.open("temp");
string tempArg;
string str;
int tempLength;
while(getline(tempfile,tempArg)){
//cout << tempArg<< endl;
if(tempArg=="args:{"){
cout << "FileStart"<<endl;
}else if(tempArg.find("\ttargetPass:")==0){
str="\ttargetPass:";
tempLength=tempArg.length();
argv[1]=tempArg.substr(str.length());
argv[1].erase(0,1);
argv[1].erase(argv[1].length()-1);
cout<< "argv[1]:" << argv[1] << endl;
}else if(tempArg.find("\tregex:")==0){
str="\tregex:";
tempLength=tempArg.length();
argv[2]=tempArg.substr(str.length());
argv[2].erase(0,1);
argv[2].erase(argv[2].length()-1);
cout<< "argv[2]:" << argv[2] << endl;
}else if(tempArg.find("\treplacedText:")==0){
str="\treplacedText:";
tempLength=tempArg.length();
argv[3]=tempArg.substr(str.length());
argv[3].erase(0,1);
argv[3].erase(argv[3].length()-1);
cout<< "argv[3]:" << argv[3] << endl;
}else if(tempArg.find("\tLoopMax:")==0){
str="\tLoopMax:";
argv[4]=tempArg.substr(str.length());
cout<< "argv[4]:" << argv[4] << endl;
}else if(tempArg.find("\tReplaceFile:")==0){
str="\tReplaceFile:";
argv[5]=tempArg.substr(str.length());
cout<< "argv[5]:" << argv[5] << endl;
}else if(tempArg=="}"){
cout << "FileEND" << endl;
break;
}
}
}catch(string e){
cout << "Argv is invalid"<< endl;
return -1;
}
string InputFilename=argv[1];
string isReplacedRegexStr(argv[2]);
regex isReplacedRegex(argv[2]);
string Replacedstring=argv[3];
//cout << InputFilename<<endl<<isReplacedRegexStr<<endl<<Replacedstring<<endl;
int LoopMax;
bool ReplaceFile;
LoopMax=stoi(argv[4]);
string temp=argv[5];
if(temp=="True"){
ReplaceFile=true;
}else{
ReplaceFile=false;
}
string OutputFileName;
int dot_position=InputFilename.find_last_of('.');
if(dot_position==string::npos){
cout << "Filename is invalid";
return -1;
}
if(ReplaceFile==true){
OutputFileName="temp.txt";
}else{
OutputFileName=InputFilename.substr(0,dot_position)+" - Replaced"+InputFilename.substr(dot_position);
}
ifstream InputFile;
ofstream OutputFile;
InputFile.open(InputFilename,ios::in);
OutputFile.open(OutputFileName,ios::out);
string reading_line_buffer;
int cnt=0;
while(getline(InputFile,reading_line_buffer)){
temp=reading_line_buffer;
cnt=0;
while(regex_search(temp,isReplacedRegex)){
string _temp;
_temp=temp;
temp=regex_replace(temp,isReplacedRegex,Replacedstring);
cnt++;
if(_temp==temp || cnt>=LoopMax){
break;
}
}
OutputFile << temp << endl;
}
InputFile.close();
OutputFile.close();
if(ReplaceFile==true){
remove(InputFilename.c_str());
rename(OutputFileName.c_str(),InputFilename.c_str());
}
}