~andreafeletto/zig-scfg

A zig library for scfg
94c38830 — Andrea Feletto 2 years ago
add support for comments
8e20d767 — Andrea Feletto 2 years ago
remove null-termination requirement
83fd4ea5 — Andrea Feletto 2 years ago
add unicode support

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~andreafeletto/zig-scfg
read/write
git@git.sr.ht:~andreafeletto/zig-scfg

You can also use your local clone with git send-email.

#zig-scfg

builds.sr.ht status

A zig library for parsing scfg configuration files.

#Setup

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" },
    };
    exe.addPackage(scfg);
    // ...
}

The library can now be imported into your zig project.

const scfg = @import("scfg");

#Usage

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 source = try std.fs.cwd().readFileAlloc(
        allocator,
        "example.scfg",
        1_000_000,
    );

    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,
};

#Contributing

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.

Do not follow this link