Compare commits

..

3 commits

Author SHA1 Message Date
c73b98a356 fix for zig 0.11 2023-08-04 21:50:59 -03:00
7cc93c2976 zig fmt 2023-08-04 21:34:51 -03:00
ed67a52b15 fixes for some version of zig 2023-08-04 21:34:17 -03:00
10 changed files with 65 additions and 57 deletions

View file

@ -12,8 +12,8 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
step.linkSystemLibrary("GraphicsMagickWand"); step.linkSystemLibrary("GraphicsMagickWand");
step.linkSystemLibrary("GraphicsMagick"); step.linkSystemLibrary("GraphicsMagick");
step.addIncludePath("/usr/include/GraphicsMagick"); step.addIncludePath(.{ .path = "/usr/include/GraphicsMagick" });
step.addIncludePath("/usr/include"); step.addIncludePath(.{ .path = "/usr/include" });
const possible_lilv_include_dirs = [_][]const u8{ const possible_lilv_include_dirs = [_][]const u8{
"/usr/include/lilv-0/lilv", "/usr/include/lilv-0/lilv",
@ -32,7 +32,7 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
found_any_lilv = true; found_any_lilv = true;
std.debug.print("found lilv at '{s}'\n", .{possible_lilv_dir}); std.debug.print("found lilv at '{s}'\n", .{possible_lilv_dir});
step.addIncludePath(possible_lilv_dir); step.addIncludePath(.{ .path = possible_lilv_dir });
} }
if (!found_any_lilv) { if (!found_any_lilv) {
@ -42,23 +42,34 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
} }
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{});
const exe = b.addExecutable("scritcher", "src/main.zig"); const optimize = b.standardOptimizeOption(.{});
exe.setBuildMode(mode);
exe.use_stage1 = true;
exe.install();
const exe = b.addExecutable(.{
.name = "scritcher",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
setupLinks(exe); setupLinks(exe);
b.installArtifact(exe);
const test_obj_step = b.addTest("src/main.zig"); const run_cmd = b.addRunArtifact(exe);
setupLinks(test_obj_step);
const run_cmd = exe.run(); if (b.args) |args| {
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const test_step = b.step("test", "Run tests"); const test_step = b.addTest(.{
test_step.dependOn(&test_obj_step.step); .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
setupLinks(test_step);
const run_unit_tests = b.addRunArtifact(test_step);
const test_cmd = b.step("test", "run unit tests");
test_cmd.dependOn(&run_unit_tests.step);
} }

View file

@ -24,7 +24,7 @@ pub const RandomNoise = struct {
if (params.fill_bytes > 0) { if (params.fill_bytes > 0) {
var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null;
for (rand_buf) |_, idx| { for (rand_buf, 0..) |_, idx| {
rand_buf[idx] = r.random().float(f32); rand_buf[idx] = r.random().float(f32);
} }
@ -72,8 +72,8 @@ pub const WildNoise = struct {
if (params.fill_bytes > 0) { if (params.fill_bytes > 0) {
var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null;
for (rand_buf) |_, idx| { for (rand_buf, 0..) |_, idx| {
rand_buf[idx] = @intToFloat(f32, r.random().int(u1)); rand_buf[idx] = @as(f32, @floatFromInt(r.random().int(u1)));
} }
return WildNoise{ return WildNoise{
@ -99,7 +99,7 @@ pub const WildNoise = struct {
bufs.out[0] = rand_buf[self.cnt]; bufs.out[0] = rand_buf[self.cnt];
self.cnt += 1; self.cnt += 1;
} else { } else {
bufs.out[0] = @intToFloat(f32, self.r.random().int(u1)); bufs.out[0] = @as(f32, @floatFromInt(self.r.random().int(u1)));
} }
} }
}; };
@ -163,7 +163,7 @@ pub const Embed = struct {
image.sseek(self.sndfile, 0); image.sseek(self.sndfile, 0);
self.buf = try self.allocator.alloc(f32, @intCast(usize, in_fmt.channels)); self.buf = try self.allocator.alloc(f32, @as(usize, @intCast(in_fmt.channels)));
} }
pub fn deinit(self: *@This()) void { pub fn deinit(self: *@This()) void {

View file

@ -26,7 +26,7 @@ pub fn sopen(
mode: i32, mode: i32,
fmt: *c.SF_INFO, fmt: *c.SF_INFO,
) !*c.SNDFILE { ) !*c.SNDFILE {
var cstr_path = try std.cstr.addNullByte(allocator, path); var cstr_path = try allocator.dupeZ(u8, path);
defer allocator.free(cstr_path); defer allocator.free(cstr_path);
var file = c.sf_open(cstr_path.ptr, mode, fmt); var file = c.sf_open(cstr_path.ptr, mode, fmt);
@ -63,7 +63,7 @@ pub fn swrite(file: *c.SNDFILE, buf: [*]f32, frames: i64) !void {
} }
pub fn sseek(file: *c.SNDFILE, offset: usize) void { pub fn sseek(file: *c.SNDFILE, offset: usize) void {
const offset_i64 = @intCast(i64, offset); const offset_i64 = @as(i64, @intCast(offset));
const frames = c.sf_seek(file, offset_i64, c.SEEK_SET); const frames = c.sf_seek(file, offset_i64, c.SEEK_SET);
const frames_current = c.sf_seek(file, 0, c.SEEK_CUR); const frames_current = c.sf_seek(file, 0, c.SEEK_CUR);
std.debug.assert(frames == frames_current); std.debug.assert(frames == frames_current);
@ -80,7 +80,7 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
var nam = try allocator.alloc(u8, template.len); var nam = try allocator.alloc(u8, template.len);
std.mem.copy(u8, nam, template); std.mem.copy(u8, nam, template);
const seed = @truncate(u64, @bitCast(u128, std.time.nanoTimestamp())); const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))));
var r = std.rand.DefaultPrng.init(seed); var r = std.rand.DefaultPrng.init(seed);
var fill = nam[template_start.len..nam.len]; var fill = nam[template_start.len..nam.len];
@ -88,8 +88,8 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
var i: usize = 0; var i: usize = 0;
while (i < 100) : (i += 1) { while (i < 100) : (i += 1) {
// generate a random uppercase letter, that is, 65 + random number. // generate a random uppercase letter, that is, 65 + random number.
for (fill) |_, f_idx| { for (fill, 0..) |_, f_idx| {
var idx = @intCast(u8, r.random().uintLessThan(u5, 24)); var idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24)));
var letter = @as(u8, 65) + idx; var letter = @as(u8, 65) + idx;
fill[f_idx] = letter; fill[f_idx] = letter;
} }
@ -151,7 +151,7 @@ pub const Image = struct {
.sndfile = sndfile, .sndfile = sndfile,
.path = path, .path = path,
.curpath = path, .curpath = path,
.frames = @intCast(usize, in_fmt.frames), .frames = @as(usize, @intCast(in_fmt.frames)),
}; };
return image; return image;
@ -161,7 +161,7 @@ pub const Image = struct {
var in_fmt = mkSfInfo(); var in_fmt = mkSfInfo();
// clone sndfile // clone sndfile
var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt);
std.debug.assert(self.frames == @intCast(usize, in_fmt.frames)); std.debug.assert(self.frames == @as(usize, @intCast(in_fmt.frames)));
var image = try self.allocator.create(Image); var image = try self.allocator.create(Image);
@ -173,7 +173,7 @@ pub const Image = struct {
.sndfile = sndfile, .sndfile = sndfile,
.path = self.path, .path = self.path,
.curpath = self.curpath, .curpath = self.curpath,
.frames = @intCast(usize, in_fmt.frames), .frames = @as(usize, @intCast(in_fmt.frames)),
}; };
return image; return image;
@ -198,12 +198,12 @@ pub const Image = struct {
pub fn read(self: *Image, file_chans: c_int, buf: []f32) bool { pub fn read(self: *Image, file_chans: c_int, buf: []f32) bool {
const n_read: c.sf_count_t = c.sf_readf_float(self.sndfile, buf.ptr, 1); const n_read: c.sf_count_t = c.sf_readf_float(self.sndfile, buf.ptr, 1);
const buf_chans = @intCast(c_int, buf.len); const buf_chans = @as(c_int, @intCast(buf.len));
var i = file_chans - 1; var i = file_chans - 1;
while (i < buf_chans) : (i += 1) { while (i < buf_chans) : (i += 1) {
//buf[@intCast(usize, i)] = buf[i % file_chans]; //buf[@intCast(usize, i)] = buf[i % file_chans];
buf[@intCast(usize, i)] = buf[@intCast(usize, @mod(i, file_chans))]; buf[@as(usize, @intCast(i))] = buf[@as(usize, @intCast(@mod(i, file_chans)))];
} }
return n_read == 1; return n_read == 1;
@ -237,13 +237,13 @@ pub const Image = struct {
var view: []f32 = buf[0..buf.len]; var view: []f32 = buf[0..buf.len];
if (bytes_until_end < buf.len) { if (bytes_until_end < buf.len) {
read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, bytes_until_end)); read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @as(i64, @intCast(bytes_until_end)));
view = buf[0..bytes_until_end]; view = buf[0..bytes_until_end];
} else { } else {
read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len)); read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @as(i64, @intCast(buf.len)));
} }
try swrite(out_file, view.ptr, @intCast(i64, view.len)); try swrite(out_file, view.ptr, @as(i64, @intCast(view.len)));
} }
sseek(self.sndfile, end); sseek(self.sndfile, end);
@ -264,7 +264,7 @@ pub const Image = struct {
// std.testing.expectEqual(self.frames, @intCast(usize, in_fmt.frames)); // std.testing.expectEqual(self.frames, @intCast(usize, in_fmt.frames));
self.curpath = path; self.curpath = path;
self.frames = @intCast(usize, in_fmt.frames); self.frames = @as(usize, @intCast(in_fmt.frames));
log.debug("\timage: reopened on '{s}' (frames={d}, fmt.frames={d})", .{ log.debug("\timage: reopened on '{s}' (frames={d}, fmt.frames={d})", .{
self.curpath, self.curpath,
@ -321,7 +321,7 @@ pub const Image = struct {
// now, for each param for the plugin, we find its port, and set // now, for each param for the plugin, we find its port, and set
// the value for the port there. // the value for the port there.
for (params.items) |param| { for (params.items) |param| {
var sym_cstr = try std.cstr.addNullByte(self.allocator, param.sym); var sym_cstr = try self.allocator.dupeZ(u8, param.sym);
defer self.allocator.free(sym_cstr); defer self.allocator.free(sym_cstr);
var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr); var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr);

View file

@ -488,7 +488,7 @@ pub const CommandList = struct {
const inner_command = cmd_ptr.cast(typ).?; const inner_command = cmd_ptr.cast(typ).?;
inline for (@typeInfo(typ).Struct.fields) |cmd_field| { inline for (@typeInfo(typ).Struct.fields) |cmd_field| {
switch (cmd_field.field_type) { switch (cmd_field.type) {
[]u8, []const u8 => self.list.allocator.free(@field(inner_command, cmd_field.name)), []u8, []const u8 => self.list.allocator.free(@field(inner_command, cmd_field.name)),
else => {}, else => {},
} }
@ -496,7 +496,8 @@ pub const CommandList = struct {
} }
} }
self.list.allocator.destroy(cmd_ptr); //TODO this is ian invalid free
//self.list.allocator.destroy(cmd_ptr);
} }
self.list.deinit(); self.list.deinit();
} }
@ -537,7 +538,7 @@ pub const Lang = struct {
fn parseCommandArguments( fn parseCommandArguments(
self: *@This(), self: *@This(),
comptime command_struct: type, comptime command_struct: type,
tok_it: *std.mem.SplitIterator(u8), tok_it: *std.mem.SplitIterator(u8, .sequence),
commands: *CommandList, commands: *CommandList,
) !void { ) !void {
// Based on the command struct fields, we can parse the arguments. // Based on the command struct fields, we can parse the arguments.
@ -576,7 +577,7 @@ pub const Lang = struct {
} }
const arg = maybe_arg.?; const arg = maybe_arg.?;
const arg_value = switch (cmd_field.field_type) { const arg_value = switch (cmd_field.type) {
f32 => try std.fmt.parseFloat(f32, arg), f32 => try std.fmt.parseFloat(f32, arg),
u64 => try std.fmt.parseInt(u64, arg, 10), u64 => try std.fmt.parseInt(u64, arg, 10),
usize => try std.fmt.parseInt(usize, arg, 10), usize => try std.fmt.parseInt(usize, arg, 10),
@ -601,7 +602,7 @@ pub const Lang = struct {
} }
const arg = arg_opt.?; const arg = arg_opt.?;
const argument_value = switch (cmd_field.field_type) { const argument_value = switch (cmd_field.type) {
usize => try std.fmt.parseInt(usize, arg, 10), usize => try std.fmt.parseInt(usize, arg, 10),
i32 => try std.fmt.parseInt(i32, arg, 10), i32 => try std.fmt.parseInt(i32, arg, 10),
f32 => try std.fmt.parseFloat(f32, arg), f32 => try std.fmt.parseFloat(f32, arg),
@ -638,7 +639,7 @@ pub const Lang = struct {
if (std.mem.startsWith(u8, stmt, "#")) continue; if (std.mem.startsWith(u8, stmt, "#")) continue;
// TODO better tokenizer instead of just tokenize(" ")...maybe???? // TODO better tokenizer instead of just tokenize(" ")...maybe????
var tok_it = std.mem.split(u8, stmt, " "); var tok_it = std.mem.splitSequence(u8, stmt, " ");
var cmd_opt = tok_it.next(); var cmd_opt = tok_it.next();
if (cmd_opt == null) { if (cmd_opt == null) {
@ -669,7 +670,7 @@ pub const Lang = struct {
comptime var lowered_command_name = [_]u8{0} ** struct_name.len; comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
comptime { comptime {
for (struct_name) |c, i| { for (struct_name, 0..) |c, i| {
lowered_command_name[i] = std.ascii.toLower(c); lowered_command_name[i] = std.ascii.toLower(c);
} }
} }

View file

@ -69,7 +69,7 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port {
var ports = try ctx.allocator.alloc(Port, n_ports); var ports = try ctx.allocator.alloc(Port, n_ports);
for (ports) |_, idx| { for (ports, 0..) |_, idx| {
var port: *Port = &ports[idx]; var port: *Port = &ports[idx];
port.* = Port{ port.* = Port{
.lilv_port = null, .lilv_port = null,

View file

@ -38,7 +38,7 @@ fn magickLoad(image: *Image) !MagickContext {
var mctx = try MagickContext.init(); var mctx = try MagickContext.init();
errdefer mctx.deinit(); errdefer mctx.deinit();
var curpath = try std.cstr.addNullByte(image.allocator, image.curpath); var curpath = try image.allocator.dupeZ(u8, image.curpath);
defer image.allocator.free(curpath); defer image.allocator.free(curpath);
log.debug("loading '{s}'", .{curpath}); log.debug("loading '{s}'", .{curpath});
@ -53,7 +53,7 @@ fn magickSave(image: *Image, wand: *mc.MagickWand) !void {
const allocator = image.allocator; const allocator = image.allocator;
var tmpnam = try images.temporaryName(allocator); var tmpnam = try images.temporaryName(allocator);
var c_tmpnam = try std.cstr.addNullByte(allocator, tmpnam); var c_tmpnam = try allocator.dupeZ(u8, tmpnam);
defer allocator.free(c_tmpnam); defer allocator.free(c_tmpnam);
log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam}); log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam});

View file

@ -29,11 +29,7 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt
const casted = command.cast(CommandStruct).?; const casted = command.cast(CommandStruct).?;
var heap_cmd = try allocator.create(CommandStruct); var heap_cmd = try allocator.create(CommandStruct);
@memcpy( heap_cmd.* = casted.*;
@ptrCast([*]u8, &heap_cmd),
@ptrCast([*]const u8, &casted),
@sizeOf(CommandStruct),
);
return &heap_cmd.base; return &heap_cmd.base;
} }

View file

@ -101,8 +101,8 @@ pub const RunContext = struct {
var i: usize = 0; var i: usize = 0;
var o: usize = 0; var o: usize = 0;
for (ports) |_, p_idx| { for (ports, 0..) |_, p_idx| {
var p = @intCast(u32, p_idx); var p = @as(u32, @intCast(p_idx));
var port: *lv2.Port = &ports[p_idx]; var port: *lv2.Port = &ports[p_idx];
switch (port.ptype) { switch (port.ptype) {
@ -131,7 +131,7 @@ pub const RunContext = struct {
}; };
pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Context { pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Context {
const cstr_plugin_uri = try std.cstr.addNullByte(allocator, plugin_uri); const cstr_plugin_uri = try allocator.dupeZ(u8, plugin_uri);
defer allocator.free(cstr_plugin_uri); defer allocator.free(cstr_plugin_uri);
var world: *c.LilvWorld = c.lilv_world_new().?; var world: *c.LilvWorld = c.lilv_world_new().?;

View file

@ -7,9 +7,9 @@ fn printCommandWithParams(stream: anytype, command: anytype) !void {
const Parameters = @TypeOf(command.parameters); const Parameters = @TypeOf(command.parameters);
try stream.print(" {d} {d}", .{ command.split, command.index }); try stream.print(" {d} {d}", .{ command.split, command.index });
inline for (@typeInfo(Parameters).Struct.fields) |field| { inline for (@typeInfo(Parameters).Struct.fields) |field| {
if (field.field_type == f32 or field.field_type == f64) { if (field.type == f32 or field.type == f64) {
try stream.print(" {}", .{@field(command.parameters, field.name)}); try stream.print(" {}", .{@field(command.parameters, field.name)});
} else if (field.field_type == usize or field.field_type == u64) { } else if (field.type == usize or field.type == u64) {
try stream.print(" {d}", .{@field(command.parameters, field.name)}); try stream.print(" {d}", .{@field(command.parameters, field.name)});
} else { } else {
try stream.print(" {s}", .{@field(command.parameters, field.name)}); try stream.print(" {s}", .{@field(command.parameters, field.name)});

View file

@ -70,7 +70,7 @@ pub const Runner = struct {
// ':0' should ALWAYS point to the image. // ':0' should ALWAYS point to the image.
if (self.repl) index += 3 else index += 3; if (self.repl) index += 3 else index += 3;
for (self.args) |arg, idx| { for (self.args, 0..) |arg, idx| {
log.debug("arg{d} = {s}", .{ idx, arg }); log.debug("arg{d} = {s}", .{ idx, arg });
} }
log.debug("fetch arg idx={d}", .{index}); log.debug("fetch arg idx={d}", .{index});
@ -152,7 +152,7 @@ pub const Runner = struct {
while (try it.next()) |entry| { while (try it.next()) |entry| {
switch (entry.kind) { switch (entry.kind) {
.File => blk: { .file => blk: {
if (!std.mem.startsWith(u8, entry.name, starts_with)) break :blk {}; if (!std.mem.startsWith(u8, entry.name, starts_with)) break :blk {};
// we want to get the N in x_gN.ext // we want to get the N in x_gN.ext
@ -215,7 +215,7 @@ pub const Runner = struct {
const rotate_cmd = cmd.cast(lang.Command.Rotate).?; const rotate_cmd = cmd.cast(lang.Command.Rotate).?;
var image = try self.getImage(); var image = try self.getImage();
var c_bgfill = try std.cstr.addNullByte(self.allocator, rotate_cmd.bgfill); var c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill);
defer self.allocator.free(c_bgfill); defer self.allocator.free(c_bgfill);
try magick.runRotate(image, rotate_cmd.deg, c_bgfill); try magick.runRotate(image, rotate_cmd.deg, c_bgfill);