/
glspi_kfile.c
415 lines (338 loc) · 8.68 KB
/
glspi_kfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* glspi_kfile.c - This file is part of the Lua scripting plugin for the Geany IDE
* See the file "geanylua.c" for copyright information.
*/
/* Minimal interface to a GKeyFile object */
#include <glib.h>
#include <glib/gi18n.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define LUA_MODULE_NAME "keyfile"
#define MetaName "_g_key_file_metatable"
#define SetTableValue(name,value,pusher) \
lua_pushstring(L, name); \
pusher(L, value); \
lua_rawset(L,-3);
#define SetTableStr(name,value) SetTableValue(name,value,lua_pushstring)
#define FAIL_STRING_ARG(argnum) \
(fail_arg_type(L,__FUNCTION__,argnum,"string"))
#define FAIL_BOOL_ARG(argnum) \
(fail_arg_type(L,__FUNCTION__,argnum,"boolean"))
#define FAIL_TABLE_ARG(argnum) \
(fail_arg_type(L,__FUNCTION__,argnum,"table"))
#define FAIL_KEYFILE_ARG(argnum) \
(fail_arg_type(L,__FUNCTION__,argnum,LuaKeyFileType))
#define push_number(L,n) lua_pushnumber(L,(lua_Number)n)
/* Subtract one from error argnum if we have implicit "self" */
static gint adjust_argnum(lua_State *L, gint argnum) {
lua_Debug ar;
if (lua_getstack(L, 0, &ar)){
lua_getinfo(L, "n", &ar);
if (g_str_equal(ar.namewhat, "method")) { return argnum-1; }
}
return argnum;
}
/* Pushes an error message onto Lua stack if script passes a wrong arg type */
static gint fail_arg_type(lua_State *L, const gchar *func, gint argnum, const gchar *type)
{
lua_pushfstring(L, _("Error in module \"%s\" at function %s():\n"
" expected type \"%s\" for argument #%d\n"),
LUA_MODULE_NAME, func+6, type, adjust_argnum(L,argnum));
lua_error(L);
return 0;
}
#define KF_FLAGS G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS
static const gchar*LuaKeyFileType="GKeyFile";
typedef struct _LuaKeyFile
{
const gchar*id;
GKeyFile*kf;
gboolean managed;
} LuaKeyFile;
typedef gint (*KeyfileAssignFunc) (lua_State *L, GKeyFile*kf);
static gint glspi_kfile_assign(lua_State *L, GKeyFile*kf)
{
LuaKeyFile*k=(LuaKeyFile*)lua_newuserdata(L,sizeof(LuaKeyFile));
k->id=LuaKeyFileType;
k->kf=kf;
luaL_getmetatable(L, MetaName);
lua_setmetatable(L, -2);
k->managed=FALSE;
return 1;
}
static gint kfile_new(lua_State *L)
{
LuaKeyFile*k=(LuaKeyFile*)lua_newuserdata(L,sizeof(LuaKeyFile));
k->id=LuaKeyFileType;
k->kf=g_key_file_new();
luaL_getmetatable(L, MetaName);
lua_setmetatable(L, -2);
k->managed=TRUE;
return 1;
}
static gint kfile_done(lua_State *L)
{
LuaKeyFile*k;
if (lua_isnil(L, 1)) { return 0; }
k=(LuaKeyFile*)lua_touserdata(L,1);
if ( (k->id!=LuaKeyFileType) || (!k->managed) ) { return 1; }
g_key_file_free(k->kf);
return 1;
}
static LuaKeyFile* tokeyfile(lua_State *L, gint argnum)
{
LuaKeyFile* rv;
if ( (lua_gettop(L)<argnum) || (!lua_isuserdata(L,argnum))) return NULL;
rv=lua_touserdata(L,argnum);
return (rv && (rv->id==LuaKeyFileType))?rv:NULL;
}
static gint kfile_data(lua_State *L)
{
LuaKeyFile*k;
gsize len=0;
GError *err=NULL;
if (lua_gettop(L)>1) {
const gchar *data=NULL;
if ((lua_gettop(L)<2)||(!lua_isstring(L,2))) {return FAIL_STRING_ARG(2); }
data=lua_tolstring(L,2,&len);
k=tokeyfile(L,1);
if (!k) {return FAIL_KEYFILE_ARG(1); }
g_key_file_load_from_data(k->kf, data, len, KF_FLAGS, &err);
if (err) {
lua_pushstring(L,err->message);
g_error_free(err);
} else {lua_pushnil(L);}
return 1;
} else {
gchar *data=NULL;
k=tokeyfile(L,1);
if (!k) {return FAIL_KEYFILE_ARG(1); }
data=g_key_file_to_data(k->kf,&len,&err);
if (!err) {
lua_pushlstring(L,data,len);
g_free(data);
return 1;
} else {
lua_pushnil(L);
lua_pushstring(L,err->message);
g_error_free(err);
if (data) {g_free(data);}
return 2;
}
}
}
/*
Lua "closure" function to iterate through each string in an array of strings
*/
static gint strings_closure(lua_State *L)
{
gchar **strings;
gchar *string;
gint i=lua_tonumber(L, lua_upvalueindex(2));
strings=lua_touserdata(L,lua_upvalueindex(1));
if (!strings) {return 0;}
string=strings[i];
if ( string ) {
lua_pushstring(L,string);
push_number(L, i+1);
lua_pushvalue(L, -1);
lua_replace(L, lua_upvalueindex(2));
return 2;
} else {
g_strfreev(strings);
return 0;
}
}
/* Iterate through the group names of a keyfile */
static gint kfile_groups(lua_State* L)
{
LuaKeyFile*k=NULL;
gchar**groups=NULL;
gsize len=0;
k=tokeyfile(L,1);
if (!k) {return FAIL_KEYFILE_ARG(1); }
groups=g_key_file_get_groups(k->kf, &len);
lua_pushlightuserdata(L,groups);
push_number(L,0);
lua_pushcclosure(L, &strings_closure, 2);
return 1;
}
/* Iterate through the key names of a group*/
static gint kfile_keys(lua_State* L)
{
LuaKeyFile*k=NULL;
gchar**keylist=NULL;
gsize len=0;
const gchar*group=NULL;
GError *err=NULL;
if ((lua_gettop(L)<2)||(!lua_isstring(L,2))) {return FAIL_STRING_ARG(2); }
group=lua_tostring(L,2);
k=tokeyfile(L,1);
if (!k) {return FAIL_KEYFILE_ARG(1); }
keylist=g_key_file_get_keys(k->kf, group, &len, &err);
if (err) {
g_error_free(err);
}
lua_pushlightuserdata(L,keylist);
push_number(L,0);
lua_pushcclosure(L, &strings_closure, 2);
return 1;
}
static gint kfile_value(lua_State* L)
{
const gchar *group=NULL;
const gchar *key=NULL;
const gchar *value=NULL;
LuaKeyFile*k=NULL;
GError *err=NULL;
if (lua_gettop(L)>=4) {
if (!lua_isstring(L,4)) {return FAIL_STRING_ARG(4);}
value=lua_tostring(L,4);
}
if ((lua_gettop(L)<3)||(!lua_isstring(L,3))) {return FAIL_STRING_ARG(3); }
key=lua_tostring(L,3);
if (!lua_isstring(L,2)) {return FAIL_STRING_ARG(2); }
group=lua_tostring(L,2);
k=tokeyfile(L,1);
if (!k) {return FAIL_KEYFILE_ARG(1); }
if (value) {
g_key_file_set_value(k->kf,group,key,value);
return 0;
} else {
gchar *kfv=g_key_file_get_value(k->kf,group,key,&err);
if (err) {
g_error_free(err);
}
if (kfv) {
lua_pushstring(L, kfv);
g_free(kfv);
return 1;
} else {
return 0;
}
}
}
#define str_or_nil(L,argnum) (lua_isstring(L,argnum)||lua_isnil(L,argnum))
static gint kfile_comment(lua_State* L)
{
const gchar *group=NULL;
const gchar *key=NULL;
const gchar *comment=NULL;
LuaKeyFile*k=NULL;
GError *err=NULL;
if (lua_gettop(L)>=4) {
if (!lua_isstring(L,4)) {return FAIL_STRING_ARG(4);}
comment=lua_tostring(L,4);
}
if ((lua_gettop(L)<3)||(!str_or_nil(L,3))) {return FAIL_STRING_ARG(3); }
key=lua_tostring(L,3);
if (!str_or_nil(L,2)) {return FAIL_STRING_ARG(2); }
group=lua_tostring(L,2);
k=tokeyfile(L,1);
if (!k) {return FAIL_KEYFILE_ARG(1); }
if (comment) {
g_key_file_set_comment(k->kf,group,key,comment,&err);
return 0;
} else {
gchar*kfc=g_key_file_get_comment(k->kf,group,key,&err);
if (err) {
g_error_free(err);
}
if (kfc) {
lua_pushstring(L, kfc);
g_free(kfc);
return 1;
} else {
return 0;
}
}
}
static gint kfile_has(lua_State* L)
{
const gchar *group=NULL;
const gchar *key=NULL;
gint argc=lua_gettop(L);
gboolean hasit=FALSE;
LuaKeyFile*k=NULL;
GError*err=NULL;
if (argc>=3) {
if (lua_isstring(L,3)) {
key=lua_tostring(L,3);
} else {
if (!lua_isnil(L,3)) { return FAIL_STRING_ARG(3); }
}
}
if ((lua_gettop(L)<2)||(!lua_isstring(L,2))) { return FAIL_STRING_ARG(2); }
group=lua_tostring(L,2);
k=tokeyfile(L,1);
if (!k) { return FAIL_KEYFILE_ARG(1); }
if (key) {
hasit=g_key_file_has_key(k->kf, group, key, &err);
} else {
hasit=g_key_file_has_group(k->kf, group);
}
lua_pushboolean(L,hasit);
if (err) {
g_error_free(err);
}
return 1;
}
static gint kfile_remove(lua_State* L)
{
const gchar *group=NULL;
const gchar *key=NULL;
gint argc=lua_gettop(L);
LuaKeyFile*k=NULL;
GError*err=NULL;
if (argc>=3) {
if (lua_isstring(L,3)) {
key=lua_tostring(L,3);
} else {
if (!lua_isnil(L,3)) { return FAIL_STRING_ARG(3); }
}
}
if ((lua_gettop(L)<2)||(!lua_isstring(L,2))) { return FAIL_STRING_ARG(2); }
group=lua_tostring(L,3);
k=tokeyfile(L,1);
if (!k) { return FAIL_KEYFILE_ARG(1); }
if (key) {
g_key_file_remove_key(k->kf, group, key, &err);
} else {
g_key_file_remove_group(k->kf, group, &err);
}
if (err) {
g_error_free(err);
}
return 0;
}
static const struct luaL_reg kfile_funcs[] = {
{"new", kfile_new},
{"data", kfile_data},
{"groups", kfile_groups},
{"keys", kfile_keys},
{"value", kfile_value},
{"comment", kfile_comment},
{"has", kfile_has},
{"remove", kfile_remove},
{NULL,NULL}
};
static gint luaopen_keyfile(lua_State *L)
{
luaL_newmetatable(L, MetaName);
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);
luaL_getmetatable(L, MetaName);
lua_pushstring(L,"__gc");
lua_pushcfunction(L,kfile_done);
lua_rawset(L,-3);
luaL_openlib(L, NULL, &kfile_funcs[1], 0);
luaL_openlib(L, LUA_MODULE_NAME, kfile_funcs, 0);
return 0;
}
void glspi_init_kfile_module(lua_State *L, KeyfileAssignFunc *func)
{
*func=glspi_kfile_assign;
luaopen_keyfile(L);
}