add temporary file name generator

This commit is contained in:
Luna 2019-07-09 14:29:54 -03:00
parent 05fe41d8b4
commit fc69a3f62f
1 changed files with 40 additions and 0 deletions

View File

@ -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);
}
};