|
1 -- |
|
2 -- ion/share/ioncore_luaext.lua |
|
3 -- |
|
4 -- Copyright (c) Tuomo Valkonen 2004-2009. |
|
5 -- |
|
6 -- See the included file LICENSE for details. |
|
7 -- |
|
8 |
|
9 |
|
10 --DOC |
|
11 -- Make \var{str} shell-safe. |
|
12 function string.shell_safe(str) |
|
13 return "'"..string.gsub(str, "'", "'\\''").."'" |
|
14 end |
|
15 |
|
16 |
|
17 --DOC |
|
18 -- Make copy of \var{table}. If \var{deep} is unset, shallow one-level |
|
19 -- copy is made, otherwise a deep copy is made. |
|
20 function table.copy(t, deep) |
|
21 local function docopy(t, deep, seen) |
|
22 local nt={} |
|
23 for k, v in pairs(t) do |
|
24 local v2=v |
|
25 if deep and type(v)=="table" then |
|
26 if seen[v] then |
|
27 error(TR("Recursive table - unable to deepcopy")) |
|
28 end |
|
29 seen[v]=true |
|
30 v2=docopy(v, deep, seen) |
|
31 seen[v]=nil |
|
32 end |
|
33 nt[k]=v2 |
|
34 end |
|
35 return nt |
|
36 end |
|
37 return docopy(t, deep, deep and {}) |
|
38 end |
|
39 |
|
40 |
|
41 --DOC |
|
42 -- Add entries that do not exist in \var{t1} from \var{t2} to \var{t1}. |
|
43 function table.append(t1, t2) |
|
44 for k, v in pairs(t2) do |
|
45 if t1[k]==nil then |
|
46 t1[k]=v |
|
47 end |
|
48 end |
|
49 return t1 |
|
50 end |
|
51 |
|
52 |
|
53 --DOC |
|
54 -- Create a table containing all entries from \var{t1} and those from |
|
55 -- \var{t2} that are missing from \var{t1}. |
|
56 function table.join(t1, t2) |
|
57 return table.append(table.copy(t1, false), t2) |
|
58 end |
|
59 |
|
60 |
|
61 --DOC |
|
62 -- Insert all positive integer entries from t2 into t1. |
|
63 function table.icat(t1, t2) |
|
64 for _, v in ipairs(t2) do |
|
65 table.insert(t1, v) |
|
66 end |
|
67 return t1 |
|
68 end |
|
69 |
|
70 |
|
71 --DOC |
|
72 -- Map all entries of \var{t} by \var{f}. |
|
73 function table.map(f, t) |
|
74 local res={} |
|
75 for k, v in pairs(t) do |
|
76 res[k]=f(v) |
|
77 end |
|
78 return res |
|
79 end |
|
80 |
|
81 |
|
82 --DOC |
|
83 -- Export a list of functions from \var{lib} into global namespace. |
|
84 function export(lib, ...) |
|
85 for k, v in pairs({...}) do |
|
86 _G[v]=lib[v] |
|
87 end |
|
88 end |
|
89 |
|
90 function io.openX(file, ...) |
|
91 local f, err = io.open(file, ...) |
|
92 if not f then |
|
93 error(file..": "..err) |
|
94 end |
|
95 return f |
|
96 end |