-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
71 lines (63 loc) · 1.62 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
--[[
|| Small utility functions
--]]
local path = require "path"
path.fs = require "path.fs"
local delim = package.config:sub (1,1)
local utils = {}
-- Iterator over lines of <str>
function utils.ilines (str)
str = (str:sub (-1) ~= "\n") and (str .. "\n") or str
return str:gmatch ("(.-)\n")
end
-- Test if <val> exists as a value in <tbl>
function utils.isValueInTable (tbl, val)
for _dummy, valTbl in pairs (tbl) do
if (valTbl == val) then
return true
end
end
return false
end
-- Test if <dir> is an existing directory
-- If error occurs, refer to <dir> as <errname>
-- If <dir> does not exist, and <nice> is true, try to create <dir>
function utils.isAvailableDirectory (dir, errname, nice)
local typeDir, errStat = path.fs.type (dir)
if not typeDir then
if not nice then
error (string.format (
"Checking %s failed!\n" ..
"%s",
errname, errStat
), 3)
else
handlerWarn (string.format (
"Checking %s failed.\n" ..
"%s",
errname, errStat
))
handlerWarn (string.format (
"Attempt to mkdir %s.",
errname
))
local successMk, errMk = path.fs.makedirs
if not successMk then
error (string.format (
"Failed to mkdir %s!\n" ..
"%s",
errname, errMk
), 3)
end
end
elseif (typeDir ~= "dir") then
error (string.format (
"%s is not a directory (type '%s')!",
errname, typeDir
), 3)
end
end
function utils.assurePathDelimiter (somepath)
return (somepath:sub (-1) ~= delim and (somepath .. delim) or somepath)
end
return utils