lpconfig.c 19.8 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>
37 38 39
#if _MSC_VER
#include <io.h>
#endif
Simon Morlat's avatar
Simon Morlat committed
40 41
#endif /*_WIN32_WCE*/

42
#ifdef _MSC_VER
43 44 45
#ifdef WINAPI_FAMILY_PHONE_APP
#include <stdlib.h>
#else
46
#include <Shlwapi.h>
47
#endif
48 49 50 51
#else
#include <libgen.h>
#endif

Simon Morlat's avatar
Simon Morlat committed
52

aymeric's avatar
aymeric committed
53 54 55 56 57 58 59 60 61

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

#include "lpconfig.h"


typedef struct _LpItem{
	char *key;
	char *value;
62
	int is_comment;
aymeric's avatar
aymeric committed
63 64
} LpItem;

65 66 67 68 69
typedef struct _LpSectionParam{
	char *key;
	char *value;
} LpSectionParam;

aymeric's avatar
aymeric committed
70 71 72
typedef struct _LpSection{
	char *name;
	MSList *items;
73
	MSList *params;
aymeric's avatar
aymeric committed
74 75 76
} LpSection;

struct _LpConfig{
77
	int refcnt;
aymeric's avatar
aymeric committed
78 79
	FILE *file;
	char *filename;
80
	char *tmpfilename;
aymeric's avatar
aymeric committed
81
	MSList *sections;
82
	int modified;
83
	int readonly;
aymeric's avatar
aymeric committed
84 85 86 87
};

LpItem * lp_item_new(const char *key, const char *value){
	LpItem *item=lp_new0(LpItem,1);
Jehan Monnier's avatar
Jehan Monnier committed
88 89
	item->key=ortp_strdup(key);
	item->value=ortp_strdup(value);
aymeric's avatar
aymeric committed
90 91 92
	return item;
}

93 94 95 96 97 98 99
LpItem * lp_comment_new(const char *comment){
	LpItem *item=lp_new0(LpItem,1);
	item->value=ortp_strdup(comment);
	item->is_comment=TRUE;
	return item;
}

100 101 102 103 104 105 106
LpSectionParam *lp_section_param_new(const char *key, const char *value){
	LpSectionParam *param = lp_new0(LpSectionParam, 1);
	param->key = ortp_strdup(key);
	param->value = ortp_strdup(value);
	return param;
}

aymeric's avatar
aymeric committed
107 108
LpSection *lp_section_new(const char *name){
	LpSection *sec=lp_new0(LpSection,1);
Jehan Monnier's avatar
Jehan Monnier committed
109
	sec->name=ortp_strdup(name);
aymeric's avatar
aymeric committed
110 111 112 113 114
	return sec;
}

void lp_item_destroy(void *pitem){
	LpItem *item=(LpItem*)pitem;
115
	if (item->key) ortp_free(item->key);
116
	ortp_free(item->value);
aymeric's avatar
aymeric committed
117 118 119
	free(item);
}

120 121 122 123 124 125 126
void lp_section_param_destroy(void *section_param){
	LpSectionParam *param = (LpSectionParam*)section_param;
	ortp_free(param->key);
	ortp_free(param->value);
	free(param);
}

aymeric's avatar
aymeric committed
127
void lp_section_destroy(LpSection *sec){
128
	ortp_free(sec->name);
aymeric's avatar
aymeric committed
129
	ms_list_for_each(sec->items,lp_item_destroy);
130
	ms_list_for_each(sec->params,lp_section_param_destroy);
aymeric's avatar
aymeric committed
131 132 133 134 135 136 137 138 139 140 141 142
	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);
}

143 144 145 146
void lp_config_add_section_param(LpSection *section, LpSectionParam *param){
	section->params = ms_list_append(section->params, (void *)param);
}

aymeric's avatar
aymeric committed
147 148 149 150 151 152 153 154 155 156 157 158 159
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;
}

160 161 162 163 164 165 166 167
static int is_a_comment(const char *str){
	while (*str==' '){
		str++;
	}
	if (*str=='#') return 1;
	return 0;
}

168
LpSection *lp_config_find_section(const LpConfig *lpconfig, const char *name){
169 170 171 172 173 174 175 176 177 178 179 180 181
	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;
}

182 183 184 185 186 187 188 189 190 191 192 193
LpSectionParam *lp_section_find_param(const LpSection *sec, const char *key){
	MSList *elem;
	LpSectionParam *param;
	for (elem = sec->params; elem != NULL; elem = ms_list_next(elem)){
		param = (LpSectionParam*)elem->data;
		if (strcmp(param->key, key) == 0) {
			return param;
		}
	}
	return NULL;
}

