eolian: rename is_ref API to is_ptr to match syntax
[platform/upstream/efl.git] / src / lib / ecore_x / xcb / ecore_xcb_xdefaults.c
1 #include "ecore_xcb_private.h"
2 #include <fnmatch.h>
3 #include <sys/types.h>
4 #include <pwd.h>
5
6 /* local function prototypes */
7 static Eina_Bool _ecore_xcb_xdefaults_glob_match(const char *str,
8                                                  const char *glob);
9
10 /* local variables */
11 static Eina_File *_ecore_xcb_xdefaults_file = NULL;
12 static char *_ecore_xcb_xdefaults_data = NULL;
13
14 void
15 _ecore_xcb_xdefaults_init(void)
16 {
17    char buff[PATH_MAX];
18
19    LOGFN(__FILE__, __LINE__, __FUNCTION__);
20
21    if (eina_environment_home_get())
22      snprintf(buff, sizeof(buff), "%s/.Xdefaults", eina_environment_home_get());
23    else return;
24    if ((_ecore_xcb_xdefaults_file = eina_file_open(buff, EINA_FALSE)))
25      {
26         eina_mmap_safety_enabled_set(EINA_TRUE);
27
28         _ecore_xcb_xdefaults_data =
29           eina_file_map_all(_ecore_xcb_xdefaults_file, EINA_FILE_SEQUENTIAL);
30      }
31 }
32
33 void
34 _ecore_xcb_xdefaults_shutdown(void)
35 {
36    LOGFN(__FILE__, __LINE__, __FUNCTION__);
37
38    if (!_ecore_xcb_xdefaults_file) return;
39    if (_ecore_xcb_xdefaults_data)
40      eina_file_map_free(_ecore_xcb_xdefaults_file, _ecore_xcb_xdefaults_data);
41    if (_ecore_xcb_xdefaults_file) eina_file_close(_ecore_xcb_xdefaults_file);
42 }
43
44 char *
45 _ecore_xcb_xdefaults_string_get(const char *prog,
46                                 const char *param)
47 {
48    char buff[1024], ret[1024];
49    char *str = NULL;
50    char **ea = NULL;
51    unsigned int count = 0, i = 0;
52
53    if ((!_ecore_xcb_xdefaults_data) || (!_ecore_xcb_xdefaults_file))
54      return NULL;
55
56    snprintf(buff, sizeof(buff), "*%s*.*%s*", prog, param);
57
58    str = _ecore_xcb_xdefaults_data;
59    ea = eina_str_split_full(str, "\n", -1, &count);
60    for (i = 0; i < count; i++)
61      {
62         if (_ecore_xcb_xdefaults_glob_match(ea[i], buff))
63           sscanf(ea[i], "%*[^:]:%*[ ]%s", ret);
64      }
65    if ((ea) && (ea[0]))
66      {
67         free(ea[0]);
68         free(ea);
69      }
70
71    return strdup(ret);
72 }
73
74 int
75 _ecore_xcb_xdefaults_int_get(const char *prog,
76                              const char *param)
77 {
78    char buff[1024];
79    char *str = NULL;
80    char **ea = NULL;
81    unsigned int count = 0, i = 0;
82    int ret = -1;
83
84    if ((!_ecore_xcb_xdefaults_data) || (!_ecore_xcb_xdefaults_file))
85      return 0;
86
87    snprintf(buff, sizeof(buff), "*%s*.*%s*", prog, param);
88
89    str = _ecore_xcb_xdefaults_data;
90    ea = eina_str_split_full(str, "\n", -1, &count);
91    for (i = 0; i < count; i++)
92      {
93         if (_ecore_xcb_xdefaults_glob_match(ea[i], buff))
94           sscanf(ea[i], "%*[^:]:%*[ ]%d", &ret);
95      }
96    if ((ea) && (ea[0]))
97      {
98         free(ea[0]);
99         free(ea);
100      }
101
102    return ret;
103 }
104
105 /* local functions */
106 static Eina_Bool
107 _ecore_xcb_xdefaults_glob_match(const char *str,
108                                 const char *glob)
109 {
110    if ((!str) || (!glob)) return EINA_FALSE;
111    if (glob[0] == 0)
112      {
113         if (str[0] == 0) return EINA_TRUE;
114         return EINA_FALSE;
115      }
116    if (!strcmp(glob, "*")) return EINA_TRUE;
117    if (!fnmatch(glob, str, 0)) return EINA_TRUE;
118    return EINA_FALSE;
119 }
120