More C-Functions (luci.cutil.instanceof, luci.cutil.pcdata)

This commit is contained in:
Steven Barth 2008-11-20 19:22:05 +00:00
parent 9a203c52dd
commit 9c260769a5
4 changed files with 115 additions and 11 deletions

View file

@ -12,20 +12,23 @@
<builder id="org.eclipse.cdt.build.core.settings.default.builder.823611263" keepEnvironmentInBuildfile="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/> <builder id="org.eclipse.cdt.build.core.settings.default.builder.823611263" keepEnvironmentInBuildfile="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.libs.1685234798" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/> <tool id="org.eclipse.cdt.build.core.settings.holder.libs.1685234798" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
<tool id="org.eclipse.cdt.build.core.settings.holder.570824141" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder"> <tool id="org.eclipse.cdt.build.core.settings.holder.570824141" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.491129717" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath"> <option id="org.eclipse.cdt.build.core.settings.holder.incpaths.491129717" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/usr/include/lua5.1"/> <listOptionValue builtIn="false" value="/usr/include/lua5.1"/>
<listOptionValue builtIn="false" value="/usr/include"/>
</option> </option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.858094059" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/> <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.858094059" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool> </tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.344617955" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder"> <tool id="org.eclipse.cdt.build.core.settings.holder.344617955" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.564526987" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath"> <option id="org.eclipse.cdt.build.core.settings.holder.incpaths.564526987" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/usr/include/lua5.1"/> <listOptionValue builtIn="false" value="/usr/include/lua5.1"/>
<listOptionValue builtIn="false" value="/usr/include"/>
</option> </option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1426860237" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/> <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1426860237" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool> </tool>
<tool id="org.eclipse.cdt.build.core.settings.holder.869454750" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder"> <tool id="org.eclipse.cdt.build.core.settings.holder.869454750" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
<option id="org.eclipse.cdt.build.core.settings.holder.incpaths.838425449" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath"> <option id="org.eclipse.cdt.build.core.settings.holder.incpaths.838425449" name="Include Paths" superClass="org.eclipse.cdt.build.core.settings.holder.incpaths" valueType="includePath">
<listOptionValue builtIn="false" value="/usr/include/lua5.1"/> <listOptionValue builtIn="false" value="/usr/include/lua5.1"/>
<listOptionValue builtIn="false" value="/usr/include"/>
</option> </option>
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1755775295" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/> <inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1755775295" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
</tool> </tool>

View file

