在 Linux 上使用 C 语言的 INI 文件
是否有一种标准方法可以使用 C 读取 Linux 的 INI 文件等配置?
我正在开发基于 Linux 的手持设备并用 C 编写代码。
否则,我想知道是否有任何替代方案。
最终更新:
我已经探索甚至使用了 LibConfig。但占用空间大,而且我的用法太简单。因此,为了减少占用空间,我推出了自己的实现。该实现并不是太通用,实际上到目前为止已经相当耦合了。配置文件在启动应用程序时被解析一次并设置为一些全局变量。
Is there a standard way of reading a kind of configuration like INI files for Linux using C?
I am working on a Linux based handheld and writing code in C.
Otherwise, I shall like to know about any alternatives.
Final update:
I have explored and even used LibConfig. But the footprint is high and my usage is too simple. So, to reduce the footprint, I have rolled out my own implementation. The implementation is not too generic, in fact quite coupled as of now. The configuration file is parsed once at the time of starting the application and set to some global variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试 libconfig:
Try libconfig:
不,没有一种标准方法。很抱歉,但这可能是最准确的答案:)
您可以查看这个列表不过,Linux 配置文件库。这可能会有帮助。
No, there isn't one standard way. I'm sorry, but that is probably the most precise answer :)
You could look at this list of Linux configuration file libraries, though. That might be helpful.
这里有四个选项:
Here are four options:
如果您可以使用(非常好,在任何基于 C 的应用程序中)glib,它有一个 键值文件解析器适用于 .ini 样式文件。当然,您还可以“免费”访问 glib 中的各种(非常好的)数据结构。
If you can use the (excellent, in any C-based application) glib, it has a key-value file parser that is suitable for .ini-style files. Of course, you'd also get access to the various (very nice) data structures in glib, "for free".
在 ccan 处有 iniparser 的更新分支,原作者无法多年来给予它很大的关注。免责声明 - 我维护它。
此外,iniparser 包含一个本身非常有用的字典。
There is an updated fork of iniparser at ccan, the original author has not been able to give it much attention over the years. Disclaimer - I maintain it.
Additionally, iniparser contains a dictionary that is very useful on its own.
如果您需要快速而小的代码来读取配置文件,我建议 inih
它加载配置文件内容只需一次,解析内容并为每个键/值对调用回调函数。
真的很小。它也可以用于嵌入式系统。
If you need a fast and small code just for reading config files I suggest the inih
It loads the config file content just once, parse the content and calls a callback function for each key/value pair.
Really small. It can be used on embedded systems too.
我讨厌在建议 XML 时提出完全不同的建议,但 libexpat 非常小,但 XML 确实如此。
我得出这个结论是因为我和你有同样的问题,但后来我意识到该项目已经链接了 libexpat —— 我可能应该使用它。
I hate to suggest something entirely different in suggesting XML, but libexpat is pretty minimal, but does XML.
I came to this conclusion as I had the same question as you did, but then I realized the project already had libexpat linked-in--and I should probably just use that.