Compare commits

...

3 Commits

Author SHA1 Message Date
Luna c73b98a356 fix for zig 0.11 2023-08-04 21:50:59 -03:00
Luna 7cc93c2976 zig fmt 2023-08-04 21:34:51 -03:00
Luna 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("GraphicsMagick");
step.addIncludePath("/usr/include/GraphicsMagick");
step.addIncludePath("/usr/include");
step.addIncludePath(.{ .path = "/usr/include/GraphicsMagick" });
step.addIncludePath(.{ .path = "/usr/include" });
const possible_lilv_include_dirs = [_][]const u8{
"/usr/include/lilv-0/lilv",
@ -32,7 +32,7 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
found_any_lilv = true;
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) {
@ -42,23 +42,34 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
}
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("scritcher", "src/main.zig");
exe.setBuildMode(mode);
exe.use_stage1 = true;
exe.install();
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "scritcher",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
setupLinks(exe);
b.installArtifact(exe);
const test_obj_step = b.addTest("src/main.zig");
setupLinks(test_obj_step);
const run_cmd = b.addRunArtifact(exe);
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&test_obj_step.step);
const test_step = b.addTest(.{
.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) {
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);
}
@ -72,8 +72,8 @@ pub const WildNoise = struct {
if (params.fill_bytes > 0) {
var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null;
for (rand_buf) |_, idx| {
rand_buf[idx] = @intToFloat(f32, r.random().int(u1));
for (rand_buf, 0..) |_, idx| {
rand_buf[idx] = @as(f32, @floatFromInt(r.random().int(u1)));
}
return WildNoise{
@ -99,7 +99,7 @@ pub const WildNoise = struct {
bufs.out[0] = rand_buf[self.cnt];
self.cnt += 1;
} 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);
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 {

View File

@ -26,7 +26,7 @@ pub fn sopen(
mode: i32,
fmt: *c.SF_INFO,
) !*c.SNDFILE {
var cstr_path = try std.cstr.addNullByte(allocator, path);
var cstr_path = try allocator.dupeZ(u8, path);
defer allocator.free(cstr_path);
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 {
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_current = c.sf_seek(file, 0, c.SEEK_CUR);
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);
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 fill = nam[template_start.len..nam.len];
@ -88,8 +88,8 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
var i: usize = 0;
while (i < 100) : (i += 1) {
// generate a random uppercase letter, that is, 65 + random number.
for (fill) |_, f_idx| {
var idx = @intCast(u8, r.random().uintLessThan(u5, 24));
for (fill, 0..) |_, f_idx| {
var idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24)));
var letter = @as(u8, 65) + idx;
fill[f_idx] = letter;
}
@ -151,7 +151,7 @@ pub const Image = struct {
.sndfile = sndfile,
.path = path,
.curpath = path,
.frames = @intCast(usize, in_fmt.frames),
.frames = @as(usize, @intCast(in_fmt.frames)),
};
return image;
@ -161,7 +161,7 @@ pub const Image = struct {
var in_fmt = mkSfInfo();
// clone sndfile
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);
@ -173,7 +173,7 @@ pub const Image = struct {
.sndfile = sndfile,
.path = self.path,
.curpath = self.curpath,
.frames = @intCast(usize, in_fmt.frames),
.frames = @as(usize, @intCast(in_fmt.frames)),
};
return image;
@ -198,12 +198,12 @@ pub const Image = struct {
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 buf_chans = @intCast(c_int, buf.len);
const buf_chans = @as(c_int, @intCast(buf.len));
var i = file_chans - 1;
while (i < buf_chans) : (i += 1) {
//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;
@ -237,13 +237,13 @@ pub const Image = struct {
var view: []f32 = buf[0..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];
} 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);
@ -264,7 +264,7 @@ pub const Image = struct {
// std.testing.expectEqual(self.frames, @intCast(usize, in_fmt.frames));
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})", .{
self.curpath,
@ -321,7 +321,7 @@ 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 std.cstr.addNullByte(self.allocator, param.sym);
var 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);

View File

@ -488,7 +488,7 @@ pub const CommandList = struct {
const inner_command = cmd_ptr.cast(typ).?;
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)),
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();
}
@ -537,7 +538,7 @@ pub const Lang = struct {
fn parseCommandArguments(
self: *@This(),
comptime command_struct: type,
tok_it: *std.mem.SplitIterator(u8),
tok_it: *std.mem.SplitIterator(u8, .sequence),
commands: *CommandList,
) !void {
// 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_value = switch (cmd_field.field_type) {
const arg_value = switch (cmd_field.type) {
f32 => try std.fmt.parseFloat(f32, arg),
u64 => try std.fmt.parseInt(u64, arg, 10),
usize => try std.fmt.parseInt(usize, arg, 10),
@ -601,7 +602,7 @@ pub const Lang = struct {
}
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),
i32 => try std.fmt.parseInt(i32, arg, 10),
f32 => try std.fmt.parseFloat(f32, arg),
@ -638,7 +639,7 @@ pub const Lang = struct {
if (std.mem.startsWith(u8, stmt, "#")) continue;
// 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();
if (cmd_opt == null) {
@ -669,7 +670,7 @@ pub const Lang = struct {
comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
comptime {
for (struct_name) |c, i| {
for (struct_name, 0..) |c, i| {
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);
for (ports) |_, idx| {
for (ports, 0..) |_, idx| {
var port: *Port = &ports[idx];
port.* = Port{
.lilv_port = null,

View File

@ -38,7 +38,7 @@ fn magickLoad(image: *Image) !MagickContext {
var mctx = try MagickContext.init();
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);
log.debug("loading '{s}'", .{curpath});
@ -53,7 +53,7 @@ fn magickSave(image: *Image, wand: *mc.MagickWand) !void {
const allocator = image.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);
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).?;
var heap_cmd = try allocator.create(CommandStruct);
@memcpy(
@ptrCast([*]u8, &heap_cmd),
@ptrCast([*]const u8, &casted),
@sizeOf(CommandStruct),
);
heap_cmd.* = casted.*;
return &heap_cmd.base;
}

View File

@ -101,8 +101,8 @@ pub const RunContext = struct {
var i: usize = 0;
var o: usize = 0;
for (ports) |_, p_idx| {
var p = @intCast(u32, p_idx);
for (ports, 0..) |_, p_idx| {
var p = @as(u32, @intCast(p_idx));
var port: *lv2.Port = &ports[p_idx];
switch (port.ptype) {
@ -131,7 +131,7 @@ pub const RunContext = struct {
};
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);
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);
try stream.print(" {d} {d}", .{ command.split, command.index });
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)});
} 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)});
} else {
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.
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("fetch arg idx={d}", .{index});
@ -152,7 +152,7 @@ pub const Runner = struct {
while (try it.next()) |entry| {
switch (entry.kind) {
.File => blk: {
.file => blk: {
if (!std.mem.startsWith(u8, entry.name, starts_with)) break :blk {};
// 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).?;
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);
try magick.runRotate(image, rotate_cmd.deg, c_bgfill);