Немного упрощено

This commit is contained in:
2026-02-27 01:59:30 +03:00
parent b7f7108f63
commit 0119e51a27

View File

@@ -58,32 +58,8 @@ fn saveAsDialog(ctx: *WindowContext) void {
defer ctx.allocator.free(path_z); defer ctx.allocator.free(path_z);
const path_raw = path_z[0..path_z.len]; const path_raw = path_z[0..path_z.len];
const resolved = ensureJsonPath(ctx.allocator, path_raw) catch |err| { document_json.saveToFile(&open_doc.document, path_raw) catch |err| {
std.debug.print("Save path error: {}\n", .{err});
return;
};
defer if (resolved.owned) ctx.allocator.free(resolved.path);
document_json.saveToFile(&open_doc.document, resolved.path) catch |err| {
std.debug.print("Save file error: {}\n", .{err}); std.debug.print("Save file error: {}\n", .{err});
return; return;
}; };
} }
const ResolvedPath = struct {
path: []const u8,
owned: bool,
};
fn ensureJsonPath(allocator: std.mem.Allocator, path: []const u8) !ResolvedPath {
if (endsWithIgnoreCase(path, ".json")) {
return .{ .path = path, .owned = false };
}
const joined = try std.mem.concat(allocator, u8, &.{ path, ".json" });
return .{ .path = joined, .owned = true };
}
fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
if (haystack.len < needle.len) return false;
return std.ascii.eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
}