This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlgenx.c
More file actions
242 lines (195 loc) · 6.72 KB
/
lgenx.c
File metadata and controls
242 lines (195 loc) · 6.72 KB
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
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include "genx.h"
#define LGENX_DOCUMENT "genx document"
#define LGENX_LIBRARY "genx"
typedef struct {
genxWriter writer;
lua_State *L; // lua state at last library call
int sender_sendref;
int sender_flushref;
int fileref;
} document;
document *newdoc(lua_State *L) {
document *doc = (document *)lua_newuserdata(L, sizeof(document));
doc->writer = NULL;
doc->L = L;
doc->sender_sendref = LUA_REFNIL;
doc->sender_flushref = LUA_REFNIL;
doc->fileref = LUA_REFNIL;
// set the new writer's identifying metatable
luaL_getmetatable(L, LGENX_DOCUMENT);
lua_setmetatable(L, -2);
// create a new genx writer with the document as userdata
doc->writer = genxNew(NULL, NULL, (void*)doc);
return doc;
}
document *checkdoc(lua_State *L, int index) {
void *ud = luaL_checkudata(L, index, LGENX_DOCUMENT);
luaL_argcheck(L, ud != NULL, index, "genx document expected");
((document *)ud)->L = L; // update the current Lua state
return (document *)ud;
}
FILE **checkfile(lua_State *L, int index) {
void *ud = luaL_checkudata(L, index, LUA_FILEHANDLE);
luaL_argcheck(L, ud != NULL, index, "file handle expected");
return (FILE **)ud;
}
static void handleError(lua_State *L, genxWriter writer, genxStatus status) {
if (status)
luaL_error(L, genxLastErrorMessage(writer));
}
genxStatus sender_send(void *userData, constUtf8 s) {
document *doc = (document *)userData; // the userdata is the document
lua_State *L = doc->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, doc->sender_sendref); // push send func
lua_pushstring(L, (char *)s); // argument: string to send
lua_call(L, 1, 0);
return GENX_SUCCESS;
}
genxStatus sender_sendBounded(void *userData, constUtf8 start, constUtf8 end) {
document *doc = (document *)userData;
lua_State *L = doc->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, doc->sender_sendref);
lua_pushlstring(L, (char *)start, (end-start));
lua_call(L, 1, 0);
return GENX_SUCCESS;
}
genxStatus sender_flush(void *userData) {
document *doc = (document *)userData;
lua_State *L = doc->L;
// the flush function is optional, so make sure we have it
if (doc->sender_flushref != LUA_REFNIL) {
lua_rawgeti(L, LUA_REGISTRYINDEX, doc->sender_flushref);
lua_call(L, 0, 0);
}
return GENX_SUCCESS;
}
static genxSender sender = {sender_send, sender_sendBounded, sender_flush};
static int lgenx_new(lua_State *L) {
document *doc;
genxStatus s;
if (lua_isfunction(L, 1)) {
// sender initialization
int sendref = LUA_REFNIL;
int flushref = LUA_REFNIL;
// hold references to the sender functions
// first, send:
lua_pushvalue(L, 1);
sendref = luaL_ref(L, LUA_REGISTRYINDEX);
// then, flush (optional):
lua_pushvalue(L, 2);
if (lua_isfunction(L, -1))
flushref = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
doc = newdoc(L);
doc->sender_sendref = sendref;
doc->sender_flushref = flushref;
s = genxStartDocSender(doc->writer, &sender);
} else if (lua_isuserdata(L, 1)) {
// file initializaiton
FILE **fh = checkfile(L, 1);
doc = newdoc(L);
// hold a reference to the file object so it doesn't get collected
lua_pushvalue(L, 1);
doc->fileref = luaL_ref(L, LUA_REGISTRYINDEX);
s = genxStartDocFile(doc->writer, *fh);
} else {
// incorrect invocation
luaL_error(L, "new() must be invoked with functions or a file");
}
handleError(L, doc->writer, s);
return 1;
}
static int lgenx_comment(lua_State *L) {
document *doc = checkdoc(L, 1);
const char *text = luaL_checkstring(L, 2);
genxStatus s = genxComment(doc->writer, (constUtf8)text);
handleError(L, doc->writer, s);
return 0;
}
static int lgenx_startElement(lua_State *L) {
document *doc = checkdoc(L, 1);
const char *name = luaL_checkstring(L, 2);
genxStatus s = genxStartElementLiteral(doc->writer, NULL, (constUtf8)name);
handleError(L, doc->writer, s);
return 0;
}
static int lgenx_endElement(lua_State *L) {
document *doc = checkdoc(L, 1);
genxStatus s = genxEndElement(doc->writer);
handleError(L, doc->writer, s);
return 0;
}
static int lgenx_attribute(lua_State *L) {
document *doc = checkdoc(L, 1);
const char *key = luaL_checkstring(L, 2);
const char *value = luaL_checkstring(L, 3);
genxStatus s = genxAddAttributeLiteral(doc->writer,
NULL,
(constUtf8)key,
(constUtf8)value);
handleError(L, doc->writer, s);
return 0;
}
static int lgenx_text(lua_State *L) {
document *doc = checkdoc(L, 1);
const char *text = luaL_checkstring(L, 2);
genxStatus s = genxAddText(doc->writer, (constUtf8)text);
handleError(L, doc->writer, s);
return 0;
}
static int lgenx_close(lua_State *L) {
document *doc = checkdoc(L, 1);
if (doc->writer) { // not closed yet
// finish document
genxStatus s = genxEndDocument(doc->writer);
handleError(L, doc->writer, s);
// release writer
genxDispose(doc->writer);
doc->writer = NULL; // mark as closed
}
// release references, if present
if (doc->sender_sendref != LUA_REFNIL) {
luaL_unref(L, LUA_REGISTRYINDEX, doc->sender_sendref);
doc->sender_sendref = LUA_REFNIL;
}
if (doc->sender_flushref != LUA_REFNIL) {
luaL_unref(L, LUA_REGISTRYINDEX, doc->sender_flushref);
doc->sender_flushref = LUA_REFNIL;
}
if (doc->fileref != LUA_REFNIL) {
luaL_unref(L, LUA_REGISTRYINDEX, doc->fileref);
doc->fileref = LUA_REFNIL;
}
return 0;
}
static const struct luaL_reg lgenx[] = {
{"new", lgenx_new},
{NULL, NULL}
};
static const struct luaL_reg lgenx_document[] = {
{"comment", lgenx_comment},
{"startElement", lgenx_startElement},
{"endElement", lgenx_endElement},
{"attribute", lgenx_attribute},
{"text", lgenx_text},
{"close", lgenx_close},
{"__gc", lgenx_close},
{NULL, NULL}
};
int luaopen_genx(lua_State *L) {
/* metatable for document objects */
luaL_newmetatable(L, LGENX_DOCUMENT);
/* __index is itself */
lua_pushliteral(L, "__index");
lua_pushvalue(L, -2);
lua_rawset(L, -3);
/* add methods to metatable */
luaL_openlib(L, NULL, lgenx_document, 0);
/* register library */
luaL_openlib(L, LGENX_LIBRARY, lgenx, 0);
return 1;
}