lpconfig.c 11.6 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/***************************************************************************
 *            lpconfig.c
 *
 *  Thu Mar 10 11:13:44 2005
 *  Copyright  2005  Simon Morlat
 *  Email simon.morlat@linphone.org
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

25
#define MAX_LEN 16384
aymeric's avatar
aymeric committed
26 27 28 29 30 31 32

#include "linphonecore.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
Jehan Monnier's avatar
Jehan Monnier committed
33
#if !defined(_WIN32_WCE)
aymeric's avatar
aymeric committed
34 35 36
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
Simon Morlat's avatar
Simon Morlat committed
37 38 39
#endif /*_WIN32_WCE*/


aymeric's avatar
aymeric committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#define lp_new0(type,n)	(type*)calloc(sizeof(type),n)

#include "lpconfig.h"


typedef struct _LpItem{
	char *key;
	char *value;
} LpItem;

typedef struct _LpSection{
	char *name;
	MSList *items;
} LpSection;

struct _LpConfig{
	FILE *file;
	char *filename;
	MSList *sections;
60
	int modified;
61
	int readonly;
aymeric's avatar
aymeric committed
62 63 64 65
};

LpItem * lp_item_new(const char *key, const char *value){
	LpItem *item=lp_new0(LpItem,1);
Jehan Monnier's avatar
Jehan Monnier committed
66 67
	item->key=ortp_strdup(key);
	item->value=ortp_strdup(value);
aymeric's avatar
aymeric committed
68 69 70 71 72
	return item;
}

LpSection *lp_section_new(const char *name){
	LpSection *sec=lp_new0(LpSection,1);
Jehan Monnier's avatar
Jehan Monnier committed
73
	sec->name=ortp_strdup(name);
aymeric's avatar
aymeric committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	return sec;
}

void lp_item_destroy(void *pitem){
	LpItem *item=(LpItem*)pitem;
	free(item->key);
	free(item->value);
	free(item);
}

void lp_section_destroy(LpSection *sec){
	free(sec->name);
	ms_list_for_each(sec->items,lp_item_destroy);
	ms_list_free(sec->items);
	free(sec);
}

void lp_section_add_item(LpSection *sec,LpItem *item){
	sec->items=ms_list_append(sec->items,(void *)item);
}

void lp_config_add_section(LpConfig *lpconfig, LpSection *section){
	lpconfig->sections=ms_list_append(lpconfig->sections,(void *)section);
}

void lp_config_remove_section(LpConfig *lpconfig, LpSection *section){
	lpconfig->sections=ms_list_remove(lpconfig->sections,(void *)section);
	lp_section_destroy(section);
}

static bool_t is_first_char(const char *start, const char *pos){
	const char *p;
	for(p=start;p<pos;p++){
		if (*p!=' ') return FALSE;
	}
	return TRUE;
}

112
LpSection *lp_config_find_section(const LpConfig *lpconfig, const char *name){
113 114 115 116 117 118 119 120 121 122 123 124 125
	LpSection *sec;
	MSList *elem;
	/*printf("Looking for section %s\n",name);*/
	for (elem=lpconfig->sections;elem!=NULL;elem=ms_list_next(elem)){
		sec=(LpSection*)elem->data;
		if (strcmp(sec->name,name)==0){
			/*printf("Section %s found\n",name);*/
			return sec;
		}
	}
	return NULL;
}

126
LpItem *lp_section_find_item(const LpSection *sec, const char *name){
127 128 129 130 131 132 133 134 135 136 137 138 139 140
	MSList *elem;
	LpItem *item;
	/*printf("Looking for item %s\n",name);*/
	for (elem=sec->items;elem!=NULL;elem=ms_list_next(elem)){
		item=(LpItem*)elem->data;
		if (strcmp(item->key,name)==0) {
			/*printf("Item %s found\n",name);*/
			return item;
		}
	}
	return NULL;
}

