forked from SeraphYuki/zimedit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_browser.c
More file actions
executable file
·109 lines (86 loc) · 2.17 KB
/
file_browser.c
File metadata and controls
executable file
·109 lines (86 loc) · 2.17 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include "types.h"
#include "file_browser.h"
void Thoth_FileBrowser_Init(Thoth_FileBrowser *fb){
memset(fb,0,sizeof(Thoth_FileBrowser));
#ifdef LINUX_COMPILE
getcwd(fb->directory, MAX_PATH_LEN);
#endif
#ifdef WINDOWS_COMPILE
sprintf(fb->directory, "C:\\");
#endif
Thoth_FileBrowser_ChangeDirectory(fb);
}
void Thoth_FileBrowser_Free(Thoth_FileBrowser *fb){
fb->nFiles = 0;
}
void Thoth_FileBrowser_ChangeDirectory(Thoth_FileBrowser *fb){
fb->nFiles = 0;
int len = strlen(fb->directory);
if(len == 0 || (fb->directory[len-1] != '/' && fb->directory[len-1] != '\\')){
#ifdef LINUX_COMPILE
fb->directory[len++] = '/';
#endif
#ifdef WINDOWS_COMPILE
fb->directory[len++] = '\\';
#endif
}
fb->directory[len] = 0;
DIR *dir;
struct dirent *dp;
dir = opendir(fb->directory);
if(dir == NULL){
int m;
// -2 for trailing '/' and 1 for 0+indexing
for(m = len-2; m >= 0; m--){
if(fb->directory[m] == '/' || fb->directory[m] == '\\'){
break;
}
}
fb->directory[m+1] = 0;
Thoth_FileBrowser_ChangeDirectory(fb);
return;
}
int max = 0;
while((dp = readdir(dir)) != NULL){
if(max++ > THOTH_FILEBROWSER_MAX) break;
struct stat s;
char temp[MAX_PATH_LEN];
strcpy(temp,fb->directory);
strcpy(&temp[strlen(temp)], dp->d_name);
stat(temp, &s);
if((s.st_mode & S_IFMT) == S_IFDIR){
if(strcmp(dp->d_name, ".") == 0) continue;
fb->nFiles++;
fb->files[fb->nFiles-1].dir = 1;
strcpy(fb->files[fb->nFiles-1].name, dp->d_name);
if(strcmp(dp->d_name, "..") == 0) {
int m;
for(m = (strlen(temp)-strlen("/.."))-1; m >= 0; m--){
if(temp[m] == '/' || temp[m] == '\\'){
break;
}
}
if(m <= 0){
fb->files[fb->nFiles-1].path[m++] = '/';
} else {
strncpy(fb->files[fb->nFiles-1].path, temp, m);
}
fb->files[fb->nFiles-1].path[m] = 0;
continue;
}
strcpy(fb->files[fb->nFiles-1].path, temp);
} else {
fb->nFiles++;
strcpy(fb->files[fb->nFiles-1].path, temp);
strcpy(fb->files[fb->nFiles-1].name, dp->d_name);
fb->files[fb->nFiles-1].dir = 0;
}
}
closedir(dir);
}