From af8d02de12c94a733a543ee346125cfa34d6372e Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 26 Jul 2015 11:43:31 +0000 Subject: MirLua: added find files iterator and registry functions to m_windows git-svn-id: http://svn.miranda-ng.org/main/trunk@14721 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_windows.cpp | 241 +++++++++++++++++++++++++++++++++++++- plugins/MirLua/src/mlua_utils.cpp | 2 + 2 files changed, 242 insertions(+), 1 deletion(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/m_windows.cpp b/plugins/MirLua/src/m_windows.cpp index f2974583f2..cf43060e1d 100644 --- a/plugins/MirLua/src/m_windows.cpp +++ b/plugins/MirLua/src/m_windows.cpp @@ -84,6 +84,74 @@ static int lua_ShellExecute(lua_State *L) return 0; } +static int lua_FindIterator(lua_State *L) +{ + HANDLE hFind = lua_touserdata(L, lua_upvalueindex(1)); + TCHAR* path = (TCHAR*)lua_touserdata(L, lua_upvalueindex(2)); + + WIN32_FIND_DATA ffd = { 0 }; + if (hFind == NULL) + { + hFind = FindFirstFile(path, &ffd); + if (hFind == INVALID_HANDLE_VALUE) + { + mir_free(path); + lua_pushnil(L); + return 1; + } + } + else + { + if (FindNextFile(hFind, &ffd) == 0) + { + FindClose(hFind); + mir_free(path); + lua_pushnil(L); + return 1; + } + } + + if (!mir_tstrcmpi(ffd.cFileName, _T(".")) || + !mir_tstrcmpi(ffd.cFileName, _T("..")) || + (ffd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) + { + lua_pushlightuserdata(L, hFind); + lua_replace(L, lua_upvalueindex(1)); + + return lua_FindFileIterator(L); + } + + lua_newtable(L); + lua_pushliteral(L, "Name"); + lua_pushstring(L, T2Utf(ffd.cFileName)); + lua_settable(L, -3); + lua_pushliteral(L, "Size"); + lua_pushinteger(L, (ffd.nFileSizeHigh * (MAXDWORD + 1)) + ffd.nFileSizeLow); + lua_settable(L, -3); + lua_pushliteral(L, "IsFile"); + lua_pushboolean(L, !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); + lua_settable(L, -3); + lua_pushliteral(L, "IsDirectory"); + lua_pushboolean(L, ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); + lua_settable(L, -3); + + lua_pushlightuserdata(L, hFind); + lua_replace(L, lua_upvalueindex(1)); + + return 1; +} + +static int lua_Find(lua_State *L) +{ + TCHAR* path = mir_utf8decodeT(luaL_checkstring(L, 1)); + + lua_pushlightuserdata(L, NULL); + lua_pushlightuserdata(L, path); + lua_pushcclosure(L, lua_FindIterator, 2); + + return 1; +} + static int lua_GetIniValue(lua_State *L) { ptrT path(mir_utf8decodeT(luaL_checkstring(L, 1))); @@ -127,14 +195,175 @@ static int lua_SetIniValue(lua_State *L) return 1; } +static int lua_DeleteIniValue(lua_State *L) +{ + ptrT path(mir_utf8decodeT(luaL_checkstring(L, 1))); + ptrT section(mir_utf8decodeT(luaL_checkstring(L, 2))); + ptrT key(mir_utf8decodeT(luaL_checkstring(L, 3))); + + bool res = ::WritePrivateProfileString(section, key, NULL, path) != 0; + lua_pushboolean(L, res); + + return 1; +} + +static int lua_GetRegValue(lua_State *L) +{ + HKEY hRootKey = (HKEY)lua_touserdata(L, 1); + ptrT path(mir_utf8decodeT(luaL_checkstring(L, 2))); + ptrT valueName(mir_utf8decodeT(luaL_checkstring(L, 3))); + + HKEY hKey = 0; + LSTATUS res = ::RegOpenKeyEx(hRootKey, path, NULL, KEY_WRITE, &hKey); + if (res != ERROR_SUCCESS) + { + lua_pushvalue(L, 4); + return 1; + } + + DWORD type = 0; + DWORD length = 1024; + BYTE* value = (BYTE*)mir_alloc(length); + res = ::RegQueryValueEx(hKey, valueName, NULL, &type, (LPBYTE)value, &length); + while (res == ERROR_MORE_DATA) + { + length += length; + value = (BYTE*)mir_realloc(value, length); + res = ::RegQueryValueEx(hKey, valueName, NULL, &type, (LPBYTE)value, &length); + } + + if (res == ERROR_SUCCESS) + { + switch (type) + { + case REG_DWORD: + case REG_DWORD_BIG_ENDIAN: + lua_pushinteger(L, (int)value); + break; + + case REG_QWORD: + lua_pushnumber(L, (int)value); + break; + + case REG_SZ: + case REG_LINK: + case REG_EXPAND_SZ: + lua_pushlstring(L, ptrA(Utf8EncodeT((TCHAR*)value)), length); + break; + + default: + lua_pushvalue(L, 4); + break; + } + } + else + lua_pushvalue(L, 4); + + ::RegCloseKey(hKey); + mir_free(value); + + return 1; +} + +static int lua_SetRegValue(lua_State *L) +{ + HKEY hRootKey = (HKEY)lua_touserdata(L, 1); + ptrT path(mir_utf8decodeT(luaL_checkstring(L, 2))); + ptrT valueName(mir_utf8decodeT(luaL_checkstring(L, 3))); + + HKEY hKey = 0; + LSTATUS res = ::RegOpenKeyEx(hRootKey, path, NULL, KEY_WRITE, &hKey); + if (res != ERROR_SUCCESS) + { + lua_pushboolean(L, FALSE); + return 1; + } + + DWORD type = 0; + DWORD length = 0; + BYTE* value = NULL; + switch (lua_type(L, 4)) + { + case LUA_TNUMBER: + if (lua_isinteger(L, 4) && lua_tointeger(L, 4) < UINT32_MAX) + { + type = REG_DWORD; + length = sizeof(DWORD); + value = (BYTE*)lua_tointeger(L, 4); + } + else + { + type = REG_QWORD; + length = sizeof(DWORD) * 2; + lua_Number num = lua_tonumber(L, 4); + value = (BYTE*)# + } + break; + + case LUA_TSTRING: + type = REG_SZ; + length = mir_strlen(lua_tostring(L, 4)); + value = (BYTE*)mir_utf8decodeT(lua_tostring(L, 4)); + break; + + default: + lua_pushboolean(L, FALSE); + break; + } + + res = ::RegSetValueEx(hKey, valueName, NULL, type, value, length); + lua_pushboolean(L, res == ERROR_SUCCESS); + + ::RegCloseKey(hKey); + if (lua_isstring(L, 4)) + mir_free(value); + + return 1; +} + +static int lua_DeleteRegValue(lua_State *L) +{ + HKEY hRootKey = (HKEY)lua_touserdata(L, 1); + ptrT path(mir_utf8decodeT(luaL_checkstring(L, 2))); + ptrT valueName(mir_utf8decodeT(luaL_checkstring(L, 3))); + + HKEY hKey = 0; + LSTATUS res = ::RegOpenKeyEx(hRootKey, path, NULL, KEY_WRITE, &hKey); + if (res != ERROR_SUCCESS) + { + lua_pushboolean(L, FALSE); + return 1; + } + + res = ::RegDeleteValue(hKey, valueName); + lua_pushboolean(L, res == ERROR_SUCCESS); + + ::RegCloseKey(hKey); + + return 1; +} + static luaL_Reg winApi[] = { { "MessageBox", lua_MessageBox }, + { "ShellExecute", lua_ShellExecute }, + + { "Find", lua_Find }, + { "GetIniValue", lua_GetIniValue }, { "SetIniValue", lua_SetIniValue }, + { "DeleteIniValue", lua_DeleteIniValue }, - { "ShellExecute", lua_ShellExecute }, + { "GetRegValue", lua_GetRegValue }, + { "SetRegValue", lua_SetRegValue }, + { "DeleteRegValue", lua_DeleteRegValue }, + + { "HKEY_CLASSES_ROOT", NULL }, + { "HKEY_CURRENT_USER", NULL }, + { "HKEY_LOCAL_MACHINE", NULL }, + { "HKEY_USERS", NULL }, + { "HKEY_CURRENT_CONFIG", NULL }, { NULL, NULL } }; @@ -142,6 +371,16 @@ static luaL_Reg winApi[] = LUAMOD_API int luaopen_m_windows(lua_State *L) { luaL_newlib(L, winApi); + lua_pushlightuserdata(L, HKEY_CLASSES_ROOT); + lua_setfield(L, -2, "HKEY_CLASSES_ROOT"); + lua_pushlightuserdata(L, HKEY_CURRENT_USER); + lua_setfield(L, -2, "HKEY_CURRENT_USER"); + lua_pushlightuserdata(L, HKEY_LOCAL_MACHINE); + lua_setfield(L, -2, "HKEY_LOCAL_MACHINE"); + lua_pushlightuserdata(L, HKEY_USERS); + lua_setfield(L, -2, "HKEY_USERS"); + lua_pushlightuserdata(L, HKEY_CURRENT_CONFIG); + lua_setfield(L, -2, "HKEY_CURRENT_CONFIG"); return 1; } diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index 31af863da2..04c41a5c6e 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -12,6 +12,8 @@ int luaM_print(lua_State *L) data.AppendFormat("%s ", "nil"); break; case LUA_TBOOLEAN: + data.AppendFormat("%s ", lua_toboolean(L, i) ? "true" : "false"); + break; case LUA_TNUMBER: case LUA_TSTRING: data.AppendFormat("%s ", lua_tostring(L, i)); -- cgit v1.2.3