Comparison

util-src/windows.c @ 4128:b6d072a3668d

windows.c: Return nil,err from functions instead of throwing errors.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 29 Jan 2011 04:40:43 +0500
parent 3868:72d68f996f45
child 5864:22b1d18eb919
comparison
equal deleted inserted replaced
4127:f80b7a92da67 4128:b6d072a3668d
36 lua_pushstring(L, ip_str); 36 lua_pushstring(L, ip_str);
37 lua_rawseti(L, -2, i+1); 37 lua_rawseti(L, -2, i+1);
38 } 38 }
39 return 1; 39 return 1;
40 } else { 40 } else {
41 luaL_error(L, "DnsQueryConfig returned %d", status); 41 lua_pushnil(L);
42 return 0; // unreachable, but prevents a compiler warning 42 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
43 return 2;
43 } 44 }
44 } 45 }
45 46
46 static void lassert(lua_State *L, BOOL test, char* string) { 47 static int lerror(lua_State *L, char* string) {
47 if (!test) { 48 lua_pushnil(L);
48 luaL_error(L, "%s: %d", string, GetLastError()); 49 lua_pushfstring(L, "%s: %d", string, GetLastError());
49 } 50 return 2;
50 } 51 }
51 52
52 static int Lget_consolecolor(lua_State *L) { 53 static int Lget_consolecolor(lua_State *L) {
53 HWND console = GetStdHandle(STD_OUTPUT_HANDLE); 54 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
54 WORD color; DWORD read_len; 55 WORD color; DWORD read_len;
55 56
56 CONSOLE_SCREEN_BUFFER_INFO info; 57 CONSOLE_SCREEN_BUFFER_INFO info;
57 58
58 lassert(L, console != INVALID_HANDLE_VALUE, "GetStdHandle"); 59 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
59 lassert(L, GetConsoleScreenBufferInfo(console, &info), "GetConsoleScreenBufferInfo"); 60 if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo");
60 lassert(L, ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len), "ReadConsoleOutputAttribute"); 61 if (!ReadConsoleOutputAttribute(console, &color, sizeof(WORD), info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute");
61 62
62 lua_pushnumber(L, color); 63 lua_pushnumber(L, color);
63 return 1; 64 return 1;
64 } 65 }
65 static int Lset_consolecolor(lua_State *L) { 66 static int Lset_consolecolor(lua_State *L) {
66 int color = luaL_checkint(L, 1); 67 int color = luaL_checkint(L, 1);
67 HWND console = GetStdHandle(STD_OUTPUT_HANDLE); 68 HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
68 lassert(L, console != INVALID_HANDLE_VALUE, "GetStdHandle"); 69 if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
69 lassert(L, SetConsoleTextAttribute(console, color), "SetConsoleTextAttribute"); 70 if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute");
70 return 0; 71 lua_pushboolean(L, 1);
72 return 1;
71 } 73 }
72 74
73 static const luaL_Reg Reg[] = 75 static const luaL_Reg Reg[] =
74 { 76 {
75 { "get_nameservers", Lget_nameservers }, 77 { "get_nameservers", Lget_nameservers },