1 /*
2  *                           0BSD
3  *
4  *                    BSD Zero Clause License
5  *
6  *  Copyright (c) 2019 Hermann Meyer
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted.
10 
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 
22 /* define _GNU_SOURCE to remove dependency of xasprintf.h */
23 
24 /*
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE 1
27 #endif
28 */
29 
30 #include <dirent.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <assert.h>
38 
39 #include <libgen.h>
40 
41 #pragma once
42 
43 #ifndef XFILEPICKER_H_
44 #define XFILEPICKER_H_
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /* comment out xdgmine.h will disable the file filter, you may implement your own.*/
51 #include "xdgmime.h"
52 /* comment out xasprintf and define _GNU_SOURCE on top of this file will remove this dependency.*/
53 #include "xasprintf.h"
54 
55 #if defined(WIN32) || defined(_WIN32)
56 #define PATH_SEPARATOR "\\"
57 #else
58 #define PATH_SEPARATOR "/"
59 #endif
60 
61 typedef struct {
62     int use_filter;
63     bool show_hidden;
64     unsigned int file_counter;
65     unsigned int dir_counter;
66     char *filter;
67     char *path;
68     char *selected_file;
69     char **file_names;
70     char **dir_names;
71 } FilePicker;
72 
73 
74 /**
75  * @brief fp_get_files             - fill file_names and dir_names with the
76  * results from readdir path
77  * @param *filepicker              - pointer to the struct holding the list pointers
78  * @param *path                    - the path to read from
79  * @param get_dirs                 - 0 = only read files 1 = refill the directory buffer as well
80  * @return int                     - return the position of the given path in the directory list
81  */
82 
83 int fp_get_files(FilePicker *filepicker, char *path, int get_dirs);
84 
85 /**
86  * @brief fp_free                  - release all memory used by the filepicker
87  * @param *filepicker              - pointer to the struct to be released
88  * @return void
89  */
90 
91 void fp_free(FilePicker *filepicker);
92 
93 /**
94  * @brief fp_init                  - set default values used by the filepicker
95  * @param *filepicker              - pointer to the struct to allocate
96  * @param *path                    - the path to read from
97  * @return void
98  */
99 
100 void fp_init(FilePicker *filepicker, const char *path);
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif //XFILEPICKER_H_
107