194
LpItem *lp_section_find_item(const LpSection *sec, const char *name){
195 196 197 198 199
	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;
200
		if (!item->is_comment && strcmp(item->key,name)==0) {
201 202 203 204 205 206 207
			/*printf("Item %s found\n",name);*/
			return item;
		}
	}
	return NULL;
}

208
static LpSection* lp_config_parse_line(LpConfig* lpconfig, const char* line, LpSection* cur) {
209
	LpSectionParam *params = NULL;
210 211
	char *pos1,*pos2;
	int nbs;
212 213 214 215
	int size=strlen(line)+1;
	char *secname=ms_malloc(size);
	char *key=ms_malloc(size);
	char *value=ms_malloc(size);
216
	LpItem *item;
Jehan Monnier's avatar
Jehan Monnier committed
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	pos1=strchr(line,'[');
	if (pos1!=NULL && is_first_char(line,pos1) ){
		pos2=strchr(pos1,']');
		if (pos2!=NULL){
			secname[0]='\0';
			/* found section */
			*pos2='\0';
			nbs = sscanf(pos1+1, "%s", secname);
			if (nbs >= 1) {
				if (strlen(secname) > 0) {
					cur = lp_config_find_section (lpconfig,secname);
					if (cur == NULL) {
						cur = lp_section_new(secname);
						lp_config_add_section(lpconfig, cur);
					}
Jehan Monnier's avatar
Jehan Monnier committed
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
					if (pos2 > pos1 + 1 + strlen(secname)) {
						/* found at least one section param */
						pos2 = pos1 + 1 + strlen(secname) + 1; // Remove the white space after the secname
						pos1 = strchr(pos2, '=');
						while (pos1 != NULL) {
							/* for each section param */
							key[0] = '\0';
							value[0] = '\0';
							*pos1 = ' ';
							if (sscanf(pos2, "%s %s", key, value) == 2) {
								params = lp_section_param_new(key, value);
								lp_config_add_section_param(cur, params);

								pos2 += strlen(key) + strlen(value) + 2; // Remove the = sign + the white space after each param
								pos1 = strchr(pos2, '=');
							} else {
								ms_warning("parse section params error !");
								pos1 = NULL;
252
							}
253
						}
aymeric's avatar
aymeric committed
254 255
					}
				}
256 257
			} else {
				ms_warning("parse error!");
aymeric's avatar
aymeric committed
258
			}
259 260
		}
	}else {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
		if (is_a_comment(line)){
			if (cur){
				LpItem *comment=lp_comment_new(line);
				lp_section_add_item(cur,comment);
			}
		}else{
			pos1=strchr(line,'=');
			if (pos1!=NULL){
				key[0]='\0';

				*pos1='\0';
				if (sscanf(line,"%s",key)>0){

					pos1++;
					pos2=strchr(pos1,'\r');
					if (pos2==NULL)
						pos2=strchr(pos1,'\n');
					if (pos2==NULL) pos2=pos1+strlen(pos1);
					else {
						*pos2='\0'; /*replace the '\n' */
					}
					/* remove ending white spaces */
					for (; pos2>pos1 && pos2[-1]==' ';pos2--) pos2[-1]='\0';

285
					if (pos2-pos1>0){
286 287 288 289 290 291 292 293 294 295 296
						/* found a pair key,value */

						if (cur!=NULL){
							item=lp_section_find_item(cur,key);
							if (item==NULL){
								lp_section_add_item(cur,lp_item_new(key,pos1));
							}else{
								ortp_free(item->value);
								item->value=ortp_strdup(pos1);
							}
							/*ms_message("Found %s=%s",key,pos1);*/
aymeric's avatar
aymeric committed
297
						}else{
298
							ms_warning("found key,item but no sections");
aymeric's avatar
aymeric committed
299 300 301 302 303 304
						}
					}
				}
			}
		}
	}
305 306 307
	ms_free(key);
	ms_free(value);
	ms_free(secname);
308 309 310 311 312 313 314 315 316 317 318 319 320
	return cur;
}

void lp_config_parse(LpConfig *lpconfig, FILE *file){
	char tmp[MAX_LEN]= {'\0'};
	LpSection* current_section = NULL;

	if (file==NULL) return;

	while(fgets(tmp,MAX_LEN,file)!=NULL){
		tmp[sizeof(tmp) -1] = '\0';
		current_section = lp_config_parse_line(lpconfig, tmp, current_section);
	}
aymeric's avatar
aymeric committed
321 322 323
}

LpConfig * lp_config_new(const char *filename){
324 325 326
	return lp_config_new_with_factory(filename, NULL);
}

