Changeset aa4df66e877563ecf9367188566252bf929b46f0
- Timestamp:
- 06/29/10 18:22:31
(3 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1277835751 +0000
- git-parent:
[5d3fa1939f21990710f00ef4463a193cc1024f3f]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1277835751 +0000
- Message:
add XML manipulation to our lua toolchest, refs #295
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r8d08f77 |
raa4df66 |
|
| 52 | 52 | #include "noit_check.h" |
|---|
| 53 | 53 | #include "noit_check_tools.h" |
|---|
| | 54 | #include "noit_xml.h" |
|---|
| 54 | 55 | #include "utils/noit_log.h" |
|---|
| 55 | 56 | #include "utils/noit_str.h" |
|---|
| … | … | |
| 1402 | 1403 | if(nodeptr != lua_touserdata(L, 1)) |
|---|
| 1403 | 1404 | luaL_error(L, "must be called as method"); |
|---|
| | 1405 | if(lua_gettop(L) == 3 && lua_isstring(L,2)) { |
|---|
| | 1406 | const char *attr = lua_tostring(L,2); |
|---|
| | 1407 | if(lua_isnil(L,3)) |
|---|
| | 1408 | xmlSetProp(*nodeptr, (xmlChar *)attr, NULL); |
|---|
| | 1409 | else |
|---|
| | 1410 | xmlSetProp(*nodeptr, (xmlChar *)attr, (xmlChar *)lua_tostring(L,3)); |
|---|
| | 1411 | return 0; |
|---|
| | 1412 | } |
|---|
| 1404 | 1413 | if(lua_gettop(L) == 2 && lua_isstring(L,2)) { |
|---|
| 1405 | 1414 | xmlChar *v; |
|---|
| … | … | |
| 1423 | 1432 | if(nodeptr != lua_touserdata(L, 1)) |
|---|
| 1424 | 1433 | luaL_error(L, "must be called as method"); |
|---|
| | 1434 | if(lua_gettop(L) == 2 && lua_isstring(L,2)) { |
|---|
| | 1435 | const char *data = lua_tostring(L,2); |
|---|
| | 1436 | xmlChar *enc = xmlEncodeEntitiesReentrant((*nodeptr)->doc, (xmlChar *)data); |
|---|
| | 1437 | xmlNodeSetContent(*nodeptr, (xmlChar *)enc); |
|---|
| | 1438 | return 0; |
|---|
| | 1439 | } |
|---|
| 1425 | 1440 | if(lua_gettop(L) == 1) { |
|---|
| 1426 | 1441 | xmlChar *v; |
|---|
| … | … | |
| 1449 | 1464 | return 1; |
|---|
| 1450 | 1465 | } |
|---|
| | 1466 | return 0; |
|---|
| | 1467 | } |
|---|
| | 1468 | static int |
|---|
| | 1469 | noit_lua_xmlnode_addchild(lua_State *L) { |
|---|
| | 1470 | xmlNodePtr *nodeptr; |
|---|
| | 1471 | nodeptr = lua_touserdata(L, lua_upvalueindex(1)); |
|---|
| | 1472 | if(nodeptr != lua_touserdata(L, 1)) |
|---|
| | 1473 | luaL_error(L, "must be called as method"); |
|---|
| | 1474 | if(lua_gettop(L) == 2 && lua_isstring(L,2)) { |
|---|
| | 1475 | xmlNodePtr *newnodeptr; |
|---|
| | 1476 | newnodeptr = (xmlNodePtr *)lua_newuserdata(L, sizeof(*nodeptr)); |
|---|
| | 1477 | *newnodeptr = xmlNewChild(*nodeptr, NULL, |
|---|
| | 1478 | (xmlChar *)lua_tostring(L,2), NULL); |
|---|
| | 1479 | luaL_getmetatable(L, "noit.xmlnode"); |
|---|
| | 1480 | lua_setmetatable(L, -2); |
|---|
| | 1481 | return 1; |
|---|
| | 1482 | } |
|---|
| | 1483 | luaL_error(L,"must be called with one argument"); |
|---|
| 1451 | 1484 | return 0; |
|---|
| 1452 | 1485 | } |
|---|
| … | … | |
| 1465 | 1498 | lua_setmetatable(L, -2); |
|---|
| 1466 | 1499 | lua_pushcclosure(L, noit_lua_xmlnode_next, 1); |
|---|
| | 1500 | return 1; |
|---|
| | 1501 | } |
|---|
| | 1502 | static int |
|---|
| | 1503 | noit_lua_xml_tostring(lua_State *L) { |
|---|
| | 1504 | int n; |
|---|
| | 1505 | xmlDocPtr *docptr; |
|---|
| | 1506 | char *xmlstring; |
|---|
| | 1507 | n = lua_gettop(L); |
|---|
| | 1508 | /* the first arg is implicitly self (it's a method) */ |
|---|
| | 1509 | docptr = lua_touserdata(L, lua_upvalueindex(1)); |
|---|
| | 1510 | if(docptr != lua_touserdata(L, 1)) |
|---|
| | 1511 | luaL_error(L, "must be called as method"); |
|---|
| | 1512 | if(n != 1) luaL_error(L, "expects no arguments, got %d", n - 1); |
|---|
| | 1513 | xmlstring = noit_xmlSaveToBuffer(*docptr); |
|---|
| | 1514 | lua_pushstring(L, xmlstring); |
|---|
| | 1515 | free(xmlstring); |
|---|
| 1467 | 1516 | return 1; |
|---|
| 1468 | 1517 | } |
|---|
| … | … | |
| 1511 | 1560 | LUA_DISPATCH(attr, noit_lua_xmlnode_attr); |
|---|
| 1512 | 1561 | LUA_DISPATCH(attribute, noit_lua_xmlnode_attr); |
|---|
| | 1562 | LUA_DISPATCH(addchild, noit_lua_xmlnode_addchild); |
|---|
| 1513 | 1563 | break; |
|---|
| 1514 | 1564 | case 'c': |
|---|
| … | … | |
| 1571 | 1621 | case 'r': |
|---|
| 1572 | 1622 | LUA_DISPATCH(root, noit_lua_xml_docroot); |
|---|
| | 1623 | break; |
|---|
| | 1624 | case 't': |
|---|
| | 1625 | LUA_DISPATCH(tostring, noit_lua_xml_tostring); |
|---|
| 1573 | 1626 | break; |
|---|
| 1574 | 1627 | case 'x': |
|---|