add support for comments
remove null-termination requirement
add unicode support
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" },
};
exe.addPackage(scfg);
// ...
}
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 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,
};
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.