082310fbfad11d2e638293b268a6b6603faadd69
[project/luci.git] / src / ffluci / util.lua
1 --[[
2 FFLuCI - Utility library
3
4 Description:
5 Several common useful Lua functions
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26
27 module("ffluci.util", package.seeall)
28
29
30 -- Lua simplified Python-style OO class support emulation
31 function class(base)
32         local class = {}
33         
34         local create = function(class, ...)
35                 local inst = {}
36                 setmetatable(inst, {__index = class})
37                 
38                 if inst.__init__ then
39                         local stat, err = pcall(inst.__init__, inst, ...)
40                         if not stat then
41                                 error(err)
42                         end
43                 end
44                 
45                 return inst
46         end
47         
48         local classmeta = {__call = create}
49         
50         if base then
51                 classmeta.__index = base
52         end
53         
54         setmetatable(class, classmeta)
55         return class
56 end
57
58
59 -- Clones an object (deep on-demand)
60 function clone(object, deep)
61         local copy = {}
62         
63         for k, v in pairs(object) do
64                 if deep and type(v) == "table" then
65                         v = clone(v, deep)
66                 end
67                 copy[k] = v
68         end
69         
70         setmetatable(copy, getmetatable(object))
71         
72         return copy
73 end
74
75
76 -- Checks whether a table has an object "value" in it
77 function contains(table, value)
78         for k,v in pairs(table) do
79                 if value == v then
80                         return true
81                 end
82         end
83         return false
84 end
85
86
87 -- Dumps a table to stdout (useful for testing and debugging)
88 function dumptable(t, i)
89         i = i or 0
90         for k,v in pairs(t) do
91                 print(string.rep("\t", i) .. k, v)
92                 if type(v) == "table" then
93                         dumptable(v, i+1)
94                 end
95         end
96 end
97
98
99 -- Escapes all occurences of c in s
100 function escape(s, c)
101         c = c or "\\"
102         return s:gsub(c, "\\" .. c)
103 end
104
105
106 -- Runs "command" and returns its output
107 function exec(command)
108         local pp   = io.popen(command)
109         local data = pp:read("*a")
110         pp:close()
111         
112         return data
113 end
114
115
116 -- Runs "command" and returns its output as a array of lines
117 function execl(command)
118         local pp   = io.popen(command)  
119         local line = ""
120         local data = {}
121         
122         while true do
123                 line = pp:read()
124                 if (line == nil) then break end
125                 table.insert(data, line)
126         end 
127         pp:close()      
128         
129         return data
130 end
131
132
133 -- Populate obj in the scope of f as key 
134 function extfenv(f, key, obj)
135         local scope = getfenv(f)
136         scope[key] = obj
137 end
138
139
140 -- Checks whether an object is an instanceof class
141 function instanceof(object, class)
142         local meta = getmetatable(object)
143     while meta and meta.__index do 
144         if meta.__index == class then
145                 return true
146         end
147         meta = getmetatable(meta.__index)
148     end
149     return false        
150 end
151
152
153 -- Resets the scope of f doing a shallow copy of its scope into a new table
154 function resfenv(f)
155         local scope = getfenv(f)
156         setfenv(f, {})
157         updfenv(f, scope)
158 end 
159
160
161 -- Returns the Haserl unique sessionid
162 function sessionid()
163         return ENV.SESSIONID
164 end
165
166
167 -- Splits a string into an array (Taken from lua-users.org)
168 function split(str, pat)
169    local t = {}
170    local fpat = "(.-)" .. pat
171    local last_end = 1
172    local s, e, cap = str:find(fpat, 1)
173    while s do
174       if s ~= 1 or cap ~= "" then
175          table.insert(t,cap)
176       end
177       last_end = e+1
178       s, e, cap = str:find(fpat, last_end)
179    end
180    if last_end <= #str then
181       cap = str:sub(last_end)
182       table.insert(t, cap)
183    end
184    return t
185 end
186
187
188 -- Updates the scope of f with "extscope"
189 function updfenv(f, extscope)
190         local scope = getfenv(f)
191         for k, v in pairs(extscope) do
192                 scope[k] = v
193         end
194 end
195
196
197 -- Validates a variable
198 function validate(value, valid, cast_number, cast_int)
199         if cast_number or cast_int then
200                 value = tonumber(value)
201         end
202         
203         if cast_int and value and not(value % 1 == 0) then
204                 value = nil
205         end
206         
207         
208         if type(valid) == "function" then
209                 value = valid(value)
210         elseif type(valid) == "table" then
211                 if not contains(valid, value) then
212                         value = nil
213                 end
214         end
215         
216         return value
217 end
218
219
220 -- Returns the filename of the calling script
221 function __file__()
222         return debug.getinfo(2, 'S').source:sub(2)
223 end