简体   繁体   English

C Library使用基于大括号的语法读取配置文件

[英]C Library to read configuration files with syntax based on curly brackets

For my C projects I'd like to use curly brackets based configuration files like: 对于我的C项目,我想使用基于大括号的配置文件,例如:

account {
    name = "test@test.com";
    password = "test";
    autoconnect = true;
}

etc. or some variations. 等或某些变化。

I'm trying to find some nice C libraries to suit my needs. 我正在尝试找到一些不错的C库来满足我的需求。 Can you please advise? 你能给些建议么?

Your desired syntax is nearly identical to Lua , which would look like this: 你想要的语法几乎与Lua相同,看起来像这样:

account = {
    name = "test@test.com",
    password = "test",
    autoconnect = true,
}

If that suits you, I highly recommend Lua, as it's designed to be embeddable in C programs as a configuration or scripting facility. 如果这适合你,我强烈推荐Lua,因为它可以作为配置或脚本工具嵌入C程序中。 You can either use the raw Lua C API, or if you prefer C++ there are things like Luabind to make certain things prettier in that language. 您可以使用原始Lua C API,或者如果您更喜欢C ++,可以使用像Luabind这样的东西来使某些东西更漂亮。

Here is a trivial example using the pure C Lua API to retrieve values from a buffer which contains a Lua "chunk": http://lua-users.org/wiki/GettingValuesFromLua . 这是一个简单的例子,使用纯C Lua API从包含Lua“块”的缓冲区中检索值: http//lua-users.org/wiki/GettingValuesFromLua You can basically read (or mmap) your configuration file in C, pass the pointer to the text to Lua, have Lua execute it, and then retrieve the bits and pieces iteratively. 您基本上可以在C中读取(或mmap)配置文件,将指向文本的指针传递给Lua,让Lua执行它,然后迭代地检索位和碎片。 An alternative is to do "binding" (for which there is also an example on the Lua wiki ). 另一种方法是进行“绑定”( Lua wiki上也有一个例子 )。 With binding the flow is more like that you set up C structures to represent your configuration data, bind them to Lua, and let the Lua configuration script actually populate (construct) a configuration object which is then accessible from C. Depending on your exact needs this may be better or worse, but in pure C (as opposed to C++), the learning curve may be steeper than the "get values" approach. 通过绑定,流更像是设置C结构来表示配置数据,将它们绑定到Lua,并让Lua配置脚本实际填充(构造)一个配置对象,然后可以从C访问。具体取决于您的确切需求这可能更好或更糟,但在纯C(而不是C ++)中,学习曲线可能比“获取值”方法更陡峭。

I would suggest using a lexer and parser for doing this, either the lex / yacc combo or flex / bison . 我建议使用lexer和解析器来执行此操作, lex / yacc combo或flex / bison

You basically write code in a .l and .y file to describe the layout and the lexer/parser generator creates C code that will process the file for you, calling functions to deliver the data to you. 您基本上在.l.y文件中编写代码来描述布局,而词法分析器/解析器生成器创建将为您处理文件的C代码,调用函数将数据传递给您。

Lexical analysis and parsing are a pain to do unless you're well versed in the art. 除非你精通艺术,否则词法分析和解析是一件痛苦的事情。 Tools like those I've mentioned make the job a lot easier. 像我提到的那些工具使工作变得容易多了。

In the lexer, you get it to recognise the lexical elements like 在词法分析器中,你可以识别词汇元素

  • e_account (account)
  • e_openbrace ({)
  • e_name (name)
  • e_string ("[^"]*")
  • e_semicolon (;)

and so on. 等等。

The lexer is used by the parser to detect the lexical elements and the parser has the higher level rules for deciding what constructs are valid. 解析器使用词法分析器来检测词法元素,并且解析器具有用于确定哪些构造有效的更高级别规则。 Things like an account section being e_account , e_openbrace , zero or more of e_stanza then finally e_closebrace . 像帐户部分e_accounte_openbrace ,零或更多的e_stanza然后最终e_closebrace And also detecting e_stanza as being (among others) e_name , e_equals , e_string then e_semicolon . 并且还将e_stanza检测为e_namee_equalse_string e_semicolon

Most of the intelligence is under the covers (and pretty ugly looking code at least for lex/yacc) but it's better than trying to write it yourself :-) 大部分情报都在幕后(至少对于lex / yacc而言,看起来非常丑陋的代码),但它比自己编写它更好:-)

A variant of what you described would be JSON: 您描述的变体是JSON:

account={
    name: "test@test.com",
    password: "test",
    autoconnect: true
}

lists ~100 libraries to read and write JSON for every conceivable platform and language. 列出~100个库,用于为每种可想到的平台和语言读写JSON。 There are seven libraries alone for C. The nice thing for JSON is interoperability of course and having a data format which is widely accepted (it even has a RFC: rfc4627) C单独有七个库.JSON的好处当然是互操作性,并且有一种被广泛接受的数据格式(它甚至有一个RFC:rfc4627)

libconfuse has nearly the syntax you require: libconfuse几乎具有您需要的语法:

/*
 * This is a C-style multi-line comment
 */

BackLog = 2147483647

bookmark heimdal {
    login = "anonymous"
    password = ${ANONPASS:-anonymous@} # environment variable substitution
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM