Update for zig 0.13 and remove libmagic

20250114T160010
This commit is contained in:
Nathan McRae 2025-01-14 20:42:54 -08:00
parent 934a89e2bf
commit 0d17afe989
3 changed files with 32 additions and 42 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
/zig-cache/
/.zig-cache/
/zig-out/

View File

@ -1,19 +1,21 @@
const Builder = @import("std").build.Builder;
const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "docmap",
.root_source_file = b.path("src/main.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});
exe.addIncludePath(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2-devel-2.26.5-mingw/SDL2-2.26.5/x86_64-w64-mingw32/include/SDL2" });
exe.addIncludePath(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2_ttf-devel-2.24.0-mingw/SDL2_ttf-2.24.0/x86_64-w64-mingw32/include/SDL2" });
exe.addObjectFile(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2-devel-2.30.11-mingw/SDL2-2.30.11/x86_64-w64-mingw32/bin/SDL2.dll" });
exe.addObjectFile(.{ .cwd_relative = "C:/Users/nathanm/Downloads/SDL2_ttf-devel-2.24.0-mingw/SDL2_ttf-2.24.0/x86_64-w64-mingw32/bin/SDL2_ttf.dll" });
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("docmap", "src/main.zig");
exe.setBuildMode(mode);
exe.linkSystemLibrary("SDL2");
exe.linkSystemLibrary("SDL2_TTF");
exe.linkSystemLibrary("magic");
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
const run = b.step("run", "Run the demo");
const run_cmd = exe.run();
run.dependOn(&run_cmd.step);
}

View File

@ -25,18 +25,12 @@ const FileDocMap = struct {
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit()) {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) {
std.debug.print("Allocator leak\n", .{});
}
}
var magic = m.magic_open(m.MAGIC_MIME | m.MAGIC_CHECK);
const load_result = m.magic_load(magic, null);
if (load_result != 0) {
std.debug.print("Unable to initialize magic: {d}", .{load_result});
return error.MagicInitializationFailed;
}
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
@ -52,7 +46,7 @@ pub fn main() !void {
}
defer c.SDL_Quit();
const screen = c.SDL_CreateWindow("Document Map Viewer", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, window_width, window_height, c.SDL_WINDOW_OPENGL) orelse
const screen = c.SDL_CreateWindow("Document Map Viewer", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, window_width, window_height, c.SDL_WINDOW_OPENGL | c.SDL_WINDOW_RESIZABLE) orelse
{
c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
return error.SDLInitializationFailed;
@ -81,12 +75,7 @@ pub fn main() !void {
}
defer c.TTF_Quit();
const text_color: c.SDL_Color = c.SDL_Color{
.r = 0,
.g = 0,
.b = 0,
.a = 255
};
const text_color: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
// TODO: can we embed this file?
const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 12);
@ -107,20 +96,19 @@ pub fn main() !void {
var args_i: usize = 1;
while (args_i < args.len) : (args_i += 1) {
const arg = args[args_i];
if (std.fs.cwd().openIterableDir(arg, .{})) |iter_dir| {
if (std.fs.cwd().openDir(arg, .{ .iterate = true })) |iter_dir| {
std.debug.print("searching dir for files: '{s}'\n", .{arg});
var iter = iter_dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind == .File) {
if (entry.kind == .file) {
const entry_path = try std.fmt.allocPrint(allocator, "{s}{c}{s}\x00", .{
arg,
std.fs.path.sep,
entry.name,
});
const magic_result = m.magic_file(magic, @ptrCast([*c]const u8, entry_path));
std.debug.print("'{s}': {s}\n", .{ entry.name, magic_result });
if (std.mem.eql(u8, magic_result[0..5], "text/")) {
if (std.mem.eql(u8, entry.name[(entry.name.len - 4)..(entry.name.len)], ".org")) {
std.debug.print("Added\n", .{});
try file_paths.append(entry_path[0..(entry_path.len - 1)]);
} else {
allocator.free(entry_path);
@ -131,8 +119,8 @@ pub fn main() !void {
if (std.fs.cwd().openFile(arg, .{})) |file| {
file.close();
std.debug.print("Will do docmap for: '{s}'\n", .{arg});
var path = try allocator.alloc(u8, arg.len);
std.mem.copy(u8, path, arg);
const path = try allocator.alloc(u8, arg.len);
@memcpy(path, arg);
try file_paths.append(path);
} else |_| {
std.debug.print("Arg not file: '{s}'\n", .{arg});
@ -149,8 +137,8 @@ pub fn main() !void {
while (it.next()) |token| {
file_name = token;
}
const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, @ptrCast([*c]const u8, file_name), text_color);
const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, @ptrCast(file_name), text_color);
if (surface == null) {
c.SDL_Log("Unable to render text");
return error.RenderTextFailed;
@ -162,7 +150,7 @@ pub fn main() !void {
return error.CreateTextureFailed;
}
var rect = c.SDL_Rect{
const rect = c.SDL_Rect{
.x = 0,
.y = 0,
.w = surface.*.w,
@ -174,7 +162,7 @@ pub fn main() !void {
defer file.close();
const stat = try file.stat();
var buffer = try allocator.alloc(u8, stat.size);
const buffer = try allocator.alloc(u8, stat.size);
const bytes_read = try file.readAll(buffer);
if (bytes_read != stat.size) {
@ -185,7 +173,7 @@ pub fn main() !void {
return error.FileReadError;
}
var docmap = FileDocMap{
const docmap = FileDocMap{
.title_text = DrawnText{
.surface = surface,
.texture = texture,
@ -214,7 +202,7 @@ pub fn main() !void {
while (!quit) {
var event: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&event) != 0) {
switch (event.@"type") {
switch (event.type) {
c.SDL_QUIT => {
quit = true;
},