From 7d46e610c0c93d68cb28e51f42691b3a404f2f7e Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 20 Jul 2015 20:35:35 +0000 Subject: MirLua: - added iterators in m_database and m_protocols - added more short aliases for functions in m_database - added some examples in m_database - version bump git-svn-id: http://svn.miranda-ng.org/main/trunk@14590 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_database.cpp | 201 +++++++++++++++++++++++++++++++++----- 1 file changed, 177 insertions(+), 24 deletions(-) (limited to 'plugins/MirLua/src/m_database.cpp') diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index a1cf6f1dc6..537322d5f4 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -12,7 +12,7 @@ static int lua_FindFirstContact(lua_State *L) static int lua_FindNextContact(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); const char *szProto = lua_tostring(L, 2); MCONTACT res = db_find_next(hContact, szProto); @@ -21,9 +21,43 @@ static int lua_FindNextContact(lua_State *L) return 1; } +static int lua_ContactIterator(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, lua_upvalueindex(1)); + const char *szProto = lua_tostring(L, lua_upvalueindex(2)); + + hContact = hContact == NULL + ? db_find_first(szProto) + : db_find_next(hContact); + + if (hContact) + { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(1)); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllContacts(lua_State *L) +{ + const char *szProto = lua_tostring(L, 1); + + lua_pushinteger(L, NULL); + lua_pushstring(L, szProto); + lua_pushcclosure(L, lua_ContactIterator, 2); + + return 1; +} + +/***********************************************/ + static int lua_GetEventCount(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); int res = ::db_event_count(hContact); lua_pushinteger(L, res); @@ -33,7 +67,7 @@ static int lua_GetEventCount(lua_State *L) static int lua_GetFirstEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); MEVENT res = ::db_event_first(hContact); lua_pushinteger(L, res); @@ -43,8 +77,8 @@ static int lua_GetFirstEvent(lua_State *L) static int lua_GetPrevEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); - MEVENT hEvent = lua_tointeger(L, 2); + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hEvent = luaL_checkinteger(L, 2); MEVENT res = ::db_event_prev(hContact, hEvent); lua_pushinteger(L, res); @@ -54,8 +88,8 @@ static int lua_GetPrevEvent(lua_State *L) static int lua_GetNextEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); - MEVENT hEvent = lua_tointeger(L, 2); + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hEvent = luaL_checkinteger(L, 2); MEVENT res = ::db_event_next(hContact, hEvent); lua_pushinteger(L, res); @@ -65,7 +99,7 @@ static int lua_GetNextEvent(lua_State *L) static int lua_GetLastEvent(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_checkinteger(L, 1); MEVENT res = ::db_event_last(hContact); lua_pushinteger(L, res); @@ -75,7 +109,7 @@ static int lua_GetLastEvent(lua_State *L) static int lua_GetEvent(lua_State *L) { - MEVENT hEvent = lua_tointeger(L, 1); + MEVENT hEvent = luaL_checkinteger(L, 1); DBEVENTINFO dbei = { sizeof(DBEVENTINFO) }; dbei.cbBlob = db_event_getBlobSize(hEvent); @@ -119,7 +153,73 @@ static int lua_GetEvent(lua_State *L) return 1; } -static int lua_WriteContactSetting(lua_State *L) +static int lua_EventIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hEvent = hEvent == NULL + ? db_event_first(hContact) + : db_event_next(hContact, hEvent); + + if (hEvent) + { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllEvents(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, lua_EventIterator, 2); + + return 1; +} + +static int lua_EventReverseIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hEvent = hEvent == NULL + ? db_event_last(hContact) + : db_event_prev(hContact, hEvent); + + if (hEvent) + { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int lua_AllEventsFromEnd(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, lua_EventReverseIterator, 2); + + return 1; +} + +/***********************************************/ + +static int lua_WriteSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); LPCSTR szModule = luaL_checkstring(L, 2); @@ -153,7 +253,7 @@ static int lua_WriteContactSetting(lua_State *L) return 1; } -static int lua_GetContactSetting(lua_State *L) +static int lua_GetSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); LPCSTR szModule = luaL_checkstring(L, 2); @@ -200,7 +300,7 @@ static int lua_GetContactSetting(lua_State *L) typedef struct { - int arrlen; + int count; char **pszSettingName; } enumDBSettingsParam; @@ -211,17 +311,62 @@ static int SettingsEnumProc(const char* szSetting, LPARAM lParam) { enumDBSettingsParam* p = (enumDBSettingsParam*)lParam; - p->arrlen++; - p->pszSettingName = (char**)mir_realloc(p->pszSettingName, p->arrlen * sizeof(char*)); - p->pszSettingName[p->arrlen - 1] = mir_strdup(szSetting); + p->count++; + p->pszSettingName = (char**)mir_realloc(p->pszSettingName, p->count * sizeof(char*)); + p->pszSettingName[p->count - 1] = mir_strdup(szSetting); } return 0; } +static int lua_SettingIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + enumDBSettingsParam* param = (enumDBSettingsParam*)lua_touserdata(L, lua_upvalueindex(2)); + + if (i < param->count) + { + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + lua_pushstring(L, ptrA(mir_utf8encode(param->pszSettingName[i]))); + mir_free(param->pszSettingName[i]); + } + else + { + lua_pushnil(L); + mir_free(param->pszSettingName); + //mir_free(param); + } + + return 1; +} + +static int lua_AllSettings(lua_State *L) +{ + const char* szModule = luaL_checkstring(L, 1); + MCONTACT hContact = lua_tointeger(L, 2); + + enumDBSettingsParam* param = (enumDBSettingsParam*)mir_alloc(sizeof(enumDBSettingsParam*)); + param->count = 0; + param->pszSettingName = NULL; + + DBCONTACTENUMSETTINGS dbces = { 0 }; + dbces.pfnEnumProc = SettingsEnumProc; + dbces.szModule = szModule; + dbces.ofsSettings = 0; + dbces.lParam = (LPARAM)param; + ::CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); + + lua_pushinteger(L, 0); + lua_pushlightuserdata(L, param); + lua_pushcclosure(L, lua_SettingIterator, 2); + + return 1; +} + static int lua_EnumSettings(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); - LPCSTR szModule = luaL_checkstring(L, 2); + LPCSTR szModule = luaL_checkstring(L, 1); + MCONTACT hContact = lua_tointeger(L, 2); if (!lua_isfunction(L, 3)) { @@ -241,7 +386,7 @@ static int lua_EnumSettings(lua_State *L) dbces.lParam = (LPARAM)¶m; INT_PTR res = ::CallService(MS_DB_CONTACT_ENUMSETTINGS, hContact, (LPARAM)&dbces); - for (int i = 0; i < param.arrlen; i++) + for (int i = 0; i < param.count; i++) { lua_rawgeti(L, LUA_REGISTRYINDEX, ref); lua_pushstring(L, mir_utf8encode(param.pszSettingName[i])); @@ -258,7 +403,7 @@ static int lua_EnumSettings(lua_State *L) return 1; } -static int lua_DeleteContactSetting(lua_State *L) +static int lua_DeleteSetting(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); LPCSTR szModule = luaL_checkstring(L, 2); @@ -333,7 +478,7 @@ static int SettingsChangedHookEventObjParam(void *obj, WPARAM wParam, LPARAM lPa return res; } -static int lua_OnContactSettingChanged(lua_State *L) +static int lua_OnSettingChanged(lua_State *L) { if (!lua_isfunction(L, 1)) { @@ -398,6 +543,7 @@ static luaL_Reg databaseApi[] = { { "FindFirstContact", lua_FindFirstContact }, { "FindNextContact", lua_FindNextContact }, + { "AllContacts", lua_AllContacts }, { "GetEventCount", lua_GetEventCount }, @@ -405,18 +551,25 @@ static luaL_Reg databaseApi[] = { "GetPrevEvent", lua_GetPrevEvent }, { "GetNextEvent", lua_GetNextEvent }, { "GetLastEvent", lua_GetLastEvent }, + { "AllEvents", lua_AllEvents }, + { "GetEventsFromEnd", lua_AllEventsFromEnd }, { "GetEvent", lua_GetEvent }, - { "WriteContactSetting", lua_WriteContactSetting }, + { "WriteContactSetting", lua_WriteSetting }, + { "WriteSetting", lua_WriteSetting }, - { "GetContactSetting", lua_GetContactSetting }, + { "GetContactSetting", lua_GetSetting }, + { "GetSetting", lua_GetSetting }, + { "AllSettings", lua_AllSettings }, { "EnumSettings", lua_EnumSettings }, - { "DeleteContactSetting", lua_DeleteContactSetting }, + { "DeleteContactSetting", lua_DeleteSetting }, + { "DeleteSetting", lua_DeleteSetting }, { "DeleteModule", lua_DeleteModule }, - { "OnContactSettingChanged", lua_OnContactSettingChanged }, + { "OnContactSettingChanged", lua_OnSettingChanged }, + { "OnSettingChanged", lua_OnSettingChanged }, { "DecodeDBCONTACTWRITESETTING", lua_DecodeDBCONTACTWRITESETTING }, { NULL, NULL } -- cgit v1.2.3