327 328 329 330 331 332 333 334
LpConfig * lp_config_new_from_buffer(const char *buffer){
	LpConfig* conf = lp_new0(LpConfig,1);
	LpSection* current_section = NULL;

	char* ptr = ms_strdup(buffer);
	char* strtok_storage = NULL;
	char* line = strtok_r(ptr, "\n", &strtok_storage);

335 336
	conf->refcnt=1;

337 338 339 340 341 342 343 344 345 346
	while( line != NULL ){
		current_section = lp_config_parse_line(conf,line,current_section);
		line = strtok_r(NULL, "\n", &strtok_storage);
	}

	ms_free(ptr);

	return conf;
}

347
LpConfig *lp_config_new_with_factory(const char *config_filename, const char *factory_config_filename) {
aymeric's avatar
aymeric committed
348
	LpConfig *lpconfig=lp_new0(LpConfig,1);
349
	lpconfig->refcnt=1;
350 351 352
	if (config_filename!=NULL){
		ms_message("Using (r/w) config information from %s", config_filename);
		lpconfig->filename=ortp_strdup(config_filename);
353
		lpconfig->tmpfilename=ortp_strdup_printf("%s.tmp",config_filename);
354
		lpconfig->file=fopen(config_filename,"r+");
aymeric's avatar
aymeric committed
355
		if (lpconfig->file!=NULL){
356
			lp_config_parse(lpconfig,lpconfig->file);
357
			fclose(lpconfig->file);
jehan's avatar
jehan committed
358
#if !defined(WIN32)
359 360 361 362 363 364 365 366
			{
				struct stat fileStat;
				if ((stat(config_filename,&fileStat) == 0) && (S_ISREG(fileStat.st_mode))) {
					/* make existing configuration files non-group/world-accessible */
					if (chmod(config_filename, S_IRUSR | S_IWUSR) == -1) {
						ms_warning("unable to correct permissions on "
							"configuration file: %s", strerror(errno));
					}
367 368
				}
			}
369
#endif /*WIN32*/
aymeric's avatar
aymeric committed
370
			lpconfig->file=NULL;
371
			lpconfig->modified=0;
aymeric's avatar
aymeric committed
372 373
		}
	}
374 375 376
	if (factory_config_filename != NULL) {
		lp_config_read_file(lpconfig, factory_config_filename);
	}
aymeric's avatar
aymeric committed
377 378 379
	return lpconfig;
}

380 381 382
int lp_config_read_file(LpConfig *lpconfig, const char *filename){
	FILE* f=fopen(filename,"r");
	if (f!=NULL){
383
		ms_message("Reading config information from %s", filename);
384 385 386 387 388 389 390 391
		lp_config_parse(lpconfig,f);
		fclose(f);
		return 0;
	}
	ms_warning("Fail to open file %s",filename);
	return -1;
}

aymeric's avatar
aymeric committed
392
void lp_item_set_value(LpItem *item, const char *value){
393
	char *prev_value=item->value;
Jehan Monnier's avatar
Jehan Monnier committed
394
	item->value=ortp_strdup(value);
395
	ortp_free(prev_value);
aymeric's avatar
aymeric committed
396 397 398
}


399
static void _lp_config_destroy(LpConfig *lpconfig){
400
	if (lpconfig->filename!=NULL) ortp_free(lpconfig->filename);
aymeric's avatar
aymeric committed
401 402 403 404 405
	ms_list_for_each(lpconfig->sections,(void (*)(void*))lp_section_destroy);
	ms_list_free(lpconfig->sections);
	free(lpconfig);
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
LpConfig *lp_config_ref(LpConfig *lpconfig){
	lpconfig->refcnt++;
	return lpconfig;
}

void lp_config_unref(LpConfig *lpconfig){
	lpconfig->refcnt--;
	if (lpconfig->refcnt==0)
		_lp_config_destroy(lpconfig);
}

void lp_config_destroy(LpConfig *lpconfig){
	lp_config_unref(lpconfig);
}

aymeric's avatar
aymeric committed
421 422 423 424 425
void lp_section_remove_item(LpSection *sec, LpItem *item){
	sec->items=ms_list_remove(sec->items,(void *)item);
	lp_item_destroy(item);
}

426 427 428 429 430 431 432 433 434 435 436
const char *lp_config_get_section_param_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_value){
	LpSection *sec;
	LpSectionParam *param;
	sec = lp_config_find_section(lpconfig, section);
	if (sec != NULL) {
		param = lp_section_find_param(sec, key);
		if (param != NULL) return param->value;
	}
	return default_value;
}

