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