LuaXML_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /**
  2. LuaXML License
  3. LuaXml is licensed under the terms of the MIT license reproduced below,
  4. the same as Lua itself. This means that LuaXml is free software and can be
  5. used for both academic and commercial purposes at absolutely no cost.
  6. Copyright (C) 2007-2013 Gerald Franz, eludi.net
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. */
  23. #if defined __WIN32__ || defined WIN32
  24. # include <windows.h>
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <lua.h>
  30. #include <lauxlib.h>
  31. #include <lualib.h>
  32. #ifdef __cplusplus
  33. }
  34. #endif
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38. #include <stdlib.h>
  39. static const char ESC=27;
  40. static const char OPN=28;
  41. static const char CLS=29;
  42. /*--- auxliary functions -------------------------------------------*/
  43. static const char* char2code(unsigned char ch, char buf[8]) {
  44. unsigned char i=0;
  45. buf[i++]='&';
  46. buf[i++]='#';
  47. if(ch>99) buf[i++]=ch/100+48;
  48. if(ch>9) buf[i++]=(ch%100)/10+48;
  49. buf[i++]=ch%10+48;
  50. buf[i++]=';';
  51. buf[i]=0;
  52. return buf;
  53. }
  54. static size_t find(const char* s, const char* pattern, size_t start) {
  55. const char* found =strstr(s+start, pattern);
  56. return found ? found-s : strlen(s);
  57. }
  58. /*--- internal tokenizer -------------------------------------------*/
  59. typedef struct Tokenizer_s {
  60. const char* s; /* stores string to be tokenized */
  61. size_t s_size; /* stores size of string to be tokenized */
  62. size_t i; /* stores current read position */
  63. int tagMode; /* stores current read context */
  64. const char* m_next; /* stores next token, if already determined */
  65. size_t m_next_size; /* size of next token */
  66. char* m_token; /* pointer to current token */
  67. size_t m_token_size; /* size of current token */
  68. size_t m_token_capacity; /* capacity of current token */
  69. } Tokenizer;
  70. Tokenizer* Tokenizer_new(const char* str, size_t str_size) {
  71. Tokenizer *tok = (Tokenizer*)malloc(sizeof(Tokenizer));
  72. memset(tok, 0, sizeof(Tokenizer));
  73. tok->s_size = str_size;
  74. tok->s = str;
  75. return tok;
  76. }
  77. void Tokenizer_delete(Tokenizer* tok) {
  78. free(tok->m_token);
  79. free(tok);
  80. }
  81. /*
  82. 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); }
  83. */
  84. static const char* Tokenizer_set(Tokenizer* tok, const char* s, size_t size) {
  85. if(!size||!s) return 0;
  86. free(tok->m_token);
  87. tok->m_token = (char*)malloc(size+1);
  88. strncpy(tok->m_token,s, size);
  89. tok->m_token[size] = 0;
  90. tok->m_token_size = tok->m_token_capacity = size;
  91. /*Tokenizer_print(tok);*/
  92. return tok->m_token;
  93. }
  94. static void Tokenizer_append(Tokenizer* tok, char ch) {
  95. if(tok->m_token_size+1>=tok->m_token_capacity) {
  96. tok->m_token_capacity = (tok->m_token_capacity==0) ? 16 : tok->m_token_capacity*2;
  97. tok->m_token = (char*)realloc(tok->m_token, tok->m_token_capacity);
  98. }
  99. tok->m_token[tok->m_token_size]=ch;
  100. tok->m_token[++tok->m_token_size]=0;
  101. }
  102. const char* Tokenizer_next(Tokenizer* tok) {
  103. const char* ESC_str = "\033";
  104. const char* OPEN_str = "\034";
  105. const char* CLOSE_str = "\035";
  106. int quotMode=0;
  107. int tokenComplete = 0;
  108. if(tok->m_token) {
  109. free(tok->m_token);
  110. tok->m_token = 0;
  111. tok->m_token_size=tok->m_token_capacity = 0;
  112. }
  113. while(tok->m_next_size || (tok->i < tok->s_size)) {
  114. if(tok->m_next_size) {
  115. Tokenizer_set(tok, tok->m_next, tok->m_next_size);
  116. tok->m_next=0;
  117. tok->m_next_size=0;
  118. return tok->m_token;
  119. }
  120. switch(tok->s[tok->i]) {
  121. case '"':
  122. case '\'':
  123. if(tok->tagMode) {
  124. if(!quotMode) quotMode=tok->s[tok->i];
  125. else if(quotMode==tok->s[tok->i]) quotMode=0;
  126. }
  127. Tokenizer_append(tok, tok->s[tok->i]);
  128. break;
  129. case '<':
  130. if(!quotMode&&(tok->i+4<tok->s_size)&&(strncmp(tok->s+tok->i,"<!--",4)==0)) /* strip comments */
  131. tok->i=find(tok->s, "-->", tok->i+4)+2;
  132. else if(!quotMode&&(tok->i+9<tok->s_size)&&(strncmp(tok->s+tok->i,"<![CDATA[",9)==0)) { /* interpet CDATA */
  133. size_t b=tok->i+9;
  134. tok->i=find(tok->s, "]]>",b)+3;
  135. if(!tok->m_token_size) return Tokenizer_set(tok, tok->s+b, tok->i-b-3);
  136. tokenComplete = 1;
  137. tok->m_next = tok->s+b;
  138. tok->m_next_size = tok->i-b-3;
  139. --tok->i;
  140. }
  141. else if(!quotMode&&(tok->i+1<tok->s_size)&&((tok->s[tok->i+1]=='?')||(tok->s[tok->i+1]=='!'))) /* strip meta information */
  142. tok->i=find(tok->s, ">", tok->i+2);
  143. else if(!quotMode&&!tok->tagMode) {
  144. if((tok->i+1<tok->s_size)&&(tok->s[tok->i+1]=='/')) {
  145. tok->m_next=ESC_str;
  146. tok->m_next_size = 1;
  147. tok->i=find(tok->s, ">", tok->i+2);
  148. }
  149. else {
  150. tok->m_next = OPEN_str;
  151. tok->m_next_size = 1;
  152. tok->tagMode=1;
  153. }
  154. tokenComplete = 1;
  155. }
  156. else Tokenizer_append(tok, tok->s[tok->i]);
  157. break;
  158. case '/':
  159. if(tok->tagMode&&!quotMode) {
  160. tokenComplete = 1;
  161. if((tok->i+1 < tok->s_size) && (tok->s[tok->i+1]=='>')) {
  162. tok->tagMode=0;
  163. tok->m_next=ESC_str;
  164. tok->m_next_size = 1;
  165. ++tok->i;
  166. }
  167. else Tokenizer_append(tok, tok->s[tok->i]);
  168. }
  169. else Tokenizer_append(tok, tok->s[tok->i]);
  170. break;
  171. case '>':
  172. if(!quotMode&&tok->tagMode) {
  173. tok->tagMode=0;
  174. tokenComplete = 1;
  175. tok->m_next = CLOSE_str;
  176. tok->m_next_size = 1;
  177. }
  178. else Tokenizer_append(tok, tok->s[tok->i]);
  179. break;
  180. case ' ':
  181. case '\r':
  182. case '\n':
  183. case '\t':
  184. if(tok->tagMode&&!quotMode) {
  185. if(tok->m_token_size) tokenComplete=1;
  186. }
  187. else if(tok->m_token_size) Tokenizer_append(tok, tok->s[tok->i]);
  188. break;
  189. default: Tokenizer_append(tok, tok->s[tok->i]);
  190. }
  191. ++tok->i;
  192. if((tok->i>=tok->s_size)||(tokenComplete&&tok->m_token_size)) {
  193. tokenComplete=0;
  194. while(tok->m_token_size&&isspace(tok->m_token[tok->m_token_size-1])) /* trim whitespace */
  195. tok->m_token[--tok->m_token_size]=0;
  196. if(tok->m_token_size) break;
  197. }
  198. }
  199. /*Tokenizer_print(tok);*/
  200. return tok->m_token;
  201. }
  202. /*--- local variables ----------------------------------------------*/
  203. static size_t sv_code_size=0; /* stores number of special character codes */
  204. static size_t sv_code_capacity=16; /* stores currently allocated capacity for special character codes */
  205. static char** sv_code=0; /* stores code table for special characters */
  206. /*--- public methods -----------------------------------------------*/
  207. static void Xml_pushDecode(lua_State* L, const char* s, size_t s_size) {
  208. luaL_Buffer b;
  209. const char* found;
  210. size_t start=0, pos;
  211. size_t i;
  212. if(!s_size)
  213. s_size=strlen(s);
  214. luaL_buffinit(L, &b);
  215. found = strstr(s, "&#");
  216. pos = found ? found-s : s_size;
  217. while(found) {
  218. char ch = 0;
  219. size_t i=0;
  220. for(found += 2; i<3; ++i, ++found)
  221. if(isdigit(*found))
  222. ch = ch * 10 + (*found - 48);
  223. else break;
  224. if(*found == ';') {
  225. if(pos>start)
  226. luaL_addlstring(&b, s+start, pos-start);
  227. luaL_addchar(&b, ch);
  228. start = pos + 3 + i;
  229. }
  230. found = strstr(found+1, "&#");
  231. pos = found ? found-s : s_size;
  232. }
  233. if(pos>start)
  234. luaL_addlstring(&b,s+start, pos-start);
  235. luaL_pushresult(&b);
  236. for(i=sv_code_size-1; i<sv_code_size; i-=2) {
  237. luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i-1]);
  238. lua_remove(L,-2);
  239. }
  240. }
  241. int Xml_eval(lua_State *L) {
  242. char* str = 0;
  243. size_t str_size=0;
  244. Tokenizer* tok;
  245. const char* token=0;
  246. int firstStatement = 1;
  247. if(lua_isuserdata(L,1))
  248. str = (char*)lua_touserdata(L,1);
  249. else {
  250. const char * sTmp = luaL_checklstring(L,1,&str_size);
  251. str = (char*)malloc(str_size+1);
  252. memcpy(str, sTmp, str_size);
  253. str[str_size]=0;
  254. }
  255. tok = Tokenizer_new(str, str_size ? str_size : strlen(str));
  256. lua_settop(L,0);
  257. while((token=Tokenizer_next(tok))!=0) if(token[0]==OPN) { /* new tag found */
  258. if(lua_gettop(L)) {
  259. int newIndex=lua_rawlen(L,-1)+1;
  260. lua_pushnumber(L,newIndex);
  261. lua_newtable(L);
  262. lua_settable(L, -3);
  263. lua_pushnumber(L,newIndex);
  264. lua_gettable(L,-2);
  265. }
  266. else {
  267. if (firstStatement) {
  268. lua_newtable(L);
  269. firstStatement = 0;
  270. }
  271. else return lua_gettop(L);
  272. }
  273. /* set metatable: */
  274. lua_newtable(L);
  275. lua_pushliteral(L, "__index");
  276. lua_getglobal(L, "xml");
  277. lua_settable(L, -3);
  278. lua_pushliteral(L, "__tostring"); /* set __tostring metamethod */
  279. lua_getglobal(L, "xml");
  280. lua_pushliteral(L,"str");
  281. lua_gettable(L, -2);
  282. lua_remove(L, -2);
  283. lua_settable(L, -3);
  284. lua_setmetatable(L, -2);
  285. /* parse tag and content: */
  286. lua_pushnumber(L,0); /* use index 0 for storing the tag */
  287. lua_pushstring(L, Tokenizer_next(tok));
  288. lua_settable(L, -3);
  289. while(((token = Tokenizer_next(tok))!=0)&&(token[0]!=CLS)&&(token[0]!=ESC)) { /* parse tag header */
  290. size_t sepPos=find(token, "=", 0);
  291. if(token[sepPos]) { /* regular attribute */
  292. const char* aVal =token+sepPos+2;
  293. size_t lenVal;
  294. lua_pushlstring(L, token, sepPos);
  295. lenVal = strlen(aVal)-1;
  296. if(!lenVal) Xml_pushDecode(L, "", 0);
  297. else Xml_pushDecode(L, aVal, lenVal);
  298. lua_settable(L, -3);
  299. }
  300. }
  301. if(!token||(token[0]==ESC)) {
  302. if(lua_gettop(L)>1) lua_settop(L,-2); /* this tag has no content, only attributes */
  303. else break;
  304. }
  305. }
  306. else if(token[0]==ESC) { /* previous tag is over */
  307. if(lua_gettop(L)>1) lua_settop(L,-2); /* pop current table */
  308. else break;
  309. }
  310. else { /* read elements */
  311. lua_pushnumber(L,lua_rawlen(L,-1)+1);
  312. Xml_pushDecode(L, token, 0);
  313. lua_settable(L, -3);
  314. }
  315. Tokenizer_delete(tok);
  316. free(str);
  317. return lua_gettop(L);
  318. }
  319. int Xml_load (lua_State *L) {
  320. const char * filename = luaL_checkstring(L,1);
  321. size_t sz;
  322. FILE * file=fopen(filename,"r");
  323. char* buffer;
  324. if(!file)
  325. return luaL_error(L,"LuaXml ERROR: \"%s\" file error or file not found!",filename);
  326. fseek (file , 0 , SEEK_END);
  327. sz = ftell (file);
  328. rewind (file);
  329. buffer = (char*)malloc(sz+1);
  330. sz = fread (buffer,1,sz,file);
  331. fclose(file);
  332. buffer[sz]=0;
  333. lua_pushlightuserdata(L,buffer);
  334. lua_replace(L,1);
  335. return Xml_eval(L);
  336. };
  337. int Xml_registerCode(lua_State *L) {
  338. const char * decoded = luaL_checkstring(L,1);
  339. const char * encoded = luaL_checkstring(L,2);
  340. size_t i;
  341. for(i=0; i<sv_code_size; i+=2)
  342. if(strcmp(sv_code[i],decoded)==0)
  343. return luaL_error(L,"LuaXml ERROR: code already exists.");
  344. if(sv_code_size+2>sv_code_capacity) {
  345. sv_code_capacity*=2;
  346. sv_code = (char**)realloc(sv_code, sv_code_capacity*sizeof(char*));
  347. }
  348. sv_code[sv_code_size]=(char*)malloc(strlen(decoded)+1);
  349. strcpy(sv_code[sv_code_size++], decoded);
  350. sv_code[sv_code_size]=(char*)malloc(strlen(encoded)+1);
  351. strcpy(sv_code[sv_code_size++],encoded);
  352. return 0;
  353. }
  354. int Xml_encode(lua_State *L) {
  355. char buf[8];
  356. size_t i, start, pos;
  357. luaL_Buffer b;
  358. const char* s;
  359. if(lua_gettop(L)!=1)
  360. return 0;
  361. luaL_checkstring(L,-1);
  362. for(i=0; i<sv_code_size; i+=2) {
  363. luaL_gsub(L, lua_tostring(L,-1), sv_code[i], sv_code[i+1]);
  364. lua_remove(L,-2);
  365. }
  366. s=lua_tostring(L,1);
  367. luaL_buffinit(L, &b);
  368. for(start=pos=0; s[pos]!=0; ++pos) if(s[pos]<0) {
  369. if(pos>start) luaL_addlstring(&b,s+start, pos-start);
  370. luaL_addstring(&b,char2code((unsigned char)(s[pos]),buf));
  371. start=pos+1;
  372. }
  373. if(pos>start)
  374. luaL_addlstring(&b,s+start, pos-start);
  375. luaL_pushresult(&b);
  376. lua_remove(L,-2);
  377. return 1;
  378. }
  379. #ifdef __cplusplus
  380. extern "C" {
  381. #endif
  382. int luaopen_LuaXML(lua_State* L) {
  383. static const struct luaL_Reg funcs[] = {
  384. {"load", Xml_load},
  385. {"eval", Xml_eval},
  386. {"encode", Xml_encode},
  387. /* {"registerCode", Xml_registerCode}, */
  388. {NULL, NULL}
  389. };
  390. luaL_newlibtable(L, funcs);
  391. luaL_setfuncs(L, funcs, 0);
  392. lua_setglobal(L, "xml");
  393. /* register default codes: */
  394. if(!sv_code) {
  395. sv_code=(char**)malloc(sv_code_capacity*sizeof(char*));
  396. sv_code[sv_code_size++]="&";
  397. sv_code[sv_code_size++]="&amp;";
  398. sv_code[sv_code_size++]="<";
  399. sv_code[sv_code_size++]="&lt;";
  400. sv_code[sv_code_size++]=">";
  401. sv_code[sv_code_size++]="&gt;";
  402. sv_code[sv_code_size++]="\"";
  403. sv_code[sv_code_size++]="&quot;";
  404. sv_code[sv_code_size++]="'";
  405. sv_code[sv_code_size++]="&apos;";
  406. }
  407. return 1;
  408. }
  409. #ifdef __cplusplus
  410. }
  411. #endif