libxputty 0.1
Loading...
Searching...
No Matches
Functions
xfilepicker.c File Reference

Go to the source code of this file.

Functions

bool is_root_directory (char *path)
 
void add_root_directory (FilePicker *filepicker, char *path)
 
bool is_file (DIR *dirp, struct dirent *dp)
 
bool is_directory (DIR *dirp, struct dirent *dp)
 
int fp_check_link (char *path, struct dirent *dp)
 
int fp_check_dir (char *path, struct dirent *dp)
 
int fp_is_link (struct dirent *dp)
 
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
 
void fp_free (FilePicker *filepicker)
 fp_free - release all memory used by the filepicker
 
void fp_init (FilePicker *filepicker, const char *path)
 fp_init - set default values used by the filepicker
 

Function Documentation

◆ add_root_directory()

void add_root_directory ( FilePicker filepicker,
char *  path 
)

Definition at line 108 of file xfilepicker.c.

108 {
109 DWORD drives = GetLogicalDrives();
110 int i;
111 for (i=0; i<='Z'-'A'; i++) {
112 if ((drives & (1 << i)) != 0) {
113 filepicker->dir_counter += 1;
114 filepicker->dir_names = (char **)realloc(filepicker->dir_names,
115 (filepicker->dir_counter) * sizeof(char *));
116 asprintf(&filepicker->dir_names[filepicker->dir_counter-1], "%c:\\", 'A'+i);
117 }
118 }
119 /* filepicker->dir_names = (char **)realloc(filepicker->dir_names,
120 (filepicker->dir_counter + 1) * sizeof(char *));
121 assert(filepicker->dir_names != NULL);
122 asprintf(&filepicker->dir_names[filepicker->dir_counter++], "c:%s", PATH_SEPARATOR);
123 assert(&filepicker->dir_names[filepicker->dir_counter] != NULL);*/
124
125}
unsigned int dir_counter
Definition xfilepicker.h:70
char ** dir_names
Definition xfilepicker.h:68
int asprintf(char *strp[], const char *fmt,...)
Definition xasprintf.c:36

References asprintf(), FilePicker::dir_counter, and FilePicker::dir_names.

◆ fp_check_dir()

int fp_check_dir ( char *  path,
struct dirent *  dp 
)

Definition at line 201 of file xfilepicker.c.

201 {
202#ifdef __linux__ //not supported on win32
203 if (dp->d_type == DT_UNKNOWN) {
204 char s[256];
205 snprintf(s, 256, (strcmp(path, PATH_SEPARATOR) != 0) ?
206 "%s" PATH_SEPARATOR "%s" : "%s%s" , path,dp->d_name);
207 struct stat sb;
208 if (stat(s, &sb) == 0 && S_ISDIR(sb.st_mode)) {
209 return 1;
210 } else {
211 return 2;
212 }
213 }
214#endif
215 return 0;
216}

Referenced by fp_get_files().

◆ fp_check_link()

int fp_check_link ( char *  path,
struct dirent *  dp 
)

Definition at line 186 of file xfilepicker.c.

186 {
187#ifdef __linux__ //not supported on win32
188 if(dp -> d_type == DT_LNK) {
189 char s[256];
190 snprintf(s, 256, (strcmp(path, PATH_SEPARATOR) != 0) ?
191 "%s" PATH_SEPARATOR "%s" : "%s%s" , path,dp->d_name);
192 struct stat sb;
193 if (stat(s, &sb) == 0 && S_ISDIR(sb.st_mode)) {
194 return 1;
195 }
196 }
197#endif
198 return 0;
199}

Referenced by fp_get_files().

◆ fp_free()

void fp_free ( FilePicker filepicker)

fp_free - release all memory used by the filepicker

Parameters
*filepicker- pointer to the struct to be released

Definition at line 275 of file xfilepicker.c.

275 {
276 fp_clear_filebuffer(filepicker);
277 fp_clear_dirbuffer(filepicker);
278 free(filepicker->selected_file);
279 free(filepicker->path);
280 free(filepicker->filter);
281}
char * filter
Definition xfilepicker.h:64
char * selected_file
Definition xfilepicker.h:66
char * path
Definition xfilepicker.h:65

References FilePicker::filter, FilePicker::path, and FilePicker::selected_file.

◆ fp_get_files()

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

Parameters
*filepicker- pointer to the struct holding the list pointers
*path- the path to read from
get_dirs- 0 = only read files 1 = refill the directory buffer as well
get_files- 0 = only read directorys 1 = refill the file buffer as well
Returns
int - return the position of the given path in the directory list

Definition at line 226 of file xfilepicker.c.

