-
Notifications
You must be signed in to change notification settings - Fork 0
/
symlink.cpp
80 lines (52 loc) · 1.89 KB
/
symlink.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
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
/* SCRIPT PURPOSE
Read whatever file name which is passed to the script
and look for the following:
1. "DOTFILES SYMLINK" which tells that the file in question should
create a symbolic link to the computers home directory
2. Additional params which can be stated inside a " [ <params> ] "
2.1 <path> which is where it should be symlinked to
2.2 "HIDE" if file should be prefixed with a dot to hide it
example: "# DOTFILES SYMLINK [ path/to/file/ HIDE ]"
$ g++ -o symlink symlink.cpp
*/
const int MAX_ARGS = 2;
using namespace std;
int main(int argc, char *argv[]) {
string filename = argv[1];
ifstream file(filename);
while (file && !file.eof()) {
string identifier = "DOTFILES SYMLINK", line;
getline(file, line);
int match = line.find(identifier);
if (match < 0) continue;
int argCount = 0,
argBegin = line.find(identifier.append(" [ ")),
argEnd = line.find(" ]") - identifier.length() -2;
if (argBegin > 0 && argEnd > 0) argBegin += identifier.length();
else break;
string argStr = line.substr(argBegin, argEnd), args[MAX_ARGS];
stringstream ssin(argStr);
bool mergeNext = false;
string tmp;
while (ssin.good() && argCount < MAX_ARGS) {
tmp = "";
ssin >> tmp;
if (tmp == "") continue;
if (mergeNext) {
args[argCount].pop_back();
args[argCount] += " " + tmp;
}
else args[argCount] = tmp;
mergeNext = false;
if (tmp.back() == '\\' ) mergeNext = true;
if (!mergeNext) argCount++;
}
for (int i = 0; i < argCount; i++) cout << args[i] << endl;
break;
}
return 0;
}