forked from 0JIEUN0/FileSearcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
207 lines (187 loc) · 6.62 KB
/
Parser.cpp
File metadata and controls
207 lines (187 loc) · 6.62 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "Parser.h"
Parser::Parser(Flags* global_data, int argc, char **argv) {
this->global_settings = global_data;
// No argument specified
if (argc == 1) {
help();
exit(-1);
}
// First see if we parsed at all
for (int i = 1; i < argc; i++) {
string args = string(argv[i]);
int find_dest = args.find("=");
if (find_dest == string::npos) {
if (args == "--help") {
help();
exit(HELP);
} else {
// Invalid Arguments
print_err(ERR_INVALID_ARGUMENT);
help();
exit(ERR_INVALID_ARGUMENT);
}
}
string key = args.substr(0, find_dest);
string value = args.substr(find_dest+1, args.length());
split(key, value);
}
// After parsing once, look for un-reset variable
if (global_data->directory_path == NOT_SPECIFIED) {
// Search Directory to default
global_data->directory_path = filesystem::current_path().string();
print_err(WARN_DEFAULT_SEARCHDIR_DETECTED);
}
if (global_data->file_verbose == -1) {
// File Verbose NOT set
global_data->file_verbose = false;
print_err(WARN_DEFAULT_VERBOSE_DETECTED);
}
if (global_data->save_path == NOT_SPECIFIED) {
global_data->save_path = "false";
print_err(WARN_DEFAULT_SOUT_DETECTED);
}
if (global_data->target_find == NOT_SPECIFIED) {
// This is error
print_err(ERR_SEARCH_NOT_DEFINED);
help();
exit(ERR_SEARCH_NOT_DEFINED);
}
if (global_data->type == NOT_SPECIFIED) {
global_data->type = "file_name";
print_err(WARN_DEFAULT_TYPE_DETECTED);
}
}
void Parser::split(string key, string value) {
bool is_failed = false;
int err_number = 0;
if (key == "--directory") {
filesystem::path path_directory(value);
if (!filesystem::exists(path_directory)) {
// Search Directory is invalid
print_err(ERR_INVALID_SEARCH_DIRECTORY);
is_failed = true; //just let them fail
err_number = ERR_INVALID_SEARCH_DIRECTORY;
} else {
// need to check value first
global_settings->directory_path = value;
}
} else if (key == "--type") {
if (value != "grep" && value != "file_name" && value != "directory_name") {
print_err(ERR_INVALID_ARGUMENT);
is_failed = true;
err_number = ERR_INVALID_ARGUMENT;
} else {
global_settings->type = value;
}
} else if (key == "--to_find") {
string tmp_use = "";
// For changing user-input wildcard to program-acceptable state.
for (int i = 0; i < value.length(); i++) {
if (value.at(i) == '*') {
tmp_use.append(".*");
} else if (value.at(i) == '?') {
tmp_use.append(".");
} else if (value.at(i) == '.') {
tmp_use.append("\\.");
} else if (value.at(i) == '^') {
tmp_use.append("\\^");
} else if (value.at(i) == '$') {
tmp_use.append("\\$");
} else if (value.at(i) == '[') {
tmp_use.append("\\[");
} else if (value.at(i) == ']') {
tmp_use.append("\\]");
} else if (value.at(i) == '-') {
tmp_use.append("\\-");
} else if (value.at(i) == '|') {
tmp_use.append("\\|");
} else if (value.at(i) == '(') {
tmp_use.append("\\(");
} else if (value.at(i) == ')') {
tmp_use.append("\\)");
} else if (value.at(i) == '+') {
tmp_use.append("\\+");
} else if (value.at(i) == '{') {
tmp_use.append("\\{");
} else if (value.at(i) == '}') {
tmp_use.append("\\}");
} else {
tmp_use += value.at(i);
}
}
global_settings->target_find = tmp_use;
} else if (key == "--file_verbose") {
if (value != "true" && value != "false") {
print_err(ERR_INVALID_ARGUMENT);
is_failed = true;
err_number = ERR_INVALID_ARGUMENT;
} else {
global_settings->file_verbose = (value == "true") ? true:false;
}
} else if (key == "--save_output") {
if (value.length() == 0) {
print_err(ERR_INVALID_OUTPUT_DIRECTORY);
is_failed = true; //just let them fail
err_number = ERR_INVALID_OUTPUT_DIRECTORY;
goto exit_final;
}
filesystem::path path_directory(value);
if (!filesystem::exists(path_directory)) {
filesystem::path abs_path = filesystem::absolute(path_directory);
if (!filesystem::exists(abs_path.parent_path())) {
// Search Directory is invalid
print_err(ERR_INVALID_OUTPUT_DIRECTORY);
is_failed = true; //just let them fail
err_number = ERR_INVALID_OUTPUT_DIRECTORY;
} else {
global_settings->save_path = value;
}
} else {
global_settings->save_path = value;
}
} else {
// invalid key
print_err(ERR_INVALID_ARGUMENT);
is_failed = true;
err_number = ERR_INVALID_ARGUMENT;
}
exit_final:
if (is_failed) {
help();
exit(err_number);
} else {
return;
}
}
#define ECODE_ARG_INVALID -1
void Parser::print_err(int err_num){
switch(err_num){
case -1:
cout<<"[ERROR] Target Search string is not specified."<<endl;
break;
case -2:
cout<<"[ERROR] Current target search directory is invalid." <<endl;
break;
case -3:
cout<<"[ERROR] Argument not specified [Invalid arguments]." <<endl;
break;
case -4:
cout<<"[ERROR] Text output directory is invalid." <<endl;
break;
case 1:
cout<<"[WARN] type is not specified. --type is set to file_name."<<endl;
break;
case 2:
cout<<"[WARN] file_verbose is not specified. --file_verbose is set to false."<<endl;
break;
case 3:
cout<<"[WARN] save_output is not specified. --save_output is set to false."<<endl;
break;
case 4:
cout<<"[WARN] --directory is not specified. --directory is set to current directory."<<endl;
break;
default:
cout<<"not defined errr_num"<<endl;
break;
}
}