C语言 linux下解析一个简单的配置文件时使用什么c lib?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2250607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What c lib to use when I need to parse a simple config file under linux?
提问by Johan
Let's say I have a simple config file that my c program needs to read/parse.
假设我有一个简单的配置文件,我的 c 程序需要读取/解析。
Let's say it looks a little bit like this:
假设它看起来有点像这样:
#Some comment
key1=data1
key2=data2
Is there a standard c lib that I can use instead of writing my own parser?
是否有标准的 c lib 可以用来代替编写自己的解析器?
Thanks Johan
谢谢约翰
Note: Today I have my own little parser, but there must be some standard libs that solves this simple problem.
注意:今天我有自己的小解析器,但必须有一些标准库来解决这个简单的问题。
回答by ahe
Why not just use GLIB?
为什么不直接使用 GLIB?
Among countless other things it has library functions for parsing INI like configuration files:
在无数其他东西中,它具有用于解析 INI 之类的配置文件的库函数:
https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html
https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html
Apart from that it also supports Datatypes (Lists, Hashtables, Strings, Caches), Threading, platform neutral abstractions, unit testing, error handling and lots of other great stuff.
除此之外,它还支持数据类型(列表、哈希表、字符串、缓存)、线程、平台中立抽象、单元测试、错误处理和许多其他很棒的东西。
For me it's the most useful C library and I would need to have a very good reason to write a C program without using this library.
对我来说,它是最有用的 C 库,我需要有一个很好的理由来编写 C 程序而不使用这个库。
回答by sampi
I find it worth mentioning libConfusehere, and quote its description:
我觉得这里值得一提libConfuse,并引用它的描述:
libConfuse is a configuration file parser library, licensed under the terms of the ISC license, and written in C. It supports sections and (lists of) values (strings, integers, floats, booleans or other sections), as well as some other features (such as single/double-quoted strings, environment variable expansion, functions and nested include statements). It makes it very easy to add configuration file capability to a program using a simple API.
The goal of libConfuse is not to be the configuration file parser library with a gazillion of features. Instead, it aims to be easy to use and quick to integrate with your code. libConfuse was called libcfg before, but its name was changed to not confuse itself with other similar libraries.
libConfuse 是一个配置文件解析器库,根据 ISC 许可条款获得许可,用 C 编写。它支持部分和(列表)值(字符串、整数、浮点数、布尔值或其他部分),以及一些其他功能(例如单/双引号字符串、环境变量扩展、函数和嵌套的包含语句)。它使使用简单的 API 向程序添加配置文件功能变得非常容易。
libConfuse 的目标不是成为具有无数功能的配置文件解析器库。相反,它旨在易于使用并快速与您的代码集成。libConfuse 之前被称为 libcfg,但它的名称已更改,以免与其他类似的库混淆。
It seems fairly similar to the already mentioned libconfig. There is a short comparison of C and C++ parsers in A study of the existing libraries to read from configuration filesthat might be a useful start for anyone choosing among the alternatives.
它似乎与已经提到的 libconfig 非常相似。在A study of the existing libraries to read from configuration files中对 C 和 C++ 解析器进行了简短的比较,这对于在替代方案中进行选择的任何人来说可能是一个有用的开始。
回答by Marki555
回答by tyilmaz
I have implemented a simple C library which parses configuration file:
我已经实现了一个简单的 C 库来解析配置文件:
https://github.com/taneryilmaz/libconfigini
https://github.com/taneryilmaz/libconfigini
See the tests directory for usage.
有关用法,请参阅测试目录。
回答by Tim Schaeffer
Here's one I've used with success:
这是我成功使用的一个:
http://ndevilla.free.fr/iniparser/
http://ndevilla.free.fr/iniparser/
It's small and independent.
它小而独立。
回答by Vladislav Rastrusny
http://code.jellycan.com/simpleini/
http://code.jellycan.com/simpleini/
You can use simple INI files.
您可以使用简单的 INI 文件。

