-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
214 lines (181 loc) · 6.62 KB
/
main.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
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
208
209
210
211
212
213
214
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <Windows.h>
struct clade {
std::string name;
int deep;
int age;
};
// ïåðåáèðàåò âåêòîð ñ êëàäàìè
// index - ññûëêà, ÷òîáû áûë ïðàâèëüíûé ïåðåáîð
std::string create_sequence(int& index, int current_deep, std::vector<clade>& list, bool use_tl=false, int prev_age=0) {
if (index + 1 >= list.size()) // åñëè çíà÷åíèå ïîñëåäíåå
return list.at(index).name + ",";
std::string result = "(";
while (index < list.size()) {
int next_index = index + 1;
// åñëè ñëåäóþùèé ñóùåñòâóåò åñëè ñî ñëåäóþùåãî íà÷èíàåòñÿ íîâûé óðîâåíü
if (next_index != list.size() && list.at(next_index).deep > current_deep)
if (use_tl)
result += create_sequence(next_index, list.at(next_index).deep, list, true, list.at(index).age);
else
result += create_sequence(next_index, list.at(next_index).deep, list);
// ñîçäàíèå óçëà ( èìÿ, ), íî ñ use_tl ( èìÿ:ðàçíèöà_âîçðàñòîâ, )
int different = (list.at(index).age < 0) ? 5 : list.at(index).age - prev_age;
std::string age_diff = ":" + std::to_string(different);
result += list.at(index).name + ((use_tl) ? age_diff : "") + ",";
index = next_index;
// åñëè ñëåäóþùèé ñóùåñòâóåò åñëè ñî ñëåäóþùåãî êîí÷àåòñÿ íûíåøíèé óðîâåíü
if (next_index != list.size() && list.at(next_index).deep < current_deep) break;
}
return result + ")";
}
// ïðîâåðêà ÷òî input ýòî îäèí èç flags
bool has_flag(char* input, std::vector<std::string> flags) {
for (const std::string& flag : flags)
if (strcmp(input, flag.c_str()) == 0)
return true;
return false;
}
// ïîìåùåíèå òåêñòà â áóôåð îáìåíà
// âçÿò ñ https://ru.stackoverflow.com/a/1232202
void TextToClipboard(const char* text) {
if (OpenClipboard(0)) {
EmptyClipboard();
char* hBuff = (char*)GlobalAlloc(GMEM_FIXED, strlen(text) + 1);
// strcpy(hBuff, text);
strcpy_s(hBuff, strlen(text) + 1, text);
SetClipboardData(CF_TEXT, hBuff);
CloseClipboard();
}
}
int main(int argc, char* argv[]) {
bool out_only_leafs = false; // âûâîä òîëüêî ëèñòüåâ
bool add_default_length = false; // äîáàâëåíèå äëèíû (èìÿ:1)
bool set_every_length = false; // ïðè îòîáðàæåíèè òîëüêî ëèñòüåâ âñ¸ èìååò äëèíó
bool to_clipboard = false; // ïîìåùåíèå ðåçóëüòàòà ñðàçó â áóôåð îáìåíà
bool use_timeline = false; // èñïîëüçîâàíèå âðåìåíè ïîÿâëåíèÿ äëÿ äëèíû âåòâåé
std::string timeline_file_name;
// áëîê óñòàíîâêè ôëàãîâ
if (argc < 2) {
std::cout << "incorrect enter\n";
return EXIT_FAILURE;
}
else if (has_flag(argv[1], {"-h", "--help"})) {
std::string help_message =
"Usage:"
"\nconvert [-h | --help]"
"\nconvert <tree-file> [flags] [[-t | --timeline] <time-file>]"
"\n"
"\nFlags:"
"\n-l --leafs - views only leaf"
"\n-a --add-length - adds a length value for named values, i.e.: (A:1,B:1),C:1"
"\n-A --all-length - adds a length value for all values if set flag -ad, i.e.: (A:1,B:1):0.5,C:1"
"\n-c --clipboard - paste result in clipboard"
"\n-t --timeline - use timeline as branch length"
"\n"
"\nIf the -t flag is specified, the -a and -A flags are ignored"
;
std::cout << help_message;
return EXIT_SUCCESS;
}
else if (argc > 2) {
// ïðîâåðêà íàëè÷èÿ ôëàãà
for (int i = 2; i < argc; i++) {
if (!out_only_leafs && has_flag(argv[i], { "-l", "--leafs" })) {
out_only_leafs = true;
continue;
}
if (!add_default_length && has_flag(argv[i], { "-a", "--add-length" })) {
add_default_length = true;
continue;
}
if (!set_every_length && has_flag(argv[i], { "-A", "--all-length" })) {
add_default_length = true;
set_every_length = true;
continue;
}
if (!to_clipboard && has_flag(argv[i], { "-c", "--clipboard" })) {
to_clipboard = true;
continue;
}
if (!use_timeline && has_flag(argv[i], { "-t", "--timeline" })) {
if (!(i + 1 < argc)) { // åñëè ôàéë íå óêàçàí
std::cout << "specify the file for the timeline\n";
return EXIT_FAILURE;
}
use_timeline = true;
timeline_file_name = argv[++i];
continue;
}
std::cout << "unknown flag: " << argv[i] << std::endl;
return EXIT_FAILURE;
}
if (use_timeline)
add_default_length = set_every_length = false;
}
std::ifstream source_file, timeline_file;
source_file.open(argv[1]);
timeline_file.open(timeline_file_name);
if (source_file.is_open() && (!use_timeline || timeline_file.is_open())) {
std::vector<clade> clades = {};
std::string rawstr;
std::string timeline;
const std::regex
cut_extra(" +\\(.*\\)"),
counting_tabs("( |\\t)"),
spaces("\\s+"),
commas(",\\)"),
cut_nodes("\\)\\w+"),
add_length("[a-zA-Z](?=,|\\))(?!:)"),
all_length("\\)(?=,|\\))");
std::string clade_age_scheme = " +(.*)\\|";
std::string start_age_str;
int start_age = 0;
if (use_timeline) {
std::getline(timeline_file, start_age_str);
start_age = std::stoi(start_age_str);
std::string clade_in_tl;
while (std::getline(timeline_file, clade_in_tl))
timeline += clade_in_tl + "|";
}
std::smatch match;
// ïðîõîä ïî òåêñòîâîìó ôàéëó
while (std::getline(source_file, rawstr)) {
// óäàëåíèå çíà÷åíèé â ñêîáêàõ
std::string tabs_and_name = std::regex_replace(rawstr, cut_extra, "");
int tabs = 0; // ïîäñ÷¸ò îòñóïîâ
if (std::regex_search(tabs_and_name, match, counting_tabs))
tabs = std::distance(
std::sregex_iterator(tabs_and_name.begin(), tabs_and_name.end(), counting_tabs),
std::sregex_iterator());
// óäàëåíèå îñòàëüíîãî
std::string name = std::regex_replace(tabs_and_name, spaces, "");
int age = -1;
if (use_timeline) {
std::regex get_age(name + clade_age_scheme);
if (std::regex_search(timeline, match, get_age))
age = std::stoi(match[1].str());
}
clades.push_back({ name, tabs, age });
}
int temp = 0;
std::string seque = std::regex_replace(create_sequence(temp, 0, clades, use_timeline, start_age), commas, ")") + ";";
// ïðèìåíåíèå ôëàãîâ
if (out_only_leafs) seque = std::regex_replace(seque, cut_nodes, ")");
if (add_default_length && !use_timeline) {
while (std::regex_search(seque, match, add_length))
seque.insert(match.position() + 1, ":1");
if (set_every_length)
seque = std::regex_replace(seque, all_length, "):0.5");
}
std::cout << seque << std::endl; // âûâîä ðåçóëüòîòà â êîíñîëü
if (to_clipboard) TextToClipboard(seque.c_str()); // êîïèðîâàíèå â áóôåð îáìåíà
}
else std::cout << "file not open\n";
source_file.close();
timeline_file.close();
return EXIT_SUCCESS;
}