@ -9,4 +9,4 @@ compile: src/luci_cutil.o
$(LINK) $(SHLIB_FLAGS) -o dist$(LUCI_LIBRARYDIR)/cutil.so src/luci_cutil.o $(LINK) $(SHLIB_FLAGS) -o dist$(LUCI_LIBRARYDIR)/cutil.so src/luci_cutil.o
clean: luaclean clean: luaclean
rm src/*.o rm -f src/*.o

View file

@ -105,6 +105,7 @@ class = cutil.class
-- @return Boolean indicating whether the object is an instance -- @return Boolean indicating whether the object is an instance
-- @see class -- @see class
-- @see clone -- @see clone
--[[
function instanceof(object, class) function instanceof(object, class)
local meta = getmetatable(object) local meta = getmetatable(object)
while meta and meta.__index do while meta and meta.__index do
@ -115,6 +116,8 @@ function instanceof(object, class)
end end
return false return false
end end
]]--
instanceof = cutil.instanceof
-- --
@ -201,6 +204,7 @@ end
--- Create valid XML PCDATA from given string. --- Create valid XML PCDATA from given string.
-- @param value String value containing the data to escape -- @param value String value containing the data to escape
-- @return String value containing the escaped data -- @return String value containing the escaped data
--[[
function pcdata(value) function pcdata(value)
return value and tostring(value):gsub("[&\"'<>]", { return value and tostring(value):gsub("[&\"'<>]", {
["&"] = "&#38;", ["&"] = "&#38;",
@ -210,6 +214,8 @@ function pcdata(value)
[">"] = "&#62;" [">"] = "&#62;"
}) })
end end
]]--
pcdata = cutil.pcdata
--- Strip HTML tags from given string. --- Strip HTML tags from given string.
-- @param value String containing the HTML text -- @param value String containing the HTML text

View file

@ -33,14 +33,12 @@ static int luci__string_mod(lua_State *L) {
lua_settop(L, 2); lua_settop(L, 2);
/* Get format and push it to the bottom of the stack */ /* Get format and push it to the bottom of the stack */
lua_pushliteral(L, ""); lua_getfield(L, 1, "format");
lua_getfield(L, -1, "format");
lua_insert(L, 1); lua_insert(L, 1);
lua_pop(L, 1);
/* If second argument is a table, unpack it */ /* If second argument is a table, unpack it */
if (lua_istable(L, 3)) { if (lua_istable(L, 3)) {
n = luaL_getn(L, 3); n = lua_objlen(L, 3);
if (n > 0) { if (n > 0) {
luaL_checkstack(L, n, "too many results to unpack"); luaL_checkstack(L, n, "too many results to unpack");
for (i=1; i<=n; i++) { for (i=1; i<=n; i++) {
@ -91,13 +89,11 @@ static int luci__instantiate(lua_State *L) {
/* luci.cutil.class(baseclass) */ /* luci.cutil.class(baseclass) */
static int luci_class(lua_State *L) { static int luci_class(lua_State *L) {
int n = lua_gettop(L);
/* Create class */ /* Create class */
lua_newtable(L); lua_newtable(L);
/* Create metatable and register parent class if any */ /* Create metatable and register parent class if any */
if (n && lua_istable(L, 1)) { if (lua_gettop(L) > 1 && lua_istable(L, 1)) {
lua_createtable(L, 0, 2); lua_createtable(L, 0, 2);
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
@ -113,10 +109,106 @@ static int luci_class(lua_State *L) {
return 1; return 1;
} }
/* luci.cutil.instanceof(object, class) */
static int luci_instanceof(lua_State *L) {
int stat = 0;
luaL_checkany(L, 1);
luaL_checkany(L, 2);
if (lua_getmetatable(L, 1)) {
/* get parent class */
lua_getfield(L, -1, "__index");
while (lua_istable(L, -1)) {
/* parent class == class */
if (lua_equal(L, -1, 2)) {
stat = 1;
break;
}
/* remove last metatable */
lua_remove(L, -2);
/* get metatable of parent class */
if (lua_getmetatable(L, -1)) {
/* remove last parent class */
lua_remove(L, -2);
/* get next parent class */
lua_getfield(L, -1, "__index");
} else {
break;
}
}
}
lua_pushboolean(L, stat);
return 1;
}
/* luci.cutil.pcdata(obj) */
static int luci_pcdata(lua_State *L) {
if (lua_isnone(L, 1)) {
lua_pushnil(L);
return 1;
}
/* Discard anything else */
lua_settop(L, 1);
/* tostring(obj) */
lua_pushvalue(L, lua_upvalueindex(1));
lua_insert(L, 1);
lua_call(L, 1, 1);
/* pattern */
lua_pushvalue(L, lua_upvalueindex(2));
/* repl */
lua_pushvalue(L, lua_upvalueindex(3));
/* get gsub function */
lua_getfield(L, -3, "gsub");
lua_insert(L, 1);
/* tostring(obj):gsub(pattern, repl) */
lua_call(L, 3, 1);
return 1;
}
/* Registration helper for luci.cutil.pcdata */
static void luci__register_pcdata(lua_State *L) {
/* tostring */
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
/* pattern */
lua_pushliteral(L, "[&\"'<>]");
/* repl */
lua_createtable(L, 0, 5);
lua_pushliteral(L, "&#38;");
lua_setfield(L, -2, "&");
lua_pushliteral(L, "&#34;");
lua_setfield(L, -2, "\"");
lua_pushliteral(L, "&#39;");
lua_setfield(L, -2, "'");
lua_pushliteral(L, "&#60;");
lua_setfield(L, -2, "<");
lua_pushliteral(L, "&#62;");
lua_setfield(L, -2, ">");
/* register function */
lua_pushcclosure(L, luci_pcdata, 3);
lua_setfield(L, -2, "pcdata");
}
/* Registry */ /* Registry */
static const luaL_Reg registry[] = { static const luaL_Reg registry[] = {
{"class", luci_class}, {"class", luci_class},
{"instanceof", luci_instanceof},
{ NULL, NULL }, { NULL, NULL },
}; };
@ -130,6 +222,9 @@ LUALIB_API int luaopen_luci_cutil(lua_State *L) {
lua_pushliteral(L, LUCI_MODCOPY); lua_pushliteral(L, LUCI_MODCOPY);
lua_setfield(L, -2, "_COPYRIGHT"); lua_setfield(L, -2, "_COPYRIGHT");
/* Additional registrations */
luci__register_pcdata(L);
/* Register pythonic printf string operator */ /* Register pythonic printf string operator */
lua_pushliteral(L, ""); lua_pushliteral(L, "");