-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
86 lines (67 loc) · 1.66 KB
/
utils.cpp
File metadata and controls
86 lines (67 loc) · 1.66 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
#include <dirent.h>
#include <fstream>
#include "completer.hpp"
static void label(Node *node, int &id) {
if (!node) return;
cout << "\tNode" << id << "[label=\"";
cout << ((node->chr <= 0) ? '~' : node->chr);
cout << "\"]\n";
}
static void edge(int from, int to) {
cout << "\tNode" << from << " -> "
<< "Node" << to << "[wieght=9]\n";
}
static int dfs(Node *node, int &id) {
int my_id = 0, n;
my_id = id++;
if (node) {
label(node, my_id);
for (auto child : node->children) {
n = dfs(child, id);
edge(my_id, n);
}
}
return my_id;
}
void dump_dot(Node *Node) {
if (!Node) return;
cout << "digraph {\n";
int id = 0;
dfs(Node, id);
cout << "}\n";
}
// this function will all command avialable on your os to completer
void load_commands(Completer &suggestor) {
char *path = strdup(getenv("PATH"));
char *token = strtok(path, ":");
while (token) {
DIR *dir = opendir(token);
if (!dir) {
token = strtok(NULL, ":");
continue;
}
dirent *entry;
while ((entry = readdir(dir))) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue;
suggestor.insert_word(entry->d_name);
}
closedir(dir);
token = strtok(NULL, ":");
};
free(path);
};
#include <sys/stat.h>
void load_words(Completer &completer, const string &filename) {
fstream file(filename, ios::in);
struct stat state;
stat(filename.c_str(), &state);
errno = S_ISDIR(state.st_mode) ? EISDIR : errno;
if (!file.good() || errno != 0) {
perror(filename.c_str());
exit(1);
}
string line;
while (!getline(file, line).eof()) {
completer.insert_word(line.c_str());
}
}