From fc69a3f62fad124981e14d2097b82d7e96f534ac Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 9 Jul 2019 14:29:54 -0300 Subject: [PATCH] add temporary file name generator --- src/image.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/image.zig b/src/image.zig index 785ae91..55639a8 100644 --- a/src/image.zig +++ b/src/image.zig @@ -37,6 +37,37 @@ fn sopen( return file.?; } +fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { + const template_start = "/temp/temp_"; + const template = "/tmp/temp_XXXXXX"; + var nam = try allocator.alloc(u8, template.len); + std.mem.copy(u8, nam, template); + + var r = std.rand.DefaultPrng.init(std.time.timestamp()); + + var fill = nam[template_start.len..nam.len]; + + 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)); + var letter = u8(65) + idx; + fill[f_idx] = letter; + } + + // if we fail to access it, we assume it doesn't exist and return it. + std.fs.File.access(nam) catch |err| { + if (err == error.FileNotFound) { + return nam; + } + }; + } + + return error.TempGenFail; +} + pub const Image = struct { allocator: *std.mem.Allocator, sndfile: *c.SNDFILE, @@ -92,6 +123,10 @@ pub const Image = struct { return n_read == 1; } + /// Run a plugin over the image. + /// This setups a new lilv world/plugin among other things. + /// The internal SNDFILE pointer is modified to point to the output of the + /// plugin run. pub fn runPlugin( self: *Image, plugin_uri: []const u8, @@ -134,5 +169,10 @@ pub const Image = struct { ); ports[idx].value = param.value; } + + // 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); + std.debug.warn("temporary name: {}\n", tmpnam); } };