437
const char *lp_config_get_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_string){
aymeric's avatar
aymeric committed
438 439 440 441 442 443 444 445 446 447
	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;
}

448
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) {
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	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;
	}
}

467

468
int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char *key, int default_value){
aymeric's avatar
aymeric committed
469
	const char *str=lp_config_get_string(lpconfig,section,key,NULL);
470 471
	if (str!=NULL) {
		int ret=0;
472

473 474
		if (strstr(str,"0x")==str){
			sscanf(str,"%x",&ret);
475
		}else
476
			sscanf(str,"%i",&ret);
477 478
		return ret;
	}
aymeric's avatar
aymeric committed
479 480 481
	else return default_value;
}

482
int64_t lp_config_get_int64(const LpConfig *lpconfig,const char *section, const char *key, int64_t default_value){
483 484 485 486 487 488 489 490 491 492 493
	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;
}

494
float lp_config_get_float(const LpConfig *lpconfig,const char *section, const char *key, float default_value){
smorlat's avatar
smorlat committed
495
	const char *str=lp_config_get_string(lpconfig,section,key,NULL);
smorlat's avatar
smorlat committed
496
	float ret=default_value;
smorlat's avatar
smorlat committed
497 498 499 500 501
	if (str==NULL) return default_value;
	sscanf(str,"%f",&ret);
	return ret;
}

aymeric's avatar
aymeric committed
502 503 504 505 506 507
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){
508
			if (value!=NULL && value[0] != '\0')
aymeric's avatar
aymeric committed
509 510 511
				lp_item_set_value(item,value);
			else lp_section_remove_item(sec,item);
		}else{
512
			if (value!=NULL && value[0] != '\0')
aymeric's avatar
aymeric committed
513 514
				lp_section_add_item(sec,lp_item_new(key,value));
		}
515
	}else if (value!=NULL && value[0] != '\0'){
aymeric's avatar
aymeric committed
516 517 518 519
		sec=lp_section_new(section);
		lp_config_add_section(lpconfig,sec);
		lp_section_add_item(sec,lp_item_new(key,value));
	}
520
	lpconfig->modified++;
aymeric's avatar
aymeric committed
521 522
}

523 524 525 526 527 528
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
529 530
void lp_config_set_int(LpConfig *lpconfig,const char *section, const char *key, int value){
	char tmp[30];
531 532 533 534
	snprintf(tmp,sizeof(tmp),"%i",value);
	lp_config_set_string(lpconfig,section,key,tmp);
}

535 536 537 538 539 540
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);
}

541 542 543 544 545 546 547
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);
}


548 549 550
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
551 552 553 554
	lp_config_set_string(lpconfig,section,key,tmp);
}

void lp_item_write(LpItem *item, FILE *file){
555 556
	if (item->is_comment)
		fprintf(file,"%s",item->value);
557
	else if (item->value && item->value[0] != '\0' )
558
		fprintf(file,"%s=%s\n",item->key,item->value);
559 560 561
	else {
		ms_warning("Not writing item %s to file, it is empty", item->key);
	}
aymeric's avatar
aymeric committed
562 563
}

564
void lp_section_param_write(LpSectionParam *param, FILE *file){
565 566 567 568 569
	if( param->value && param->value[0] != '\0') {
		fprintf(file, " %s=%s", param->key, param->value);
	} else {
		ms_warning("Not writing param %s to file, it is empty", param->key);
	}
570 571
}

aymeric's avatar
aymeric committed
572
void lp_section_write(LpSection *sec, FILE *file){
573 574 575 576 577
	fprintf(file, "[%s",sec->name);
	ms_list_for_each2(sec->params, (void (*)(void*, void*))lp_section_param_write, (void *)file);
	fprintf(file, "]\n");
	ms_list_for_each2(sec->items, (void (*)(void*, void*))lp_item_write, (void *)file);
	fprintf(file, "\n");
aymeric's avatar
aymeric committed
578 579 580 581 582
}

int lp_config_sync(LpConfig *lpconfig){
	FILE *file;
	if (lpconfig->filename==NULL) return -1;
583
	if (lpconfig->readonly) return 0;
aymeric's avatar
aymeric committed
584 585 586 587
#ifndef WIN32
	/* don't create group/world-accessible files */
	(void) umask(S_IRWXG | S_IRWXO);
#endif
588
	file=fopen(lpconfig->tmpfilename,"w");
aymeric's avatar
aymeric committed
589
	if (file==NULL){
590 591
		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
592 593 594 595
		return -1;
	}
	ms_list_for_each2(lpconfig->sections,(void (*)(void *,void*))lp_section_write,(void *)file);
	fclose(file);
596 597 598
	if (rename(lpconfig->tmpfilename,lpconfig->filename)!=0){
		ms_error("Cannot rename %s into %s: %s",lpconfig->tmpfilename,lpconfig->filename,strerror(errno));
	}
599
	lpconfig->modified=0;
aymeric's avatar
aymeric committed
600 601 602
	return 0;
}

