ANSI C 中的超便携、小型复杂配置文件库?

发布于 2024-10-30 01:43:31 字数 303 浏览 2 评论 0原文

我正在寻找一个非常可移植、简约/小型的 ANSI C 语言库,没有外部依赖项(或很少),编译后大小小于 100K。我需要它来创建一个中等复杂的配置文件,并且它必须支持 Unicode。

更多要求:

  1. 可以使用/嵌入/静态链接到专有代码。在应得的信用处,总会给予信用。
  2. 不一定是 XML。
  3. 确实,干净的代码/没有奇怪或不一致的字符串处理。
  4. UTF-8

谢谢你们。

I'm looking for a very portable, minimalistic/small XML/configuration language library in ANSI C with no external dependencies (or very few), compiling down to less than 100K. I need it for a moderately complex configuration file, and it must support Unicode.

Some more requirements:

  1. OK to use/embed/statically link into proprietary code. Credit will always will be given where credit is due.
  2. Not necessarily XML.
  3. Really, clean code/no weird or inconsistent string handling.
  4. UTF-8.

Thank you fellas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

断舍离 2024-11-06 01:43:31

这在某种程度上类似于这个问题: Is there a good tiny XML parser for an嵌入式 C 项目吗?

我能够调整以下 C 语言 XML 解析器库的编译标志,并在我的 Ubuntu 机器上减少了 50% 以上的大小。 Mini-XML 是唯一接近您要求的:

  • Mini-XML (36K)
  • < a href="http://expat.sourceforge.net/" rel="nofollow noreferrer">外籍人士 (124K)
  • RXP (184K)

This is somehow similar to this question: Is there a good tiny XML parser for an embedded C project?

I was able to tweak the compilation flags of the following XML parser libraries for C, and cut down more than 50% of their size on my Ubuntu machine. Mini-XML is the only one close to what you requested:

烟火散人牵绊 2024-11-06 01:43:31

对于此类用例,IMO Protocol Buffers 是比 XML 更好得多的解决方案。使用协议缓冲区,您可以获得真实的类型和模式,而无需将它们分层在基本 XML 之上。而且语法也更好。

// The schema file: can serve as documentation for what
// configuration values are available.
message MyAppConfig {
  // Set to control the port the app listens on.
  optional int32 port = 1 [default=1234];

  // Set to control the local hostname.
  optional string hostname = 2 [default="localhost"];
}

那么用户的实际配置将如下所示:

# I want to listen on a very high port.
port: 50000

主协议缓冲区库不符合您的标准,因为它是用 C++ 编写的并且非常大。我正在研究一个更小的实现,称为 upb (即“micro”protobufs) 。它由大约 5k 行 ANSI C 编写而成,编译后的结果为 <50k。

Protocol buffers 有二进制格式和文本格式,它们是等效的。我的库(尚)不支持读取文本格式,但您可以让您的用户使用 Google 标准工具提前将其配置的文本版本转换为二进制格式。然后你的应用程序本身只需要读取二进制格式,并且可以只使用 upb。

upb 刚刚达到了喜欢冒险的用户可以尝试的阶段,但它的边缘仍然有点粗糙,API 仍在发生一些变化。如果您对此感到满意,那么您现在可以尝试深入了解。如果你喜欢更稳定的东西,至少要保持警惕。

IMO Protocol Buffers are a much, much better solution for this kind of use case than XML. With protocol buffers you get real types and a schema without having to layer them on top of base XML. Also the syntax is nicer.

// The schema file: can serve as documentation for what
// configuration values are available.
message MyAppConfig {
  // Set to control the port the app listens on.
  optional int32 port = 1 [default=1234];

  // Set to control the local hostname.
  optional string hostname = 2 [default="localhost"];
}

Then the user's actual config would look like this:

# I want to listen on a very high port.
port: 50000

The main protocol buffer library does not fit your criteria because it is in C++ and is very large. I am working on a much smaller implementation of the same called upb (ie. "micro" protobufs). It is written in ~5k lines of ANSI C and compiles to <50k.

Protocol buffers have both a binary and a text format, which are equivalent. My library does not (yet) support reading the text format, but you could have your users use the Google standard tool for converting the text version of their config to binary format ahead of time. Then your app itself would only need to read the binary format, and could just use upb.

upb is just now getting to the point where adventurous users could try it out, but it's a bit rough around the edges still and the APIs are still changing somewhat. If you're ok with this, you might try diving in now. If you prefer something more stable, at least keep upb on your radar.

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