-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaggerCore.cpp
More file actions
319 lines (292 loc) · 6.16 KB
/
TaggerCore.cpp
File metadata and controls
319 lines (292 loc) · 6.16 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "TaggerCore.h"
#include <string>
#include <string.h>
#include <fstream>
#include <algorithm>
#include "nlohmann/json.hpp"
const wchar_t AdsTaggerIni[] = _T("\\adstagger.ini");
const wchar_t RecentTags[] = _T("\\recent_tags.ini");
bool isTag(char c)
{
return c >= '0'&&c <= '9' || c >= 'A'&&c <= 'Z' || c >= 'a'&&c <= 'z';
}
bool isVal(char c)
{
return c != ' ' && c != '\t' && c != '\0';
}
bool isDef(char c)
{
return c == '=' || c == ':';
}
std::wstring GetTagsPath(LPCWSTR fpath)
{
std::wstring adspath;
if (_tcsstr(fpath, _T("\\\\?\\")))
return fpath;
if (!_tcsncmp(fpath, _T("\\\\"), 2)) {
adspath = _T("\\\\?\\UNC\\");
adspath += (fpath + 2);
}
else {
adspath = _T("\\\\?\\");
adspath += fpath;
}
adspath += _T(":Tags");
return adspath;
}
bool GetFTime(LPCWSTR fpath, FILETIME &ftc, FILETIME &tfa, FILETIME &tfw)
{
HANDLE h = CreateFileW(fpath, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
if (h == INVALID_HANDLE_VALUE)
return false;
bool r = GetFileTime(h, &ftc, &tfa, &tfw);
CloseHandle(h);
return r;
}
bool SetFTime(LPCWSTR fpath, FILETIME &ftc, FILETIME &tfa, FILETIME &tfw)
{
HANDLE h = CreateFileW(fpath, GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h == INVALID_HANDLE_VALUE)
return false;
bool r = SetFileTime(h, &ftc, &tfa, &tfw);
CloseHandle(h);
return r;
}
TaggerCore Core;
TagIter TaggerCore::parseTags(const char* tags, std::list<Tag*> *pFileTags)
{
enum EState {
S_SP1,
S_TAG,
S_SP2,
S_SP3,
S_VAL
} s = S_SP1;
const char* start = tags;
TagIter it = m_Tags.end(), first = m_Tags.end();
bool found;
while (1) {
switch (s) {
case S_SP1: // waiting for the start of the tag
if (isTag(*tags)) {
s = S_TAG;
start = tags;
}
break;
case S_TAG: // waiting for the end of the tag
if (!isTag(*tags)) {
std::string tag(start, tags - start);
if(addTag(tag.c_str(), 1, &it))
if (first == m_Tags.end())
first = it;
if (pFileTags)
pFileTags->push_back(&(*it));
if (isDef(*tags)) // if there is a ':' or '=', then we switch to waiting for the beginning of the value
s = S_SP3;
else
s = S_SP2;
}
break;
case S_SP2: // waiting for the ':' or '=' sign or the beginning of the next tag
if (isDef(*tags)) {
s = S_SP3;
}
else if (isTag(*tags)) {
s = S_TAG;
start = tags;
}
break;
case S_SP3: // waiting for the beginning of the value
if (isVal(*tags)) {
s = S_VAL;
start = tags;
}
break;
case S_VAL: // waiting for the end of the value
if (!isVal(*tags)) {
std::string val(start, tags - start);
it->val = val;
s = S_SP1;
}
break;
}
// exit from the middle, we need at least one iteration, for a null character it is valid
if (!*tags)
break;
tags++;
}
return first;
}
void TaggerCore::loadFileTags(FileTags &f)
{
// load file tags from ADS
std::wstring tpath = GetTagsPath(f.m_fpath.c_str());
std::ifstream t;
t.open(tpath);
if (t) {
std::string line;
std::getline(t, line);
parseTags(line.c_str(), &f.m_tags);
}
t.close();
}
void TaggerCore::saveFileTags(FileTags &f)
{
// save file tags to ADS
std::wstring tpath = GetTagsPath(f.m_fpath.c_str());
std::ofstream t;
t.open(tpath, std::ofstream::out | std::ofstream::trunc);
if (t) {
std::string line;
makeTags(f.m_tags, line);
t << line;
}
t.close();
SetFTime(f.m_fpath.c_str(), f.ftc, f.tfa, f.tfw);
}
void TaggerCore::makeTags(std::list<Tag*> &fileTags, std::string &tags)
{
// make
for (auto &t : fileTags) {
if (t->chk == -1) {
if (!tags.empty())
tags += " ";
tags += t->tag;
if (!t->val.empty()) {
tags += ":";
tags += t->val;
}
}
}
//
for (auto &t : m_Tags) {
if (t.chk == 1) {
if (!tags.empty())
tags += " ";
tags += t.tag;
// val of first file (!!!)
if (!t.val.empty()) {
tags += ":";
tags += t.val;
}
}
}
}
void TaggerCore::parseCommandLine()
{
// parse command line
auto cl = GetCommandLine();
int nArgc = 0;
LPWSTR *pArgv = ::CommandLineToArgvW(cl, &nArgc);
for (int i = 1; i < nArgc; i++) {
if (std::filesystem::exists(pArgv[i])) {
FileTags f;
f.m_fpath = pArgv[i];
GetFTime(pArgv[i], f.ftc, f.tfa, f.tfw);
loadFileTags(f);
m_Files.push_back(f);
}
}
}
void TaggerCore::buildUsedTags()
{
// convert count to check status
for (auto &t : m_Tags) {
if (t.chk == Core.m_Files.size())
t.chk = 1;
else if (t.chk != 0)
t.chk = -1;
}
}
void TaggerCore::init()
{
wchar_t path[MAX_PATH]=_T("");
GetModuleFileName(NULL, path, MAX_PATH);
wchar_t *ls = _tcsrchr(path, '\\');
if (ls)
*ls = 0;
m_AppPath = path;
parseCommandLine();
buildUsedTags();
loadRecentTags();
}
bool TaggerCore::loadIni()
{
std::ifstream file(m_AppPath + AdsTaggerIni);
nlohmann::json j;
try {
j = nlohmann::json::parse(file);
}
catch (...) {
Cfg.x1 = 100;
Cfg.y2 = 100;
Cfg.x2 = 300;
Cfg.y2 = 300;
return false;
}
Cfg.x1 = j["x1"];
Cfg.y1 = j["y1"];
Cfg.x2 = j["x2"];
Cfg.y2 = j["y2"];
return true;
}
void TaggerCore::saveIni()
{
nlohmann::json j;
j["x1"] = Cfg.x1;
j["y1"] = Cfg.y1;
j["x2"] = Cfg.x2;
j["y2"] = Cfg.y2;
std::ofstream file(m_AppPath + AdsTaggerIni);
file << j.dump(2);
}
void TaggerCore::apply()
{
for (auto &f : m_Files)
saveFileTags(f);
saveRecentTags();
}
void TaggerCore::loadRecentTags()
{
// load recent tags from ini
std::ifstream t;
t.open(m_AppPath + RecentTags);
std::string line;
while (t) {
std::getline(t, line);
if(!line.empty())
addTag(line.c_str(), 0, nullptr);
}
t.close();
}
void TaggerCore::saveRecentTags()
{
// save all used tags to ini
std::string input;
std::ofstream out(m_AppPath + RecentTags, std::ofstream::out | std::ofstream::trunc);
for (Tag &t : m_Tags) {
out << t.tag << "\n";
}
out.close();
}
bool TaggerCore::addTag(const char* t, int chk, TagIter *pIt)
{
// find tag in comon list
auto it = std::find_if(m_Tags.begin(), m_Tags.end(), [&t](Tag &a) {
return a.tag == t;
});
// if not found - insert at end
bool added = false;
if (it == m_Tags.end()) {
it = m_Tags.insert(it, Tag{ t, "", chk });
added = true;
}
else if (chk != 0) {
it->chk++;
}
if (pIt)
*pIt = it;
return added;
}