summaryrefslogtreecommitdiff
path: root/util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua50
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,
+}