-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvfudir.cpp
1322 lines (1190 loc) · 32 KB
/
vfudir.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
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
/****************************************************************************
#
# Copyright (c) 1996-2023 Vladi Belperchinov-Shabanski "Cade"
# https://cade.noxrun.com/ <[email protected]> <[email protected]>
# https://cade.noxrun.com/projects/vfu https://github.com/cade-vs/vfu
#
# SEE `README',`LICENSE' OR `COPYING' FILE FOR LICENSE AND OTHER DETAILS!
#
****************************************************************************/
#include "vfudir.h"
#include "vfuopt.h"
#include "vfuuti.h"
#include "vfusys.h"
#include "vfufiles.h"
#include "vfuview.h"
#include "vfumenu.h"
VArray size_cache;
int size_cache_count;
int size_cache_sorted_by_name;
/*###########################################################################*/
void __glob_gdn( const char* a_path, const char* a_fnpattern, VArray &a_va, int type = 'D' ) // glob getdirname, type = 'F'ile, 'D'ir, 'A'ny
{
VString pat = a_fnpattern;
pat += "*";
ASSERT( type == 'F' || type == 'D' || type == 'A' );
a_va.undef();
DIR *dir;
dirent *de;
if ( !a_path || a_path[0] == 0 )
dir = opendir(".");
else
dir = opendir( a_path );
if (dir)
{
while ( (de = readdir(dir)) )
{
if ( strcmp( de->d_name, "." ) == 0 ||
strcmp( de->d_name, ".." ) == 0 ) continue;
int match_ok = 0;
if( opt.no_case_glob )
match_ok = FNMATCH_NC( pat, de->d_name) == 0;
else
match_ok = FNMATCH( pat, de->d_name) == 0;
if ( a_fnpattern[0] == 0 || match_ok )
{
VString str; // = a_path;
str += de->d_name;
if( file_is_dir( a_path + str ) )
{
str += "/";
if( type != 'F' )
a_va.push(str);
}
else
{
if( type != 'D' )
a_va.push(str);
}
}
}
closedir(dir);
}
}
int vfu_get_dir_name( const char *prompt, VString &target_in, int should_exist, int type )
{
int res = -1;
/*
#ifdef _TARGET_UNIX_
leaveok(stdscr, FALSE);
#endif
*/
VArray dir_list;
say1( prompt );
say2( "" );
int pos = 0;
int page = 0;
int wch = 0;
int insert = 1;
int firsthit = 1;
WString target = target_in;
pos = str_len( target );
//------------------------------------------------------------------
con_cshow();
// say2( target, firsthit ? cINPUT2 : cINPUT );
while(1)
{
int mx = con_max_x() - 1;
WString target_out = target;
if ( (pos < page) || (pos+1 > page + mx) || (page > 0 && pos == page) )
page = pos - mx / 2;
if ( page < 0 ) page = 0;
str_trim_left( target_out, page );
str_sleft( target_out, mx );
str_pad( target_out, -mx );
if ( page > 0 )
str_set_ch( target_out, 0, L'<' );
if ( str_len( target ) - page > mx )
str_set_ch( target_out, mx-1, L'>' );
say2( VString( target_out ).data(), firsthit ? cINPUT2 : cINPUT );
con_xy( pos-page+1, con_max_y() );
if ( wch == 0 ) wch = con_getwch();
if ( wch == L'\\') wch = L'/'; /* dos hack :)) */
if ( wch == L'/' && str_find( target, L'/' ) == -1 && target[0] == L'~' )
{
target.set( tilde_expand( VString( target ) ) );
str_fix_path( target );
pos = str_len( target );
wch = 0;
}
if ( ( wch == 8 || wch == UKEY_BACKSPACE ) && pos > 0 )
{
pos--;
str_del( target, pos, 1 );
}
else
if ( wch == UKEY_CTRL_A && str_len( target ) > 0)
{
int z = str_len( target )-1;
if ( str_get_ch(target, z) == L'/' ) z--;
while ( z > 0 && str_get_ch(target,z) != L'/' ) z--;
z++;
str_sleft(target,z);
pos = z;
}
else
if ( wch == 9 && str_len( target ) > 0 )
{
int z = -1;
dir_list.undef();
VString dmain; /* main/base path */
VString dtail; /* item that should be expanded/glob */
dmain.set( str_file_path( target ) );
dtail.set( str_file_name_ext( target ) );
/*
int lastslash = str_rfind(target, '/');
if ( lastslash == -1 )
{
dmain = "";
dtail = target;
}
else
{
dmain = target;
dtail = target;
str_sleft( dmain, lastslash+1 );
str_trim_left( dtail, lastslash+1 );
}
*/
int dtlen = str_len( dtail );
__glob_gdn( dmain, dtail, dir_list, type );
if (dir_list.count())
{
if ( dir_list.count() > 1)
{
int li = 0; // counter
int ll = 0; // longest directory entry
int xm = 0; // exact match entry
for ( li = 0; li < dir_list.count(); li++ )
{
int len = strlen( dir_list[li] );
if( len > ll )
ll = len;
VString current_dtail;
if( dtail != str_copy( current_dtail, dir_list[li], 0, dtlen ) )
continue;
xm = li;
break;
}
int mc = 0; // match count
int mi = dtlen; // match letter index
while(4)
{
mc = 0;
for ( li = 0; li < dir_list.count(); li++ )
{
char ch1 = str_get_ch( dir_list[xm], mi );
char ch2 = str_get_ch( dir_list[li], mi );
if( opt.no_case_glob )
{
ch1 = toupper( ch1 );
ch2 = toupper( ch2 );
}
if ( ch1 == ch2 )
mc++;
}
if ( mc != dir_list.count() )
break;
mi++;
// if( dir_list[xm][mi] == 0 || dir_list[li][mi] == 0 )
// break;
if( mi >= ll )
break;
}
VString exact_dtail_max;
str_copy( exact_dtail_max, dir_list[xm], 0, mi );
target = dmain + exact_dtail_max;
pos = str_len( target );
say2( VString( target ), cINPUT );
con_xy( pos+1, con_max_y() );
vfu_beep();
wch = con_getwch();
if ( wch != 9 ) { dir_list.undef(); continue; }
dir_list.sort();
WArray w_dir_list;
int di;
for( di = 0; di < dir_list.count(); di++ )
{
WString ws;
ws = dir_list[di];
w_dir_list.push( ws );
}
con_chide();
z = vfu_menu_box( 10, 5, L"Complete...", &w_dir_list );
con_cshow();
wch = 0;
}
else
{
z = 0;
wch = 0;
}
if ( z >= 0 )
{
int sp = str_rfind( target, L'/' );
if( sp < 0 )
target.undef();
else
str_copy( target, target, 0, sp + 1 );
target += dir_list[z];
}
pos = str_len( target );
dir_list.undef();
if ( wch != 0 ) continue;
}
else
{ /* no match found -- cannot complete */
vfu_beep();
}
}
else
if ( wch == 13 )
{
res = 1;
break;
}
else
if ( wch == 27 )
{
target.undef();
res = 0;
break;
}
if ( wch == UKEY_CTRL_U )
{
target.undef();
pos = 0;
}
else
if ( wch == UKEY_CTRL_X )
{
if ( target[0] == L'~' )
target.set( tilde_expand( VString( target ) ) );
target.set( expand_path( VString( target ) ) );
str_fix_path( target );
pos = str_len( target );
}
else
if ( wch >= 32 && wch != 8 && wch != UKEY_BACKSPACE && ( ! UKEY_IS_WIDE_CTRL( wch ) ) )
{
if( firsthit )
{
target.undef();
pos = 0;
}
if ( ! insert ) str_del( target, pos, 1 );
str_ins_ch( target, pos, wch );
pos++;
} else
if( wch == UKEY_LEFT )
{
if (pos > 0)
pos--;
} else
if( wch == UKEY_RIGHT )
{
if (pos < str_len( target ))
pos++;
} else
if ( wch == UKEY_INS ) insert = !insert; else
if ( wch == UKEY_HOME )
{
if( opt.smart_home_end && pos == 0 )
{
int sp = str_len( target ) - 1;
if( sp > 0 )
{
while( sp > 0 && target[sp] == L'/' ) sp--;
while( sp > 0 && target[sp] != L'/' ) sp--;
if( sp > 0 ) pos = sp + 1;
}
}
else
{
pos = 0;
}
}
else
if ( wch == UKEY_END ) pos = str_len(target); else
if ( wch == UKEY_DEL && pos < str_len(target) ) str_del( target, pos, 1 ); else
if ( wch == UKEY_PGUP || wch == UKEY_PGDN )
{
con_chide();
int zz = vfu_hist_menu( 5, 5, ( wch == UKEY_PGUP ) ? L"Dir Entry History" : L"ChDir History",
( wch == UKEY_PGUP ) ? HID_GETDIR : HID_CHDIR );
con_cshow();
if (zz != -1)
{
const char* pc = vfu_hist_get( ( wch == UKEY_PGUP ) ? HID_GETDIR : HID_CHDIR, zz );
if ( pc )
{
target = pc;
pos = str_len( target );
}
}
} else
if ( wch == UKEY_CTRL_O )
{
mb.undef();
mb.push( L"A Add current DATE+TIME" );
mb.push( L"D Add current DATE only" );
mb.push( L"N Add current directory name" );
vfu_menu_box( 20, -10, L" Directory helper" );
int ec = menu_box_info.ec;
if( ec == 'A' or ec == 'D' )
{
char time_str[32];
time_t timenow = time( NULL );
tm tmnow;
localtime_r( &timenow, &tmnow );
if( strftime( time_str, sizeof(time_str) - 1, ec == L'A' ? "%Y%m%d_%H%M%S" : "%Y%m%d", &tmnow ) )
{
if( target[-1] != '/' ) target += "_";
target += time_str;
}
} else
if( ec == 'N' )
{
VString str = work_path;
str_cut_right( str, "/" );
str = str_file_name_ext( str );
str_cut_right( target, L"/" );
target += "/" + WString( str );
}
pos = str_len( target );
}
wch = 0;
firsthit = 0;
}
con_chide();
//------------------------------------------------------------------
str_cut_spc( target );
if ( res == 1 && target[0] == L'~' )
{
target.set( tilde_expand( VString( target ) ) );
str_fix_path( target );
}
/*
#ifdef _TARGET_UNIX_
leaveok(stdscr, TRUE);
#endif
*/
if ( res == 1 && str_len( target ) > 0 && should_exist && type == 'D' && !dir_exist( VString( target ) ))
{
vfu_beep();
wchar_t wch = towlower( vfu_ask( L"Directory does not exist! Create? "
L"( ENTER=Yes, ESC=cancel )",
L"\r" ));
if ( wch == 27 )
{
res = 0;
target = "";
}
else if ( wch == 13 )
if ( make_path( VString( target ) ))
{
if(vfu_ask( L"Cannot create path! ( ESC=cancel, C=continue-anyway )", L"Cc" ) == 27 )
{
res = 0;
target.undef();
}
}
}
say1(" ");
say2(" ");
str_cut_spc( target );
if ( str_len( target ) > 0 )
{
if( file_is_dir( VString( target ) ) )
str_fix_path( target );
vfu_hist_add( HID_GETDIR, VString( target ) );
}
target_in = target;
ASSERT( res == 0 || res == 1 );
return res;
}
/*-----------------------------------------------------------------------*/
void vfu_chdir( const char *a_new_dir )
{
fname_t t;
VString target;
if ( a_new_dir && a_new_dir[0] )
{
target = a_new_dir;
str_fix_path( target );
}
else
{
target = vfu_hist_get( HID_CHDIR, 0 );
if (!vfu_get_dir_name( "ChDir to? (use keys: TAB, PageUp, PageDown, Ctrl+X, Ctrl+A)",
target, 0 ))
return; /* get_dir_name canceled */
}
/* get_dir_name confirmed */
/*
if ( work_path[0] != target[0] && DirTreeChanged && opt.AutoTree ) SaveTree();
*/
if ( work_mode == WM_ARCHIVE )
{
archive_name = "";
archive_path = "";
}
vfu_hist_add( HID_CHDIR, work_path );
char ch = work_path[0];
if (opt.tree_cd)
if (!dir_exist( target ))
{
int z = 0;
if ( dir_tree.count() == 0 ) tree_load();
mb.undef();
z = tree_find( target, &mb );
if (z > 1)
{
z = vfu_menu_box( 10, 5, L"Change dir to..." );
if (z > -1)
target = mb.get(z);
else
return;
}
else
if (z == 1)
target = mb.get(0);
}
VString str = target;
if (str[0] == '/')
{ /* root directory */
target = str;
}
else
{
str = work_path + str;
str_fix_path( str );
target = str_reduce_path( str );
}
if (chdir( target ) != 0)
{
snprintf( t, sizeof(t), "chdir: %s", target.data() );
say1( t );
say2errno();
return;
}
else
{
work_path = target;
if ( work_mode == WM_ARCHIVE )
work_mode = WM_NORMAL;
}
if ( ch != work_path[0] ) tree_drop(); /* drop tree--it is for another drive*/
vfu_read_files();
}
/*-----------------------------------------------------------------------*/
void vfu_chdir_history()
{
int z = vfu_hist_menu( 5, 5, L"ChDir History", HID_CHDIR );
if (z == -1) return;
do_draw = 1;
//strcpy(opt.LastDir, CPath);
vfu_chdir( vfu_hist_get( HID_CHDIR, z ) );
}
/*###########################################################################*/
void tree_load()
{
if( dir_tree.fload( filename_tree ) )
say1( "DirTree load error." );
else
{
say1( "DirTree loaded ok." );
dir_tree_changed = 0;
}
}
/*-----------------------------------------------------------------------*/
void tree_save()
{
if( dir_tree.fsave( filename_tree ) )
say1( "DirTree save error." );
else
{
say1( "DirTree saved ok." );
dir_tree_changed = 0;
}
}
/*-----------------------------------------------------------------------*/
void tree_drop()
{
if ( dir_tree_changed )
tree_save();
dir_tree.undef();
dir_tree_changed = 0;
}
/*-----------------------------------------------------------------------*/
void tree_fix()
{
int z;
for( z = dir_tree.count() - 1; z >= 0; z-- )
{
VString s1 = dir_tree[z];
VString s2;
if (z < dir_tree.count() - 1)
s2 = dir_tree[z+1];
else
s2 = "";
int i = -1;
int n = str_count( s1, "/" );
int p = 0;
while(n > 2)
{
i = str_find( s1, '/', i+1 );
int q = 0;
if ( str_len( s2 ) > i )
q = s1[i] != s2[i];
if ( q || ( str_count(s2,"/",i+1) < 2))
{
p = 1;
str_set_ch(s1, i, '\\');
}
n--;
}
if ( p )
dir_tree.set( z, s1 );
}
}
/*-----------------------------------------------------------------------*/
fsize_t __tree_rebuild_process( const char* path )
{
if ( vfu_break_op() ) return -1;
DIR* dir;
dirent* de;
struct stat st;
fsize_t size = 0;
fname_t new_name;
dir = opendir( path );
if ( !dir ) return 0;
while( (de = readdir(dir)) )
{
if ( strcmp( de->d_name, "." ) == 0 ||
strcmp( de->d_name, ".." ) == 0 ) continue;
snprintf( new_name, sizeof(new_name), "%s%s", path, de->d_name );
lstat(new_name, &st);
int is_link = int(S_ISLNK(st.st_mode));
if (is_link) continue;
stat(new_name, &st);
int is_dir = S_ISDIR(st.st_mode);
if ( is_dir )
{ /* directory */
strcat( new_name, "/" );
int z;
int trim = 0;
for ( z = 0; z < trim_tree.count(); z++ )
{
VString trim_temp = trim_tree[z];
str_fix_path( trim_temp );
if ( pathcmp(trim_temp, new_name) == 0 )
{ /* trim_tree item found */
trim = 1;
break;
}
}
if (trim)
continue; /* cut this branch */
int pos = dir_tree.count();
fsize_t dir_size = __tree_rebuild_process( new_name );
if ( dir_size < 0 )
{ /* canceled */
closedir(dir);
return -1;
}
dir_tree.ins( pos, new_name );
size_cache_set( new_name, dir_size );
size += dir_size;
}
else
{ /* file */
size += st.st_size;
}
}
closedir(dir);
/* show some progress :) */
say2( str_dot_reduce( path, con_max_x()-1 ) );
return size;
}
void tree_rebuild()
{
dir_tree.undef();
size_cache.undef();
say1( "Rebuilding tree..." );
__tree_rebuild_process( "/" );
tree_fix();
dir_tree_changed = 1;
tree_save();
}
/*-----------------------------------------------------------------------*/
void tree_draw_item( int page, int index, int hilite )
{
if ( page + index >= dir_tree.count() ) return;
VString s1 = dir_tree[page+index];
str_trim_right(s1,1);
VString s2 = s1;
int j = str_rfind( s1,'/');
str_trim_right(s1,str_len(s2)-j-1);
str_trim_left(s2,j+1);
for(j = 0; j < str_len(s1); j++)
{
if (s1[j] == '/')
str_set_ch(s1,j, '|');
else
if (s1[j] == '\\')
str_set_ch(s1,j, '\\');
else
str_set_ch(s1,j, '+');
}
if (opt.tree_compact)
{
str_replace(s1,"+", "");
str_replace(s1,"|", "| ");
str_replace(s1,"\\"," ");
str_trim_right(s1,2);
s1 += "--";
}
else
{
str_replace(s1,"+", " ");
str_replace(s1,"\\", " ");
s1 += "--";
}
VString str = dir_tree[page+index];
str_tr( str,"\\", "/" );
VString sz;
sz.fi( size_cache_get( str ) );
if ( sz == "-1" )
sz = "n/a";
else
vfu_str_comma( sz );
str_pad( sz, 14 );
s1 = sz + " " + s1;
int m = con_max_x() - 1; /* doesn't speed the code... :) */
if ( str_len( s1 ) > m )
{
str_sleft( s1, m );
s2 = "";
}
else if ( str_len( s1 ) + str_len( s2 ) > m )
{
str_sleft( s2, m - str_len( s1 ) );
}
con_xy(1,3+1+index);
if (hilite)
{
con_puts( s1, cBAR );
con_puts( s2, cBAR );
con_ce( cBAR );
}
else
{
con_puts( s1, cSTATUS );
con_puts( s2, cMESSAGE );
con_ce( cSTATUS );
}
}
/*-----------------------------------------------------------------------*/
void tree_draw_page( ScrollPos &scroll )
{
VString str = " ";
str_mul( str, con_max_x() );
str = " SiZE DiRECTORY" + str;
str_sleft( str, con_max_x()-16 );
vfu_con_out(1,3, str, cHEADER );
int z = 0;
for(z = 0; z < scroll.pagesize(); z++)
{
if (scroll.page() + z <= scroll.max())
{
tree_draw_item( scroll.page(), z );
}
else
{
con_xy( 1, 3+1+z );
con_puts( "~", cCYAN );
con_ce( cCYAN );
}
}
}
/*-----------------------------------------------------------------------*/
void tree_draw_pos( ScrollPos &scroll, int opos )
{
int z = scroll.pos() - scroll.page();
if ( opos != -1 ) tree_draw_item( scroll.page(), opos );
tree_draw_item( scroll.page(), z, 1 );
VString str;
str = dir_tree[scroll.pos()];
str_tr( str,"\\", "/" );
VString sz;
sz.fi( size_cache_get( str ) );
vfu_str_comma( sz );
str_pad( sz, 14 );
str = sz + " " + str;
str = str_dot_reduce( str, con_max_x()-1 );
say1( str, cINFO );
say2( " Help: R Rebuild, S Incremental search, Z Recalc directory size", cINFO );
show_pos( scroll.pos()+1, scroll.max()+1 );
}
/*-----------------------------------------------------------------------*/
void tree_view()
{
VString str;
if (dir_tree.count() == 0)
{
tree_load();
if (dir_tree.count() == 0)
tree_rebuild();
}
say1( " " );
int new_pos = tree_index( work_path );
if ( new_pos == -1 )
new_pos = 0;
VString search_set;
str_add_ch_range( search_set, 'a', 'z' );
str_add_ch_range( search_set, 'A', 'Z' );
str_add_ch_range( search_set, '0', '9' );
search_set.cat( "._-~?*>[]" );
ScrollPos scroll;
scroll.set_min_max( 0, dir_tree.count()-1 );
scroll.set_pagesize( FLPS + 2 );
scroll.set_pagestep( OPT_SCROLL_PAGESTEP(opt.scroll_pagestep) );
scroll.go( new_pos );
int key = 0;
int opos = -1;
int opage = -1;
while( key != 27 && key != 13 && key != '-' &&
toupper( key ) != 'Q' && toupper( key ) != 'X' && key != UKEY_ALT_X )
{
if ( key >= 'A' && key <= 'Z' ) key = tolower( key );
if ( key == 's' )
{
str = "";
say1( "Enter search pattern: ( use TAB to advance )" );
key = con_getch();
while( str_find( search_set, key ) >= 0 || key == 8 || key == UKEY_BACKSPACE || key == 9 )
{
if ( key == 8 || key == UKEY_BACKSPACE )
str_trim_right( str, 1 );
else
if ( key != 9 )
str_add_ch( str, key );
say2( str );
if ( dir_tree.count() == 0 ) { key = con_getch(); continue; }
int z;
if ( key == 9 )
{
z = scroll.pos() + 1;
if (z > scroll.max() ) z = scroll.min();
}
else
z = scroll.pos();
int direction = 1;
int found = 0;
int loops = 0;
VString s_mask = str;
vfu_expand_mask( s_mask );
while(1)
{
if ( z > scroll.max() ) z = scroll.min();
if ( z < scroll.min() ) z = scroll.max();
VString str1 = dir_tree[z];
str_trim_right( str1, 1 );
int j = str_rfind(str1,'/');
if (j < 0)
str1 = "";
else
str_trim_left( str1, j+1 );
found = ( FNMATCH( s_mask, str1 ) == 0 );
if ( found ) break;
z += direction;
if ( loops++ > dir_tree.count() ) break;
}
if (found)
{
scroll.go(z);
tree_draw_page( scroll );
tree_draw_pos( scroll, opos );
}
key = con_getch();
}
say1( "" );
say2( "" );
}
else
switch( key )
{
case UKEY_UP : scroll.up(); break;
case UKEY_DOWN : scroll.down(); break;
case UKEY_PGUP : scroll.ppage(); break;
case UKEY_PGDN : scroll.npage(); break;
case UKEY_HOME : scroll.home(); break;
case UKEY_END : scroll.end(); break;
case 'r' : tree_rebuild();
scroll.set_min_max( 0, dir_tree.count()-1 );
scroll.home();
say1( "Rebuild done." );
break;
case 'w' : tree_save(); break;
case 'l' : tree_load();
scroll.set_min_max( 0, dir_tree.count()-1 );
scroll.home();
break;
case 'z' :
case UKEY_CTRL_Z :
str = dir_tree[scroll.pos()];
str_tr( str, "\\", "/" );
size_cache_set( str, vfu_dir_size( str ) );
tree_draw_page( scroll );
tree_draw_pos( scroll, opos );
say1( "Done." );
break;
}
if (opage != scroll.page())
tree_draw_page( scroll );
if (opos != scroll.pos() - scroll.page() || opage != scroll.page())
tree_draw_pos( scroll, opos );
opos = scroll.pos() - scroll.page();
opage = scroll.page();
key = con_getch();
}
if ( key == 13 )
{
str = dir_tree[scroll.pos()];
str_tr( str, "\\", "/" );
vfu_chdir( str );
}
do_draw = 2;
}
/*###########################################################################*/
#define SIZE_CACHE_OFFSET 15
#define SIZE_CACHE_OFFSET_CLEAN (SIZE_CACHE_OFFSET+8+1)
#define SIZE_CACHE_OFFSET_STR "15"
void size_cache_load()
{
size_cache_sorted_by_name = 0;
size_cache.set_block_size( 1024*1024 );
size_cache.undef();
size_cache_count = 0;
size_cache.fload( filename_size_cache );
// removes old-style size cache entries
if( size_cache.count() > 0 && ( size_cache[0][SIZE_CACHE_OFFSET] != '|' || size_cache[0][SIZE_CACHE_OFFSET_CLEAN] != '|' ) )
size_cache.undef();
size_cache_count = size_cache.count();
}
void size_cache_save()
{
size_cache.fsave( filename_size_cache );
}
int size_cache_cmp( const char* s1, const char* s2 )
{
int off = size_cache_sorted_by_name ? SIZE_CACHE_OFFSET_CLEAN : SIZE_CACHE_OFFSET;
/*
const unsigned char *ss1 = (const unsigned char *) s1 + off;
const unsigned char *ss2 = (const unsigned char *) s2 + off;
unsigned char c1, c2;
do
{
c1 = (unsigned char) *ss1++;
c2 = (unsigned char) *ss2++;
if ( c1 == '\0' )
return c1 - c2;
}
while (c1 == c2);
if( c1 == '/' ) return -1;
if( c2 == '/' ) return +1;
return c1 - c2;
*/
return strcmp( s1 + off, s2 + off );
}
VString size_cache_compose_key( const char *s, fsize_t size )
{
const char *ps;
fname_t ss;
expand_path( s, ss );
str_fix_path( ss );
// replace / with 0x01 to properly pass strcmp (using / is wrong!)