]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_luaext.lua
b36159397f510c4114c798075c32e4333c26563e
[ion3.git] / ioncore / ioncore_luaext.lua
1 --
2 -- ion/share/ioncore_luaext.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
5 --
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
10 --
11
12
13 --DOC
14 -- Make \var{str} shell-safe.
15 function string.shell_safe(str)
16     return "'"..string.gsub(str, "'", "'\\''").."'"
17 end
18
19
20 --DOC
21 -- Make copy of \var{table}. If \var{deep} is unset, shallow one-level
22 -- copy is made, otherwise a deep copy is made.
23 function table.copy(t, deep)
24     local function docopy(t, deep, seen)
25         local nt={}
26         for k, v in pairs(t) do
27             local v2=v
28             if deep and type(v)=="table" then
29                 if seen[v] then
30                     error(TR("Recursive table - unable to deepcopy"))
31                 end
32                 seen[v]=true
33                 v2=docopy(v, deep, seen)
34                 seen[v]=nil
35             end
36             nt[k]=v2
37         end
38         return nt
39     end
40     return docopy(t, deep, deep and {})
41 end
42
43
44 --DOC
45 -- Add entries that do not exist in \var{t1} from \var{t2} to \var{t1}.
46 function table.append(t1, t2)
47     for k, v in pairs(t2) do
48         if t1[k]==nil then
49             t1[k]=v
50         end
51     end
52     return t1
53 end
54
55
56 --DOC
57 -- Create a table containing all entries from \var{t1} and those from
58 -- \var{t2} that are missing from \var{t1}.
59 function table.join(t1, t2)
60     return table.append(table.copy(t1, false), t2)
61 end
62
63
64 --DOC
65 -- Insert all positive integer entries from t2 into t1.
66 function table.icat(t1, t2)
67     for _, v in ipairs(t2) do
68         table.insert(t1, v)
69     end
70     return t1
71 end
72
73
74 --DOC
75 -- Map all entries of \var{t} by \var{f}.
76 function table.map(f, t)
77     local res={}
78     for k, v in pairs(t) do
79         res[k]=f(v)
80     end
81     return res
82 end
83
84
85 --DOC
86 -- Export a list of functions from \var{lib} into global namespace.
87 function export(lib, ...)
88     for k, v in pairs(arg) do
89         _G[v]=lib[v]
90     end
91 end
92