* CBI updates
[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                         inst:__init__(...)
40                 end
41                 
42                 return inst
43         end
44         
45         local classmeta = {__call = create}
46         
47         if base then
48                 classmeta.__index = base
49         end
50         
51         setmetatable(class, classmeta)
52         return class
53 end
54
55
56 -- Checks whether a table has an object "value" in it
57 function contains(table, value)
58         for k,v in pairs(table) do
59                 if value == v then
60                         return true
61                 end
62         end
63         return false
64 end
65
66
67 -- Dumps a table to stdout (useful for testing and debugging)
68 function dumptable(t, i)
69         i = i or 0
70         for k,v in pairs(t) do
71                 print(string.rep("\t", i) .. k, v)
72                 if type(v) == "table" then
73                         dumptable(v, i+1)
74                 end
75         end
76 end
77
78
79 -- Escapes all occurences of c in s
80 function escape(s, c)
81         c = c or "\\"
82         return s:gsub(c, "\\" .. c)
83 end
84
85
86 -- Runs "command" and returns its output
87 function exec(command)
88         local pp   = io.popen(command)
89         local data = pp:read("*a")
90         pp:close()
91         
92         return data
93 end
94
95
96 -- Runs "command" and returns its output as a array of lines
97 function execl(command)
98         local pp   = io.popen(command)  
99         local line = ""
100         local data = {}
101         
102         while true do
103                 line = pp:read()
104                 if (line == nil) then break end
105                 table.insert(data, line)
106         end 
107         pp:close()      
108         
109         return data
110 end
111
112
113 -- Populate obj in the scope of f as key 
114 function extfenv(f, key, obj)
115         local scope = getfenv(f)
116         scope[key] = obj
117 end
118
119
120 -- Checks whether an object is an instanceof class
121 function instanceof(object, class)
122         local meta = getmetatable(object)
123     while meta and meta.__index do 
124         if meta.__index == class then
125                 return true
126         end
127         meta = getmetatable(meta.__index)
128     end
129     return false        
130 end
131
132
133 -- Resets the scope of f doing a shallow copy of its scope into a new table
134 function resfenv(f)
135         local scope = getfenv(f)
136         setfenv(f, {})
137         updfenv(f, scope)
138 end 
139
140
141 -- Updates the scope of f with "extscope"
142 function updfenv(f, extscope)
143         local scope = getfenv(f)
144         for k, v in pairs(extscope) do
145                 scope[k] = v
146         end
147 end
148
149
150 -- Validates a variable
151 function validate(value, cast_number, cast_int, valid)
152         if cast_number or cast_int then
153                 value = tonumber(value)
154         end
155         
156         if cast_int and not(value % 1 == 0) then
157                 value = nil
158         end
159         
160         
161         if type(valid) == "function" then
162                 value = valid(value)
163         elseif type(valid) == "table" then
164                 if not contains(valid, value) then
165                         value = nil
166                 end
167         end
168         
169         return value
170 end
171
172
173 -- Returns the filename of the calling script
174 function __file__()
175         return debug.getinfo(2, 'S').source:sub(2)
176 end