A zig library for scfg
28 days ago

builds.sr.ht via public-inbox

28 days ago


builds.sr.ht status

A zig library for parsing scfg configuration files.


Clone this repository as a submodule.

git submodule add https://git.sr.ht/~andreafeletto/zig-scfg deps/zig-scfg

Than add the following to your build.zig.

pub fn build(b: *std.build.Builder) void {
    // ...
    const scfg: Pkg = .{
        .name = "scfg",
        .path = .{ .path = "deps/zig-scfg/scfg.zig" },
    // ...

The library can now be imported into your zig project.

const scfg = @import("scfg");


I suggested to use an arena allocator. The resulting tree structure can be quite complex, so manual deallocation could be tricky.

const std = @import("std");
const scfg = @import("scfg");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = arena.allocator();

    const file = try std.fs.cwd().openFile("example.scfg", .{});
    // `source` must be a null-terminated string
    const source = try file.readToEndAllocOptions(
        allocator, 1_000_000, null, @alignOf(u8), 0

    const root = try scfg.parse(allocator, source);
    std.log.info("identifier of the first directive: {s}", .{root[0].name});

The result of the parse function is a slice of pointers to directives. Each directive has the following recursive structure:

const Directive = struct {
    name: []const u8,
    params: [][]const u8,
    blocks: [][]*Directive,


The code in this repository is released under the MIT license. You are welcome to send patches to the mailing list or report bugs on the issue tracker.

If you aren't familiar with git send-email, you can use the web interface or learn about it following this excellent tutorial.