Mercurial > hg
annotate mcabber/src/settings.c @ 284:f879b17ecb8e
Add compl_del_category_word()
This will allow us to remove an alias completion when it is unset.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Thu, 07 Jul 2005 22:12:29 +0100 |
parents | 87d6ac21cd1b |
children | 1eea0fa0955e |
rev | line source |
---|---|
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> | |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
24 #include <ctype.h> |
279 | 25 |
26 #include "settings.h" | |
27 | |
28 static GSList *option; | |
29 static GSList *alias; | |
30 static GSList *binding; | |
31 | |
32 | |
33 typedef struct { | |
34 gchar *name; | |
35 gchar *value; | |
36 } T_setting; | |
37 | |
38 inline GSList **get_list_ptr(guint type) | |
39 { | |
40 if (type == SETTINGS_TYPE_OPTION) return &option; | |
41 else if (type == SETTINGS_TYPE_ALIAS) return &alias; | |
42 else if (type == SETTINGS_TYPE_BINDING) return &binding; | |
43 return NULL; | |
44 } | |
45 | |
46 // Return a pointer to the node with the requested key, or NULL if none found | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
47 GSList *settings_find(GSList *list, const gchar *key) |
279 | 48 { |
49 GSList *ptr; | |
50 | |
51 if (!list) return NULL; | |
52 | |
53 for (ptr = list ; ptr; ptr = g_slist_next(ptr)) | |
54 if (!strcasecmp(key, ((T_setting*)ptr->data)->name)) | |
55 break; | |
56 | |
57 return ptr; | |
58 } | |
59 | |
60 /* -- */ | |
61 | |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
62 // parse_assigment(assignment, pkey, pval) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
63 // Read assignment and split it to key, value |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
64 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
65 // If this is an assignment, the function will return TRUE and |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
66 // set *pkey and *pval (*pval is set to NULL if value field is empty). |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
67 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
68 // If this isn't a assignment (no = char), the function will set *pval |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
69 // to NULL and return FALSE. |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
70 // |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
71 // The called should g_free() *pkey and *pval (if not NULL) after use. |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
72 guint parse_assigment(gchar *assignment, const gchar **pkey, const gchar **pval) |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
73 { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
74 char *key, *val, *t; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
75 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
76 *pkey = *pval = NULL; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
77 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
78 key = assignment; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
79 // Remove leading spaces in option name |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
80 while ((!isalnum(*key)) && (*key != '=') && *key) { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
81 //if (!isblank(*key)) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
82 // scr_LogPrint("Error in setting parsing!\n"); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
83 key++; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
84 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
85 if (!*key) return FALSE; // Empty assignment |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
86 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
87 if (*key == '=') { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
88 //scr_LogPrint("Cannot parse setting!\n"); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
89 return FALSE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
90 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
91 // Ok, key points to the option name |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
92 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
93 for (val = key+1 ; *val && (*val != '=') ; val++) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
94 if (!isalnum(*val) && !isblank(*val) && (*val != '_') && (*val != '-')) { |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
95 // Key should only have alnum chars... |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
96 //scr_LogPrint("Error in setting parsing!\n"); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
97 return FALSE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
98 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
99 // Remove trailing spaces in option name: |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
100 for (t = val-1 ; t > key && isblank(*t) ; t--) |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
101 ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
102 *pkey = g_strndup(key, t+1-key); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
103 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
104 if (!*val) return FALSE; // Not an assignment |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
105 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
106 // Remove leading and trailing spaces in option value: |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
107 for (val++; *val && isblank(*val) ; val++) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
108 for (t = val ; *t ; t++) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
109 for (t-- ; t >= val && isblank(*t) ; t--) ; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
110 |
282
87d6ac21cd1b
Fix "Cannot unset option" bug
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 if (t < val) return TRUE; // no value (variable reset for example) |
280
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
112 |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
113 *pval = g_strndup(val, t+1-val); |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
114 return TRUE; |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
115 } |
68ce34b4243b
Add parse_assigment() function
Mikael Berthe <mikael@lilotux.net>
parents:
279
diff
changeset
|
116 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
117 void settings_set(guint type, const gchar *key, const gchar *value) |
279 | 118 { |
119 GSList **plist; | |
120 GSList *sptr; | |
121 T_setting *setting; | |
122 | |
123 plist = get_list_ptr(type); | |
124 if (!plist) return; | |
125 | |
126 sptr = settings_find(*plist, key); | |
127 if (sptr) { | |
128 // The setting has been found. We will update it or delete it. | |
129 setting = (T_setting*)sptr->data; | |
130 if (setting->value) | |
131 g_free(setting->value); | |
132 if (!value) { | |
133 // Let's remove the setting | |
134 g_free(setting->name); | |
135 *plist = g_slist_delete_link(*plist, sptr); | |
136 } else { | |
137 // Let's update the setting | |
138 setting->value = g_strdup(value); | |
139 } | |
140 } else if (value) { | |
141 setting = g_new(T_setting, 1); | |
142 setting->name = g_strdup(key); | |
143 setting->value = g_strdup(value); | |
144 *plist = g_slist_append(*plist, setting); | |
145 } | |
146 } | |
147 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
148 void settings_del(guint type, const gchar *key) |
279 | 149 { |
150 settings_set(type, key, NULL); | |
151 } | |
152 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
153 const gchar *settings_get(guint type, const gchar *key) |
279 | 154 { |
155 GSList **plist; | |
156 GSList *sptr; | |
157 T_setting *setting; | |
158 | |
159 plist = get_list_ptr(type); | |
160 sptr = settings_find(*plist, key); | |
161 if (!sptr) return NULL; | |
162 | |
163 setting = (T_setting*)sptr->data; | |
164 return setting->value; | |
165 } | |
166 | |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
167 int settings_get_int(guint type, const gchar *key) |
279 | 168 { |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
280
diff
changeset
|
169 const gchar *setval = settings_get(type, key); |
279 | 170 |
171 if (setval) return atoi(setval); | |
172 return 0; | |
173 } | |
174 |