diff options
Diffstat (limited to 'src/host/Host.zig')
| -rw-r--r-- | src/host/Host.zig | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig index 4f1214a..05d08f7 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -107,6 +107,9 @@ commands: std.ArrayList(abi.Command) = .empty, pending: std.ArrayList(abi.Event) = .empty, input: abi.Input = .{}, files: std.StringHashMapUnmanaged([]align(memory.page) const u8) = .empty, +/// The paths `appendFile` wrote, each once, 0-terminated: the sketch keeps +/// them. +written: std.StringHashMapUnmanaged(void) = .empty, canvas: [2]u32 = .{ 0, 0 }, cursor: abi.Cursor = .arrow, quit: bool = false, @@ -259,6 +262,9 @@ pub fn deinit(self: *Host) void { std.posix.munmap(entry.value_ptr.*); } self.files.deinit(self.gpa); + var written = self.written.keyIterator(); + while (written.next()) |path| self.gpa.free(path.ptr[0 .. path.len + 1]); + self.written.deinit(self.gpa); self.timeline.deinit(); self.session.deinit(self.gpa); self.store.close(); @@ -1014,6 +1020,7 @@ const api: abi.Host.VTable = .{ .newSound = apiNewSound, .samples = apiSamples, .readFile = apiReadFile, + .appendFile = apiAppendFile, .env = apiEnv, .clock = apiClock, }; @@ -1090,6 +1097,35 @@ fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 { self.files.put(self.gpa, key, bytes) catch self.gpa.free(key); return bytes; } +fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize) callconv(.c) [*:0]const u8 { + const self = hostOf(ctx); + const name = name_ptr[0..name_len]; + if (name.len == 0 or std.mem.indexOfScalar(u8, name, '/') != null) { + self.print("appendFile: {s}: a name, not a path", .{name}); + return ""; + } + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = std.mem.print(&buf, "{s}.{s}", .{ self.options.state, name }) catch return ""; + const file = std.Io.Dir.cwd().createFile(self.io, path, .{ .truncate = false }) catch |err| { + self.print("appendFile: {s}: {t}", .{ path, err }); + return ""; + }; + defer file.close(self.io); + const end = file.length(self.io) catch return ""; + file.writePositionalAll(self.io, bytes[0..len], end) catch |err| { + self.print("appendFile: {s}: {t}", .{ path, err }); + return ""; + }; + const kept = self.written.getOrPut(self.gpa, path) catch return ""; + if (!kept.found_existing) { + const copy = self.gpa.dupeSentinel(u8, path, 0) catch { + self.written.removeByPtr(kept.key_ptr); + return ""; + }; + kept.key_ptr.* = copy; + } + return @ptrCast(kept.key_ptr.*.ptr); +} fn apiClock(_: *anyopaque) u64 { return c.SDL_GetTicksNS(); } @@ -1295,3 +1331,27 @@ test "a state file's snapshots from before sessions become marks, and the file o try std.testing.expectEqual(@as(usize, 1), again.slots); try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, again.slot(0)[0..4], .little)); } + +test "appendFile appends to a file beside the state and names it for readFile" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + var host: Host = undefined; + host.gpa = std.testing.allocator; + host.io = io; + host.written = .empty; + host.options = .{ .state = try std.mem.print(&buf, ".zig-cache/tmp/{s}/t.state", .{&tmp.sub_path}) }; + defer { + var written = host.written.keyIterator(); + while (written.next()) |path| host.gpa.free(path.ptr[0 .. path.len + 1]); + host.written.deinit(host.gpa); + } + const name = "take-1.f32"; + const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2)); + try std.testing.expect(std.mem.endsWith(u8, path, "/t.state.take-1.f32")); + try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2)); + const bytes = try tmp.dir.readFileAlloc(io, "t.state.take-1.f32", host.gpa, .unlimited); + defer host.gpa.free(bytes); + try std.testing.expectEqualStrings("abcd", bytes); +} |
