Skip to content

Commit

Permalink
Move from JSON to ini for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Aug 21, 2023
1 parent b87ff4c commit 7573bb7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
zig-out/
zig-cache/
fresh_presence_config.json
fresh_presence_config.ini
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn build(b: *std.Build) !void {
try exe.include_dirs.appendSlice(zboxer_lib.include_dirs.items);
exe.linkLibC();
exe.addModule("rpc", b.dependency("zig-discord", .{}).module("rpc"));
exe.addModule("zini", b.dependency("zini", .{}).module("zini"));
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
.url = "https://github.com/hexops/xcode-frameworks-pkg/archive/d486474a6af7fafd89e8314e0bf7eca4709f811b.tar.gz",
.hash = "1220293eae4bf67a7c27a18a8133621337cde91a97bc6859a9431b3b2d4217dfb5fb",
},
.zini = .{
.url = "https://github.com/Beyley/zini/archive/48af291bf585a71960e8fc4dcbaf42e92a79d5df.tar.gz",
.hash = "12207734f3073b9e9cd73a477e3ab59b47956da56f6e198acde61aa73a946d6aa181",
},
},
}
18 changes: 9 additions & 9 deletions src/config.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const zini = @import("zini");

const c = @import("c.zig").c;

Expand All @@ -18,7 +19,7 @@ pub fn deinit(self: Self, allocator: std.mem.Allocator) void {
pub fn getConfig(allocator: std.mem.Allocator) !Self {
var cwd = std.fs.cwd();

const config_path = "fresh_presence_config.json";
const config_path = "fresh_presence_config.ini";

var file = cwd.openFile(config_path, .{}) catch |err| {
if (err == std.fs.File.OpenError.FileNotFound) {
Expand All @@ -28,7 +29,7 @@ pub fn getConfig(allocator: std.mem.Allocator) !Self {
defer file.close();

var buffered_writer = std.io.bufferedWriter(file.writer());
try std.json.stringify(default_config, .{}, buffered_writer.writer());
try zini.stringify(buffered_writer.writer(), default_config);
try buffered_writer.flush();

_ = c.boxerShow(
Expand All @@ -47,15 +48,14 @@ pub fn getConfig(allocator: std.mem.Allocator) !Self {

var buffered_reader = std.io.bufferedReader(file.reader());

var reader = std.json.reader(allocator, buffered_reader.reader());
defer reader.deinit();
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();

const temp_config = try std.json.parseFromTokenSource(Self, allocator, &reader, .{});
defer temp_config.deinit();
const temp_config = try zini.readStruct(buffered_reader.reader(), Self, arena.allocator());

return Self{
.username = try allocator.dupe(u8, temp_config.value.username),
.instance_url = try allocator.dupe(u8, temp_config.value.instance_url),
.close_upon_game_exit = temp_config.value.close_upon_game_exit,
.username = try allocator.dupe(u8, temp_config.username),
.instance_url = try allocator.dupe(u8, temp_config.instance_url),
.close_upon_game_exit = temp_config.close_upon_game_exit,
};
}

0 comments on commit 7573bb7

Please sign in to comment.