C语言通过定义结构体UCI读写配置文件

**C语言通过定义结构体UCI读写配置文件

1、配置文件名称:roaming
#define UCI_ROAM_FILE "/etc/config/roaming" //配置文件
config wlan "wlan0"
option basicnetwork_name 'siot'
option basicstate 'enable'
option securitysuite 'wpa2'
option securitywpaxauthentication 'psk'
option securitywpaxieee_80211r 'disabled'
option securitywpaxpassphrase '12345678'

2、需要定义结构体数组

KEY Roaming_Conf_Value[6] ={{"basicnetwork_name",0,0},\
{"basicstate",1,0},\
{"securitysuite",2,0},\
{"securitywpaxauthentication",3,0},\
{"securitywpaxieee_80211r",4,0},\
{"securitywpaxpassphrase",5,0}};

3、然后需要定义UCI对象
static struct uci_context ctx = NULL;
4、然后编写单个写入配置文件函数;
void write_roam_config(char
package_char,char section_char,char roam_conf_name,char roam_conf_value)
{ //写入配置文件roaming
int i=0;
printf("write_roam_config.......start .....\n");
struct uci_package
pkg= NULL;
ctx = uci_alloc_context();

if (UCI_OK != uci_load(ctx, UCI_ROAM_FILE, &pkg))
    goto cleanup; //if open false then goto cleanup:
char p[100];
char s[100];
char o[100];
char c[100];

    sprintf(p,"%s",package_char);   //配置文件名
    sprintf(s,"%s",section_char);   //wlan0 
    sprintf(o,"%s",roam_conf_name); //option 对应的名称如:basicnetwork_name 
    sprintf(c,"%s",roam_conf_value);//value值: siot

    struct uci_ptr ptr ={
       .package= p,
       .section= s,
       .option = o,
       .value = c,
    };

    uci_set(ctx, &ptr); //write config
    uci_commit(ctx, &ptr.p, false); //save
    uci_unload(ctx, ptr.p); //unload

cleanup:
uci_free_context(ctx);
ctx=NULL;

}
5、读配置文件,给结构体数组赋值函数;
void read_config_init()
{
printf("read_config_init() is begining \n");
struct uci_package pkg = NULL;
struct uci_element
e;

const char *value; 
ctx = uci_alloc_context(); //  
if (UCI_OK != uci_load(ctx, UCI_ROAM_FILE, &pkg)) 
    goto cleanup;  
//foreach "power" element
uci_foreach_element(&pkg->sections, e) 
{
    struct uci_section *s = uci_to_section(e); 
    //print section type
    //printf("section s's type is %s.\n",s->type);
    //if(!strcmp) if(!strcmp("power",s->type));

         for(int i=0;i<=5;i++){
             if (NULL != (value = uci_lookup_option_string(ctx,\
                    s, Roaming_Conf_Value[i].name))) {
             strcpy(Roaming_Conf_Value[i].value,strdup(value));
             //printf("%s's %s is %s.\n",s->e.name,Roaming_Conf_Value[i].name,value);
            }
        }
} 
uci_unload(ctx, pkg);  

cleanup:
uci_free_context(ctx);
ctx = NULL;
printf("read_config_init() is end \n");
}**

上一篇:基于UCI数据集,比较线性回归和岭回归的错误率


下一篇:python安装包mayavi等问题https://www.lfd.uci.edu/~gohlke/pythonlibs