Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ultra-portable, small complex config file library in ANSI C?

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.

like image 469
soze Avatar asked Apr 02 '11 18:04

soze


2 Answers

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:

  • Mini-XML (36K)
  • Expat (124K)
  • RXP (184K)
like image 129
karlphillip Avatar answered Oct 15 '22 05:10

karlphillip


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.

like image 33
Josh Haberman Avatar answered Oct 15 '22 05:10

Josh Haberman