wmclockmon: build against GTK+ 2.0.
[dockapps.git] / wmclockmon / wmclockmon-config / tools.c
blob74a5cae80d77bf449500246582fc432afa057e60
1 /*
2 * Tools : memory management, file loading and saving
3 */
5 #include "../config.h"
6 #include "defines.h"
7 #include "variables.h"
8 #include "tools.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <sys/types.h>
14 #include <ctype.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include "configfile.h"
21 int fexist(const char *filename) {
22 FILE *file;
24 if ( (file = fopen(filename, "r") ) == NULL) return FALSE;
25 fclose(file);
27 return TRUE;
31 void alrm_add(Alarm **list, const char *value) {
32 Alarm *lst = *list;
33 int ok = TRUE;
34 char *time = NULL, *date = NULL, *ison = NULL, *mesg = NULL, *at;
35 char *tokstr = xstrdup(value);
36 char *toksav = tokstr;
38 if (! value) return;
39 if (! lst) {
40 lst = xmalloc(sizeof(Alarm));
41 *list = lst;
42 } else {
43 if (strcmp(value, lst->entry) == 0) ok = FALSE;
44 while ( (lst->next) && ok) {
45 lst = lst->next;
46 if (strcmp(value, lst->entry) == 0) ok = FALSE;
48 if (! ok) return;
49 lst->next = xmalloc(sizeof(Alarm));
50 lst = lst->next;
52 at = strchr(value, '@');
53 if (at) ison = strtok(tokstr, "@");
54 time = strtok(at ? NULL : tokstr, "-.");
55 if (strchr(value, '-')) date = strtok(NULL, ".");
56 mesg = strtok(NULL, "\n\0");
57 lst->entry = xstrdup(value);
58 lst->time = time ? xstrdup(time) : NULL;
59 lst->date = date ? xstrdup(date) : NULL;
60 lst->on = ison ? getbool(ison) : TRUE;
61 lst->msg = mesg ? xstrdup(mesg) : NULL;
62 lst->next = NULL;
63 FREE(toksav);
67 void free_alrm(Alarm **list) {
68 Alarm *lst = *list, *next;
69 while (lst) {
70 next = lst->next;
71 FREE(lst->entry);
72 FREE(lst->time);
73 FREE(lst->date);
74 FREE(lst->msg);
75 free(lst);
76 lst = next;
78 *list = NULL;
82 int nb_alrm(Alarm *list) {
83 Alarm *alrm = list;
84 int n = 0;
85 while (alrm) {
86 n++;
87 alrm = alrm->next;
89 return n;
95 void *xmalloc(size_t size) {
96 void *ret = malloc(size);
97 if (ret == NULL) {
98 perror("malloc() ");
99 exit(-1);
100 } else
101 return ret;
105 char *xstrdup(const char *string) {
106 char *ret = strdup(string);
107 if (ret == NULL) {
108 perror("strdup() ");
109 exit(-1);
110 } else
111 return ret;
115 int getbool(char *value) {
116 int i;
117 for (i = 0 ; value[i] ; i++) value[i] = tolower(value[i]);
118 if (strcmp(value, "0") == 0) return FALSE;
119 if (strcmp(value, "1") == 0) return TRUE;
120 if (strcmp(value, "true") == 0) return TRUE;
121 if (strcmp(value, "false") == 0) return FALSE;
122 if (strcmp(value, "yes") == 0) return TRUE;
123 if (strcmp(value, "no") == 0) return FALSE;
124 if (strcmp(value, "on") == 0) return TRUE;
125 if (strcmp(value, "off") == 0) return FALSE;
126 printf("Error in converting \"%s\" to boolean value.\n", value);
127 return FALSE;
131 void load_cfgfile() {
132 FILE *file;
133 int i = 0;
134 char line[MAXSTRLEN + 1];
135 char *value;
137 if ((file = fopen(config_file, "r")) == NULL) {
138 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
139 printf("Unable to open configuration file \"%s\".\n", config_file);
140 return;
142 while (! feof(file)) {
143 memset(line, 0, MAXSTRLEN + 1);
144 fgets(line, MAXSTRLEN, file);
145 i++;
146 if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = 0;
147 if ((line[0] == '#') || (line[0] == 0)) continue;
148 value = strchr(line, '=') + 1;
149 while ((value[0] == ' ') && (value[0] != 0)) value++;
150 if (value[0] == 0) continue;
152 if (strncmp(line, "Backlight", 9) == 0)
153 backlight = getbool(value);
154 else if (strncmp(line, "Color", 5) == 0)
155 light_color = xstrdup(value);
156 else if (strncmp(line, "Alarm", 5) == 0)
157 alrm_add(&alarms, value);
158 else if (strncmp(line, "Command", 7) == 0)
159 command = xstrdup(value);
160 else if (strncmp(line, "MessageCmd", 10) == 0)
161 msgcmd = xstrdup(value);
162 else if (strncmp(line, "Blink", 5) == 0)
163 switch_authorized = getbool(value);
164 else if (strncmp(line, "H12", 3) == 0)
165 h12 = getbool(value);
166 else if (strncmp(line, "Locale", 6) == 0)
167 use_locale = getbool(value);
168 else if (strncmp(line, "StyleDir", 8) == 0)
169 style_dir = xstrdup(value);
170 else if (strncmp(line, "Style", 5) == 0)
171 style_name = xstrdup(value);
172 else if (strncmp(line, "TimeMode", 5) == 0)
173 time_mode = atoi(value);
174 else if (strncmp(line, "ShowCal", 7) == 0)
175 showcal = getbool(value);
176 else if (strncmp(line, "CalAlrms", 8) == 0)
177 calalrms = getbool(value);
178 else
179 printf("Error in %s at line %d :\n[%s].\n", config_file, i, line);
184 char *robust_home() {
185 if (getenv("HOME"))
186 return getenv("HOME");
187 else if (getenv("USER") && getpwnam(getenv("USER")))
188 return getpwnam (getenv ("USER") )->pw_dir;
189 else if (getenv ("LOGNAME") && getpwnam(getenv("LOGNAME")))
190 return getpwnam(getenv("LOGNAME"))->pw_dir;
191 else if ((getuid() != -1) && (getpwuid(getuid())))
192 return getpwuid(getuid())->pw_dir;
193 else
194 return "/";
198 void save_cfgfile() {
199 FILE *file;
200 Alarm *alrm = alarms;
202 if ((file = fopen(config_file, "w")) == NULL) {
203 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
204 printf("Unable to open configuration file \"%s\".\n", config_file);
205 return;
208 fprintf(file, configfile,
209 backlight ? "On" : "Off",
210 light_color ? light_color : "", /*"#6ec63b",*/
211 command ? command : "",
212 msgcmd ? msgcmd : "",
213 switch_authorized ? "Yes" : "No",
214 h12 ? "True" : "False",
215 time_mode,
216 use_locale ? "Yes" : "No",
217 style_dir ? style_dir : "",
218 style_name ? style_name : "",
219 showcal ? "Yes" : "No",
220 calalrms ? "On" : "Off");
221 while (alrm) {
222 fprintf(file, alarmline,
223 alrm->on ? "On" : "Off",
224 alrm->time ? alrm->time : "12:00",
225 alrm->date ? "-" : "",
226 alrm->date ? alrm->date : "",
227 alrm->msg ? "." : "",
228 alrm->msg ? alrm->msg : "");
229 alrm = alrm->next;
231 fclose(file);