void lp_config_parse(LpConfig *lpconfig, FILE *file){
141
	char tmp[MAX_LEN]= {'\0'};
aymeric's avatar
aymeric committed
142
	LpSection *cur=NULL;
Jehan Monnier's avatar
Jehan Monnier committed
143

144
	if (file==NULL) return;
Jehan Monnier's avatar
Jehan Monnier committed
145

146
	while(fgets(tmp,MAX_LEN,file)!=NULL){
147
		tmp[sizeof(tmp) -1] = '\0';
aymeric's avatar
aymeric committed
148 149 150 151 152 153 154 155 156 157 158 159 160
		char *pos1,*pos2;
		pos1=strchr(tmp,'[');
		if (pos1!=NULL && is_first_char(tmp,pos1) ){
			pos2=strchr(pos1,']');
			if (pos2!=NULL){
				int nbs;
				char secname[MAX_LEN];
				secname[0]='\0';
				/* found section */
				*pos2='\0';
				nbs = sscanf(pos1+1,"%s",secname);
				if (nbs == 1 ){
					if (strlen(secname)>0){
161 162 163 164 165
						cur=lp_config_find_section (lpconfig,secname);
						if (cur==NULL){
							cur=lp_section_new(secname);
							lp_config_add_section(lpconfig,cur);
						}
aymeric's avatar
aymeric committed
166 167 168 169 170 171 172 173 174 175
					}
				}else{
					ms_warning("parse error!");
				}
			}
		}else {
			pos1=strchr(tmp,'=');
			if (pos1!=NULL){
				char key[MAX_LEN];
				key[0]='\0';
Jehan Monnier's avatar
Jehan Monnier committed
176

aymeric's avatar
aymeric committed
177 178
				*pos1='\0';
				if (sscanf(tmp,"%s",key)>0){
Jehan Monnier's avatar
Jehan Monnier committed
179

aymeric's avatar
aymeric committed
180
					pos1++;
181 182 183
					pos2=strchr(pos1,'\r');
					if (pos2==NULL)
						pos2=strchr(pos1,'\n');
184
					if (pos2==NULL) pos2=pos1+strlen(pos1);
aymeric's avatar
aymeric committed
185 186 187 188
					else {
						*pos2='\0'; /*replace the '\n' */
					}
					/* remove ending white spaces */
189 190
					for (; pos2>pos1 && pos2[-1]==' ';pos2--) pos2[-1]='\0';

aymeric's avatar
aymeric committed
191 192 193
					if (pos2-pos1>=0){
						/* found a pair key,value */
						if (cur!=NULL){
194 195 196 197 198 199 200
							LpItem *item=lp_section_find_item(cur,key);
							if (item==NULL){
								lp_section_add_item(cur,lp_item_new(key,pos1));
							}else{
								ms_free(item->value);
								item->value=strdup(pos1);
							}
201
							/*printf("Found %s %s={%s}\n",cur->name,key,pos1);*/
aymeric's avatar
aymeric committed
202 203 204 205 206 207 208 209 210 211 212 213 214
						}else{
							ms_warning("found key,item but no sections");
						}
					}
				}
			}
		}
	}
}

LpConfig * lp_config_new(const char *filename){
	LpConfig *lpconfig=lp_new0(LpConfig,1);
	if (filename!=NULL){
Jehan Monnier's avatar
Jehan Monnier committed
215
		lpconfig->filename=ortp_strdup(filename);
aymeric's avatar
aymeric committed
216 217
		lpconfig->file=fopen(filename,"rw");
		if (lpconfig->file!=NULL){
218 219
			lp_config_parse(lpconfig,lpconfig->file);
			fclose(lpconfig->file);			
Jehan Monnier's avatar
Jehan Monnier committed
220
#if !defined(_WIN32_WCE)
aymeric's avatar
aymeric committed
221 222 223 224 225
			/* make existing configuration files non-group/world-accessible */
			if (chmod(filename, S_IRUSR | S_IWUSR) == -1)
				ms_warning("unable to correct permissions on "
				  	  "configuration file: %s",
					   strerror(errno));
Jehan Monnier's avatar
Jehan Monnier committed
226
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
227
			lpconfig->file=NULL;
228
			lpconfig->modified=0;
aymeric's avatar
aymeric committed
229 230 231 232 233
		}
	}
	return lpconfig;
}

234 235 236 237 238 239 240 241 242 243 244
int lp_config_read_file(LpConfig *lpconfig, const char *filename){
	FILE* f=fopen(filename,"r");
	if (f!=NULL){
		lp_config_parse(lpconfig,f);
		fclose(f);
		return 0;
	}
	ms_warning("Fail to open file %s",filename);
	return -1;
}

