mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-30 08:51:10 +02:00
CLuaInstance: Add strFind() and strSub() functions
- These functions are possible substitutes for the Lua functions string.find() and string.sub() - This Lua functions have problems e.g. with the contents of some websites.
This commit is contained in:
@@ -447,6 +447,8 @@ const luaL_Reg CLuaInstance::methods[] =
|
|||||||
{ "GetLanguage", CLuaInstance::GetLanguage },
|
{ "GetLanguage", CLuaInstance::GetLanguage },
|
||||||
{ "runScript", CLuaInstance::runScriptExt },
|
{ "runScript", CLuaInstance::runScriptExt },
|
||||||
{ "PlayFile", CLuaInstance::PlayFile },
|
{ "PlayFile", CLuaInstance::PlayFile },
|
||||||
|
{ "strFind", CLuaInstance::strFind },
|
||||||
|
{ "strSub", CLuaInstance::strSub },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -632,6 +634,62 @@ int CLuaInstance::PlayFile(lua_State *L)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CLuaInstance::strFind(lua_State *L)
|
||||||
|
{
|
||||||
|
int numargs = lua_gettop(L);
|
||||||
|
if (numargs < 3) {
|
||||||
|
printf("CLuaInstance::%s: not enough arguments (%d, expected 2 (or 3 or 4))\n", __func__, numargs);
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const char *s1;
|
||||||
|
const char *s2;
|
||||||
|
int pos=0, n=0, ret=0;
|
||||||
|
s1 = luaL_checkstring(L, 2);
|
||||||
|
s2 = luaL_checkstring(L, 3);
|
||||||
|
if (numargs > 3)
|
||||||
|
pos = luaL_checkint(L, 4);
|
||||||
|
if (numargs > 4)
|
||||||
|
n = luaL_checkint(L, 5);
|
||||||
|
|
||||||
|
std::string str(s1);
|
||||||
|
if (numargs > 4)
|
||||||
|
ret = str.find(s2, pos, n);
|
||||||
|
else
|
||||||
|
ret = str.find(s2, pos);
|
||||||
|
|
||||||
|
// printf("####[%s:%d] str_len: %d, s2: %s, pos: %d, n: %d, ret: %d\n", __func__, __LINE__, str.length(), s2, pos, n, ret);
|
||||||
|
if (ret == (int)std::string::npos)
|
||||||
|
lua_pushnil(L);
|
||||||
|
else
|
||||||
|
lua_pushinteger(L, ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CLuaInstance::strSub(lua_State *L)
|
||||||
|
{
|
||||||
|
int numargs = lua_gettop(L);
|
||||||
|
if (numargs < 3) {
|
||||||
|
printf("CLuaInstance::%s: not enough arguments (%d, expected 2 (or 3))\n", __func__, numargs);
|
||||||
|
lua_pushstring(L, "");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const char *s1;
|
||||||
|
int pos=0, len=std::string::npos;
|
||||||
|
std::string ret="";
|
||||||
|
s1 = luaL_checkstring(L, 2);
|
||||||
|
pos = luaL_checkint(L, 3);
|
||||||
|
if (numargs > 3)
|
||||||
|
len = luaL_checkint(L, 4);
|
||||||
|
|
||||||
|
std::string str(s1);
|
||||||
|
ret = str.substr(pos, len);
|
||||||
|
|
||||||
|
// printf("####[%s:%d] str_len: %d, pos: %d, len: %d, ret_len: %d\n", __func__, __LINE__, str.length(), pos, len, ret.length());
|
||||||
|
lua_pushstring(L, ret.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int CLuaInstance::GetSize(lua_State *L)
|
int CLuaInstance::GetSize(lua_State *L)
|
||||||
{
|
{
|
||||||
DBG("CLuaInstance::%s %d\n", __func__, lua_gettop(L));
|
DBG("CLuaInstance::%s %d\n", __func__, lua_gettop(L));
|
||||||
|
@@ -195,6 +195,9 @@ private:
|
|||||||
static int DisplayImage(lua_State *L);
|
static int DisplayImage(lua_State *L);
|
||||||
static int PlayFile(lua_State *L);
|
static int PlayFile(lua_State *L);
|
||||||
|
|
||||||
|
static int strFind(lua_State *L);
|
||||||
|
static int strSub(lua_State *L);
|
||||||
|
|
||||||
void MenuRegister(lua_State *L);
|
void MenuRegister(lua_State *L);
|
||||||
static int MenuNew(lua_State *L);
|
static int MenuNew(lua_State *L);
|
||||||
static int MenuDelete(lua_State *L);
|
static int MenuDelete(lua_State *L);
|
||||||
|
Reference in New Issue
Block a user