39#if LUA_VERSION_NUM < 502
42#define lua_rawlen(L, index) lua_objlen(L, index)
45#define luaL_newlib(L, funcs) \
48 luaL_register(L, NULL, funcs); \
53#if LUA_VERSION_NUM >= 502
56#if !defined(lua_equal)
57#define lua_equal(L, index1, index2) lua_compare(L, index1, index2, LUA_OPEQ)
62#if LUA_VERSION_NUM >= 503
65#if !defined(luaL_optint)
66#define luaL_optint(L, arg, d) luaL_optinteger(L, arg, d)
72#define LUAXML_META "LuaXML"
77find(
const char *
s,
const char *pattern,
size_t start)
79 const char *found = strstr(
s + start, pattern);
80 return found ? (size_t)(found -
s) : strlen(
s);
106 "%s() error: invalid type at %d - expected table, got %s",
185 char c = *matched ==
'x' ? strtol(++matched,
NULL, 16) : atoi(matched);
284 : (tok->m_token[0] ==
ESC)
286 : (tok->m_token[0] ==
OPN)
288 : (tok->m_token[0] ==
CLS) ?
"(close)"
293#define Tokenizer_print(tok)
326 static const char ESC_str[] = {
ESC, 0};
327 static const char OPEN_str[] = {
OPN, 0};
328 static const char CLOSE_str[] = {
CLS, 0};
337 int tokenComplete = 0;
348 switch (tok->
s[tok->
i]) {
354 quotMode = tok->
s[tok->
i];
355 else if (quotMode == tok->
s[tok->
i])
362 if (!quotMode && (tok->
i + 4 < tok->
s_size)
363 && (strncmp(tok->
s + tok->
i,
"<!--", 4) == 0))
364 tok->
i =
find(tok->
s,
"-->", tok->
i + 4) + 2;
365 else if (!quotMode && (tok->
i + 9 < tok->
s_size)
366 && (strncmp(tok->
s + tok->
i,
"<![CDATA[", 9) == 0)) {
372 size_t b = tok->
i + 9;
373 tok->
i =
find(tok->
s,
"]]>", b) + 3;
374 size_t cdata_len = tok->
i - b - 3;
381 }
else if (!quotMode && (tok->
i + 1 < tok->
s_size)
382 && ((tok->
s[tok->
i + 1] ==
'?')
383 || (tok->
s[tok->
i + 1] ==
'!')))
385 find(tok->
s,
">", tok->
i + 2);
386 else if (!quotMode && !tok->
tagMode) {
387 if ((tok->
i + 1 < tok->
s_size) && (tok->
s[tok->
i + 1] ==
'/')) {
391 tok->
i =
find(tok->
s,
">", tok->
i + 2);
404 if (tok->
tagMode && !quotMode) {
406 if ((tok->
i + 1 < tok->
s_size) && (tok->
s[tok->
i + 1] ==
'>')) {
419 if (!quotMode && tok->
tagMode) {
433 if (tok->
tagMode && !quotMode) {
605 const unsigned char *
s = (
unsigned char *)
lua_tostring(L, -1);
612 int len =
snprintf(buf,
sizeof(buf),
"&#%d;", *
s);
699 str_size = strlen(str);
703 if (str_size >= 3 && strncmp(str,
"\xEF\xBB\xBF", 3) == 0) {
712 int firstStatement = 1;
723 if (firstStatement) {
737 && (*token !=
ESC)) {
739 size_t sepPos =
find(token,
"=", 0);
741 const char *aVal = token + sepPos + 2;
747 if (!token || (*token ==
ESC)) {
754 }
else if (*token ==
ESC) {
773 "Malformed XML: non-empty string '%s' before any "
774 "tag (parser pos %d)",
794 FILE *file = fopen(filename,
"r");
797 "LuaXML ERROR: \"%s\" file error or file not found!",
800 fseek(file, 0, SEEK_END);
801 size_t sz = ftell(file);
803 char *buffer =
malloc(sz + 1);
804 sz =
fread(buffer, 1, sz, file);
941 size_t table_attr = 0;
980 if (count == 0 && table_attr == 0) {
987 if (count == 1 && table_attr == 0) {
1007 for (k = 1; k <= count; k++) {
1008#if LUA_VERSION_NUM < 503
1033 for (k = 1; k <= table_attr; k++) {
1211 if (maxdepth < 0 ||
depth <= maxdepth) {
static int Xml_find(lua_State *L)
static int Xml_load(lua_State *L)
static int Xml_append(lua_State *L)
static const char * do_gsub(lua_State *L, int index, const char *p, const char *r)
int _EXPORT luaopen_LuaXML_lib(lua_State *L)
#define Tokenizer_print(tok)
static int Xml_tag(lua_State *L)
static void push_indentStr(lua_State *L, int level)
struct Tokenizer_s Tokenizer
#define luaL_newlib(L, funcs)
static const char * Tokenizer_next(Tokenizer *tok)
static void Xml_pushEncode(lua_State *L, int index)
static int Xml_eval(lua_State *L)
static Tokenizer * Tokenizer_new(const char *str, size_t str_size, enum whitespace_mode mode)
static int Xml_iterate(lua_State *L)
static int XMLencoding_replacement(lua_State *L)
static void push_TAG_key(lua_State *L)
static int find_on_match(lua_State *L)
static void make_xml_object(lua_State *L, int index)
whitespace_mode
strip all leading / trailing whitespace
static bool is_lead_token(const char *s)
static int Xml_str(lua_State *L)
static int Xml_match(lua_State *L)
static int Xml_encode(lua_State *L)
static void Xml_pushDecode(lua_State *L, const char *s, int size)
static void Tokenizer_append(Tokenizer *tok, char ch)
static size_t find(const char *s, const char *pattern, size_t start)
#define lua_rawlen(L, index)
static void Tokenizer_delete(Tokenizer *tok)
static int Xml_decode(lua_State *L)
static int Xml_new(lua_State *L)
static bool is_whitespace(const char *s)
static const char * Tokenizer_set(Tokenizer *tok, const char *s, size_t size)
static int Xml_registerCode(lua_State *L)
LUA_API void lua_pushstring(lua_State *L, const char *s)
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API void lua_pushnil(lua_State *L)
LUA_API void lua_concat(lua_State *L, int n)
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_rawget(lua_State *L, int idx)
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_pushboolean(lua_State *L, int b)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_type(lua_State *L, int idx)
LUA_API void lua_gettable(lua_State *L, int idx)
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
LUA_API int lua_isuserdata(lua_State *L, int idx)
LUA_API int lua_next(lua_State *L, int idx)
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
LUA_API void lua_settop(lua_State *L, int idx)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API const char * lua_typename(lua_State *L, int t)
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
LUA_API int lua_equal(lua_State *L, int index1, int index2)
LUA_API void lua_rawset(lua_State *L, int idx)
LUA_API int lua_gettop(lua_State *L)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
LUALIB_API int luaL_ref(lua_State *L, int t)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
LUALIB_API const char * luaL_gsub(lua_State *L, const char *s, const char *p, const char *r)
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
#define luaL_getmetatable(L, n)
#define luaL_typename(L, i)
#define luaL_addchar(B, c)
#define luaL_optint(L, n, d)
#define luaL_checkstring(L, n)
#define lua_istable(L, n)
#define lua_pushcfunction(L, f)
#define LUA_REGISTRYINDEX
#define lua_pushliteral(L, s)
#define lua_getglobal(L, s)
#define lua_upvalueindex(i)
#define lua_tostring(L, i)
#define lua_isnoneornil(L, n)
#define lua_tointeger(L, i)
#define lua_call(L, n, r)
#define lua_replace(L, idx)
#define lua_insert(L, idx)
#define lua_remove(L, idx)
static const luaL_Reg funcs[]
CURL_EXTERN CURLMcode curl_socket_t s
size_t m_token_capacity
capacity of current token
size_t i
stores current read position
size_t s_size
stores size of string to be tokenized
enum whitespace_mode mode
whitespace handling
int tagMode
stores current read context
size_t m_next_size
size of next token
char * m_token
pointer to current token
const char * m_next
stores next token, if already determined
const char * s
stores string to be tokenized
size_t m_token_size
size of current token
int cdata
stores flag for "raw" byte sequence, DON'T decode any further