1 /*
2 * Copyright (C) 2002 2003 2004 2005 2008, Magnus Hjorth
3 *
4 * This file is part of mhWaveEdit.
5 *
6 * mhWaveEdit is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * mhWaveEdit is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with mhWaveEdit; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <sys/stat.h>
26 #include "main.h"
27 #include "gtkfiles.h"
28 #include "inifile.h"
29 #include "gettext.h"
30
31 static GHashTable *settings;
32 static gchar *ininame;
33 static gboolean settings_changed = FALSE;
34
35 struct inifile_setting {
36 gchar *name;
37 gchar *value;
38 };
39
xisspace(gchar c)40 static gboolean xisspace(gchar c)
41 {
42 return (c==' ' || c=='\t');
43 }
44
skipspace(gchar * c)45 static gchar *skipspace(gchar *c)
46 {
47 while (xisspace(*c)) c++;
48 return c;
49 }
50
inifile_init(void)51 void inifile_init(void)
52 {
53 EFILE *f;
54 gchar *c,*d,*namestart,*valuestart;
55 size_t s = 0;
56 int x;
57 settings = g_hash_table_new(g_str_hash,g_str_equal);
58 c = g_strjoin(NULL,get_home_directory(),"/.mhwaveedit",NULL);
59 mkdir(c,CONFDIR_PERMISSION);
60 g_free(c);
61 ininame = g_strjoin(NULL,get_home_directory(),"/.mhwaveedit/config",NULL);
62 if (!file_exists(ininame)) return;
63 f = e_fopen(ininame,EFILE_READ);
64 if (!f) return;
65 c = NULL;
66 while (1) {
67 x = e_readline(&c,&s,f);
68 if (x <= 0) break;
69
70 /* Ta bort kommentar och avslutande space*/
71 d = strchr(c,'#');
72 if (!d) d=strchr(c,0);
73 while (d>c && xisspace(*(d-1))) d--;
74 *d = 0;
75
76 /* Skippa inledande space */
77 d = skipspace(c);
78
79 if (*d == 0) continue;
80
81 namestart = d;
82 while (*d!=0 && *d!=' ' && *d!='\t' && *d!='=') d++;
83 if (*d == 0) {
84 g_printerr(_("%s: Expected '=': %s\n"),ininame,c);
85 break;
86 }
87 *d = 0;
88 d++;
89 d = skipspace(d);
90 if (*d!='=') {
91 g_printerr(_("%s: Expected '=': %s\n"),ininame,c);
92 break;
93 }
94 d++;
95 d = skipspace(d);
96 if (*d==0) {
97 g_printerr(_("%s: Expected value: %s\n"),ininame,c);
98 break;
99 }
100 valuestart = d;
101 g_hash_table_insert(settings,g_strdup(namestart),
102 g_strdup(valuestart));
103 }
104 e_fclose(f);
105 g_free(c);
106 }
107
inifile_get(gchar * setting,gchar * defaultValue)108 gchar *inifile_get(gchar *setting, gchar *defaultValue)
109 {
110 gchar *c;
111 c = g_hash_table_lookup(settings, setting);
112 if (c == NULL) return defaultValue;
113 return c;
114 }
115
inifile_get_guint32(gchar * setting,guint32 defaultValue)116 guint32 inifile_get_guint32(gchar *setting, guint32 defaultValue)
117 {
118 gchar *c,*d;
119 guint32 ui;
120 c = inifile_get(setting, NULL);
121 if (c) {
122 ui = strtoul(c,&d,10);
123 if (*d==0) return ui;
124 inifile_set_guint32(setting,defaultValue);
125 }
126 return defaultValue;
127 }
128
inifile_get_gint32(gchar * setting,gint32 defaultValue)129 gint32 inifile_get_gint32(gchar *setting, gint32 defaultValue)
130 {
131 gchar *c,*d;
132 gint32 ui;
133 c = inifile_get(setting, NULL);
134 if (c) {
135 ui = strtol(c,&d,10);
136 if (*d==0) return ui;
137 inifile_set_gint32(setting,defaultValue);
138 }
139 return defaultValue;
140 }
141
inifile_get_gfloat(gchar * setting,gfloat defaultValue)142 gfloat inifile_get_gfloat(gchar *setting, gfloat defaultValue)
143 {
144 gchar *c,*d;
145 double x;
146 c = inifile_get(setting,NULL);
147 if (c) {
148 x = strtod(c,&d);
149 if (*d==0) return (gfloat)x;
150 inifile_set_gfloat(setting,defaultValue);
151 }
152 return defaultValue;
153 }
154
inifile_get_gboolean(gchar * setting,gboolean defaultValue)155 gboolean inifile_get_gboolean(gchar *setting, gboolean defaultValue)
156 {
157 gchar *c;
158 c = inifile_get(setting, NULL);
159 // printf("inifile_get_gboolean: c == %s\n",c);
160 if (c) {
161 if ( !g_strcasecmp(c,"y") || !strcmp(c,"1") ||
162 !g_strcasecmp(c,"yes") || !g_strcasecmp(c,"on") ||
163 !g_strcasecmp(c,"true") || !g_strcasecmp(c,"enabled"))
164 return TRUE;
165 if (!g_strcasecmp(c,"n") || !strcmp(c,"0") ||
166 !g_strcasecmp(c,"no") || !g_strcasecmp(c,"off") ||
167 !g_strcasecmp(c,"false") || !g_strcasecmp(c,"disabled"))
168 return FALSE;
169 inifile_set_gboolean(setting,defaultValue);
170 }
171 return defaultValue;
172 }
173
inifile_set(gchar * setting,gchar * value)174 gboolean inifile_set(gchar *setting, gchar *value)
175 {
176 gboolean b;
177 gpointer c,d;
178
179 b = g_hash_table_lookup_extended(settings,setting,&c,&d);
180 if (!b && value == NULL) return FALSE;
181 if (b && value != NULL && !strcmp(d,value)) return FALSE;
182
183 /* printf("key: '%s', from: '%s', to: '%s'\n",setting,b?d:"(none)",value);
184 */
185
186 if (b) {
187 g_hash_table_remove(settings,c);
188 g_free(c);
189 g_free(d);
190 }
191
192 if (value != NULL)
193 g_hash_table_insert(settings,g_strdup(setting),g_strdup(value));
194 settings_changed = TRUE;
195 return TRUE;
196 }
197
inifile_set_guint32(gchar * setting,guint32 value)198 gboolean inifile_set_guint32(gchar *setting, guint32 value)
199 {
200 gchar c[32];
201 g_snprintf(c,sizeof(c),"%lu",(unsigned long int)value);
202 return inifile_set(setting,c);
203 }
204
inifile_set_gint32(gchar * setting,gint32 value)205 gboolean inifile_set_gint32(gchar *setting, gint32 value)
206 {
207 gchar c[32];
208 g_snprintf(c,sizeof(c),"%ld",(signed long int)value);
209 return inifile_set(setting,c);
210 }
211
inifile_set_gboolean(gchar * setting,gboolean value)212 gboolean inifile_set_gboolean(gchar *setting, gboolean value)
213 {
214 return inifile_set(setting,value?"true":"false");
215 }
216
inifile_set_gfloat(gchar * setting,gfloat value)217 gboolean inifile_set_gfloat(gchar *setting, gfloat value)
218 {
219 gchar c[128];
220 g_snprintf(c,sizeof(c),"%f",(float)value);
221 return inifile_set(setting,c);
222 }
223
do_save_setting(gchar * setting,gchar * value,EFILE * f)224 static void do_save_setting(gchar *setting, gchar *value, EFILE *f)
225 {
226 e_fwrite(setting,strlen(setting),f);
227 e_fwrite(" = ",3,f);
228 e_fwrite(value,strlen(value),f);
229 e_fwrite("\n",1,f);
230 }
231
inifile_save(void)232 void inifile_save(void)
233 {
234 EFILE *f = e_fopen(ininame,EFILE_WRITE);
235 char *c =
236 "# mhWaveEdit configuration file.\n"
237 "# Automatically generated by " PROGRAM_VERSION_STRING "\n"
238 "# May be hand edited but extra comments will be removed when the \n"
239 "# settings are saved.\n"
240 "# Remove this file to restore default settings.\n"
241 "\n";
242 if (f == NULL) return;
243 if (e_fwrite(c,strlen(c),f)) return;
244 g_hash_table_foreach(settings,(GHFunc)do_save_setting,f);
245 e_fclose(f);
246 settings_changed = FALSE;
247 }
248
do_quit(gchar * setting,gchar * value)249 static void do_quit(gchar *setting, gchar *value)
250 {
251 g_free(setting);
252 g_free(value);
253 }
254
inifile_quit(void)255 void inifile_quit(void)
256 {
257 if (settings_changed) inifile_save();
258 g_hash_table_foreach(settings,(GHFunc)do_quit,NULL);
259 g_hash_table_destroy(settings);
260 }
261