void lp_config_parse(LpConfig *lpconfig){ char tmp[MAX_LEN]; LpSection *cur=NULL; if (lpconfig->file==NULL) return; while(fgets(tmp,MAX_LEN,lpconfig->file)!=NULL){ 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){ 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,'\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){ lp_section_add_item(cur,lp_item_new(key,pos1,FALSE)); /*printf("Found %s %s=%s\n",cur->name,key,pos1);*/ }else{ ms_warning("found key,item but no sections"); } } } } } } }
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++; }
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; }
void lp_config_parse(LpConfig *lpconfig){ int nrow = 0, ncolumn = 0; char **sectionResult; //二维数组存放结果 int nIndex=0; LpSection *cur=NULL; char szSQL[] = "SELECT DISTINCT section FROM ua_config;"; if(sqlite3_get_table(lpconfig->db, szSQL ,§ionResult ,&nrow ,&ncolumn ,0) == SQLITE_OK) { int index = ncolumn; int i,j; for(i=0;i<nrow;i++){ for(j=0;j<ncolumn;j++) { if ( strcmp(sectionResult[j],"section") == 0) { int itemnrow = 0, itemncolumn = 0; char **itemResult; //二维数组存放结果 int itemIndex=0; char szSQLitem[128]; cur = lp_section_new(sectionResult[index]); lp_config_add_section(lpconfig,cur); sprintf(szSQLitem,"SELECT var_name, var_value FROM ua_config WHERE section = '%s';",sectionResult[index]); if(sqlite3_get_table(lpconfig->db, szSQLitem ,&itemResult ,&itemnrow ,&itemncolumn ,0) == SQLITE_OK) { int idx = itemncolumn; int k,l; char *val_name; char *val_value; val_name = val_value = NULL; for(k=0;k<itemnrow;k++){ for(l=0;l<itemncolumn;l++) { if ( strcmp(itemResult[l],"var_name") == 0) { val_name = itemResult[idx]; } if ( strcmp(itemResult[l],"var_value") == 0) { val_value = itemResult[idx]; } ++idx; } ms_message("lp_config_parse section: %s, var_name: %s, var_value: %s",cur->name,val_name,val_value); lp_section_add_item(cur,lp_item_new(val_name,val_value,FALSE)); } sqlite3_free_table(itemResult); } } ++index; } } sqlite3_free_table(sectionResult); } }