2 FFLuCI - Utility library
5 Several common useful Lua functions
11 Copyright 2008 Steven Barth <steven@midlink.org>
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
17 http://www.apache.org/licenses/LICENSE-2.0
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.
27 module("ffluci.util", package.seeall)
30 -- Lua simplified Python-style OO class support emulation
34 local create = function(class, ...)
36 setmetatable(inst, {__index = class})
39 local stat, err = pcall(inst.__init__, inst, ...)
48 local classmeta = {__call = create}
51 classmeta.__index = base
54 setmetatable(class, classmeta)
59 -- Clones an object (deep on-demand)
60 function clone(object, deep)
63 for k, v in pairs(object) do
64 if deep and type(v) == "table" then
70 setmetatable(copy, getmetatable(object))
76 -- Checks whether a table has an object "value" in it
77 function contains(table, value)
78 for k,v in pairs(table) do
87 -- Dumps a table to stdout (useful for testing and debugging)
88 function dumptable(t, i)
90 for k,v in pairs(t) do
91 print(string.rep("\t", i) .. k, v)
92 if type(v) == "table" then
99 -- Escapes all occurences of c in s
100 function escape(s, c)
102 return s:gsub(c, "\\" .. c)
106 -- Runs "command" and returns its output
107 function exec(command)
108 local pp = io.popen(command)
109 local data = pp:read("*a")
116 -- Runs "command" and returns its output as a array of lines
117 function execl(command)
118 local pp = io.popen(command)
124 if (line == nil) then break end
125 table.insert(data, line)
133 -- Populate obj in the scope of f as key
134 function extfenv(f, key, obj)
135 local scope = getfenv(f)
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
147 meta = getmetatable(meta.__index)
153 -- Creates valid XML PCDATA from a string
154 function pcdata(value)
155 value = value:gsub("&", "&")
156 value = value:gsub('"', """)
157 value = value:gsub("'", "'")
158 value = value:gsub("<", "<")
159 return value:gsub(">", ">")
163 -- Resets the scope of f doing a shallow copy of its scope into a new table
165 setfenv(f, clone(getfenv(f)))
169 -- Returns the Haserl unique sessionid
175 -- Splits a string into an array (Taken from lua-users.org)
176 function split(str, pat)
180 local fpat = "(.-)" .. pat
182 local s, e, cap = str:find(fpat, 1)
185 if s ~= 1 or cap ~= "" then
189 s, e, cap = str:find(fpat, last_end)
192 if last_end <= #str then
193 cap = str:sub(last_end)
201 -- Updates given table with new values
202 function update(t, updates)
203 for k, v in pairs(updates) do
209 -- Updates the scope of f with "extscope"
210 function updfenv(f, extscope)
211 update(getfenv(f), extscope)
215 -- Validates a variable
216 function validate(value, cast_number, cast_int)
217 if cast_number or cast_int then
218 value = tonumber(value)
221 if cast_int and value and not(value % 1 == 0) then
229 -- Returns the filename of the calling script
231 return debug.getinfo(2, 'S').source:sub(2)