c语言程序读取uci配置,UCI (统一配置接口) – 技术参考资料

花一点时间来理解 "section" 和 "type" 之间的差异是值得的. 让我们从一个例程开始:

#uci show system

system.@system[0]=system

system.@system[0].hostname=OpenWrt

system.@system[0].timezone=UTC

system.@rdate[0]=rdate

system.@rdate[0].server=ac-ntp0.net.cmu.edu ptbtime1.ptb.de ac-ntp1.net.cmu.edu ntp.xs4all.nl ptbtime2.ptb.de cudns.cit.cornell.edu ptbtime3.ptb.de

这里, x:get("system","@rdate[0]","server") 将不正常工作. 因为 rdate 是个 type, 不是 section.

执行 x:get_all("system")的返回结果如下:{

cfg02f02f={[".name"]="cfg02f02f",[".type"]="system",hostname="OpenWrt",[".index"]=0,[".anonymous"]=true,timezone="UTC"},

cfg04e10c={[".name"]="cfg04e10c",[".type"]="rdate",[".index"]=1,[".anonymous"]=true,

server={"ac-ntp0.net.cmu.edu","ptbtime1.ptb.de","ac-tp1.net.cmu.edu","ntp.xs4all.nl","ptbtime2.ptb.de","cudns.cit.cornell.edu","ptbtime3.ptb.de"}}

}

[".type"] 列出了段的类型 [".name"] 列出了段实际名称。这里可以看到,这些名称是系统生成的。 [".index"] 是列表的索引(+1)

据我所知,好像还没有办法直接访问"@rdate[0]"。你必须用x:foreach 迭代列出所给定类型的所有元素。 我一般用下面的函数:

uci=require("uci")

function getConfType(conf,type)

local curs=uci.cursor()

local ifce={}

curs:foreach(conf,type,function(s) ifce[s[".index"]]=s end)

return ifce

end

getConfType("system","rdate") 返回:{{[".name"]="cfg04e10c",[".type"]="rdate",[".index"]=1,[".anonymous"]=true,

server={"ac-ntp0.net.cmu.edu","ptbtime1.ptb.de","ac-ntp1.net.cmu.edu","ntp.xs4all.nl","ptbtime2.ptb.de","cudns.cit.cornell.edu","ptbtime3.ptb.de"}}}

所以,如果你想修改system.@rdate[0].server,你需要迭代所需类型的段名([".name"]),然后调用x:set("system","cfg04e10c","server","zzz.com") 希望这对你有帮助 Sophana (不过, Luci有一个Cursor:get_first 函数实现了类似的功能, 不同的是他类型而不是段作为第二个参数.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值