From f3911ead38805b28f59d79f27a43c8955adebc73 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 30 Oct 2023 13:48:02 +0100 Subject: [PATCH 1/2] add main state --- src/main.zig | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main.zig b/src/main.zig index 6963fb9..381f173 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,6 +4,16 @@ const c = @cImport({ @cInclude("sqlite3.h"); }); +const StateTag = enum { + unloaded, + loaded, +}; + +const State = union(StateTag) { + unloaded: void, + loaded: *c.sqlite3, +}; + pub fn main() !void { const alloc = std.heap.page_allocator; @@ -12,12 +22,12 @@ pub fn main() !void { raylib.InitWindow(screen_width, screen_height, "FabApp"); raylib.SetTargetFPS(60); + var state = State{ .unloaded = {} }; + var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory()); // const ext = ".sqlite3"; // @memcpy(file_dialog_state.filterExt[0..ext.len], ext); - var db: ?*c.sqlite3 = undefined; - while (!raylib.WindowShouldClose()) { // // Update @@ -32,12 +42,16 @@ pub fn main() !void { const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file }); defer alloc.free(db_path); - const result = c.sqlite3_open(db_path.ptr, &db); - if (result != c.SQLITE_OK or db == null) { - std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db) }); - _ = c.sqlite3_close(db); - } else { + var db_opt: ?*c.sqlite3 = undefined; + + const result = c.sqlite3_open(db_path.ptr, &db_opt); + if (result != c.SQLITE_OK) { + std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) }); + _ = c.sqlite3_close(db_opt); + } + if (db_opt) |db| { std.debug.print("[SQLite] Success\n", .{}); + state = State{ .loaded = db }; } } file_dialog_state.SelectFilePressed = false; @@ -51,14 +65,20 @@ pub fn main() !void { defer raylib.EndDrawing(); raylib.ClearBackground(raylib.RAYWHITE); - { - if (file_dialog_state.windowActive) raylib.GuiLock(); - defer raylib.GuiUnlock(); + switch (state) { + .unloaded => { + if (file_dialog_state.windowActive) raylib.GuiLock(); + defer raylib.GuiUnlock(); - const button_size = 200; - if (raylib.GuiButton(.{ .x = (screen_width - button_size) / 2, .y = (screen_height - button_size) / 2, .width = 200, .height = 200 }, "Load db file") == 1) { - file_dialog_state.windowActive = true; - } + const button_size = 200; + if (raylib.GuiButton(.{ .x = (screen_width - button_size) / 2, .y = (screen_height - button_size) / 2, .width = 200, .height = 200 }, "Load db file") == 1) { + file_dialog_state.windowActive = true; + } + }, + + .loaded => |*db| { + _ = db; + }, } raylib.GuiWindowFileDialog(&file_dialog_state); From 133b5a95599de4e1c59251daf34797aa7b175fe6 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 30 Oct 2023 14:02:11 +0100 Subject: [PATCH 2/2] load sqlite from cli --- justfile | 2 +- src/main.zig | 48 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/justfile b/justfile index ca2aa4c..15f5c3d 100644 --- a/justfile +++ b/justfile @@ -28,7 +28,7 @@ build: # # zig build -Dtarget=wasm32-emscripten run: - nixGL zig build run + nixGL zig build run -- {{ justfile_directory() }}/life.sqlite3 test: zig build test diff --git a/src/main.zig b/src/main.zig index 381f173..166c99d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -14,16 +14,45 @@ const State = union(StateTag) { loaded: *c.sqlite3, }; +fn loadSqlite(path: [:0]const u8) State { + var db_opt: ?*c.sqlite3 = undefined; + + const result = c.sqlite3_open(path.ptr, &db_opt); + if (result != c.SQLITE_OK) { + std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) }); + _ = c.sqlite3_close(db_opt); + return State{ .unloaded = {} }; + } + + if (db_opt) |db| { + std.debug.print("[SQLite] Success\n", .{}); + return State{ .loaded = db }; + } + + return State{ .unloaded = {} }; +} + pub fn main() !void { - const alloc = std.heap.page_allocator; + var state = State{ .unloaded = {} }; + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + + const args = try std.process.argsAlloc(alloc); + defer std.process.argsFree(alloc, args); + + std.debug.print("Arguments: {s}\n", .{args}); + + if (args.len > 1) { + state = loadSqlite(args[1]); + } const screen_width = 800; const screen_height = 450; raylib.InitWindow(screen_width, screen_height, "FabApp"); raylib.SetTargetFPS(60); - var state = State{ .unloaded = {} }; - var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory()); // const ext = ".sqlite3"; // @memcpy(file_dialog_state.filterExt[0..ext.len], ext); @@ -41,18 +70,7 @@ pub fn main() !void { // slices -> C-string (null-terminated) const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file }); defer alloc.free(db_path); - - var db_opt: ?*c.sqlite3 = undefined; - - const result = c.sqlite3_open(db_path.ptr, &db_opt); - if (result != c.SQLITE_OK) { - std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) }); - _ = c.sqlite3_close(db_opt); - } - if (db_opt) |db| { - std.debug.print("[SQLite] Success\n", .{}); - state = State{ .loaded = db }; - } + state = loadSqlite(db_path); } file_dialog_state.SelectFilePressed = false; }