-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_interface.cpp
More file actions
75 lines (70 loc) · 3.23 KB
/
user_interface.cpp
File metadata and controls
75 lines (70 loc) · 3.23 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
#include "user_interface.h"
#include "tree.h"
using namespace std;
FamilyTree::Tree<string, 2> OpenFrom(const string& filename) {
return FamilyTree::Tree<string, 2>::ParseFrom(ReadEverythingFromFile(filename));
}
void RunInteraction(const string& start_filename, istream& command_stream, ostream& output) {
using Tree = FamilyTree::Tree<string, 2>;
Tree family_tree;
if (!start_filename.empty()) {
family_tree = OpenFrom(start_filename);
}
for (string command; getline(command_stream, command); ) {
vector<string> tokens = Split(command);
if (tokens.empty()) {
continue;
}
string command_name = MakeLower(tokens[0]);
vector<string> arguments(make_move_iterator(tokens.begin() + 1),
make_move_iterator(tokens.end()));
if (command_name == "exit") {
break;
} else if (command_name == "add" || command_name == "addnode") {
family_tree.AddNode(Tree::Node(arguments[0], arguments.begin() + 1, arguments.end()));
} else if (command_name == "open") {
family_tree = OpenFrom(arguments[0]);
} else if (command_name == "save") {
ofstream f_output(arguments[0]);
f_output << family_tree;
} else if (command_name == "print") {
output << family_tree;
} else if (command_name == "render") {
auto svg_doc = family_tree.RenderSvg();
if (arguments.empty()) {
svg_doc.Render(output);
} else {
ofstream f_output(arguments[0]);
svg_doc.Render(f_output);
}
} else if (command_name == "lowestcommonancestors" || command_name == "lca") {
auto common_ancestors = family_tree.LowestCommonAncestors(arguments[0], arguments[1]);
if (common_ancestors.empty()) {
output << "No common ancestors";
} else {
PrintSequenceWithDelimiter(output, begin(common_ancestors), end(common_ancestors));
}
output << endl;
} else if (command_name == "merge") {
Tree other_tree = OpenFrom(arguments[0]);
family_tree = Tree::Merge(family_tree, other_tree);
} else if (command_name == "help") {
output << R"(Every command consists of command_name and arguments separated by whitespaces:
command_name argument1 argument2 ...
command_name is case insensitive
Valid commands:
1) Exit
2) Add or AddNode node_name [parent1_name parent2_name]
3) Open family_tree_filename - loads family tree from file family_tree_filename
4) Save family_tree filename - saves family tree to file family_tree_filename
5) Print - prints tree in output stream (console by default)
6) Render render_filename - renders svg document to file render_filename (most browsers support svg document rendering)
7) LowestCommonAncestors or LCA node1_name node2_name - finds lowest common ancestors for given nodes,
"lowest" means that this ancestor doesn't have common ancestors in offspring
8) Merge other_family_tree_filename - merges tree from file other_family_tree_filename to current family tree
9) Help)" << endl;
} else {
output << "Unknown command" << endl;
}
}
}