const char *lp_config_get_string(LpConfig *lpconfig, const char *section, const char *key, const char *default_string){ 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; }
bool_t lp_config_get_skip_flag_for_entry(const LpConfig *lpconfig, const char *section, const char *key) { 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->skip; } return FALSE; }
void lp_config_clean_entry(LpConfig *lpconfig, const char *section, const char *key) { LpSection *sec; LpItem *item; sec=lp_config_find_section(lpconfig,section); if (sec!=NULL){ item=lp_section_find_item(sec,key); if (item!=NULL) lp_section_remove_item(sec,item); } return ; }
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)); } lpconfig->modified++; }
MSList * lp_config_get_string_list(const LpConfig *lpconfig, const char *section, const char *key, MSList *default_list) { LpItem *item; LpSection *sec = lp_config_find_section(lpconfig, section); if (sec != NULL) { item = lp_section_find_item(sec, key); if (item != NULL) { MSList *l = NULL; char *str; char *ptr; str = ptr = ms_strdup(item->value); while (ptr != NULL) { char *next = strstr(ptr, ","); if (next != NULL) { *(next++) = '\0'; } l = ms_list_append(l, ms_strdup(ptr)); ptr = next; } ms_free(str); return l; } } return default_list; }
void lp_config_parse(LpConfig *lpconfig, FILE *file){ char tmp[MAX_LEN]= {'\0'}; LpSection *cur=NULL; if (file==NULL) return; while(fgets(tmp,MAX_LEN,file)!=NULL){ char *pos1,*pos2; tmp[sizeof(tmp) -1] = '\0'; 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){ cur=lp_config_find_section (lpconfig,secname); if (cur==NULL){ cur=lp_section_new(secname); lp_config_add_section(lpconfig,cur); } } }else{ ms_warning("parse error!"); } } }else { pos1=strchr(tmp,'='); if (pos1!=NULL){ char key[MAX_LEN]; key[0]='\0'; *pos1='\0'; if (sscanf(tmp,"%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' */ pos2--; } /* remove ending white spaces */ for (; pos2>pos1 && *pos2==' ';pos2--) *pos2='\0'; if (pos2-pos1>=0){ /* found a pair key,value */ if (cur!=NULL){ 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); } /*printf("Found %s %s=%s\n",cur->name,key,pos1);*/ }else{ ms_warning("found key,item but no sections"); } } } } } } }
static LpSection* lp_config_parse_line(LpConfig* lpconfig, const char* line, LpSection* cur) { LpSectionParam *params = NULL; char *pos1,*pos2; int nbs; int size=strlen(line)+1; char *secname=ms_malloc(size); char *key=ms_malloc(size); char *value=ms_malloc(size); LpItem *item; 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); } 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; } } } } } else { ms_warning("parse error!"); } } }else { 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'; if (pos2-pos1>0){ /* 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);*/ }else{ ms_warning("found key,item but no sections"); } } } } } } ms_free(key); ms_free(value); ms_free(secname); return cur; }