9 #define WHELK_KEYHANDLE "HKEY"
10 #define whelk_checkkeyptr(L,n) ((HKEY *)luaL_checkudata(L,n,WHELK_KEYHANDLE))
12 static HKEY whelk_checkkey(lua_State *L,int n)
14 HKEY *key=whelk_checkkeyptr(L,n);
15 if (*key==INVALID_HANDLE_VALUE)
16 luaL_error(L,"attempt to use a closed WIN32 registry key");
20 static HKEY *newkey(lua_State *L)
22 HKEY *key=(HKEY *)lua_newuserdata(L,sizeof(HKEY));
23 *key=INVALID_HANDLE_VALUE;
24 luaL_getmetatable(L,WHELK_KEYHANDLE);
25 lua_setmetatable(L,-2);
29 static int get_value(lua_State *L)
35 HKEY key=whelk_checkkey(L,1);
36 const char *subkey=luaL_checkstring(L,2);
37 const char *value=luaL_checkstring(L,3);
38 result=whelk_reg_get_value(key,subkey,value,&type,&data,&nb);
39 if (result!=ERROR_SUCCESS)
42 lua_pushfstring(L,"%s: Failed to get value",value);
43 lua_pushinteger(L,result);
51 lua_pushstring(L,(char *)data);
54 lua_pushnumber(L,*(DWORD *)data);
58 lua_pushfstring(L,"%s: Data is not a string or number",value);
59 lua_pushinteger(L,ERROR_INVALID_PARAMETER);
66 static int set_value(lua_State *L)
74 HKEY key=whelk_checkkey(L,1);
75 const char *subkey=luaL_checkstring(L,2);
76 const char *value=luaL_checkstring(L,3);
80 data=lua_tostring(L,4);
85 number=lua_tonumber(L,4);
87 if ((lua_Number)d!=number)
90 lua_pushfstring(L,"%s: Data is not storable as DWORD",value);
91 lua_pushinteger(L,ERROR_INVALID_PARAMETER);
104 lua_pushfstring(L,"%s: Data is not a string or number",value);
105 lua_pushinteger(L,ERROR_INVALID_PARAMETER);
108 result=whelk_reg_set_value(key,subkey,value,type,data,nb);
109 if (result!=ERROR_SUCCESS)
112 lua_pushfstring(L,"%s: Failed to set value",value);
113 lua_pushinteger(L,result);
119 static int delete_key(lua_State *L)
122 HKEY key=whelk_checkkey(L,1);
123 const char *subkey=luaL_checkstring(L,2);
124 result=whelk_reg_delete_key(key,subkey);
125 if (result!=ERROR_SUCCESS)
128 lua_pushfstring(L,"%s: Failed to delete key",subkey);
129 lua_pushinteger(L,result);
135 static int reg_gc(lua_State *L)
137 HKEY key=*whelk_checkkeyptr(L,1);
138 if (key!=INVALID_HANDLE_VALUE)
139 (void)whelk_reg_close_key(key);
143 static const luaL_Reg reg_methods[]={
144 { "GetValue",get_value },
145 { "SetValue",set_value },
146 { "DeleteKey",delete_key },
151 static void create_predefined_key(lua_State *L,HKEY key,const char *name)
154 lua_setfield(L,-3,name);
157 void whelk_open_reg_keys(lua_State *L)
159 luaL_newmetatable(L,WHELK_KEYHANDLE);
161 lua_setfield(L,-2,"__index");
162 luaL_register(L,NULL,reg_methods);
163 create_predefined_key(L,HKEY_CLASSES_ROOT,"KEY_CLASSES_ROOT");
164 create_predefined_key(L,HKEY_CURRENT_CONFIG,"KEY_CURRENT_CONFIG");
165 create_predefined_key(L,HKEY_CURRENT_USER,"KEY_CURRENT_USER");
166 create_predefined_key(L,HKEY_LOCAL_MACHINE,"KEY_LOCAL_MACHINE");
167 create_predefined_key(L,HKEY_USERS,"KEY_USERS");