This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
LuaXML_lib.c
429 lines (390 loc) · 12.3 KB
/
LuaXML_lib.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
LuaXML License
LuaXML is licensed under the terms of the MIT license reproduced below,
the same as Lua itself. This means that LuaXML is free software and can be
used for both academic and commercial purposes at absolutely no cost.
Copyright (C) 2007-2009 Gerald Franz, www.viremo.de
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#if defined __WIN32__ || defined WIN32
# include <windows.h>
# define _EXPORT __declspec(dllexport)
#else
# define _EXPORT
#endif
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
static const char ESC=27;
static const char OPN=28;
static const char CLS=29;
//--- auxliary functions -------------------------------------------
static const char* char2code(unsigned char ch, char buf[8]) {
unsigned char i=0;
buf[i++]='&';
buf[i++]='#';
if(ch>99) buf[i++]=ch/100+48;
if(ch>9) buf[i++]=(ch%100)/10+48;
buf[i++]=ch%10+48;
buf[i++]=';';
buf[i]=0;
return buf;
}
static size_t find(const char* s, const char* pattern, size_t start) {
const char* found =strstr(s+start, pattern);
return found ? found-s : strlen(s);
}
//--- internal tokenizer -------------------------------------------
typedef struct Tokenizer_s {
/// stores string to be tokenized
const char* s;
/// stores size of string to be tokenized
size_t s_size;
/// stores current read position
size_t i;
/// stores current read context
int tagMode;
/// stores next token, if already determined
const char* m_next;
/// size of next token
size_t m_next_size;
/// pointer to current token
char* m_token;
/// size of current token
size_t m_token_size;
/// capacity of current token
size_t m_token_capacity;
} Tokenizer;
Tokenizer* Tokenizer_new(const char* str, size_t str_size) {
Tokenizer *tok = (Tokenizer*)malloc(sizeof(Tokenizer));
memset(tok, 0, sizeof(Tokenizer));
tok->s_size = str_size;
tok->s = str;
return tok;
}
void Tokenizer_delete(Tokenizer* tok) {
free(tok->m_token);
free(tok);
}
//void Tokenizer_print(Tokenizer* tok) { printf(" @%u %s\n", tok->i, !tok->m_token ? "(null)" : (tok->m_token[0]==ESC)?"(esc)" : (tok->m_token[0]==OPN)?"(open)": (tok->m_token[0]==CLS)?"(close)" : tok->m_token); fflush(stdout); }
static const char* Tokenizer_set(Tokenizer* tok, const char* s, size_t size) {
if(!size||!s) return 0;
free(tok->m_token);
tok->m_token = (char*)malloc(size+1);
strncpy(tok->m_token,s, size);
tok->m_token[size] = 0;
tok->m_token_size = tok->m_token_capacity = size;
//Tokenizer_print(tok);
return tok->m_token;
}
static void Tokenizer_append(Tokenizer* tok, char ch) {
if(tok->m_token_size+1>=tok->m_token_capacity) {
tok->m_token_capacity = (tok->m_token_capacity==0) ? 16 : tok->m_token_capacity*2;
tok->m_token = (char*)realloc(tok->m_token, tok->m_token_capacity);
}
tok->m_token[tok->m_token_size]=ch;
tok->m_token[++tok->m_token_size]=0;
}
const char* Tokenizer_next(Tokenizer* tok) {
const char* ESC_str = "\033";
const char* OPEN_str = "\034";
const char* CLOSE_str = "\035";
if(tok->m_token) {
free(tok->m_token);
tok->m_token = 0;
tok->m_token_size=tok->m_token_capacity = 0;
}
int quotMode=0;
int tokenComplete = 0;
while(tok->m_next_size || (tok->i < tok->s_size)) {
if(tok->m_next_size) {
Tokenizer_set(tok, tok->m_next, tok->m_next_size);
tok->m_next=0;
tok->m_next_size=0;
return tok->m_token;
}
switch(tok->s[tok->i]) {
case '"':
case '\'':
if(tok->tagMode) {
if(!quotMode) quotMode=tok->s[tok->i];
else if(quotMode==tok->s[tok->i]) quotMode=0;
}
Tokenizer_append(tok, tok->s[tok->i]);
break;
case '<':
if(!quotMode&&(tok->i+4<tok->s_size)&&(strncmp(tok->s+tok->i,"<!--",4)==0)) // strip comments
tok->i=find(tok->s, "-->", tok->i+4)+2;
else if(!quotMode&&(tok->i+9<tok->s_size)&&(strncmp(tok->s+tok->i,"<![CDATA[",9)==0)) { // interpet CDATA
size_t b=tok->i+9;
tok->i=find(tok->s, "]]>",b)+3;
if(!tok->m_token_size) return Tokenizer_set(tok, tok->s+b, tok->i-b-3);
tokenComplete = 1;
tok->m_next = tok->s+b;
tok->m_next_size = tok->i-b-3;
--tok->i;
}
else if(!quotMode&&(tok->i+1<tok->s_size)&&((tok->s[tok->i+1]=='?')||(tok->s[tok->i+1]=='!'))) // strip meta information
tok->i=find(tok->s, ">", tok->i+2);
else if(!quotMode&&!tok->tagMode) {
if((tok->i+1<tok->s_size)&&(tok->s[tok->i+1]=='/')) {
tok->m_next=ESC_str;
tok->m_next_size = 1;
tok->i=find(tok->s, ">", tok->i+2);
}
else {
tok->m_next = OPEN_str;
tok->m_next_size = 1;
tok->tagMode=1;
}
tokenComplete = 1;
}
else Tokenizer_append(tok, tok->s[tok->i]);
break;
case '/':
if(tok->tagMode&&!quotMode) {
tokenComplete = 1;
if((tok->i+1 < tok->s_size) && (tok->s[tok->i+1]=='>')) {
tok->tagMode=0;
tok->m_next=ESC_str;
tok->m_next_size = 1;
++tok->i;
}
else Tokenizer_append(tok, tok->s[tok->i]);
}
else Tokenizer_append(tok, tok->s[tok->i]);
break;
case '>':
if(!quotMode&&tok->tagMode) {
tok->tagMode=0;
tokenComplete = 1;
tok->m_next = CLOSE_str;
tok->m_next_size = 1;
}
else Tokenizer_append(tok, tok->s[tok->i]);
break;
case ' ':
case '\r':
case '\n':
case '\t':
if(tok->tagMode&&!quotMode) {
if(tok->m_token_size) tokenComplete=1;
}
else if(tok->m_token_size) Tokenizer_append(tok, tok->s[tok->i]);
break;
default: Tokenizer_append(tok, tok->s[tok->i]);
}
++tok->i;
if((tok->i>=tok->s_size)||(tokenComplete&&tok->m_token_size)) {
tokenComplete=0;
while(tok->m_token_size&&isspace(tok->m_token[tok->m_token_size-1])) // trim whitespace
tok->m_token[--tok->m_token_size]=0;
if(tok->m_token_size) break;
}
}
//Tokenizer_print(tok);
return tok->m_token;
}
//--- local variables ----------------------------------------------
/// stores number of special character codes
static size_t sv_code_size=0;
/// stores currently allocated capacity for special character codes
static size_t sv_code_capacity=16;
/// stores code table for special characters
static char** sv_code=0;
//--- public methods -----------------------------------------------
static void Xml_pushDecode(lua_State* L, const char* s, size_t s_size) {
size_t start=0, pos;
if(!s_size) s_size=strlen(s);
luaL_Buffer b;
luaL_buffinit(L, &b);
const char* found = strstr(s, "&#");
if(!found) pos = s_size;
else pos = found-s;
while(found&&(pos+5<s_size)&&(*(found+5)==';')&&isdigit(*(found+2))&&isdigit(*(found+3))&&isdigit(*(found+4)) ) {
if(pos>start) luaL_addlstring(&b,s+start, pos-start);
luaL_addchar(&b, 100*(s[pos+2]-48)+10*(s[pos+3]-48)+(s[pos+4]-48));
start=pos+6;
found = strstr(found+6, "&#");
if(!found) pos = s_size;
else pos = found-s;
}
if(pos>start) luaL_addlstring(&b,s+start, pos-start);
luaL_pushresult(&b);
size_t i;
for(i=sv_code_size-1; i<sv_code_size; i-=2) {
luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i-1]);
lua_remove(L,-2);
}
}
int Xml_eval(lua_State *L) {
char* str = 0;
size_t str_size=0;
if(lua_isuserdata(L,1)) str = (char*)lua_touserdata(L,1);
else {
const char * sTmp = luaL_checklstring(L,1,&str_size);
str = (char*)malloc(str_size+1);
memcpy(str, sTmp, str_size);
str[str_size]=0;
}
Tokenizer* tok = Tokenizer_new(str, str_size ? str_size : strlen(str));
lua_settop(L,0);
const char* token=0;
int firstStatement = 1;
while((token=Tokenizer_next(tok))!=0) if(token[0]==OPN) { // new tag found
if(lua_gettop(L)) {
int newIndex=lua_objlen(L,-1)+1;
lua_pushnumber(L,newIndex);
lua_newtable(L);
lua_settable(L, -3);
lua_pushnumber(L,newIndex);
lua_gettable(L,-2);
}
else {
if (firstStatement) {
lua_newtable(L);
firstStatement = 0;
}
else return lua_gettop(L);
}
// set metatable:
lua_newtable(L);
lua_pushliteral(L, "__index");
lua_getglobal(L, "xml");
lua_settable(L, -3);
lua_pushliteral(L, "__tostring"); // set __tostring metamethod
lua_getglobal(L, "xml");
lua_pushliteral(L,"str");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_settable(L, -3);
lua_setmetatable(L, -2);
// parse tag and content:
lua_pushnumber(L,0); // use index 0 for storing the tag
lua_pushstring(L, Tokenizer_next(tok));
lua_settable(L, -3);
while(((token = Tokenizer_next(tok))!=0)&&(token[0]!=CLS)&&(token[0]!=ESC)) { // parse tag header
size_t sepPos=find(token, "=", 0);
if(token[sepPos]) { // regular attribute
const char* aVal =token+sepPos+2;
lua_pushlstring(L, token, sepPos);
Xml_pushDecode(L, aVal, strlen(aVal)-1);
lua_settable(L, -3);
}
}
if(!token||(token[0]==ESC)) {
if(lua_gettop(L)>1) lua_settop(L,-2); // this tag has no content, only attributes
else break;
}
}
else if(token[0]==ESC) { // previous tag is over
if(lua_gettop(L)>1) lua_settop(L,-2); // pop current table
else break;
}
else { // read elements
lua_pushnumber(L,lua_objlen(L,-1)+1);
Xml_pushDecode(L, token, 0);
lua_settable(L, -3);
}
Tokenizer_delete(tok);
free(str);
return lua_gettop(L);
}
int Xml_load (lua_State *L) {
const char * filename = luaL_checkstring(L,1);
FILE * file=fopen(filename,"r");
if(!file) return luaL_error(L,"LuaXML ERROR: \"%s\" file error or file not found!",filename);
fseek (file , 0 , SEEK_END);
size_t sz = ftell (file);
rewind (file);
char* buffer = (char*)malloc(sz+1);
fread (buffer,1,sz,file);
fclose(file);
buffer[sz]=0;
lua_pushlightuserdata(L,buffer);
lua_replace(L,1);
return Xml_eval(L);
};
int Xml_registerCode(lua_State *L) {
const char * decoded = luaL_checkstring(L,1);
const char * encoded = luaL_checkstring(L,2);
size_t i;
for(i=0; i<sv_code_size; i+=2)
if(strcmp(sv_code[i],decoded)==0)
return luaL_error(L,"LuaXML ERROR: code already exists.");
if(sv_code_size+2>sv_code_capacity) {
sv_code_capacity*=2;
sv_code = (char**)realloc(sv_code, sv_code_capacity*sizeof(char*));
}
sv_code[sv_code_size]=(char*)malloc(strlen(decoded)+1);
strcpy(sv_code[sv_code_size++], decoded);
sv_code[sv_code_size]=(char*)malloc(strlen(encoded)+1);
strcpy(sv_code[sv_code_size++],encoded);
return 0;
}
int Xml_encode(lua_State *L) {
if(lua_gettop(L)!=1) return 0;
luaL_checkstring(L,-1);
size_t i;
for(i=0; i<sv_code_size; i+=2) {
luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i+1]);
lua_remove(L,-2);
}
char buf[8];
const char* s=lua_tostring(L,1);
size_t start, pos;
luaL_Buffer b;
luaL_buffinit(L, &b);
for(start=pos=0; s[pos]!=0; ++pos) if(s[pos]<0) {
if(pos>start) luaL_addlstring(&b,s+start, pos-start);
luaL_addstring(&b,char2code((unsigned char)(s[pos]),buf));
start=pos+1;
}
if(pos>start) luaL_addlstring(&b,s+start, pos-start);
luaL_pushresult(&b);
lua_remove(L,-2);
return 1;
}
int _EXPORT luaopen_LuaXML_lib (lua_State* L) {
static const struct luaL_Reg funcs[] = {
{"load", Xml_load},
{"eval", Xml_eval},
{"encode", Xml_encode},
{"registerCode", Xml_registerCode},
{NULL, NULL}
};
luaL_register(L, "xml", funcs);
// register default codes:
if(!sv_code) {
sv_code=(char**)malloc(sv_code_capacity*sizeof(char*));
sv_code[sv_code_size++]="&";
sv_code[sv_code_size++]="&";
sv_code[sv_code_size++]="<";
sv_code[sv_code_size++]="<";
sv_code[sv_code_size++]=">";
sv_code[sv_code_size++]=">";
sv_code[sv_code_size++]="\"";
sv_code[sv_code_size++]=""";
sv_code[sv_code_size++]="'";
sv_code[sv_code_size++]="'";
}
return 1;
}