226 {
227 int ret = 0;
228 fp_clear_filebuffer(filepicker);
229
230 DIR *dirp;
231 struct dirent *dp;
232 if((dirp = opendir(path)) == NULL) {
233 path =(char*)PATH_SEPARATOR;
234 dirp = opendir(PATH_SEPARATOR);
235 assert(dirp);
236 }
237
238 if(get_dirs) {
239 fp_clear_dirbuffer(filepicker);
240 ret = fp_prefill_dirbuffer(filepicker, path);
241 }
242
243 while ((dp = readdir(dirp)) != NULL) {
244
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)) ) {
248
249 filepicker->file_names = (char **)realloc(filepicker->file_names,
250 (filepicker->file_counter + 1) * sizeof(char *));
251 assert(filepicker->file_names != NULL);
252 asprintf(&filepicker->file_names[filepicker->file_counter++],"%s",dp->d_name);
253 assert(&filepicker->file_names[filepicker->file_counter-1] != NULL);
254
255 } else if(get_dirs && (is_directory(dirp, dp) || fp_is_link(dp) || (fp_check_dir(path, dp) == 1))
256 && strlen(dp->d_name)!=0 && strcmp(dp->d_name,"..")!=0 && fp_show_hidden_files(filepicker, dp->d_name)) {
257
258 if (fp_is_link(dp)) {
259 if (!fp_check_link(path, dp)) continue;
260 }
261 filepicker->file_names = (char **)realloc(filepicker->file_names,
262 (filepicker->file_counter + 1) * sizeof(char *));
263 assert(filepicker->file_names != NULL);
264 asprintf(&filepicker->file_names[filepicker->file_counter++], (strcmp(path, PATH_SEPARATOR) != 0) ?
265 "%s" PATH_SEPARATOR "%s" : "%s%s" , path,dp->d_name);
266 //asprintf(&filepicker->file_names[filepicker->file_counter++],"%s%s" , path,dp->d_name);
267 assert(&filepicker->dir_names[filepicker->file_counter-1] != NULL);
268 }
269 }
270 closedir(dirp);
271 fp_sort_buffers(filepicker, get_dirs);
272 return ret;
273}
unsigned int file_counter
Definition xfilepicker.h:69
char ** file_names
Definition xfilepicker.h:67
int fp_check_link(char *path, struct dirent *dp)
bool is_file(DIR *dirp, struct dirent *dp)
int fp_check_dir(char *path, struct dirent *dp)
int fp_is_link(struct dirent *dp)
bool is_directory(DIR *dirp, struct dirent *dp)

References asprintf(), FilePicker::dir_names, FilePicker::file_counter, FilePicker::file_names, fp_check_dir(), fp_check_link(), fp_is_link(), is_directory(), and is_file().

Referenced by open_directory_dialog(), open_file_dialog(), and save_file_dialog().

◆ fp_init()

void fp_init ( FilePicker filepicker,
const char *  path 
)

fp_init - set default values used by the filepicker

Parameters
*filepicker- pointer to the struct to alocate
*path- the path to read from

Definition at line 283 of file xfilepicker.c.

283 {
284 filepicker->file_counter=0;
285 filepicker->dir_counter=0;
286 filepicker->use_filter = 0;
287 filepicker->show_hidden = false;
288 filepicker->file_names = NULL;
289 filepicker->dir_names = NULL;
290 filepicker->selected_file = NULL;
291 filepicker->path = NULL;
292 filepicker->filter = NULL;
293#ifdef _WIN32 //file/drive functions
294 asprintf(&filepicker->path, "%s", "C:\\");
295#else
296 asprintf(&filepicker->path, "%s", path);
297#endif
298 assert(filepicker->path != NULL);
299}
bool show_hidden
Definition xfilepicker.h:72

References asprintf(), FilePicker::dir_counter, FilePicker::dir_names, FilePicker::file_counter, FilePicker::file_names, FilePicker::filter, FilePicker::path, FilePicker::selected_file, FilePicker::show_hidden, and FilePicker::use_filter.

Referenced by open_directory_dialog(), open_file_dialog(), and save_file_dialog().

◆ fp_is_link()

int fp_is_link ( struct dirent *  dp)

Definition at line 218 of file xfilepicker.c.

218 {
219#ifdef _WIN32 //not supported on win32
220 return 0;
221#else
222 return dp -> d_type == DT_LNK;
223#endif
224}

Referenced by fp_get_files().

◆ is_directory()

bool is_directory ( DIR *  dirp,
struct dirent *  dp 
)

Definition at line 131 of file xfilepicker.c.

131 {
132 return ((dirp->dd_dta.attrib & _A_SUBDIR) != 0);
133}

Referenced by fp_get_files().

◆ is_file()

bool is_file ( DIR *  dirp,
struct dirent *  dp 
)

Definition at line 127 of file xfilepicker.c.

127 {
128 return ((dirp->dd_dta.attrib & _A_SUBDIR)==0);
129}

Referenced by fp_get_files().

◆ is_root_directory()

bool is_root_directory ( char *  path)

Definition at line 103 of file xfilepicker.c.

103 {
104 return (((strlen(path)==3) && path[1] == ':' && path[2] == '\\')
105 ||((strlen(path)==1) && path[0] == '\\'));
106}