c语言程序读取uci配置,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(charpackage_char,char section_char,charroam_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_elemente;

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");

}**

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值