diff options
| author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-03 17:36:51 +0200 |
|---|---|---|
| committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-03 17:39:15 +0200 |
| commit | a42c94e103ecf7cb365a8888c3f5afc785def284 (patch) | |
| tree | 7522b24cf920dd2c62b733c1b65ab6a169977b36 /util.lua | |
| parent | d9996907bc5fb78449a3e3b6192fe57d76056071 (diff) | |
| download | r6p-a42c94e103ecf7cb365a8888c3f5afc785def284.tar.xz | |
util: better rand_string, add mkdir
Diffstat (limited to 'util.lua')
| -rw-r--r-- | util.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..0628691 --- /dev/null +++ b/util.lua @@ -0,0 +1,50 @@ +local base64 = require("vendor.base64") +local json = require("vendor.JSON") +local table_unpack = table.unpack or unpack + +local function base64_dec(x) + local succ, dec = pcall(base64.decode, x) + if succ then return dec end +end + +local function json_dec(x) + local succ, dec = pcall(json.decode, json, x) + if succ then return dec end +end + +local function json_enc(x) + return json:encode(x) +end + +local mkdir, rand_string + +if love then + rand_string = function(n) + local b = {} + for i = 1, n do + table.insert(b, love.math.random(0, 255)) + end + return string.char(table_unpack(b)) + end + mkdir = love.filesystem.mkdir +else + local rand_file + rand_string = function(n) + local rand_file = rand_file or io.open("/dev/random") + return rand_file:read(n) + end + -- awful + mkdir = function(x) + local status = os.execute("mkdir -p " .. x) + return status == true + end +end + +return { + rand_string = rand_string, + mkdir = mkdir, + base64_dec = base64_dec, + base64_enc = base64.encode, + json_dec = json_dec, + json_enc = json_enc, +} |
