-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit-cfg.cc
executable file
·125 lines (109 loc) · 3.15 KB
/
edit-cfg.cc
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
// Little cmdline utility to change or delete values from .cfg files.
// Useful for batch editing of set of keyframes.
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(int argc, char* argv[]) {
int arg = 1;
bool doDelete = false;
bool doRename = false;
bool doClean = false;
if (arg < argc && argv[arg][0] == '-') {
if (!strcmp(argv[arg], "-d"))
doDelete = true;
if (!strcmp(argv[arg], "-r"))
doRename = true;
if (!strcmp(argv[arg], "-c"))
doClean = true;
++arg;
}
string kv(arg<argc?argv[arg++]:"");
if (!doClean && kv.empty()) {
fprintf(stderr, "usage: edit-cfg [-drc] \"param value\" *.cfg\n");
return 1;
}
string key(kv);
string value;
size_t space = key.find(' ');
if (space != string::npos) {
value.assign(key.substr(space + 1));
key.erase(space + 1);
}
fprintf(stderr, "key %s, value %s\n", key.c_str(), value.c_str());
// arg points to file(s) to process
bool cleaning = false;
while (arg < argc) {
string filename(argv[arg++]);
printf("processing %s: ", filename.c_str());
vector<string> content;
// read file content
ifstream infile(filename.c_str());
if (!infile.is_open()) {
printf("failed to open for read\n");
continue;
}
string line;
while(infile.good()) {
getline(infile, line);
if (line.find_first_of("\r\n") != string::npos)
line.erase(line.find_first_of("\r\n"));
// Clean svn diff pollution..
if (doClean && line.find("<<<<<") == 0) {
cleaning = true;
continue;
}
if (doClean && line.find("=====") == 0) {
cleaning = false;
continue;
}
if (doClean && line.find(">>>>>") == 0) {
cleaning = false;
continue;
}
if (!cleaning && !line.empty())
content.push_back(line);
}
infile.close();
if (!doClean) {
// change key value
bool found = false;
for (vector<string>::iterator it = content.begin();
it != content.end(); ++it) {
if (doRename && it->find(value) == 0) {
string line(*it);
it->clear();
} else
if (it->find(key) == 0) {
string line(*it);
it->clear();
if (!doDelete && !doRename) it->assign(kv);
if (doRename) {
string newline = line.replace(0, key.size() - 1, value);
it->assign(newline);
}
found = true;
}
}
if (!found && !doDelete && !doRename)
content.insert(content.begin(), kv);
}
// write file
ofstream outfile(filename.c_str());
if (outfile.is_open()) {
for(vector<string>::const_iterator it = content.begin();
it != content.end(); ++it) {
if (!it->empty()) {
outfile.write(it->data(), it->size());
outfile.write("\n", 1);
}
}
outfile.close();
}
printf("done\n");
}
return 0;
}