279
|
1 /*
|
|
2 * settings.c -- Configuration stuff
|
|
3 *
|
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
|
5 *
|
|
6 * This program 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 (at
|
|
9 * your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
19 * USA
|
|
20 */
|
|
21
|
|
22 #include <strings.h>
|
|
23 #include <stdlib.h>
|
|
24
|
|
25 #include "settings.h"
|
|
26
|
|
27 static GSList *option;
|
|
28 static GSList *alias;
|
|
29 static GSList *binding;
|
|
30
|
|
31
|
|
32 typedef struct {
|
|
33 gchar *name;
|
|
34 gchar *value;
|
|
35 } T_setting;
|
|
36
|
|
37 inline GSList **get_list_ptr(guint type)
|
|
38 {
|
|
39 if (type == SETTINGS_TYPE_OPTION) return &option;
|
|
40 else if (type == SETTINGS_TYPE_ALIAS) return &alias;
|
|
41 else if (type == SETTINGS_TYPE_BINDING) return &binding;
|
|
42 return NULL;
|
|
43 }
|
|
44
|
|
45 // Return a pointer to the node with the requested key, or NULL if none found
|
|
46 GSList *settings_find(GSList *list, gchar *key)
|
|
47 {
|
|
48 GSList *ptr;
|
|
49
|
|
50 if (!list) return NULL;
|
|
51
|
|
52 for (ptr = list ; ptr; ptr = g_slist_next(ptr))
|
|
53 if (!strcasecmp(key, ((T_setting*)ptr->data)->name))
|
|
54 break;
|
|
55
|
|
56 return ptr;
|
|
57 }
|
|
58
|
|
59 /* -- */
|
|
60
|
|
61 void settings_set(guint type, gchar *key, gchar *value)
|
|
62 {
|
|
63 GSList **plist;
|
|
64 GSList *sptr;
|
|
65 T_setting *setting;
|
|
66
|
|
67 plist = get_list_ptr(type);
|
|
68 if (!plist) return;
|
|
69
|
|
70 sptr = settings_find(*plist, key);
|
|
71 if (sptr) {
|
|
72 // The setting has been found. We will update it or delete it.
|
|
73 setting = (T_setting*)sptr->data;
|
|
74 if (setting->value)
|
|
75 g_free(setting->value);
|
|
76 if (!value) {
|
|
77 // Let's remove the setting
|
|
78 g_free(setting->name);
|
|
79 *plist = g_slist_delete_link(*plist, sptr);
|
|
80 } else {
|
|
81 // Let's update the setting
|
|
82 setting->value = g_strdup(value);
|
|
83 }
|
|
84 } else if (value) {
|
|
85 setting = g_new(T_setting, 1);
|
|
86 setting->name = g_strdup(key);
|
|
87 setting->value = g_strdup(value);
|
|
88 *plist = g_slist_append(*plist, setting);
|
|
89 }
|
|
90 }
|
|
91
|
|
92 void settings_del(guint type, gchar *key)
|
|
93 {
|
|
94 settings_set(type, key, NULL);
|
|
95 }
|
|
96
|
|
97 gchar *settings_get(guint type, gchar *key)
|
|
98 {
|
|
99 GSList **plist;
|
|
100 GSList *sptr;
|
|
101 T_setting *setting;
|
|
102
|
|
103 plist = get_list_ptr(type);
|
|
104 sptr = settings_find(*plist, key);
|
|
105 if (!sptr) return NULL;
|
|
106
|
|
107 setting = (T_setting*)sptr->data;
|
|
108 return setting->value;
|
|
109 }
|
|
110
|
|
111 int settings_get_int(guint type, gchar *key)
|
|
112 {
|
|
113 gchar *setval = settings_get(type, key);
|
|
114
|
|
115 if (setval) return atoi(setval);
|
|
116 return 0;
|
|
117 }
|
|
118
|