Update for zig 0.13 and remove libmagic
20250114T160010
This commit is contained in:
parent
934a89e2bf
commit
0d17afe989
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
/zig-cache/
|
/.zig-cache/
|
||||||
/zig-out/
|
/zig-out/
|
||||||
|
26
build.zig
26
build.zig
@ -1,19 +1,21 @@
|
|||||||
const Builder = @import("std").build.Builder;
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn build(b: *Builder) void {
|
pub fn build(b: *std.Build) void {
|
||||||
const mode = b.standardReleaseOptions();
|
const exe = b.addExecutable(.{
|
||||||
const exe = b.addExecutable("docmap", "src/main.zig");
|
.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" });
|
||||||
|
|
||||||
exe.setBuildMode(mode);
|
|
||||||
exe.linkSystemLibrary("SDL2");
|
|
||||||
exe.linkSystemLibrary("SDL2_TTF");
|
|
||||||
exe.linkSystemLibrary("magic");
|
|
||||||
exe.linkSystemLibrary("c");
|
exe.linkSystemLibrary("c");
|
||||||
|
|
||||||
b.default_step.dependOn(&exe.step);
|
b.default_step.dependOn(&exe.step);
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
|
|
||||||
const run = b.step("run", "Run the demo");
|
|
||||||
const run_cmd = exe.run();
|
|
||||||
run.dependOn(&run_cmd.step);
|
|
||||||
}
|
}
|
||||||
|
42
src/main.zig
42
src/main.zig
@ -25,18 +25,12 @@ const FileDocMap = struct {
|
|||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer {
|
defer {
|
||||||
if (gpa.deinit()) {
|
const deinit_status = gpa.deinit();
|
||||||
|
if (deinit_status == .leak) {
|
||||||
std.debug.print("Allocator leak\n", .{});
|
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 allocator = gpa.allocator();
|
||||||
|
|
||||||
const args = try std.process.argsAlloc(allocator);
|
const args = try std.process.argsAlloc(allocator);
|
||||||
@ -52,7 +46,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
defer c.SDL_Quit();
|
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());
|
c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
|
||||||
return error.SDLInitializationFailed;
|
return error.SDLInitializationFailed;
|
||||||
@ -81,12 +75,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
defer c.TTF_Quit();
|
defer c.TTF_Quit();
|
||||||
|
|
||||||
const text_color: c.SDL_Color = c.SDL_Color{
|
const text_color: c.SDL_Color = c.SDL_Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
|
||||||
.r = 0,
|
|
||||||
.g = 0,
|
|
||||||
.b = 0,
|
|
||||||
.a = 255
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: can we embed this file?
|
// TODO: can we embed this file?
|
||||||
const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 12);
|
const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 12);
|
||||||
@ -107,20 +96,19 @@ pub fn main() !void {
|
|||||||
var args_i: usize = 1;
|
var args_i: usize = 1;
|
||||||
while (args_i < args.len) : (args_i += 1) {
|
while (args_i < args.len) : (args_i += 1) {
|
||||||
const arg = args[args_i];
|
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});
|
std.debug.print("searching dir for files: '{s}'\n", .{arg});
|
||||||
|
|
||||||
var iter = iter_dir.iterate();
|
var iter = iter_dir.iterate();
|
||||||
while (try iter.next()) |entry| {
|
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", .{
|
const entry_path = try std.fmt.allocPrint(allocator, "{s}{c}{s}\x00", .{
|
||||||
arg,
|
arg,
|
||||||
std.fs.path.sep,
|
std.fs.path.sep,
|
||||||
entry.name,
|
entry.name,
|
||||||
});
|
});
|
||||||
const magic_result = m.magic_file(magic, @ptrCast([*c]const u8, entry_path));
|
if (std.mem.eql(u8, entry.name[(entry.name.len - 4)..(entry.name.len)], ".org")) {
|
||||||
std.debug.print("'{s}': {s}\n", .{ entry.name, magic_result });
|
std.debug.print("Added\n", .{});
|
||||||
if (std.mem.eql(u8, magic_result[0..5], "text/")) {
|
|
||||||
try file_paths.append(entry_path[0..(entry_path.len - 1)]);
|
try file_paths.append(entry_path[0..(entry_path.len - 1)]);
|
||||||
} else {
|
} else {
|
||||||
allocator.free(entry_path);
|
allocator.free(entry_path);
|
||||||
@ -131,8 +119,8 @@ pub fn main() !void {
|
|||||||
if (std.fs.cwd().openFile(arg, .{})) |file| {
|
if (std.fs.cwd().openFile(arg, .{})) |file| {
|
||||||
file.close();
|
file.close();
|
||||||
std.debug.print("Will do docmap for: '{s}'\n", .{arg});
|
std.debug.print("Will do docmap for: '{s}'\n", .{arg});
|
||||||
var path = try allocator.alloc(u8, arg.len);
|
const path = try allocator.alloc(u8, arg.len);
|
||||||
std.mem.copy(u8, path, arg);
|
@memcpy(path, arg);
|
||||||
try file_paths.append(path);
|
try file_paths.append(path);
|
||||||
} else |_| {
|
} else |_| {
|
||||||
std.debug.print("Arg not file: '{s}'\n", .{arg});
|
std.debug.print("Arg not file: '{s}'\n", .{arg});
|
||||||
@ -150,7 +138,7 @@ pub fn main() !void {
|
|||||||
file_name = 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) {
|
if (surface == null) {
|
||||||
c.SDL_Log("Unable to render text");
|
c.SDL_Log("Unable to render text");
|
||||||
return error.RenderTextFailed;
|
return error.RenderTextFailed;
|
||||||
@ -162,7 +150,7 @@ pub fn main() !void {
|
|||||||
return error.CreateTextureFailed;
|
return error.CreateTextureFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
var rect = c.SDL_Rect{
|
const rect = c.SDL_Rect{
|
||||||
.x = 0,
|
.x = 0,
|
||||||
.y = 0,
|
.y = 0,
|
||||||
.w = surface.*.w,
|
.w = surface.*.w,
|
||||||
@ -174,7 +162,7 @@ pub fn main() !void {
|
|||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
const stat = try file.stat();
|
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);
|
const bytes_read = try file.readAll(buffer);
|
||||||
if (bytes_read != stat.size) {
|
if (bytes_read != stat.size) {
|
||||||
@ -185,7 +173,7 @@ pub fn main() !void {
|
|||||||
return error.FileReadError;
|
return error.FileReadError;
|
||||||
}
|
}
|
||||||
|
|
||||||
var docmap = FileDocMap{
|
const docmap = FileDocMap{
|
||||||
.title_text = DrawnText{
|
.title_text = DrawnText{
|
||||||
.surface = surface,
|
.surface = surface,
|
||||||
.texture = texture,
|
.texture = texture,
|
||||||
@ -214,7 +202,7 @@ pub fn main() !void {
|
|||||||
while (!quit) {
|
while (!quit) {
|
||||||
var event: c.SDL_Event = undefined;
|
var event: c.SDL_Event = undefined;
|
||||||
while (c.SDL_PollEvent(&event) != 0) {
|
while (c.SDL_PollEvent(&event) != 0) {
|
||||||
switch (event.@"type") {
|
switch (event.type) {
|
||||||
c.SDL_QUIT => {
|
c.SDL_QUIT => {
|
||||||
quit = true;
|
quit = true;
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user