base port to zig 0.12

This commit is contained in:
Luna 2024-06-01 15:46:37 -03:00
parent c73b98a356
commit c0da5e68b7
8 changed files with 66 additions and 62 deletions

View file

@ -1,8 +1,6 @@
const std = @import("std");
const builds = std.build;
const Builder = std.build.Builder;
fn setupLinks(step: *builds.LibExeObjStep) void {
fn setupLinks(step: *std.Build.Step.Compile) void {
step.linkSystemLibrary("c");
step.linkSystemLibrary("lilv-0");
@ -41,13 +39,13 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
}
}
pub fn build(b: *Builder) void {
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "scritcher",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
@ -64,7 +62,7 @@ pub fn build(b: *Builder) void {
run_step.dependOn(&run_cmd.step);
const test_step = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

View file

@ -26,10 +26,10 @@ pub fn sopen(
mode: i32,
fmt: *c.SF_INFO,
) !*c.SNDFILE {
var cstr_path = try allocator.dupeZ(u8, path);
const cstr_path = try allocator.dupeZ(u8, path);
defer allocator.free(cstr_path);
var file = c.sf_open(cstr_path.ptr, mode, fmt);
const file = c.sf_open(cstr_path.ptr, mode, fmt);
const st: i32 = c.sf_error(file);
if (st != 0) {
@ -78,7 +78,7 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXXXXXXXXXXXXXXX";
var nam = try allocator.alloc(u8, template.len);
std.mem.copy(u8, nam, template);
std.mem.copyForwards(u8, nam, template);
const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))));
var r = std.rand.DefaultPrng.init(seed);
@ -89,8 +89,8 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
while (i < 100) : (i += 1) {
// generate a random uppercase letter, that is, 65 + random number.
for (fill, 0..) |_, f_idx| {
var idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24)));
var letter = @as(u8, 65) + idx;
const idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24)));
const letter = @as(u8, 65) + idx;
fill[f_idx] = letter;
}
@ -139,9 +139,9 @@ pub const Image = struct {
/// Open a BMP image for later.
pub fn open(allocator: std.mem.Allocator, path: []const u8) !*Image {
var in_fmt = mkSfInfo();
var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
const sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
var image = try allocator.create(Image);
const image = try allocator.create(Image);
std.debug.assert(in_fmt.frames > @as(i64, 0));
std.debug.assert(in_fmt.seekable == @as(i32, 1));
@ -160,10 +160,10 @@ pub const Image = struct {
pub fn clone(self: *Image) !*Image {
var in_fmt = mkSfInfo();
// clone sndfile
var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt);
const sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt);
std.debug.assert(self.frames == @as(usize, @intCast(in_fmt.frames)));
var image = try self.allocator.create(Image);
const image = try self.allocator.create(Image);
std.debug.assert(in_fmt.frames > @as(i64, 0));
std.debug.assert(in_fmt.seekable == @as(i32, 1));
@ -180,7 +180,7 @@ pub const Image = struct {
}
pub fn close(self: *Image) void {
var st: i32 = c.sf_close(self.sndfile);
const st: i32 = c.sf_close(self.sndfile);
if (st != 0) {
log.debug("Failed to close {s} ({s})", .{
self.path,
@ -252,7 +252,7 @@ pub const Image = struct {
fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos {
const file_end = self.frames;
var seek_pos = position.seekPos(file_end);
const seek_pos = position.seekPos(file_end);
log.debug("\tstart {d} end {d}", .{ seek_pos.start, seek_pos.end });
return seek_pos;
}
@ -321,10 +321,10 @@ pub const Image = struct {
// now, for each param for the plugin, we find its port, and set
// the value for the port there.
for (params.items) |param| {
var sym_cstr = try self.allocator.dupeZ(u8, param.sym);
const sym_cstr = try self.allocator.dupeZ(u8, param.sym);
defer self.allocator.free(sym_cstr);
var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr);
const sym = c.lilv_new_string(ctx.world, sym_cstr.ptr);
const port = c.lilv_plugin_get_port_by_symbol(ctx.plugin, sym) orelse {
log.debug("assert fail: symbol {s} not found on port", .{param.sym});
return ImageError.InvalidSymbol;
@ -332,7 +332,7 @@ pub const Image = struct {
c.lilv_node_free(sym);
var idx = c.lilv_port_get_index(ctx.plugin, port);
const idx = c.lilv_port_get_index(ctx.plugin, port);
log.debug("\tset sym={s}, idx={d} to val={}", .{
param.sym,
idx,
@ -343,11 +343,11 @@ pub const Image = struct {
// now we need to generate a temporary file and put the output of
// running the plugin on that file
var tmpnam = try temporaryName(self.allocator);
const tmpnam = try temporaryName(self.allocator);
log.debug("\trunning plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam });
var out_fmt = mkSfInfo();
var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
const out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
var rctx = try plugins.RunContext.init(self.allocator, ctx.plugin);
defer rctx.deinit();
@ -380,7 +380,7 @@ pub const Image = struct {
log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end });
var inbuf = &rctx.buffers.in;
var outbuf = &rctx.buffers.out;
const outbuf = &rctx.buffers.out;
while (i <= seek_pos.end) : (i += 1) {
inbuf[0] = 0;
@ -416,7 +416,7 @@ pub const Image = struct {
try self.reopen(tmpnam);
try self.checkValid();
var time_taken = timer.read();
const time_taken = timer.read();
log.debug("\ttook {d:.2}ms running plugin", .{time_taken / std.time.us_per_ms});
}
@ -431,7 +431,7 @@ pub const Image = struct {
position: plugins.Position,
extra: anytype,
) !void {
var plugin_opt: ?Plugin = Plugin.init(self.allocator, extra);
const plugin_opt: ?Plugin = Plugin.init(self.allocator, extra);
if (plugin_opt == null) {
return ImageError.PluginLoadFail;
}
@ -448,11 +448,11 @@ pub const Image = struct {
// the code here is a copypaste of runPlugin() without the specific
// lilv things.
var tmpnam = try temporaryName(self.allocator);
const tmpnam = try temporaryName(self.allocator);
log.debug("\trunning CUSTOM plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam });
var out_fmt = mkSfInfo();
var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
const out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
var bufs = plugins.RunBuffers{};
const seek_pos = self.getSeekPos(position);
@ -478,8 +478,8 @@ pub const Image = struct {
var i: usize = seek_pos.start;
log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end });
var inbuf = &bufs.in;
var outbuf = &bufs.out;
const inbuf = &bufs.in;
const outbuf = &bufs.out;
while (i <= seek_pos.end) : (i += 1) {
const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1);

View file

@ -143,7 +143,8 @@ pub const Command = struct {
if (base.tag != T.base_tag)
return null;
return @fieldParentPtr(T, "base", base);
const ptr: *const T = @alignCast(@fieldParentPtr("base", base));
return ptr;
}
pub fn print(base: *const @This()) void {
@ -641,7 +642,7 @@ pub const Lang = struct {
// TODO better tokenizer instead of just tokenize(" ")...maybe????
var tok_it = std.mem.splitSequence(u8, stmt, " ");
var cmd_opt = tok_it.next();
const cmd_opt = tok_it.next();
if (cmd_opt == null) {
self.doError("No command given", .{});
continue;
@ -669,11 +670,14 @@ pub const Lang = struct {
}
comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
var runtime_lowered_command_name = [_]u8{0} ** struct_name.len;
comptime {
for (struct_name, 0..) |c, i| {
lowered_command_name[i] = std.ascii.toLower(c);
}
}
const c_l = lowered_command_name;
std.mem.copyForwards(u8, &runtime_lowered_command_name, &c_l);
// if we have a match, we know the proper struct type
// to use. this actually works compared to storing command_struct
@ -685,7 +689,7 @@ pub const Lang = struct {
// Attempting to use ComptimeHashMap hits compiler bugs and I'm
// not sure if we can map strings to *types* in it.
if ((!found) and std.mem.eql(u8, &lowered_command_name, command_string)) {
if ((!found) and std.mem.eql(u8, &runtime_lowered_command_name, command_string)) {
found = true;
try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds);
}

View file

@ -64,13 +64,13 @@ pub const Port = struct {
///
/// Caller owns returned memory.
pub fn setupPorts(ctx: *plugin.Context) ![]Port {
var world = ctx.world;
const world = ctx.world;
const n_ports: u32 = c.lilv_plugin_get_num_ports(ctx.plugin);
var ports = try ctx.allocator.alloc(Port, n_ports);
for (ports, 0..) |_, idx| {
var port: *Port = &ports[idx];
const port: *Port = &ports[idx];
port.* = Port{
.lilv_port = null,
.ptype = .Control,
@ -81,23 +81,23 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port {
};
}
var values: []f32 = try ctx.allocator.alloc(f32, n_ports);
const values: []f32 = try ctx.allocator.alloc(f32, n_ports);
defer ctx.allocator.free(values);
c.lilv_plugin_get_port_ranges_float(ctx.plugin, null, null, values.ptr);
var lv2_InputPort = c.lilv_new_uri(world, LV2_CORE__InputPort.ptr).?;
const lv2_InputPort = c.lilv_new_uri(world, LV2_CORE__InputPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_InputPort);
var lv2_OutputPort = c.lilv_new_uri(world, LV2_CORE__OutputPort.ptr).?;
const lv2_OutputPort = c.lilv_new_uri(world, LV2_CORE__OutputPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_OutputPort);
var lv2_AudioPort = c.lilv_new_uri(world, LV2_CORE__AudioPort.ptr).?;
const lv2_AudioPort = c.lilv_new_uri(world, LV2_CORE__AudioPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_AudioPort);
var lv2_ControlPort = c.lilv_new_uri(world, LV2_CORE__ControlPort.ptr).?;
const lv2_ControlPort = c.lilv_new_uri(world, LV2_CORE__ControlPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_ControlPort);
var lv2_connection_string = c.lilv_new_uri(world, LV2_CORE__connectionOptional.ptr).?;
const lv2_connection_string = c.lilv_new_uri(world, LV2_CORE__connectionOptional.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_connection_string);
var i: u32 = 0;

View file

@ -15,7 +15,7 @@ pub const MagickContext = struct {
pub fn init() !MagickContext {
mc.InitializeMagick(null);
var wand = mc.NewMagickWand();
const wand = mc.NewMagickWand();
if (wand == null) return error.WandCreateFail;
return MagickContext{
@ -38,7 +38,7 @@ fn magickLoad(image: *Image) !MagickContext {
var mctx = try MagickContext.init();
errdefer mctx.deinit();
var curpath = try image.allocator.dupeZ(u8, image.curpath);
const curpath = try image.allocator.dupeZ(u8, image.curpath);
defer image.allocator.free(curpath);
log.debug("loading '{s}'", .{curpath});
@ -52,8 +52,8 @@ fn magickLoad(image: *Image) !MagickContext {
fn magickSave(image: *Image, wand: *mc.MagickWand) !void {
const allocator = image.allocator;
var tmpnam = try images.temporaryName(allocator);
var c_tmpnam = try allocator.dupeZ(u8, tmpnam);
const tmpnam = try images.temporaryName(allocator);
const c_tmpnam = try allocator.dupeZ(u8, tmpnam);
defer allocator.free(c_tmpnam);
log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam});
@ -72,7 +72,7 @@ pub fn runRotate(image: *Image, deg: f32, bgfill: []const u8) !void {
var mctx = try magickLoad(image);
defer mctx.deinit();
var bg = mc.NewPixelWand();
const bg = mc.NewPixelWand();
defer mc.DestroyPixelWand(bg);
if (mc.PixelSetColor(bg, bgfill.ptr) != 1)

View file

@ -30,6 +30,8 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt
var heap_cmd = try allocator.create(CommandStruct);
heap_cmd.* = casted.*;
log.debug("casted: {}", .{casted});
log.debug("heap_cmd: {}", .{heap_cmd});
return &heap_cmd.base;
}
@ -54,7 +56,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
defer lang.deinit();
if (total_bytes > 0) {
var scri_existing = try allocator.alloc(u8, total_bytes);
const scri_existing = try allocator.alloc(u8, total_bytes);
_ = try file_read_opt.?.read(scri_existing);
defer allocator.free(scri_existing);
@ -94,7 +96,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
defer file.close();
var out = file.writer();
var stream = &out;
const stream = &out;
// since we opened the file for writing, it becomes empty, so, to ensure
// we don't fuck up later on, we print cmds before starting the repl
@ -118,7 +120,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
// run the load command
try runner.runCommands(cmds, true);
const wanted_runner: []const u8 = std.os.getenv("SCRITCHER_RUNNER") orelse "ristretto";
const wanted_runner: []const u8 = std.posix.getenv("SCRITCHER_RUNNER") orelse "ristretto";
var runqs_cmd = langs.Command.RunQS{
.base = langs.Command{ .tag = langs.Command.Tag.runqs },
@ -136,7 +138,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
readline.add_history(rd_line);
//defer std.heap.c_allocator.destroy(rd_line);
var line = rd_line[0..std.mem.len(rd_line)];
const line = rd_line[0..std.mem.len(rd_line)];
if (std.mem.eql(u8, line, "push")) {
const heap_cmd = switch (current.tag) {
@ -250,7 +252,7 @@ fn doRun(allocator: std.mem.Allocator, args_it: anytype) !void {
// sadly, we read it all into memory. such is life
const total_bytes = try file.getEndPos();
var data = try allocator.alloc(u8, total_bytes);
const data = try allocator.alloc(u8, total_bytes);
defer allocator.free(data);
_ = try file.read(data);

View file

@ -36,7 +36,7 @@ pub const Position = struct {
pub fn seekPos(self: Position, total_size: usize) SeekPos {
std.debug.assert(self.index <= self.split);
var tot = total_size / self.split;
const tot = total_size / self.split;
return SeekPos{
.start = self.index * tot,
@ -80,7 +80,7 @@ pub const RunContext = struct {
) !RunContext {
_ = allocator; // TODO batch RunBuffers?
var instance = c.lilv_plugin_instantiate(plugin, @as(f64, 44100), null);
const instance = c.lilv_plugin_instantiate(plugin, @as(f64, 44100), null);
errdefer c.lilv_instance_free(instance);
if (instance == null) {
@ -102,7 +102,7 @@ pub const RunContext = struct {
var o: usize = 0;
for (ports, 0..) |_, p_idx| {
var p = @as(u32, @intCast(p_idx));
const p = @as(u32, @intCast(p_idx));
var port: *lv2.Port = &ports[p_idx];
switch (port.ptype) {
@ -134,12 +134,12 @@ pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Contex
const cstr_plugin_uri = try allocator.dupeZ(u8, plugin_uri);
defer allocator.free(cstr_plugin_uri);
var world: *c.LilvWorld = c.lilv_world_new().?;
const world: *c.LilvWorld = c.lilv_world_new().?;
errdefer c.lilv_world_free(world);
c.lilv_world_load_all(world);
var uri: *c.LilvNode = c.lilv_new_uri(world, cstr_plugin_uri.ptr) orelse {
const uri: *c.LilvNode = c.lilv_new_uri(world, cstr_plugin_uri.ptr) orelse {
log.debug("Invalid plugin URI <{s}>", .{plugin_uri});
return ImageError.InvalidPlugin;
};
@ -147,7 +147,7 @@ pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Contex
const plugins: *const c.LilvPlugins = c.lilv_world_get_all_plugins(world).?;
var plugin: *const c.LilvPlugin = c.lilv_plugins_get_by_uri(plugins, uri) orelse {
const plugin: *const c.LilvPlugin = c.lilv_plugins_get_by_uri(plugins, uri) orelse {
log.debug("Plugin <{s}> not found", .{plugin_uri});
return ImageError.UnknownPlugin;
};

View file

@ -47,7 +47,7 @@ pub const Runner = struct {
}
pub fn clone(self: *Runner) !Runner {
var cloned_image = if (self.image) |image| try image.clone() else null;
const cloned_image = if (self.image) |image| try image.clone() else null;
return Runner{
.allocator = self.allocator,
.image = cloned_image,
@ -128,12 +128,12 @@ pub const Runner = struct {
fn makeGlitchedPath(self: *Runner) ![]const u8 {
// we want to transform basename, if it is "x.bmp" to "x_gN.bmp", where
// N is the maximum non-used integer.
var image = try self.getImage();
const image = try self.getImage();
const basename = std.fs.path.basename(image.path);
const dirname = std.fs.path.dirname(image.path).?;
var dir = try std.fs.cwd().openIterableDir(dirname, .{});
var dir = try std.fs.cwd().openDir(dirname, .{ .iterate = true });
defer dir.close();
const period_idx = std.mem.lastIndexOf(u8, basename, ".").?;
@ -214,8 +214,8 @@ pub const Runner = struct {
fn rotateCmd(self: *Runner, cmd: lang.Command) !void {
const rotate_cmd = cmd.cast(lang.Command.Rotate).?;
var image = try self.getImage();
var c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill);
const image = try self.getImage();
const c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill);
defer self.allocator.free(c_bgfill);
try magick.runRotate(image, rotate_cmd.deg, c_bgfill);
@ -332,7 +332,7 @@ test "running noop" {
var cmds = lang.CommandList.init(allocator);
defer cmds.deinit();
var command = lang.Command{ .tag = .noop };
const command = lang.Command{ .tag = .noop };
var noop = try allocator.create(lang.Command.Noop);
noop.* = lang.Command.Noop{ .base = command };