27static inline int fp_compare_fun (
const void *p1,
const void *p2) {
28 return strcasecmp(*(
const char**) p1, *(
const char**) p2);
31static inline int fp_compare_hidden_dirs_fun (
const void *p1,
const void *p2) {
32 if(strstr(*(
const char**)p1, PATH_SEPARATOR
".") && strstr(*(
const char**)p2, PATH_SEPARATOR
"."))
return 0;
33 if(strstr(*(
const char**)p1, PATH_SEPARATOR
"."))
return 1;
34 if(strstr(*(
const char**)p2, PATH_SEPARATOR
"."))
return -1;
35 return strcasecmp(*(
const char**) p1, *(
const char**) p2);
38static inline int fp_compare_hidden_files_fun (
const void *p1,
const void *p2) {
39 if(strncmp(*(
const char**)p1,
".",1)==0 && strncmp(*(
const char**)p2,
".",1)==0)
return 0;
40 if(strncmp(*(
const char**)p1,
".",1)==0 )
return 1;
41 if(strncmp(*(
const char**)p2,
".",1)==0 )
return -1;
42 return strcasecmp(*(
const char**) p1, *(
const char**) p2);
45static inline bool fp_show_hidden_files(
FilePicker *filepicker,
char* file) {
46 return (filepicker->
show_hidden) ? strcmp(file,
".")!=0 : (file[0] !=
'.');
49static inline bool fp_show_filter_files(
FilePicker *filepicker,
char* file) {
53 if(strstr(filepicker->
filter,
"."))
54 return strstr(file, filepicker->
filter);
63static void fp_sort_buffers(
FilePicker *filepicker,
int get_dirs) {
67 fp_compare_hidden_dirs_fun : fp_compare_fun);
72 fp_compare_hidden_files_fun : fp_compare_fun);
76static void fp_clear_filebuffer(
FilePicker *filepicker) {
89static void fp_clear_dirbuffer(
FilePicker *filepicker) {
104 return (((strlen(path)==3) && path[1] ==
':' && path[2] ==
'\\')
105 ||((strlen(path)==1) && path[0] ==
'\\'));
109 DWORD drives = GetLogicalDrives();
111 for (i=0; i<=
'Z'-
'A'; i++) {
112 if ((drives & (1 << i)) != 0) {
128 return ((dirp->dd_dta.attrib & _A_SUBDIR)==0);
132 return ((dirp->dd_dta.attrib & _A_SUBDIR) != 0);
137 return (strcmp (path, PATH_SEPARATOR) == 0);
148bool is_file(DIR *dirp,
struct dirent *dp) {
149 return (dp-> d_type != DT_DIR && dp->d_type != DT_UNKNOWN);
153 return (dp -> d_type == DT_DIR);
158static inline int fp_prefill_dirbuffer(
FilePicker *filepicker,
char *path) {
166 while (strcmp (ho, PATH_SEPARATOR) != 0) {
174 if (strcmp (path, PATH_SEPARATOR) != 0) {
188 if(dp -> d_type == DT_LNK) {
190 snprintf(s, 256, (strcmp(path, PATH_SEPARATOR) != 0) ?
191 "%s" PATH_SEPARATOR
"%s" :
"%s%s" , path,dp->d_name);
193 if (stat(s, &sb) == 0 && S_ISDIR(sb.st_mode)) {
203 if (dp->d_type == DT_UNKNOWN) {
205 snprintf(s, 256, (strcmp(path, PATH_SEPARATOR) != 0) ?
206 "%s" PATH_SEPARATOR
"%s" :
"%s%s" , path,dp->d_name);
208 if (stat(s, &sb) == 0 && S_ISDIR(sb.st_mode)) {
222 return dp -> d_type == DT_LNK;
228 fp_clear_filebuffer(filepicker);
232 if((dirp = opendir(path)) == NULL) {
233 path =(
char*)PATH_SEPARATOR;
234 dirp = opendir(PATH_SEPARATOR);
239 fp_clear_dirbuffer(filepicker);
240 ret = fp_prefill_dirbuffer(filepicker, path);
243 while ((dp = readdir(dirp)) != NULL) {
245 if((get_files && (
is_file(dirp, dp) || (
fp_check_dir(path, dp) == 2)) && strlen(dp->d_name)!=0
246 && strcmp(dp->d_name,
"..")!=0 && fp_show_hidden_files(filepicker, dp->d_name)
247 && fp_show_filter_files(filepicker, dp->d_name) && !
fp_check_link(path, dp)) ) {
256 && strlen(dp->d_name)!=0 && strcmp(dp->d_name,
"..")!=0 && fp_show_hidden_files(filepicker, dp->d_name)) {
265 "%s" PATH_SEPARATOR
"%s" :
"%s%s" , path,dp->d_name);
271 fp_sort_buffers(filepicker, get_dirs);
276 fp_clear_filebuffer(filepicker);
277 fp_clear_dirbuffer(filepicker);
279 free(filepicker->
path);
291 filepicker->
path = NULL;
292 filepicker->
filter = NULL;
298 assert(filepicker->
path != NULL);
unsigned int file_counter
int asprintf(char *strp[], const char *fmt,...)
const char * xdg_mime_get_mime_type_from_file_name(const char *file_name)
int fp_check_link(char *path, struct dirent *dp)
void fp_init(FilePicker *filepicker, const char *path)
fp_init - set default values used by the filepicker
bool is_file(DIR *dirp, struct dirent *dp)
int fp_check_dir(char *path, struct dirent *dp)
int fp_is_link(struct dirent *dp)
void add_root_directory(FilePicker *filepicker, char *path)
bool is_root_directory(char *path)
int fp_get_files(FilePicker *filepicker, char *path, int get_dirs, int get_files)
fp_get_files - fill file_names and dir_names with the results from readdir path
bool is_directory(DIR *dirp, struct dirent *dp)
void fp_free(FilePicker *filepicker)
fp_free - release all memory used by the filepicker