aymeric's avatar
aymeric committed
245 246
void lp_item_set_value(LpItem *item, const char *value){
	free(item->value);
Jehan Monnier's avatar
Jehan Monnier committed
247
	item->value=ortp_strdup(value);
aymeric's avatar
aymeric committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}


void lp_config_destroy(LpConfig *lpconfig){
	if (lpconfig->filename!=NULL) free(lpconfig->filename);
	ms_list_for_each(lpconfig->sections,(void (*)(void*))lp_section_destroy);
	ms_list_free(lpconfig->sections);
	free(lpconfig);
}

void lp_section_remove_item(LpSection *sec, LpItem *item){
	sec->items=ms_list_remove(sec->items,(void *)item);
	lp_item_destroy(item);
}

263
const char *lp_config_get_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_string){
aymeric's avatar
aymeric committed
264 265 266 267 268 269 270 271 272 273
	LpSection *sec;
	LpItem *item;
	sec=lp_config_find_section(lpconfig,section);
	if (sec!=NULL){
		item=lp_section_find_item(sec,key);
		if (item!=NULL) return item->value;
	}
	return default_string;
}

274
bool_t lp_config_get_range(const LpConfig *lpconfig, const char *section, const char *key, int *min, int *max, int default_min, int default_max) {
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	const char *str = lp_config_get_string(lpconfig, section, key, NULL);
	if (str != NULL) {
		char *minusptr = strchr(str, '-');
		if ((minusptr == NULL) || (minusptr == str)) {
			*min = default_min;
			*max = default_max;
			return FALSE;
		}
		*min = atoi(str);
		*max = atoi(minusptr + 1);
		return TRUE;
	} else {
		*min = default_min;
		*max = default_max;
		return TRUE;
	}
}

293
int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char *key, int default_value){
aymeric's avatar
aymeric committed
294
	const char *str=lp_config_get_string(lpconfig,section,key,NULL);
295 296 297 298 299 300 301
	if (str!=NULL) {
		int ret=0;
		if (strstr(str,"0x")==str){
			sscanf(str,"%x",&ret);
		}else ret=atoi(str);
		return ret;
	}
aymeric's avatar
aymeric committed
302 303 304
	else return default_value;
}

305
int64_t lp_config_get_int64(const LpConfig *lpconfig,const char *section, const char *key, int64_t default_value){
306 307 308 309 310 311 312 313 314 315 316
	const char *str=lp_config_get_string(lpconfig,section,key,NULL);
	if (str!=NULL) {
#ifdef WIN32
		return (int64_t)_atoi64(str);
#else
		return atoll(str);
#endif
	}
	else return default_value;
}

317
float lp_config_get_float(const LpConfig *lpconfig,const char *section, const char *key, float default_value){
smorlat's avatar
smorlat committed
318
	const char *str=lp_config_get_string(lpconfig,section,key,NULL);
smorlat's avatar
smorlat committed
319
	float ret=default_value;
smorlat's avatar
smorlat committed
320 321 322 323 324
	if (str==NULL) return default_value;
	sscanf(str,"%f",&ret);
	return ret;
}

aymeric's avatar
aymeric committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *key, const char *value){
	LpItem *item;
	LpSection *sec=lp_config_find_section(lpconfig,section);
	if (sec!=NULL){
		item=lp_section_find_item(sec,key);
		if (item!=NULL){
			if (value!=NULL)
				lp_item_set_value(item,value);
			else lp_section_remove_item(sec,item);
		}else{
			if (value!=NULL)
				lp_section_add_item(sec,lp_item_new(key,value));
		}
	}else if (value!=NULL){
		sec=lp_section_new(section);
		lp_config_add_section(lpconfig,sec);
		lp_section_add_item(sec,lp_item_new(key,value));
	}
343
	lpconfig->modified++;
aymeric's avatar
aymeric committed
344 345
}

346 347 348 349 350 351
void lp_config_set_range(LpConfig *lpconfig, const char *section, const char *key, int min_value, int max_value) {
	char tmp[30];
	snprintf(tmp, sizeof(tmp), "%i-%i", min_value, max_value);
	lp_config_set_string(lpconfig, section, key, tmp);
}

