commonlib
common lib funcions
Title |
common lib funcions |
Author(s) |
LiXizhi |
Date |
2006/11/25 |
File |
script/ide/commonlib.lua |
Description
basic debugging,serialization functions, etc.
Sample Code
NPL.load("(gl)script/ide/commonlib.lua");
Member Functions
commonlib.GetUIObject
get an UI object by specifying a serie of names separated by #, such as childname#childname#childname,
e.g. commonlib.GetUIObject("wndParent#button1");
- param name : e.g. "wndParent#button1"
- param parent : nil or a parent UI object inside which the name is searched. If nil, first childname is searched globally.
- return __ : return the ParaUIObject found. if not found, the returned object IsValid() returns false.
syntax
function commonlib.GetUIObject(name, parent)
parameters
name |
e.g. "wndParent#button1" |
parent |
|
return |
return the ParaUIObject found. if not found, the returned object IsValid() returns false. |
commonlib.getfield
We rely on gfind, from the string library, to iterate over all words in f (where "word" is a sequence of one or more alphanumeric characters and underscores).
- param f : f is a string like "a.b.c.d"
- param rootEnv : it can be a table from which to search for f, if nil, the global table _G is used.
- return __ : return the field in LUA, it may be nil, a value, or a table, etc.
syntax
function commonlib.getfield (f, rootEnv)
parameters
f |
f is a string like "a.b.c.d" |
rootEnv |
|
return |
return the field in LUA, it may be nil, a value, or a table, etc. |
commonlib.setfield
set a variable v to f, where f is a string
the call setfield("t.x.y", 10) creates a global table t, another table t.x, and assigns 10 to t.x.y
- param f : f is a string like "a.b.c.d"
- param rootEnv : it can be a table from which to search for f, if nil, the global table _G is used.
syntax
function commonlib.setfield (f, v, rootEnv)
parameters
f |
f is a string like "a.b.c.d" |
v |
|
rootEnv |
|
commonlib.MetaClone
clone a obj using just meta table. It does not actually copy parameters.
Note: one can not serialize a meta cloned object, because it is an empty table with the same meta table as input obj.
syntax
function commonlib.MetaClone(obj)
parameters
commonlib.deepcopy
[[ code from:
http://lua-users.org/wiki/CopyTable
This function returns a deep copy of a given table. The function below also copies the metatable to the new table if there is one,
so the behaviour of the copied table is the same as the original. But the 2 tables share the same metatable,
you can avoid this by changing this 'getmetatable(object)' to '_copy( getmetatable(object) )'.
]]
syntax
function commonlib.deepcopy(object)
parameters
commonlib.partialcopy
alias for deepcopy
commonlib.clone = commonlib.deepcopy;
this function simply does, dest = src, but it copies value from src to dest.
It is mostly used with tables.It just copies what is in src to dest, but dest retains its other fields that does not exist in src.
syntax
function commonlib.partialcopy(dest, src)
parameters
commonlib.mincopy
this function simply does, dest = src, but it copies value from src to dest.
It is mostly used with tables.It only copies src field to dest field, if src field name does not exist in dest. In other words, dest will preserve all its named fields plus additional fields in dest
syntax
function commonlib.mincopy(dest, src)
parameters
commonlib.partialfields
compare all fields in dest with src and return true if equal.
syntax
function commonlib.partialfields(dest, src, fields)
parameters
commonlib.compare
strict compare all fields reccursively in dest and src, value by value
Note: it also compare indexed array items
- param tolerance : if not nil, it will be used to compare number type values. smaller than this will be regarded as equal.
- return true : if equal.
syntax
function commonlib.compare(dest, src, tolerance)
parameters
dest |
|
src |
|
tolerance |
if not nil, it will be used to compare number type values. smaller than this will be regarded as equal. |
commonlib.resize
resize a table to a new size. It ensures that all elements are nil.
this function uses table.getn() instead of #t, use table.resize for #t
syntax
function commonlib.resize(t, size, v)
parameters
table.resize
resize a table to a new size. It ensures that all elements are nil.
- param t : table to resize
- param size : the new size
- param v : the item value;
syntax
function table.resize(t, size, v)
parameters
t |
table to resize |
size |
|
v |
the item value; |
commonlib.removeArrayItem
remove an item from a table. The table size will be minored by 1.
- param t : table array.
- param nIndex : 1 based index, at which to remove the item
syntax
function commonlib.removeArrayItem(t, nIndex)
parameters
commonlib.insertArrayItem
insert an array item to a table array. The table size will be increased by 1.
- param t : table array.
- param nIndex : 1 based index, at which to insert the item. if nil, it will inserted to the end
- return return : 1 based index at which the item is inserted
syntax
function commonlib.insertArrayItem(t, nIndex, item)
parameters
t |
table array. |
nIndex |
|
item |
|
return |
1 based index at which the item is inserted |
commonlib.swapArrayItem
swap two items in a table.
syntax
function commonlib.swapArrayItem(t, nIndex1, nIndex2)
parameters
utf8.len
a simple UTF8 lib
use:
commonlib.utf8.len("abc");
commonlib.utf8.sub("abc", 1, nil)
local utf8 = {};
commonlib.utf8 = utf8;
return the number of characters in UTF8 encoding.
more info at:
http://lua-users.org/wiki/LuaUnicode
syntax
function utf8.len(unicode_string)
parameters
utf8.sub
similar to string.sub(), except that nFrom, nTo refers to characters, instead of byte
syntax
function utf8.sub(ustring, nFrom, nTo)
parameters
commonlib.SearchFiles
[[ search files and directories in a given path. Results are returned in a table array.
e.g. commonlib.SearchFiles(o, "temp/", "*.txt", 0, 150, true)
Users can override the default behaviors of the UI controls. the Default behavior is this:
listbox_dir shows directories, and is initialized to display sub directories of sInitDir.
single click an item will display files in that directory in listbox_file.
double click an item will display sub directories in listbox_dir.
- param output : values are stored in the out arrays. it must be a table.
- param sInitDir : the initial directory. it must ends with slash /
- param sFilePattern : e.g."*.", "*.x" or it could be table like {"*.lua", "*.raw"}
- param nMaxFileLevels : max file levels. 0 shows files in the current directory.
- param nMaxNumFiles : max number of files in file listbox. e.g. 150
- param listFile : True to include file. This can be nil.
- param listDir : True to include directory. This can be nil.
- param btn :_dir_uplevel: a valid ParaUIObject of type button for displaying the last directory.
]]
syntax
function commonlib.SearchFiles(output, sInitDir, sFilePattern, nMaxFileLevels, nMaxNumFiles, listFile, listDir)
parameters
output |
values are stored in the out arrays. it must be a table. |
sInitDir |
|
sFilePattern |
e.g."*.", "*.x" or it could be table like {"*.lua", "*.raw"} |
nMaxFileLevels |
|
nMaxNumFiles |
max number of files in file listbox. e.g. 150 |
listFile |
|
listDir |
True to include directory. This can be nil. |
commonlib.Absolute
get the absolute of the given number, if param not number type return nil
syntax
function commonlib.Absolute(num)
parameters
commonlib.shellsort
array = shellsort(array, before, n)
- param array : is an array of comparable elements to be sorted in place
- param before : is a function of two arguments which returns true if its first argument should be before the second argument in the second result. It must define a total order on the elements of array.
- param n : is the number of elements in the array. If it is omitted, #array will be used.
- return returns : its first argument.
syntax
function commonlib.shellsort(t, n, before)
parameters
t |
|
n |
is the number of elements in the array. If it is omitted, #array will be used. |
before |
|
commonlib.multi_inherit
look up for `k' in list of tables `plist'
local function search (k, plist)
for i=1, table.getn(plist) do
local v = plist[i][k] -- try `i'-th superclass
if v then return v end
end
end
multiple inheritance
see.
http://www.lua.org/pil/16.3.html
syntax
function commonlib.multi_inherit(...)
commonlib.inherit
create a new class inheriting from a baseClass.
the new class has new(), _super, isa() function.
- param baseClass : the base class from which to inherite the new one. it can be nil if there is no base class.
- param new :_class: nil or a raw table.
- return the : new class is created. One can later create and instance of the new class by calling its new function().
syntax
function commonlib.inherit(baseClass, new_class)
parameters
baseClass |
the base class from which to inherite the new one. it can be nil if there is no base class. |
new |
|
class |
|
return |
new class is created. One can later create and instance of the new class by calling its new function(). |
commonlib.tonumber
convert the string to number
this function will handle the string will additional "0" digits in the front of the string
e.x. input: "010" return: 10
syntax
function commonlib.tonumber(s)
parameters
timer.GetNewTimerID
[[commonlib.timer is a simpler timer ID generator. Use
GetNewTimerID() to create
new timer id to avoid timer id collide.
]]
local timer = {};
timer.id = 0;
timer.count = 0;
commonlib.timer = timer;
syntax
function timer.GetNewTimerID()