Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
1416 lines (1408 loc) · 39.4 KB
/
Copy pathmain.cpp
File metadata and controls
1416 lines (1408 loc) · 39.4 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <cstdlib>
#include <ShlObj.h>
#include <fstream>
#include <direct.h>
#include <sstream>
#include <streambuf>
#include <map>
#include <deque>
#include <vector>
#include <time.h>
#include <thread>
#include <conio.h>
#include <mutex>
#include <shellapi.h>
#include <tchar.h>
#include "USN.h"
#include "reg_match.h"
#include "file.h"
#include "screen_settings.h"
using namespace std;
#define ull unsigned long long
#define MAXN 10000000
namespace color
{
int background = 0;
int foreground_default = 0xF,
foreground_chosen = 0x02,
foreground_folder = 0xB,
foreground_buffer = 0xB;
} // namespace color
int disk_num = 0, page = 1;
USN last_USN[255]; //读取到的上一次运行时各个硬盘的最后一个USN编号
USN USN_ID[255]; //每一个硬盘中的USN日志编号
bool flag_rescanned[30]; //标记该磁盘是否被重新扫描过
vector<int> rm_pos[30]; //被删除的文件在文件中的位置
vector<dat> data[30], tmp_dat, crt, rnm; //保存的文件所有信息,创建的文件信息和重命名的信息
vector<string> new_name; //保存一个被重命名文件的最终名字
vector<int> new_prnum; //保存被重命名后新的ParentReferenceNumber,煞笔windows的剪切和删除操作本质都是重命名
vector<File> result;
vector<string> egg;
bool removed[MAXN], exist[MAXN]; //标记是否被删除,是否已经存在
int renamed[MAXN]; //被重命名的文件的USN编号->new_name中的编号
char con_buffer[10000]; //控制台缓冲区
bool is_chinese[10000];
int bufferlen = -1, now_result = 0;
int rpp = 5; //result per page
int sim_tot = 30; //模糊搜索的总结果数
double threshold = 0.8; //模糊匹配阈值
project match;
struct Page
{
int l, r;
Page()
{
l = 0, r = 0;
}
Page(int l, int r) : l(l), r(r) {}
};
vector<Page> pa;
void mainwindow();
void save_settings();
void read_egg()
{
srand(time(0));
string tmp;
ifstream fin("egg.db");
while (getline(fin, tmp))
{
egg.emplace_back(tmp);
}
fin.close();
return;
}
void show_egg()
{
if (egg.size() == 0)
return;
if (rand() <= 5000)
{
SetTitle(egg[rand() % egg.size()].c_str());
}
return;
}
void exit()
{
printf("Press any key to exit");
getche();
exit(0);
}
void admincheck() //检查是否以管理员权限运行
{
bool flag = IsUserAnAdmin();
if (!flag)
{
SetColor(0x04, color::background);
printf("Fatal Error : Please run as Administrator.\n");
SetColor(color::foreground_default, color::background);
exit();
}
}
bool check_local()
{
//检查本地文件是否存在,此处为简便只检查setting和info
fstream _file;
_file.open("database\\settings.db", ios::in);
if (!_file)
return 1;
_file.close();
_file.open("database\\info.db", ios::in);
if (!_file)
return 1;
_file.close();
return 0;
}
int check_disk()
{ //检查本地磁盘数量
//可能存在的潜在bug:ntfs格式磁盘不连续时会跳过末尾的磁盘
int tot = 0;
for (char dst[] = "C";; dst[0]++)
{
if (is_ntfs(dst))
tot++;
else
break;
}
return tot;
}
USN write_MFT(const char *disk_path, const char *data_dst, vector<dat> &v)
{
//输出指定磁盘的所有信息并返回该磁盘USN的ID
USN now_USN;
now_USN = GET_MFT(data_dst, disk_path, 0, v);
return now_USN;
}
void read_info()
{
ifstream fin("database\\info.db");
fin >> disk_num;
for (int i = 0; i < disk_num; i++)
fin >> USN_ID[i];
for (int i = 0; i < disk_num; i++)
fin >> last_USN[i];
fin.close();
}
bool check_info()
{
//检查信息是否合理
bool flag = 0;
char disk_path[] = "C", data_dst[] = "database\\c.db";
for (int i = 0; i < disk_num; i++, disk_path[0]++, data_dst[9]++)
{
USN now_USN = GET_MFT(data_dst, disk_path, 1, tmp_dat);
if (now_USN != USN_ID[i])
{
//若现有的USN的ID与上一次运行时的ID不符,说明USN日志遭到了改动
USN_ID[i] = now_USN;
flag_rescanned[i] = 1;
SetColor(0x04, color::background);
printf("Fatal Error: Change in USN Journal of %s detected. Please wait the program to re-scan the disk. This may take several minutes.\n", disk_path);
SetColor(color::foreground_default, color::background);
printf("Rescanning %s: please wait.\n", disk_path);
write_MFT(disk_path, data_dst, data[i]);
register int *tmp;
tmp = new int[MAXN];
for (int j = 0; j < data[i].size(); j++)
tmp[data[i][j].rnum] = j;
for (int j = 0; j < data[i].size(); j++)
data[i][j].ppos = tmp[data[i][j].prnum];
SetColor(color::foreground_chosen, color::background);
printf("Successfully rescanned %s.\n", disk_path);
SetColor(color::foreground_default, color::background);
}
}
}
bool update_info()
{
ofstream fout("database\\info.db");
fout << disk_num << endl;
for (int i = 0; i < disk_num; i++)
fout << USN_ID[i] << endl;
char disk_path[] = "C";
for (int i = 0; i < disk_num; i++)
fout << last_USN[i] << endl;
fout.close();
}
void first_run()
{
mkdir("database"); //创建文件夹
/****************Debug************/
ofstream fout("database\\settings.db");
fout.close();
char disk_path[] = "C", data_dst[] = "database\\c.db";
disk_num = check_disk();
for (int i = 0; i < disk_num; i++, disk_path[0]++, data_dst[9]++)
{
flag_rescanned[i] = 1;
USN_ID[i] = write_MFT(disk_path, data_dst, data[i]);
last_USN[i] = GET_USN(disk_path, 0, 0, removed, tmp_dat, new_name, new_prnum, renamed);
register int *tmp;
tmp = new int[MAXN];
for (int j = 0; j < data[i].size(); j++)
tmp[data[i][j].rnum] = j;
for (int j = 0; j < data[i].size(); j++)
data[i][j].ppos = tmp[data[i][j].prnum];
SetColor(color::foreground_chosen, color::background);
printf("Successfully scanned %s:\n", disk_path);
SetColor(color::foreground_default, color::background);
delete[] tmp;
}
}
void update_database()
{
char disk_path[] = "C";
char data_dst[] = "database\\c.db";
for (int x = 0; x < disk_num; x++, disk_path[0]++, data_dst[9]++)
{
printf("Now updating disk %s, please wait.\n", disk_path);
if (flag_rescanned[x])
continue;
crt.clear();
last_USN[x] = GET_USN(disk_path, last_USN[x], 1, removed, crt, new_name, new_prnum, renamed);
register dat buffer;
ifstream fin(data_dst, std::ios::binary);
while (1)
{
fin.read((char *)&buffer.rnum, sizeof(int));
if (buffer.rnum == -1)
break;
fin.read((char *)&buffer.prnum, sizeof(int));
fin.read((char *)&buffer.len, sizeof(int));
buffer.filename = new char[buffer.len];
fin.read(buffer.filename, sizeof(char) * buffer.len);
//cout<<buffer.filename<<endl;
if (!removed[buffer.rnum])
{
data[x].push_back(buffer);
exist[buffer.rnum] = 1;
}
}
fin.close();
cout << "read database: " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
for (int i = 0; i < crt.size(); i++)
{
if (!removed[crt[i].rnum] && !exist[crt[i].rnum])
{
data[x].push_back(crt[i]);
exist[crt[i].rnum] = 1;
}
}
register int *tmp;
tmp = new int[MAXN];
memset(tmp, 0, sizeof(tmp));
cout << "read usn : " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
ofstream fout(data_dst, std::ios::binary);
for (register int i = 0; i < data[x].size(); i++)
{
if (renamed[data[x][i].rnum])
{
register int nlen = new_name[renamed[data[x][i].rnum] - 1].length();
delete[] data[x][i].filename;
data[x][i].filename = new char[nlen + 1];
data[x][i].len = nlen + 1;
strcpy(data[x][i].filename, new_name[renamed[data[x][i].rnum] - 1].c_str());
*(data[x][i].filename + nlen) = '\0';
data[x][i].prnum = new_prnum[renamed[data[x][i].rnum] - 1];
}
fout.write((char *)&data[x][i].rnum, sizeof(int));
fout.write((char *)&data[x][i].prnum, sizeof(int));
fout.write((char *)&data[x][i].len, sizeof(int));
fout.write(data[x][i].filename, sizeof(char) * data[x][i].len);
tmp[data[x][i].rnum] = i;
}
int end = -1;
fout.write((char *)&end, sizeof(int));
fout.close();
cout << "write database : " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
/***建立文件索引路径***/
for (int i = 0; i < data[x].size(); i++)
{
data[x][i].ppos = tmp[data[x][i].prnum];
}
delete[] tmp;
/***初始化***/
crt.clear();
new_name.clear();
new_prnum.clear();
memset(removed, 0, sizeof(removed));
memset(renamed, 0, sizeof(renamed));
memset(exist, 0, sizeof(exist));
SetColor(color::foreground_chosen, color::background);
printf("Successfully updated disk %s.\n", disk_path);
SetColor(color::foreground_default, color::background);
}
}
string get_path(int x, int pos)
{
string path;
path = data[x][pos].filename;
register int fa = data[x][pos].ppos;
while (1)
{
if (fa == 0)
break;
//cout<<data[x][fa].filename<<' '<<data[x][fa].rnum<<' '<<data[x][fa].prnum<<endl;
path = "\\" + path;
path = data[x][fa].filename + path;
fa = data[x][fa].ppos;
}
return path;
}
void print_res1(const File &tmp, bool selected = 0)
{
if (selected == 1)
{
SetColor(color::foreground_chosen, color::background);
}
else if (tmp.filesize == -1)
SetColor(color::foreground_folder, color::background);
else
SetColor(color::foreground_default, color::background);
cout << "NAME: " << tmp.filepath << endl;
}
void print_res(const File &tmp, bool selected = 0)
{
if (selected == 1)
{
SetColor(color::foreground_chosen, color::background);
}
else if (tmp.filesize == -1)
SetColor(color::foreground_folder, color::background);
else
SetColor(color::foreground_default, color::background);
cout << "NAME: " << tmp.filepath;
if (tmp.filesize != -1)
cout << "\nSIZE: " << tmp.filesize << "KB\n";
else
cout << '\n';
cout << "Creat Time Change Time Access Time\n";
print_time(tmp.CreatT);
cout << " ";
print_time(tmp.WriteT);
cout << " ";
print_time(tmp.AccessT);
cout << "\n\n";
SetColor(color::foreground_default, color::background);
}
int restart_searching = 0; //是否有字符的变化
recursive_mutex mu; //线程互斥锁
bool finished = 0;
bool port_searching = 0, unport_searching = 0;
int cnt = 0, on_screen = 0, new_result = 0; //总结果数,屏幕上的结果数
int page_tot = 0, page_now = 1; //总页数,当前页
void sear();
thread task01(sear);
void init_files()
{
admincheck();
bool is_first_run = check_local(); //若本地文件不存在,则判定为首次运行
/*初始化信息*/
//is_first_run=1;
//cin>>is_first_run;
if (is_first_run)
{
printf("This is your first run, program needs some time to initialize.\n");
printf("Scanning, please wait several minutes.\n");
SetColor(0x04, color::background);
printf("Please close all anti-virus program running background\n");
SetColor(color::foreground_default, color::background);
}
else
{ //判定是否出现不合法信息,若出现则当作第一次运行处理
read_info(); //读入USN64位ID和上次的最后一个USN
if (disk_num != check_disk())
{ //磁盘数量改变属于严重错误,应重新初始化
SetColor(0x04, color::background);
cout << "Fatal Error: Change in disk number detected.\n";
cout << "The program will rescan all your disks, this will take several minutes.\n";
SetColor(color::foreground_default, color::background);
is_first_run = 1;
}
else
{
check_info(); //检查每块磁盘的USN64位ID是否改变,若改变则更新一下
}
}
if (is_first_run)
first_run();
else
update_database(); //先分析USN记录,记录要删除的文件再在读入时标记
update_info();
cout << "Totle Time : " << (double)clock() / CLOCKS_PER_SEC << "s" << endl;
//Sleep(1000);
cout << "Press any key to continue\n";
getche();
system("cls");
register char c;
cout << "Input any word to start searching\n";
/********************************************************/
//十分重要!首先要把搜索线程分离出去,不然会卡死!!!!!!!!
task01.detach(); //分离线程
}
/*******************************/
//mainwindow.cpp
/*******************************/
const int INFO_MAX_SIZE = 256;
const int FILTERS_MAX_SIZE = 256;
class info
{
public:
void print();
void add_string(string adding_str);
void clear() { size = cur_pt = 0; }
void setPos(int Posx, int Posy)
{
posx = Posx;
posy = Posy;
}
int get_choice()
{
if (cur_pt <= 0)
cur_pt = 1;
if (cur_pt >= size)
cur_pt = size;
return cur_pt;
}
void operator+=(int i) { cur_pt += i; }
void operator-=(int i) { cur_pt -= i; }
int run();
info() { size = cur_pt = posx = posy = 0; }
private:
int size, cur_pt, posx, posy;
string info_str[INFO_MAX_SIZE];
} window_main, window_filters, window_sorting,
window_help, window_other_settings;
void info::print()
{
if (size == 0)
{
puts("Error in info::print()");
return;
}
if (cur_pt <= 0)
cur_pt = 1;
if (cur_pt >= size)
cur_pt = size;
for (int i = 1; i <= size; i++)
{
SetPos(posx, posy + i - 1);
if (cur_pt == i)
{
SetColor(color::foreground_buffer, color::background);
putchar('>');
cout << info_str[i];
putchar('<');
SetColor(color::foreground_default, color::background);
}
else
cout << info_str[i];
putchar('\n');
}
}
void info::add_string(string adding_str)
{
info_str[++size] = adding_str;
}
int info::run()
{
char ch;
while (1)
{
system("cls");
print();
ch = getch();
if (ch == '\n' || ch == '\r')
return get_choice();
if (ch == 80 || ch == 77)
(*this) += 1;
if (ch == 72 || ch == 75)
(*this) -= 1;
}
}
struct filter
{
/*
filter_type = 1. 忽略文件夹
filter_type = 2. 忽略文件
filter_type = 3. 屏蔽带有filter_str后缀的文件
filter_type = 4. 屏蔽filter_str文件目录下的所有文件
filter_type = 5. 只搜索filter_str文件目录下的文件
*/
int filter_type;
string filter_str;
filter()
{
filter_type = 0;
filter_str.clear();
}
};
class filters_package
{
public:
filter filters[FILTERS_MAX_SIZE];
int size;
void addfilter(int filter_type, string filter_str)
{
if (filter_type == 5)
{
for (int i = 1; i <= size; i++)
if (filters[i].filter_type == 5)
{
filters[i].filter_str = filter_str;
return;
}
}
filters[++size].filter_type = filter_type;
filters[size].filter_str = filter_str;
}
void addfilter(int filter_type)
{
for (int i = 1; i <= size; i++)
if (filters[i].filter_type == filter_type)
return;
filters[++size].filter_type = filter_type;
filters[size].filter_str.clear();
}
void print()
{
puts("Current Filters:");
if (size == 0)
puts("None!");
for (int i = 1; i <= size; i++)
switch (filters[i].filter_type)
{
case 1:
puts("Ignore folders.\n");
break;
case 2:
puts("Ignore files.\n");
break;
case 3:
cout << "Ignore suffix: " << filters[i].filter_str << "\n\n";
break;
case 4:
cout << "Ignore path: " << filters[i].filter_str << "\n\n";
break;
case 5:
cout << "Searching path: " << filters[i].filter_str << "\n\n";
break;
}
}
void file_print(ofstream &fout) {
fout<<endl<<size<<endl;
for(int i=1;i<=size;i++) {
fout<<filters[i].filter_type<<' ';
if(filters[i].filter_type == 3 || filters[i].filter_type == 4 || filters[i].filter_type == 5)
fout<<filters[i].filter_str;
fout<<endl;
}
}
void file_read(ifstream &fin) {
int i, filter_type; fin>>i;
string filter_str;
while(i--) {
fin>>filter_type;
if(filter_type == 3 || filter_type == 4 || filter_type == 5) {
fin>>filter_str;
addfilter(filter_type, filter_str);
}
else
addfilter(filter_type);
}
}
bool match(File matching_file);
void clear() { size = 0; }
filters_package() { size = 0; }
} filters;
int get_public_suffix_length(string x, string y)
{
int lenx = x.length(), leny = y.length();
for (int i = 1, maxi = min(lenx, leny); i <= maxi; i++)
if (x[lenx - i] != y[leny - i])
return i - 1;
return min(lenx, leny);
}
int get_public_suffix_length(char *x, string y)
{
int lenx = strlen(x), leny = y.length();
for (int i = 1, maxi = min(lenx, leny); i <= maxi; i++)
if (x[lenx - i] != y[leny - i])
return i - 1;
return min(lenx, leny);
}
int get_public_prefix_length(string x, string y)
{
int lenx = x.length(), leny = y.length();
for (int i = 1, maxi = min(lenx, leny); i <= maxi; i++)
if (x[i - 1] != y[i - 1])
return i - 1;
return min(lenx, leny);
}
bool filters_package::match(File matching_file)
{
// cout<<matching_file.filepath<<' '<<matching_file.filename<<' '<<matching_file.filesize<<endl;
for (int i = 1; i <= size; i++)
{
if (filters[i].filter_type == 1)
{
if (matching_file.filesize == -1ll)
return false;
}
else if (filters[i].filter_type == 2)
{
if (matching_file.filesize != -1ll)
return false;
}
else if (filters[i].filter_type == 3)
{
// cout<<"\n\n\n"<<get_public_suffix_length(matching_file.filename,filters[i].filter_str)<<' '<<filters[i].filter_str<<endl;
if (get_public_suffix_length(matching_file.filename, filters[i].filter_str) == filters[i].filter_str.length())
return false;
}
else if (filters[i].filter_type == 4)
{
// cout<<"\n\n\n"<<get_public_prefix_length(matching_file.filepath,filters[i].filter_str)<<' '<<filters[i].filter_str<<endl;
if (get_public_prefix_length(matching_file.filepath, filters[i].filter_str) == filters[i].filter_str.length())
return false;
}
else
{
if (get_public_prefix_length(matching_file.filepath, filters[i].filter_str) != filters[i].filter_str.length())
return false;
}
}
return true;
}
bool return_to_main = 0, switch_filters = 0, //是否在搜索过程中应用筛选条件
search_finished = 0; //后台搜索是否完成
int sorting_mode = 1; //0-不排序(default) 1&-1:大小 2&-2:创建时间 3&-3:修改时间 4&-4:访问时间
void sear()
{
FINISH:
search_finished = 1;
// cout<<"FINISHED"<<endl;
while (!restart_searching); //开始时等待信号
// cout<<"START"<<endl;
RESTART:
search_finished = 0;
return_to_main = 0;
// cout<<"RESTART "<<con_buffer<<endl;
// cout<<bufferlen<<' '<<(int)con_buffer[bufferlen+1]<<' '<<strlen(con_buffer)<<endl;
/**********初始化************/
restart_searching = 0;
result.clear(); //清空一下现有的结果
pa.clear(); //清空现有的页码记录
pa.emplace_back(0, 0); //从1开始
cnt = 0, on_screen = 0;
page_now = 1, page_tot = 0;
new_result = 0;
/***************************/
if (!match.read_reg(con_buffer, 0))
goto FINISH;
string disk = "C:\\", filepath;
for (int x = 0; x < disk_num; x++, disk[0]++)
{
for (int i = 0; i < data[x].size(); i++)
{
if (restart_searching)
{ //每次判断是否有字符的增加或删除,若有,则重新搜索
goto RESTART;
}
if (return_to_main)
{
return_to_main = 0; //返回主界面
goto FINISH;
}
if (new_result == rpp)
{ //第一次输出特殊判断一下
new_result = 0;
page_tot++;
pa.emplace_back(cnt - rpp, cnt);
}
if (match.match(data[x][i].filename))
{
File tmp(data[x][i], x, disk + get_path(x, i));
if (switch_filters && !filters.match(tmp))
continue;
cnt++;
result.push_back(tmp);
if (on_screen < rpp)
print_res(tmp);
on_screen++;
new_result++;
}
}
}
if (cnt != pa[page_tot].r)
{ //最后一页可能有不完整的结果,特殊判断
page_tot++;
pa.emplace_back(pa[page_tot - 1].r, cnt);
}
goto FINISH; //万一搜索完了就返回第一行,不能退出!
}
/***************模糊搜索*************/
struct link
{
dat info;
int num, disk;
double similarity;
link *l, *r;
link()
{
num = 0;
disk = 0;
similarity = 0;
l = NULL;
r = NULL;
}
};
int nod_cnt = 0; //总的节点数
link *link_head = NULL, *link_tail = NULL; //链表的头尾
void init_sim_sort()
{ //清空链表
link *now = link_head;
while (now != NULL)
{
link *nex = now->r;
delete now;
now = nex;
}
}
link *add_nod(link *pos, dat x, double similarity, int num, int disk)
{ //在pos之后添加一个
link *tmp = new link;
tmp->info = x;
tmp->similarity = similarity;
tmp->num = num;
tmp->disk = disk;
if (pos->r != NULL)
{
tmp->r = pos->r;
(pos->r)->l = tmp;
}
pos->r = tmp;
tmp->l = pos;
return tmp;
}
void del_nod(link *pos)
{ //删除pos
if (pos->l != NULL)
(pos->l)->r = pos->r;
if (pos->r != NULL)
(pos->r)->l = pos->l;
delete pos;
}
int Stop_sim_sort = 0;
void sim_sort()
{
nod_cnt = 0;
link_head = new link; //新建一个空的头
link_head->similarity = 2;
link_tail = new link; //新建一个空的尾
link_tail->r = NULL;
link_tail->l = link_head;
link_head->r = link_tail;
link_head->l = NULL;
string disk = "C:\\";
for (int x = 0; x < disk_num; x++)
{
for (int i = 0; i < data[x].size(); i++)
{
//cout<<i<<endl;
if (Stop_sim_sort)
{ //停止了
init_sim_sort();
return;
}
//cout<<i<<endl;
//cout<<"********"<<endl;
//cout<<data[i].filename<<endl;
double similarity = data[x][i].Cosine_Similarity(con_buffer);
if (similarity < threshold)
continue; //匹配度低于阈值,跳过
//cout<<"fuck "<<data[x][i].filename<<' '<<similarity<<endl;
if (nod_cnt < sim_tot || link_tail->similarity < similarity || (link_tail->similarity == similarity && link_tail->info.len > data[x][i].len))
{
if (nod_cnt == 0)
{ //第一个要特判
link *tmp = add_nod(link_head, data[x][i], similarity, i, x);
delete link_tail;
link_tail = tmp;
link_tail->r = NULL;
nod_cnt++;
}
else
{
if (nod_cnt == sim_tot)
{
//cout<<"delete!!!! "<<link_tail<<endl;
link *tmp = link_tail->l; //删除tail
del_nod(link_tail);
link_tail = tmp;
//cout<<link_tail<<endl;
nod_cnt--;
}
link *now = link_tail;
while (now->similarity < similarity || (now->similarity == similarity && now->info.len > data[x][i].len))
{
now = now->l;
}
bool flag = 0;
if (now->r == NULL)
flag = 1;
//cout<<now<<' '<<now->r<<endl;
link *tmp = add_nod(now, data[x][i], similarity, i, x);
if (flag)
{
link_tail = tmp;
}
nod_cnt++;
}
link *now = link_head->r;
system("cls");
SetColor(color::foreground_chosen, color::background);
cout << con_buffer << endl;
SetColor(color::foreground_default, color::background);
//cout<<link_head<<endl;
while (true)
{
string tmp1 = disk;
tmp1[0] += now->disk;
File tmp(now->info, now->disk, tmp1 + get_path(now->disk, now->num));
//cout<<now->similarity<<endl;
print_res1(tmp);
//cout<<now->info.filename<<' '<<now->similarity<<' '<<now<<' '<<now->l<<' '<<now->r<<endl;
if (now == link_tail)
break;
now = now->r;
}
//cout<<"fuck"<<endl;
//getchar();
}
}
}
}
void start_searching()
{
bool F5_flag = 0, F3_flag = 0;
port_searching = 0;
unport_searching = 1;
char c;
page = 1;
system("cls");
SetColor(color::foreground_buffer, color::background);
cout << "Input any word to start searching\n";
SetColor(color::foreground_default, color::background);
while (1)
{
c = getch();
show_egg();
bool is_changed = 0;
if (c == 0)
{ //按Fn键组切换排序方式与筛选器开关
int fn_valume = getch() - 58;
// cout<<"DDDD "<<fn_valume<<endl;
switch (fn_valume)
{
case 3:
{
system("cls");
SetColor(color::foreground_chosen, color::background);
cout << con_buffer << endl;
SetColor(color::foreground_default, color::background);
return_to_main = 1; //停止搜索进程
Stop_sim_sort = 0;
thread task02(sim_sort); //新建搜索线程
task02.detach(); //分离出线程
char cc;
int is_print_info=0;
while (1)
{
cc=getche();
if (cc == 27)
{
Stop_sim_sort = 1;
goto QUIT_SORTING;
}
else if(!is_print_info){
is_print_info=1;
cout<<"Program is still running backwards, please wait for the program to get results.\n";
cout<<"If you want to quit sorting, please press ESC.\n";
}
}
break;
}
case 2:
sorting_mode = window_sorting.run()-1;
break;
case 10:
if (sorting_mode != 0)
{ //满足条件时对结果进行排序
fileh_switch_debug = 1;
if (!search_finished)
{
cout << "The program is still searching results, please wait.\n";
cout << "If you want to quit sorting, please press ESC button\n";
char cc;
while (1)
{
if (search_finished)
break;
if (!kbhit())
continue;
cc = getche();
if (cc == 0 || cc == -32)
cc = getche(); //艹可能会有输入奇妙的符号的操作
if (cc == 27)
{
goto QUIT_SORTING;
}
}
}
system("cls");
puts("Sorting begin!");
switch (sorting_mode)
{
case 1:
merge_sort(result, Name_cmp_s);
case 2:
merge_sort(result, size_cmp_s);
break;
case 3:
merge_sort(result, size_cmp_l);
break;
case 4:
merge_sort(result, CreatT_cmp_s);
break;
case 5:
merge_sort(result, CreatT_cmp_l);
break;
case 6:
merge_sort(result, WriteT_cmp_s);
break;
case 7:
merge_sort(result, WriteT_cmp_l);
break;
case 8:
merge_sort(result, AccessT_cmp_s);
break;
case 9:
merge_sort(result, AccessT_cmp_l);
break;
}
puts("Sorting completed!");
fileh_switch_debug = 0;
}
else if (!search_finished)
puts("Search hasn't finished. Please wait...");
break;
case 5:
if (!F5_flag)
{ //上次如果按了F5就不要再输出了
F5_flag = 1;
cout << "Sorting mode = " << sorting_mode << " ;" << (switch_filters ? "Filters activated" : "Filters disabled") << endl;
cout << "Press F2 to select sorting mode.\n";
cout << "If you want to set filters, please press ESC to return to main menu.\n";
cout << "For more information, press F1 to open manual.\n";
}
continue;
case 1: //help
ShellExecute(NULL, _T("open"), _T("help.txt"), NULL, NULL, SW_SHOW);
break;
}
F5_flag = 0;
now_result = 0;
if (fn_valume == 10)
page_now = 1;
system("cls");
SetColor(color::foreground_chosen, color::background);
cout << con_buffer << endl;
SetColor(color::foreground_default, color::background);
cout << "PAGE: " << page_now << endl;
if (pa.size() != 1)
for (int i = pa[page_now].l; i < pa[page_now].r; i++)
{
print_res(result[i]);
}
SetColor(color::foreground_default, color::background);
if (search_finished)
cout << "#Search completed: Total " << result.size() << " results!\n\n";
continue;
}