在 Linux 上使用 C 语言的 INI 文件

发布于 2024-08-21 20:26:48 字数 333 浏览 3 评论 0原文

是否有一种标准方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

倾听心声的旋律 2024-08-28 20:26:48

Try libconfig:

a simple library for processing structured configuration files, like this one: test.cfg. This file format is more compact and more readable than XML. And unlike XML, it is type-aware, so it is not necessary to do string parsing in application code.

Libconfig is very compact — a fraction of the size of the expat XML parser library. This makes it well-suited for memory-constrained systems like handheld devices.

The library includes bindings for both the C and C++ languages. It works on POSIX-compliant UNIX and UNIX-like systems (GNU/Linux, Mac OS X, Solaris, FreeBSD), Android, and Windows (2000, XP and later)...

辞别 2024-08-28 20:26:48

不,没有一种标准方法。很抱歉,但这可能是最准确的答案:)

您可以查看这个列表不过,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.

寄离 2024-08-28 20:26:48

这里有四个选项:

  1. Iniparser
  2. libini
  3. sdl-cfg
  4. RWini

Here are four options:

  1. Iniparser
  2. libini
  3. sdl-cfg
  4. RWini
入画浅相思 2024-08-28 20:26:48

如果您可以使用(非常好,在任何基于 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".

回忆那么伤 2024-08-28 20:26:48

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.

ペ泪落弦音 2024-08-28 20:26:48

如果您需要快速而小的代码来读取配置文件,我建议 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.

酷遇一生 2024-08-28 20:26:48

我讨厌在建议 XML 时提出完全不同的建议,但 l​​ibexpat 非常小,但 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文