From 07d34128dd729d0500ae25d3d3a9a8b3439d6f1b Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 7 May 2024 23:58:26 +0200 Subject: [PATCH] use config file as sole param --- .gitignore | 2 +- src/main.zig | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index c33c93b..7545796 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ zig-cache zig-out *.bdf out -*.font.zon \ No newline at end of file +*.font.json \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index af0df05..e0fe883 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,11 @@ const std = @import("std"); const bdf = @import("bdf.zig"); +const FontConfig = struct { + inputFile: []const u8, + outputFile: []const u8, +}; + pub fn main() !void { // Get allocator var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -11,21 +16,36 @@ pub fn main() !void { const args = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, args); - if (args.len < 4) { - std.log.err("Usage: {s} ", .{args[0]}); + if (args.len < 2) { + std.log.err("Usage: {s} ", .{args[0]}); std.process.exit(1); } - const inputFile = args[1]; - const inputConfig = args[2]; - _ = inputConfig; // autofix - const outputPath = args[3]; - _ = outputPath; // autofix + const inputConfig = args[1]; - // Read font - const input = try std.fs.cwd().openFile(inputFile, .{}); + // Read config + const configFile = try std.fs.cwd().readFileAlloc(allocator, inputConfig, std.math.maxInt(usize)); + defer allocator.free(configFile); + + const fontConfig = try readConfig(allocator, configFile); + defer fontConfig.deinit(); + + const input = try std.fs.cwd().openFile(fontConfig.value.inputFile, .{}); defer input.close(); var font = try bdf.parse(allocator, input.reader()); defer font.deinit(allocator); } + +fn readConfig(allocator: std.mem.Allocator, input: []u8) !std.json.Parsed(FontConfig) { + const fontConfig = try std.json.parseFromSlice( + FontConfig, + allocator, + input, + .{ + .ignore_unknown_fields = true, + }, + ); + + return fontConfig; +}