]> sjero.net Git - linphone/blob - linphone/coreapi/lpconfig.c
remove mediastreamer2 and add it as a submodule instead.
[linphone] / linphone / coreapi / lpconfig.c
1 /***************************************************************************
2  *            lpconfig.c
3  *
4  *  Thu Mar 10 11:13:44 2005
5  *  Copyright  2005  Simon Morlat
6  *  Email simon.morlat@linphone.org
7  ****************************************************************************/
8
9 /*
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Library General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24
25 #define MAX_LEN 1024
26
27 #include "linphonecore.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #if !defined(_WIN32_WCE)
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #endif /*_WIN32_WCE*/
38
39
40
41 #define lp_new0(type,n) (type*)calloc(sizeof(type),n)
42
43 #include "lpconfig.h"
44
45
46 typedef struct _LpItem{
47         char *key;
48         char *value;
49 } LpItem;
50
51 typedef struct _LpSection{
52         char *name;
53         MSList *items;
54 } LpSection;
55
56 struct _LpConfig{
57         FILE *file;
58         char *filename;
59         MSList *sections;
60         int modified;
61         int readonly;
62 };
63
64 LpItem * lp_item_new(const char *key, const char *value){
65         LpItem *item=lp_new0(LpItem,1);
66         item->key=ortp_strdup(key);
67         item->value=ortp_strdup(value);
68         return item;
69 }
70
71 LpSection *lp_section_new(const char *name){
72         LpSection *sec=lp_new0(LpSection,1);
73         sec->name=ortp_strdup(name);
74         return sec;
75 }
76
77 void lp_item_destroy(void *pitem){
78         LpItem *item=(LpItem*)pitem;
79         free(item->key);
80         free(item->value);
81         free(item);
82 }
83
84 void lp_section_destroy(LpSection *sec){
85         free(sec->name);
86         ms_list_for_each(sec->items,lp_item_destroy);
87         ms_list_free(sec->items);
88         free(sec);
89 }
90
91 void lp_section_add_item(LpSection *sec,LpItem *item){
92         sec->items=ms_list_append(sec->items,(void *)item);
93 }
94
95 void lp_config_add_section(LpConfig *lpconfig, LpSection *section){
96         lpconfig->sections=ms_list_append(lpconfig->sections,(void *)section);
97 }
98
99 void lp_config_remove_section(LpConfig *lpconfig, LpSection *section){
100         lpconfig->sections=ms_list_remove(lpconfig->sections,(void *)section);
101         lp_section_destroy(section);
102 }
103
104 static bool_t is_first_char(const char *start, const char *pos){
105         const char *p;
106         for(p=start;p<pos;p++){
107                 if (*p!=' ') return FALSE;
108         }
109         return TRUE;
110 }
111
112 LpSection *lp_config_find_section(LpConfig *lpconfig, const char *name){
113         LpSection *sec;
114         MSList *elem;
115         /*printf("Looking for section %s\n",name);*/
116         for (elem=lpconfig->sections;elem!=NULL;elem=ms_list_next(elem)){
117                 sec=(LpSection*)elem->data;
118                 if (strcmp(sec->name,name)==0){
119                         /*printf("Section %s found\n",name);*/
120                         return sec;
121                 }
122         }
123         return NULL;
124 }
125
126 LpItem *lp_section_find_item(LpSection *sec, const char *name){
127         MSList *elem;
128         LpItem *item;
129         /*printf("Looking for item %s\n",name);*/
130         for (elem=sec->items;elem!=NULL;elem=ms_list_next(elem)){
131                 item=(LpItem*)elem->data;
132                 if (strcmp(item->key,name)==0) {
133                         /*printf("Item %s found\n",name);*/
134                         return item;
135                 }
136         }
137         return NULL;
138 }
139
140 void lp_config_parse(LpConfig *lpconfig, FILE *file){
141         char tmp[MAX_LEN];
142         LpSection *cur=NULL;
143
144         if (file==NULL) return;
145
146         while(fgets(tmp,MAX_LEN,file)!=NULL){
147                 char *pos1,*pos2;
148                 pos1=strchr(tmp,'[');
149                 if (pos1!=NULL && is_first_char(tmp,pos1) ){
150                         pos2=strchr(pos1,']');
151                         if (pos2!=NULL){
152                                 int nbs;
153                                 char secname[MAX_LEN];
154                                 secname[0]='\0';
155                                 /* found section */
156                                 *pos2='\0';
157                                 nbs = sscanf(pos1+1,"%s",secname);
158                                 if (nbs == 1 ){
159                                         if (strlen(secname)>0){
160                                                 cur=lp_config_find_section (lpconfig,secname);
161                                                 if (cur==NULL){
162                                                         cur=lp_section_new(secname);
163                                                         lp_config_add_section(lpconfig,cur);
164                                                 }
165                                         }
166                                 }else{
167                                         ms_warning("parse error!");
168                                 }
169                         }
170                 }else {
171                         pos1=strchr(tmp,'=');
172                         if (pos1!=NULL){
173                                 char key[MAX_LEN];
174                                 key[0]='\0';
175
176                                 *pos1='\0';
177                                 if (sscanf(tmp,"%s",key)>0){
178
179                                         pos1++;
180                                         pos2=strchr(pos1,'\n');
181                                         if (pos2==NULL) pos2=pos1+strlen(pos1);
182                                         else {
183                                                 *pos2='\0'; /*replace the '\n' */
184                                                 pos2--;
185                                         }
186                                         /* remove ending white spaces */
187                                         for (; pos2>pos1 && *pos2==' ';pos2--) *pos2='\0';
188                                         if (pos2-pos1>=0){
189                                                 /* found a pair key,value */
190                                                 if (cur!=NULL){
191                                                         LpItem *item=lp_section_find_item(cur,key);
192                                                         if (item==NULL){
193                                                                 lp_section_add_item(cur,lp_item_new(key,pos1));
194                                                         }else{
195                                                                 ms_free(item->value);
196                                                                 item->value=strdup(pos1);
197                                                         }
198                                                         /*printf("Found %s %s=%s\n",cur->name,key,pos1);*/
199                                                 }else{
200                                                         ms_warning("found key,item but no sections");
201                                                 }
202                                         }
203                                 }
204                         }
205                 }
206         }
207 }
208
209 LpConfig * lp_config_new(const char *filename){
210         LpConfig *lpconfig=lp_new0(LpConfig,1);
211         if (filename!=NULL){
212                 lpconfig->filename=ortp_strdup(filename);
213                 lpconfig->file=fopen(filename,"rw");
214                 if (lpconfig->file!=NULL){
215                         lp_config_parse(lpconfig,lpconfig->file);
216                         fclose(lpconfig->file);                 
217 #if !defined(_WIN32_WCE)
218                         /* make existing configuration files non-group/world-accessible */
219                         if (chmod(filename, S_IRUSR | S_IWUSR) == -1)
220                                 ms_warning("unable to correct permissions on "
221                                           "configuration file: %s",
222                                            strerror(errno));
223 #endif /*_WIN32_WCE*/
224                         lpconfig->file=NULL;
225                         lpconfig->modified=0;
226                 }
227         }
228         return lpconfig;
229 }
230
231 int lp_config_read_file(LpConfig *lpconfig, const char *filename){
232         FILE* f=fopen(filename,"r");
233         if (f!=NULL){
234                 lp_config_parse(lpconfig,f);
235                 fclose(f);
236                 return 0;
237         }
238         ms_warning("Fail to open file %s",filename);
239         return -1;
240 }
241
242 void lp_item_set_value(LpItem *item, const char *value){
243         free(item->value);
244         item->value=ortp_strdup(value);
245 }
246
247
248 void lp_config_destroy(LpConfig *lpconfig){
249         if (lpconfig->filename!=NULL) free(lpconfig->filename);
250         ms_list_for_each(lpconfig->sections,(void (*)(void*))lp_section_destroy);
251         ms_list_free(lpconfig->sections);
252         free(lpconfig);
253 }
254
255 void lp_section_remove_item(LpSection *sec, LpItem *item){
256         sec->items=ms_list_remove(sec->items,(void *)item);
257         lp_item_destroy(item);
258 }
259
260 const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const char *key, const char *default_string){
261         LpSection *sec;
262         LpItem *item;
263         sec=lp_config_find_section(lpconfig,section);
264         if (sec!=NULL){
265                 item=lp_section_find_item(sec,key);
266                 if (item!=NULL) return item->value;
267         }
268         return default_string;
269 }
270
271 int lp_config_get_int(LpConfig *lpconfig,const char *section, const char *key, int default_value){
272         const char *str=lp_config_get_string(lpconfig,section,key,NULL);
273         if (str!=NULL) return atoi(str);
274         else return default_value;
275 }
276
277 float lp_config_get_float(LpConfig *lpconfig,const char *section, const char *key, float default_value){
278         const char *str=lp_config_get_string(lpconfig,section,key,NULL);
279         float ret=default_value;
280         if (str==NULL) return default_value;
281         sscanf(str,"%f",&ret);
282         return ret;
283 }
284
285 void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *key, const char *value){
286         LpItem *item;
287         LpSection *sec=lp_config_find_section(lpconfig,section);
288         if (sec!=NULL){
289                 item=lp_section_find_item(sec,key);
290                 if (item!=NULL){
291                         if (value!=NULL)
292                                 lp_item_set_value(item,value);
293                         else lp_section_remove_item(sec,item);
294                 }else{
295                         if (value!=NULL)
296                                 lp_section_add_item(sec,lp_item_new(key,value));
297                 }
298         }else if (value!=NULL){
299                 sec=lp_section_new(section);
300                 lp_config_add_section(lpconfig,sec);
301                 lp_section_add_item(sec,lp_item_new(key,value));
302         }
303         lpconfig->modified++;
304 }
305
306 void lp_config_set_int(LpConfig *lpconfig,const char *section, const char *key, int value){
307         char tmp[30];
308         snprintf(tmp,30,"%i",value);
309         lp_config_set_string(lpconfig,section,key,tmp);
310         lpconfig->modified++;
311 }
312
313 void lp_item_write(LpItem *item, FILE *file){
314         fprintf(file,"%s=%s\n",item->key,item->value);
315 }
316
317 void lp_section_write(LpSection *sec, FILE *file){
318         fprintf(file,"[%s]\n",sec->name);
319         ms_list_for_each2(sec->items,(void (*)(void*, void*))lp_item_write,(void *)file);
320         fprintf(file,"\n");
321 }
322
323 int lp_config_sync(LpConfig *lpconfig){
324         FILE *file;
325         if (lpconfig->filename==NULL) return -1;
326         if (lpconfig->readonly) return 0;
327 #ifndef WIN32
328         /* don't create group/world-accessible files */
329         (void) umask(S_IRWXG | S_IRWXO);
330 #endif
331         file=fopen(lpconfig->filename,"w");
332         if (file==NULL){
333                 ms_warning("Could not write %s ! Maybe it is read-only. Configuration will not be saved.",lpconfig->filename);
334                 lpconfig->readonly=1;
335                 return -1;
336         }
337         ms_list_for_each2(lpconfig->sections,(void (*)(void *,void*))lp_section_write,(void *)file);
338         fclose(file);
339         lpconfig->modified=0;
340         return 0;
341 }
342
343 int lp_config_has_section(LpConfig *lpconfig, const char *section){
344         if (lp_config_find_section(lpconfig,section)!=NULL) return 1;
345         return 0;
346 }
347
348 void lp_config_clean_section(LpConfig *lpconfig, const char *section){
349         LpSection *sec=lp_config_find_section(lpconfig,section);
350         if (sec!=NULL){
351                 lp_config_remove_section(lpconfig,sec);
352         }
353         lpconfig->modified++;
354 }
355
356 int lp_config_needs_commit(const LpConfig *lpconfig){
357         return lpconfig->modified>0;
358 }