aymeric's avatar
aymeric committed
352 353
void lp_config_set_int(LpConfig *lpconfig,const char *section, const char *key, int value){
	char tmp[30];
354 355 356 357
	snprintf(tmp,sizeof(tmp),"%i",value);
	lp_config_set_string(lpconfig,section,key,tmp);
}

358 359 360 361 362 363
void lp_config_set_int_hex(LpConfig *lpconfig,const char *section, const char *key, int value){
	char tmp[30];
	snprintf(tmp,sizeof(tmp),"0x%x",value);
	lp_config_set_string(lpconfig,section,key,tmp);
}

364 365 366 367 368 369 370
void lp_config_set_int64(LpConfig *lpconfig,const char *section, const char *key, int64_t value){
	char tmp[30];
	snprintf(tmp,sizeof(tmp),"%lli",(long long)value);
	lp_config_set_string(lpconfig,section,key,tmp);
}


371 372 373
void lp_config_set_float(LpConfig *lpconfig,const char *section, const char *key, float value){
	char tmp[30];
	snprintf(tmp,sizeof(tmp),"%f",value);
aymeric's avatar
aymeric committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	lp_config_set_string(lpconfig,section,key,tmp);
}

void lp_item_write(LpItem *item, FILE *file){
	fprintf(file,"%s=%s\n",item->key,item->value);
}

void lp_section_write(LpSection *sec, FILE *file){
	fprintf(file,"[%s]\n",sec->name);
	ms_list_for_each2(sec->items,(void (*)(void*, void*))lp_item_write,(void *)file);
	fprintf(file,"\n");
}

int lp_config_sync(LpConfig *lpconfig){
	FILE *file;
	if (lpconfig->filename==NULL) return -1;
390
	if (lpconfig->readonly) return 0;
aymeric's avatar
aymeric committed
391 392 393 394 395 396
#ifndef WIN32
	/* don't create group/world-accessible files */
	(void) umask(S_IRWXG | S_IRWXO);
#endif
	file=fopen(lpconfig->filename,"w");
	if (file==NULL){
397 398
		ms_warning("Could not write %s ! Maybe it is read-only. Configuration will not be saved.",lpconfig->filename);
		lpconfig->readonly=1;
aymeric's avatar
aymeric committed
399 400 401 402
		return -1;
	}
	ms_list_for_each2(lpconfig->sections,(void (*)(void *,void*))lp_section_write,(void *)file);
	fclose(file);
403
	lpconfig->modified=0;
aymeric's avatar
aymeric committed
404 405 406
	return 0;
}

407
int lp_config_has_section(const LpConfig *lpconfig, const char *section){
aymeric's avatar
aymeric committed
408 409 410 411
	if (lp_config_find_section(lpconfig,section)!=NULL) return 1;
	return 0;
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
void lp_config_for_each_section(const LpConfig *lpconfig, void (*callback)(const char *section, void *ctx), void *ctx) {
	LpSection *sec;
	MSList *elem;
	for (elem=lpconfig->sections;elem!=NULL;elem=ms_list_next(elem)){
		sec=(LpSection*)elem->data;
		callback(sec->name, ctx);
	}
}

void lp_config_for_each_entry(const LpConfig *lpconfig, const char *section, void (*callback)(const char *entry, void *ctx), void *ctx) {
	LpItem *item;
	MSList *elem;
	LpSection *sec=lp_config_find_section(lpconfig,section);
	if (sec!=NULL){
		for (elem=sec->items;elem!=NULL;elem=ms_list_next(elem)){
			item=(LpItem*)elem->data;
			callback(item->key, ctx);
		}
	}
}

aymeric's avatar
aymeric committed
433 434 435 436 437
void lp_config_clean_section(LpConfig *lpconfig, const char *section){
	LpSection *sec=lp_config_find_section(lpconfig,section);
	if (sec!=NULL){
		lp_config_remove_section(lpconfig,sec);
	}
438 439 440 441 442
	lpconfig->modified++;
}

int lp_config_needs_commit(const LpConfig *lpconfig){
	return lpconfig->modified>0;
aymeric's avatar
aymeric committed
443
}