-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX_FileList.c
1855 lines (1563 loc) · 40.2 KB
/
EX_FileList.c
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) 2011 azazello and ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* File List
*
* display directory contents, allows for changing dir,
* drive etc..
*
* meant for use by MP3 player, mayby demo player too
*
* FileList have four colums:
* - file name (with ext)
* - size
* - date (last modification)
* - time (last modification)
*
* and allows to display different sets of columns
* also do sorting
*
*/
#include "quakedef.h"
#include "localtime.h"
#include "Ctrl.h"
#include "EX_FileList.h"
#include "utils.h"
#include "keys.h"
#include "hash.h"
#include "fs.h"
#include "vfs.h"
// column width
#define COL_SIZE 4
#define COL_DATE 8
#define COL_TIME 5
#define FL_SEARCH_TIMEOUT 2.0
static double last_search_enter_time = 0.0;
static void OnChange_file_browser_sort_mode(cvar_t *var, char *string, qbool *cancel);
static cvar_t file_browser_show_size = {"file_browser_show_size", "1"};
static cvar_t file_browser_show_date = {"file_browser_show_date", "1"};
static cvar_t file_browser_show_time = {"file_browser_show_time", "0"};
static cvar_t file_browser_sort_mode = {"file_browser_sort_mode", "1", CVAR_NONE, OnChange_file_browser_sort_mode};
static cvar_t file_browser_show_status = {"file_browser_show_status", "1"};
static cvar_t file_browser_strip_names = {"file_browser_strip_names", "1"};
static cvar_t file_browser_interline = {"file_browser_interline", "0"};
static cvar_t file_browser_scrollnames = {"file_browser_scrollnames" , "1"};
static cvar_t file_browser_selected_color = {"file_browser_selected_color", "0 150 235 255", CVAR_COLOR};
static cvar_t file_browser_file_color = {"file_browser_file_color", "255 255 255 255", CVAR_COLOR};
static cvar_t file_browser_dir_color = {"file_browser_dir_color", "170 80 0 255", CVAR_COLOR};
static cvar_t file_browser_archive_color = {"file_browser_archive_color", "255 170 0 255", CVAR_COLOR};
static cvar_t file_browser_sort_archives = {"file_browser_sort_archives", "0", CVAR_NONE, OnChange_file_browser_sort_mode };
void EX_FileList_Init(void)
{
Cvar_SetCurrentGroup(CVAR_GROUP_MENU);
Cvar_Register(&file_browser_show_size);
Cvar_Register(&file_browser_show_date);
Cvar_Register(&file_browser_show_time);
Cvar_Register(&file_browser_sort_mode);
Cvar_Register(&file_browser_show_status);
Cvar_Register(&file_browser_strip_names);
Cvar_Register(&file_browser_interline);
Cvar_Register(&file_browser_scrollnames);
Cvar_Register(&file_browser_file_color);
Cvar_Register(&file_browser_selected_color);
Cvar_Register(&file_browser_dir_color);
Cvar_Register(&file_browser_archive_color);
Cvar_Register(&file_browser_sort_archives);
Cvar_ResetCurrentGroup();
}
//
// Create list
//
void FL_Init(filelist_t *fl, char *initdir)
{
Sys_getcwd(fl->current_dir, MAX_PATH);
FL_SetCurrentDir(fl, initdir);
fl->error = false;
fl->need_refresh = true;
fl->num_entries = 0;
fl->current_entry = 0;
fl->num_filetypes = 0;
fl->search_string[0] = 0;
fl->last_page_size = 0;
fl->search_valid = false;
fl->cdup_find = false;
fl->show_dirup = true;
fl->show_dirs = true;
fl->scrollbar = ScrollBar_Create(NULL, fl->current_dir);
#ifdef WITH_ZIP
fl->current_archive[0] = 0;
fl->in_archive = false;
#endif // WITH_ZIP
}
void FL_Shutdown(filelist_t* fl)
{
Q_free(fl->scrollbar);
}
//
// Set directory
//
void FL_SetCurrentDir(filelist_t *fl, const char *dir)
{
char buf[MAX_PATH+1];
if (Sys_fullpath(buf, dir, MAX_PATH + 1) == NULL) {
return;
}
if (strlen(buf) > MAX_PATH) {
// Should never fail in this
return;
}
// Try changing directory, block if this fails
{
char olddir[MAX_PATH + 1];
// Save the current directory. (we want to restore this later)
if (Sys_getcwd(olddir, MAX_PATH + 1) == NULL) {
return;
}
// Change to the new dir.
if (!Sys_chdir(buf)) {
return;
}
// restore
Sys_chdir(olddir);
}
strlcpy(fl->current_dir, buf, sizeof(fl->current_dir));
fl->need_refresh = true;
}
//
// Get current directory
//
char *FL_GetCurrentDir(filelist_t *fl)
{
return fl->current_dir;
}
//
// add new file type (.qwd, .qwz, .mp3)
//
void FL_AddFileType(filelist_t *fl, int id, char *ext)
{
int num = fl->num_filetypes;
if (num >= MAX_FILELIST_TYPES)
return;
if (strlen(ext) > MAX_EXTENSION_LENGTH)
return;
strlcpy (fl->filetypes[num].extension, ext, sizeof (fl->filetypes[num].extension));
fl->filetypes[num].id = id;
fl->num_filetypes ++;
}
//
// hides the ".." option to traverse in the dirs hierarchy
//
void FL_SetDirUpOption(filelist_t *fl, qbool show)
{
fl->show_dirup = show;
}
//
// hides the ".." option to traverse in the dirs hierarchy
//
void FL_SetDirsOption(filelist_t *fl, qbool show)
{
fl->show_dirs = show;
}
//
// get current entry
//
filedesc_t * FL_GetCurrentEntry(filelist_t *fl)
{
if (fl->num_entries <= 0)
return NULL;
return &fl->entries[fl->current_entry];
}
//
// get current path
//
char *FL_GetCurrentPath(filelist_t *fl)
{
if (fl->num_entries <= 0)
return NULL;
return fl->entries[fl->current_entry].name;
}
//
// get current display
//
char *FL_GetCurrentDisplay(filelist_t *fl)
{
if (fl->num_entries <= 0)
return NULL;
return fl->entries[fl->current_entry].display;
}
//
// get current entry type
//
int FL_GetCurrentEntryType(filelist_t *fl)
{
if (fl->num_entries <= 0)
return -1;
return fl->filetypes[fl->entries[fl->current_entry].type_index].id;
}
//
// is current entry a dir ?
//
qbool FL_IsCurrentDir(filelist_t *fl)
{
if (fl->num_entries <= 0)
return true; // we can handle that
return fl->entries[fl->current_entry].is_directory;
}
#ifdef WITH_ZIP
//
// Is current entry a zip file?
//
qbool FL_IsCurrentArchive(filelist_t *fl)
{
if (fl->num_entries <= 0)
{
return true;
}
return fl->entries[fl->current_entry].is_archive;
}
#endif // WITH_ZIP
void FL_StripFileName(filelist_t *fl, filedesc_t *f)
{
int i;
char namebuf[_MAX_FNAME] = {0};
char extbuf[_MAX_EXT] = {0};
char *t;
// Don't try to strip this.
if (!strcmp(f->name, ".."))
{
strlcpy (f->display, "/..", sizeof(f->display));
return;
}
// Common for dir/file, get name without path but with ext
strlcpy (namebuf, COM_SkipPath (f->name), sizeof (namebuf));
// File specific.
if (!f->is_directory)
{
// Get extension.
snprintf (extbuf, sizeof(extbuf), ".%s", COM_FileExtension (namebuf));
// If the extension is only a . it means we have no extension.
if (strlen (extbuf) == 1)
{
extbuf[0] = '\0';
}
// Remove extension from the name.
COM_StripExtension(namebuf, namebuf, sizeof(namebuf));
}
if (file_browser_strip_names.value && !f->is_directory)
{
char *s;
for (i=0; i < strlen(namebuf); i++)
if (namebuf[i] == '_')
namebuf[i] = ' ';
// Remove spaces from the beginning
s = namebuf;
while (*s == ' ')
s++;
memmove(namebuf, s, strlen(s) + 1);
// Remove spaces from the end
s = namebuf + strlen(namebuf);
while (s > namebuf && *(s-1) == ' ')
s--;
*s = 0;
// Remove few spaces in a row
s = namebuf;
while (s < namebuf + strlen(namebuf))
{
if (*s == ' ')
{
char *q = s+1;
while (*q == ' ')
q++;
if (q > s+1)
memmove(s+1, q, strlen(q)+1);
}
s++;
}
if (namebuf[0] == 0)
strlcpy (namebuf, "_", sizeof (namebuf));
}
// Replace all non-standard characters with '_'
for (i=0; i < strlen(namebuf); i++)
{
if (namebuf[i] < ' ' || namebuf[i] > '~')
namebuf[i] = '_';
}
f->display[0] = 0;
t = f->display;
// Add a slash before a dir.
if (f->is_directory)
*t++ = '/';
// Add the name and extension to the final buffer.
for (i=0; i < strlen(namebuf) && (t - f->display) < MAX_PATH; i++)
*t++ = namebuf[i];
for (i=0; i < strlen(extbuf) && (t - f->display) < MAX_PATH; i++)
*t++ = extbuf[i];
*t = 0;
}
//
// Goto specific file by its name
//
void FL_GotoFile(filelist_t *fl, char *name)
{
int i;
fl->current_entry = 0;
if (!name[0])
return;
for (i=0; i < fl->num_entries; i++)
{
if (!strcmp(name, fl->entries[i].name))
{
fl->current_entry = i;
return;
}
}
return;
}
//
// file compare func
// set FL_CompareFunc_FileList before calling..
//
filelist_t * FL_CompareFunc_FileList; // global
int FL_CompareFunc(const void * p_d1, const void * p_d2)
{
extern int SYSTEMTIMEcmp(const SYSTEMTIME *, const SYSTEMTIME *);
int reverse = 0;
filelist_t *fl = FL_CompareFunc_FileList;
char *sort_string = file_browser_sort_mode.string;
const filedesc_t *d1 = (filedesc_t *)p_d1;
const filedesc_t *d2 = (filedesc_t *)p_d2;
// Directories always first.
if (d1->is_directory && !d2->is_directory)
return -1;
if (d2->is_directory && !d1->is_directory)
return 1;
// Directories sorted always by name, ascending
if (d1->is_directory && d2->is_directory)
return strcasecmp(d1->name, d2->name);
#ifdef WITH_ZIP
// Zips after directories.
if (d1->is_archive && !d2->is_archive)
return -1;
if (d2->is_archive && !d1->is_archive)
return 1;
if (!file_browser_sort_archives.integer && d1->is_archive && d2->is_archive)
return strcasecmp(d1->name, d2->name);
#endif // WITH_ZIP
while (true)
{
long int d; // difference
char c = *sort_string++;
if (c & 128)
{
reverse = 1;
c -= 128;
}
switch (c)
{
case '1': // name
d = strcasecmp(d1->name, d2->name); break;
case '2': // size
d = d1->size - d2->size;
break;
case '3': // date / time
d = SYSTEMTIMEcmp(&(d1->time), &(d2->time));
break;
case '4': // type
{
char *ext1 = fl->filetypes[d1->type_index].extension;
char *ext2 = fl->filetypes[d2->type_index].extension;
d = strcasecmp(ext1, ext2);
break;
}
default:
d = d1 - d2;
}
if (d)
return reverse ? -d : d;
}
}
//
// sort directory
//
void FL_SortDir (filelist_t *fl)
{
char name[MAX_PATH+1] = "";
if (fl->num_entries <= 0 ||
file_browser_sort_mode.string == NULL ||
file_browser_sort_mode.string[0] == 0)
return;
FL_CompareFunc_FileList = fl;
if (fl->current_entry >= 0 && fl->current_entry < fl->num_entries)
strlcpy (name, fl->entries[fl->current_entry].name, sizeof (name));
qsort (fl->entries, fl->num_entries, sizeof(filedesc_t), FL_CompareFunc);
FL_GotoFile (fl, name);
fl->need_resort = false;
}
//
// Find out if a given directory entry is a registered file type and
// if so, returns the index of the file type. Otherwise -1 is returned.
//
static int FL_FindRegisteredType(filelist_t *fl, sys_dirent *ent)
{
int i = 0;
int result = -1;
if (!ent->directory)
{
for (i=0; i < fl->num_filetypes; i++)
{
char ext[_MAX_EXT];
snprintf (ext, sizeof(ext), ".%s", COM_FileExtension (ent->fname));
if (!strcasecmp(fl->filetypes[i].extension, ext))
{
result = i;
break;
}
}
}
return result;
}
//
// Finds which entry to highlight after doing "cd up".
//
static void FL_FindHighlightEntry (filelist_t *fl)
{
int i = 0;
// Look for what we have to highlight when listing the files in the
// parent directory. If we're in c:\quake\qw\some_directory\ and do a cdup ".."
// we want to highlight (move the cursor to) "some_directory" in the file list
// of c:\quake\qw\ so that it's obvious which directory we just left.
if (fl->cdup_find)
{
int uplen = strlen (fl->cdup_name);
fl->cdup_find = false;
for (i=0; i < fl->num_entries; i++)
{
int clen = strlen (fl->entries[i].name);
if (uplen > clen)
{
continue;
}
if (strcmp(fl->cdup_name, fl->entries[i].name + clen - uplen))
{
continue;
}
fl->current_entry = i;
break;
}
}
}
#ifdef WITH_ZIP
//
// Read the ZIP file.
//
void FL_ReadArchive (filelist_t *fl)
{
int temp = 0;
unzFile zip_file;
sys_dirent ent;
SYSTEMTIME archive_time;
// Save the file modification time of the current archive
archive_time = fl->entries[fl->current_entry].time;
fl->error = true;
fl->need_refresh = false;
fl->display_entry = 0;
fl->num_entries = 0;
fl->current_entry = 0;
// Open the zip file.
if (fl->current_archive[0])
{
zip_file = FS_ZipUnpackOpenFile (fl->current_archive);
}
else
{
return;
}
if (zip_file == NULL)
{
return;
}
// Get the first file from the zip file.
if (!FS_ZipGetFirst (zip_file, &ent))
{
goto finish;
}
fl->error = false;
// Make sure we don't have some garbage left from the previous dir.
// This caused a bug where some files inside of a zip would be
// regarded as zip files, since the files with the same index
// in the parent directory were zip files.
memset(fl->entries, 0, sizeof(fl->entries));
// Add ".." entry to allow navigating out of archive if setting says so.
if (fl->show_dirup)
{
// Pointer to the current file entry.
filedesc_t *f = &fl->entries[fl->num_entries];
// Populate the file descriptor
f->type_index = -1;
f->is_directory = true;
snprintf(f->name, sizeof(f->name), "..");
f->size = 0;
f->time = archive_time;
// Find friendly name.
FL_StripFileName(fl, f);
// Increase counter of how many files have been found.
fl->num_entries++;
if (fl->num_entries >= MAX_FILELIST_ENTRIES)
{
goto finish;
}
}
do
{
// Pointer to the current file entry.
filedesc_t *f = &fl->entries[fl->num_entries];
f->type_index = -1;
// Skip current/above dir and hidden files.
if (!strcmp(ent.fname, ".") || !strcmp(ent.fname, "..") || ent.hidden)
{
goto skip;
}
// Find registered type if it's not a directory.
f->type_index = FL_FindRegisteredType (fl, &ent);
// We're not interested in this file type since it wasn't registered.
if (!ent.directory && f->type_index < 0)
{
goto skip;
}
// We found a file that we're interested in so save the info about it
// in the file description structure.
f->is_directory = ent.directory;
// Get the full path for the file.
snprintf (f->name, sizeof(f->name), "%s%s%s", fl->current_archive, PATH_SEPARATOR, ent.fname);
f->size = ent.size;
memcpy(&f->time, &ent.time, sizeof(f->time));
// Find friendly name.
FL_StripFileName(fl, f);
// Increase counter of how many files have been found.
fl->num_entries++;
if (fl->num_entries >= MAX_FILELIST_ENTRIES)
{
break;
}
skip:
// Get next filesystem entry
temp = FS_ZipGetNextFile (zip_file, &ent);
}
while (temp > 0);
fl->need_resort = true;
finish:
// Close the zip file.
FS_ZipUnpackCloseFile (zip_file);
// Re-sort the file list if needed.
if (fl->need_resort)
{
FL_SortDir (fl);
}
// Resort might have changed this.
fl->current_entry = 0;
// Find which item to highlight in the list.
FL_FindHighlightEntry(fl);
}
#endif // WITH_ZIP
//
// read directory
//
void FL_ReadDir(filelist_t *fl)
{
sys_dirent ent;
SysDirEnumHandle search;
int temp;
char olddir[MAX_PATH+1];
fl->error = true;
fl->need_refresh = false;
fl->display_entry = 0;
// Save the current directory. (we want to restore this later)
if (Sys_getcwd(olddir, MAX_PATH+1) == NULL)
{
return;
}
// Change to the new dir.
if (!Sys_chdir(fl->current_dir))
{
// Set the current dir.
return;
}
fl->num_entries = 0;
fl->current_entry = 0;
fl->error = false;
// Get the first entry in the dir.
search = Sys_ReadDirFirst(&ent);
if (!search)
{
goto finish;
}
do
{
// Pointer to the current file entry.
filedesc_t *f = &fl->entries[fl->num_entries];
memset (f, 0, sizeof(filedesc_t));
f->type_index = -1;
// Skip current/above dir and hidden files.
if (!strcmp(ent.fname, ".") || ent.hidden)
{
goto skip;
}
// Skip the "up one level" option if setting says so
if (!strcmp(ent.fname, "..") && !fl->show_dirup)
{
goto skip;
}
// Skip directories, if settings say so
if (ent.directory && !fl->show_dirs)
{
goto skip;
}
// Find registered type if it's not a directory.
f->type_index = FL_FindRegisteredType (fl, &ent);
#ifdef WITH_ZIP
if (FS_IsArchive (ent.fname))
{
f->is_archive = true;
}
#endif
// We're not interested in this file type since it wasn't registered.
if (!ent.directory && f->type_index < 0
#ifdef WITH_ZIP
// Or isn't a zip.
&& !f->is_archive
#endif
)
{
goto skip;
}
// We found a file that we're interested in so save the info about it
// in the file description structure.
{
f->is_directory = ent.directory;
if (!strcmp(ent.fname, ".."))
{
// Don't get the full path for the ".." (parent) dir, that's just confusing.
strlcpy (f->name, ent.fname, sizeof (f->name));
}
else
{
// Get full path for normal files/dirs.
Sys_fullpath(f->name, ent.fname, MAX_PATH+1);
}
f->size = ent.size;
memcpy(&f->time, &ent.time, sizeof(f->time));
}
// Find friendly name.
FL_StripFileName(fl, f);
// Increase counter of how many files have been found.
fl->num_entries++;
if (fl->num_entries >= MAX_FILELIST_ENTRIES)
{
break;
}
skip:
memset (&ent, 0, sizeof(ent));
// Get next filesystem entry
temp = Sys_ReadDirNext(search, &ent);
}
while (temp > 0);
// Close the handle for the directory.
Sys_ReadDirClose(search);
fl->need_resort = true;
finish:
// Change the current dir back to what it was.
Sys_chdir (olddir);
// Re-sort the file list if needed.
if (fl->need_resort)
{
FL_SortDir (fl);
}
// Resort might have changed this.
fl->current_entry = 0;
// Find which item to highlight in the list.
FL_FindHighlightEntry(fl);
return;
}
//
// Search by name for next item.
//
qbool FL_Search(filelist_t *fl)
{
int start = fl->current_entry;
int stop = fl->num_entries;
int i = 0;
char *s = NULL;
char tmp[512];
fl->search_dirty = false;
search :
// First search at the beginning of the string (like when you type in a window in explorer).
for (i = start; i < stop; i++)
{
strlcpy (tmp, fl->entries[i].display, sizeof (tmp));
s = tmp;
// Get rid of slashes for dirs.
while (s && (*s) == '/')
{
s++;
}
if (!strncasecmp (s, fl->search_string, strlen (fl->search_string)))
{
fl->current_entry = i;
return true;
}
}
// Nothing found, so search in any part of the string.
for (i = start; i < stop; i++)
{
strlcpy (tmp, fl->entries[i].display, sizeof (tmp));
FunToSort (tmp);
if (strstr(tmp, fl->search_string))
{
fl->current_entry = i;
return true;
}
}
// Try to search from the start if we didn't find it below the
// current position of the cursor.
if (start > 0)
{
start = 0;
stop = fl->current_entry; // No idea searching anything below this again (we just did that).
goto search;
}
// We couldn't find anything.
fl->search_error = true;
return false;
}
//
// FL_ChangeZip - enter a zip file
//
void FL_ChangeArchive(filelist_t *fl, char *archive)
{
strlcpy (fl->current_archive, archive, sizeof(fl->current_archive));
fl->in_archive = true;
fl->need_refresh = true;
}
//
// FL_ChangeDirUp - cd ..
//
void FL_ChangeDirUp(filelist_t *fl)
{
// No point doing anything.
if (strlen(fl->current_dir) < 2)
{
return;
}
// Get the name of the directory we're leaving, so that we can highlight it
// in the file list of it's parent. (makes it easier keep track of where you are)
{
fl->cdup_find = true;
#ifdef WITH_ZIP
if (fl->in_archive)
{
strlcpy (fl->cdup_name, COM_SkipPath(fl->current_archive), sizeof(fl->cdup_name));
}
else
#endif // WITH_ZIP
{
strlcpy (fl->cdup_name, COM_SkipPath(fl->current_dir), sizeof(fl->cdup_name));
}
// fl->cdup_name will be:
// the_directory_were_leaving
// If the full path was:
// c:\quake\qw\the_directory_were_leaving
}
}
//
// FL_ChangeDir - changes directory
//
void FL_ChangeDir(filelist_t *fl, char *newdir)
{
char olddir[MAX_PATH+1];
// Get the current dir from the OS and save it.
if (Sys_getcwd(olddir, MAX_PATH+1) == NULL)
{
return;
}
// Check if changing to parent directory or leaving archive
if (newdir != NULL && strcmp(newdir, "..") == 0)
{
FL_ChangeDirUp(fl);
#ifdef WITH_ZIP
if (fl->in_archive)
{
// Leaving a zip file.
fl->current_archive[0] = 0;
fl->in_archive = false;
fl->need_refresh = true;
return;
}
#endif // WITH_ZIP
}
// Change to the current dir that we're in (might be different from the OS's).
// If we're changing dirs in a relative fashion ".." for instance we need to
// be in this dir, and not the dir that the OS is in.
if (Sys_chdir(fl->current_dir) == 0)
{
// Normal directory.
return;
}
// Change to the new dir requested.
Sys_chdir (newdir);
// Save the current dir we just changed to.
Sys_getcwd (fl->current_dir, MAX_PATH+1);
// Go back to where the OS wants to be.
Sys_chdir (olddir);
fl->need_refresh = true;
#ifdef WITH_ZIP
// Since we just changed to a new directory we can't be in a zip file.
fl->current_archive[0] = 0;
fl->in_archive = false;
#endif // WITH_ZIP
}