build: add oneshot parsing example
This commit is contained in:
29
examples/parse.zig
Normal file
29
examples/parse.zig
Normal file
@@ -0,0 +1,29 @@
|
||||
const std = @import("std");
|
||||
|
||||
const nice = @import("nice");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const args = try std.process.argsAlloc(allocator);
|
||||
defer std.process.argsFree(allocator, args);
|
||||
if (args.len < 2) return;
|
||||
|
||||
const data = try std.fs.cwd().readFileAlloc(allocator, args[1], 4_294_967_295);
|
||||
var needfree = true;
|
||||
defer if (needfree) allocator.free(data);
|
||||
|
||||
var parser = nice.Parser{ .allocator = allocator };
|
||||
|
||||
const document = try parser.parseBuffer(data);
|
||||
defer document.deinit();
|
||||
|
||||
// free data memory to ensure that the parsed document is not holding
|
||||
// references to it.
|
||||
allocator.free(data);
|
||||
needfree = false;
|
||||
|
||||
document.printDebug();
|
||||
}
|
Reference in New Issue
Block a user