603
int lp_config_has_section(const LpConfig *lpconfig, const char *section){
aymeric's avatar
aymeric committed
604 605 606 607
	if (lp_config_find_section(lpconfig,section)!=NULL) return 1;
	return 0;
}

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
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;
624 625
			if (!item->is_comment)
				callback(item->key, ctx);
626 627 628 629
		}
	}
}

aymeric's avatar
aymeric committed
630 631 632 633 634
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);
	}
635 636 637 638 639
	lpconfig->modified++;
}

int lp_config_needs_commit(const LpConfig *lpconfig){
	return lpconfig->modified>0;
aymeric's avatar
aymeric committed
640
}
641 642 643 644 645 646 647

static const char *DEFAULT_VALUES_SUFFIX = "_default_values";

int lp_config_get_default_int(const LpConfig *lpconfig, const char *section, const char *key, int default_value) {
	char default_section[MAX_LEN];
	strcpy(default_section, section);
	strcat(default_section, DEFAULT_VALUES_SUFFIX);
648

649 650 651 652 653 654 655
	return lp_config_get_int(lpconfig, default_section, key, default_value);
}

int64_t lp_config_get_default_int64(const LpConfig *lpconfig, const char *section, const char *key, int64_t default_value) {
	char default_section[MAX_LEN];
	strcpy(default_section, section);
	strcat(default_section, DEFAULT_VALUES_SUFFIX);
656

657 658 659 660 661 662 663
	return lp_config_get_int64(lpconfig, default_section, key, default_value);
}

float lp_config_get_default_float(const LpConfig *lpconfig, const char *section, const char *key, float default_value) {
	char default_section[MAX_LEN];
	strcpy(default_section, section);
	strcat(default_section, DEFAULT_VALUES_SUFFIX);
664

665 666 667 668 669 670 671
	return lp_config_get_float(lpconfig, default_section, key, default_value);
}

const char* lp_config_get_default_string(const LpConfig *lpconfig, const char *section, const char *key, const char *default_value) {
	char default_section[MAX_LEN];
	strcpy(default_section, section);
	strcat(default_section, DEFAULT_VALUES_SUFFIX);
672

673 674
	return lp_config_get_string(lpconfig, default_section, key, default_value);
}
675 676

static char *_lp_config_dirname(char *path) {
677
#ifdef _MSC_VER
678 679 680 681 682 683 684 685
#ifdef WINAPI_FAMILY_PHONE_APP
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];
	_splitpath(path, drive, dir, fname, ext);
	return ms_strdup_printf("%s%s", drive, dir);
#else
686 687 688
	char *dir = ms_strdup(path);
	PathRemoveFileSpec(dir);
	return dir;
689
#endif
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
#else
	char *tmp = ms_strdup(path);
	char *dir = ms_strdup(dirname(tmp));
	ms_free(tmp);
	return dir;
#endif
}

void lp_config_write_relative_file(const LpConfig *lpconfig, const char *filename, const char *data) {
	if(strlen(data) > 0) {
		char *dir = _lp_config_dirname(lpconfig->filename);
		char *filepath = ms_strdup_printf("%s/%s", dir, filename);
		FILE *file = fopen(filepath, "w");
		if(file != NULL) {
			fprintf(file, "%s", data);
			fclose(file);
		} else {
			ms_error("Could not open %s for write", filepath);
		}
		ms_free(dir);
		ms_free(filepath);
	} else {
		ms_warning("%s has not been created because there is no data to write", filename);
	}
}

char *lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename) {
	char *dir = _lp_config_dirname(lpconfig->filename);
	char *filepath = ms_strdup_printf("%s/%s", dir, filename);
	char *result = NULL;
720
	if(ortp_file_exist(filepath) == 0) {
721 722 723
		FILE *file = fopen(filepath, "r");
		if(file != NULL) {
			result = ms_new0(char, MAX_LEN);
724 725 726
			if(fgets(result, MAX_LEN, file) == NULL) {
				ms_error("%s could not be loaded", filepath);
			}
727 728 729 730 731 732 733 734 735 736 737
			fclose(file);
		} else {
			ms_error("Could not open %s for read", filepath);
		}
	} else {
		ms_message("%s does not exist", filepath);
	}
	ms_free(dir);
	ms_free(filepath);
	return result;
}