Github User Fetcher 1.0.0
C Application with Server and GUI
|
#include "LuaXML_lib.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
Data Structures | |
struct | Tokenizer_s |
Macros | |
#define | lua_rawlen(L, index) lua_objlen(L, index) |
#define | luaL_newlib(L, funcs) |
#define | LUAXML_META "LuaXML" |
#define | ESC 27 /* end of scope, closing tag */ |
#define | OPN 28 /* "open", start of tag */ |
#define | CLS 29 /* closes opening tag, actual content follows */ |
#define | Tokenizer_print(tok) /* ignore */ |
Typedefs | |
typedef struct Tokenizer_s | Tokenizer |
Enumerations | |
enum | whitespace_mode { WHITESPACE_TRIM , WHITESPACE_NORMALIZE , WHITESPACE_PRESERVE } |
strip all leading / trailing whitespace More... | |
Functions | |
static size_t | find (const char *s, const char *pattern, size_t start) |
static void | push_TAG_key (lua_State *L) |
static void | make_xml_object (lua_State *L, int index) |
static void | push_indentStr (lua_State *L, int level) |
static bool | is_whitespace (const char *s) |
static bool | is_lead_token (const char *s) |
static const char * | do_gsub (lua_State *L, int index, const char *p, const char *r) |
static int | XMLencoding_replacement (lua_State *L) |
static int | find_on_match (lua_State *L) |
static Tokenizer * | Tokenizer_new (const char *str, size_t str_size, enum whitespace_mode mode) |
static void | Tokenizer_delete (Tokenizer *tok) |
static const char * | Tokenizer_set (Tokenizer *tok, const char *s, size_t size) |
static void | Tokenizer_append (Tokenizer *tok, char ch) |
static const char * | Tokenizer_next (Tokenizer *tok) |
static int | Xml_tag (lua_State *L) |
static int | Xml_new (lua_State *L) |
static int | Xml_append (lua_State *L) |
static void | Xml_pushEncode (lua_State *L, int index) |
static void | Xml_pushDecode (lua_State *L, const char *s, int size) |
static int | Xml_eval (lua_State *L) |
static int | Xml_load (lua_State *L) |
static int | Xml_registerCode (lua_State *L) |
static int | Xml_encode (lua_State *L) |
static int | Xml_decode (lua_State *L) |
static int | Xml_str (lua_State *L) |
static int | Xml_match (lua_State *L) |
static int | Xml_iterate (lua_State *L) |
static int | Xml_find (lua_State *L) |
int _EXPORT | luaopen_LuaXML_lib (lua_State *L) |
Variables | |
static int | sv_code_ref |
#define CLS 29 /* closes opening tag, actual content follows */ |
Definition at line 231 of file LuaXML_lib.c.
Referenced by Tokenizer_next(), and Xml_eval().
#define ESC 27 /* end of scope, closing tag */ |
Definition at line 229 of file LuaXML_lib.c.
Referenced by Tokenizer_next(), and Xml_eval().
#define lua_rawlen | ( | L, | |
index ) lua_objlen(L, index) |
Definition at line 42 of file LuaXML_lib.c.
Referenced by luaB_rawlen(), luaB_rawlen(), luaB_rawlen(), luaL_ref(), read_line(), read_line(), read_line(), Xml_append(), Xml_eval(), and Xml_str().
#define luaL_newlib | ( | L, | |
funcs ) |
Definition at line 45 of file LuaXML_lib.c.
Referenced by luaopen_LuaXML_lib().
#define LUAXML_META "LuaXML" |
Definition at line 72 of file LuaXML_lib.c.
Referenced by luaopen_LuaXML_lib(), and make_xml_object().
#define OPN 28 /* "open", start of tag */ |
Definition at line 230 of file LuaXML_lib.c.
Referenced by Tokenizer_next(), and Xml_eval().
#define Tokenizer_print | ( | tok | ) | /* ignore */ |
Definition at line 293 of file LuaXML_lib.c.
Referenced by Tokenizer_next(), and Tokenizer_set().
typedef struct Tokenizer_s Tokenizer |
enum whitespace_mode |
strip all leading / trailing whitespace
remove "lead in" whitespace before tags preserve all whitespace, even between tags
Enumerator | |
---|---|
WHITESPACE_TRIM | |
WHITESPACE_NORMALIZE | |
WHITESPACE_PRESERVE |
Definition at line 222 of file LuaXML_lib.c.
|
static |
Definition at line 162 of file LuaXML_lib.c.
References index, lua_gettop(), lua_replace, lua_tostring, and luaL_gsub().
Referenced by Xml_pushDecode(), and Xml_pushEncode().
|
static |
Definition at line 77 of file LuaXML_lib.c.
References s.
Referenced by str_find_aux(), str_find_aux(), str_find_aux(), str_find_aux(), Tokenizer_next(), and Xml_eval().
|
static |
Definition at line 204 of file LuaXML_lib.c.
References lua_pushboolean(), lua_rawseti(), lua_settop(), and lua_upvalueindex.
Referenced by Xml_find().
|
static |
Definition at line 148 of file LuaXML_lib.c.
References is_whitespace(), and s.
Referenced by Xml_eval().
|
static |
Definition at line 133 of file LuaXML_lib.c.
References s.
Referenced by is_lead_token(), and Xml_eval().
Definition at line 1280 of file LuaXML_lib.c.
References funcs, lua_newtable, lua_pop, lua_pushcfunction, lua_pushinteger(), lua_pushliteral, lua_pushvalue(), lua_rawset(), LUA_REGISTRYINDEX, lua_setfield(), luaL_newlib, luaL_newmetatable(), luaL_ref(), LUAXML_META, NULL, sv_code_ref, WHITESPACE_NORMALIZE, WHITESPACE_PRESERVE, WHITESPACE_TRIM, Xml_append(), Xml_decode(), Xml_encode(), Xml_eval(), Xml_find(), Xml_iterate(), Xml_load(), Xml_match(), Xml_new(), Xml_registerCode(), Xml_str(), and Xml_tag().
|
static |
Definition at line 100 of file LuaXML_lib.c.
References index, lua_gettop(), lua_istable, lua_setmetatable(), luaL_error(), luaL_getmetatable, luaL_typename, and LUAXML_META.
Referenced by Xml_eval(), Xml_match(), and Xml_new().
|
static |
Definition at line 117 of file LuaXML_lib.c.
References lua_pushliteral, luaL_addchar, luaL_buffinit(), and luaL_pushresult().
Referenced by Xml_str().
|
inlinestatic |
Definition at line 85 of file LuaXML_lib.c.
References lua_pushinteger().
Referenced by Xml_eval(), Xml_match(), Xml_str(), and Xml_tag().
|
static |
Definition at line 311 of file LuaXML_lib.c.
References Tokenizer_s::m_token, Tokenizer_s::m_token_capacity, Tokenizer_s::m_token_size, and realloc.
Referenced by Tokenizer_next().
|
static |
Definition at line 271 of file LuaXML_lib.c.
References free, and Tokenizer_s::m_token.
Referenced by Xml_eval().
|
static |
Definition at line 261 of file LuaXML_lib.c.
References calloc, Tokenizer_s::mode, Tokenizer_s::s, and Tokenizer_s::s_size.
Referenced by Xml_eval().
|
static |
Definition at line 323 of file LuaXML_lib.c.
References Tokenizer_s::cdata, CLS, ESC, find(), free, Tokenizer_s::i, Tokenizer_s::m_next, Tokenizer_s::m_next_size, Tokenizer_s::m_token, Tokenizer_s::m_token_capacity, Tokenizer_s::m_token_size, Tokenizer_s::mode, NULL, OPN, Tokenizer_s::s, Tokenizer_s::s_size, Tokenizer_s::tagMode, Tokenizer_append(), Tokenizer_print, Tokenizer_set(), and WHITESPACE_TRIM.
Referenced by Xml_eval().
|
static |
Definition at line 297 of file LuaXML_lib.c.
References free, Tokenizer_s::m_token, Tokenizer_s::m_token_capacity, Tokenizer_s::m_token_size, malloc, NULL, s, and Tokenizer_print.
Referenced by Tokenizer_next().
|
static |
appends a new subordinate LuaXML object to an existing one. optionally sets tag
@function append
var | the parent LuaXML object |
?string | tag the tag of the appended LuaXML object |
nil
in case of errors Definition at line 558 of file LuaXML_lib.c.
References lua_call, lua_insert, lua_pushcfunction, lua_pushvalue(), lua_rawlen, lua_rawseti(), lua_settop(), LUA_TTABLE, lua_type(), and Xml_new().
Referenced by luaopen_LuaXML_lib().
|
static |
converts a string from XML encoding. This function transformsstr
by replacing any special XML encodings with their "plain text" counterparts.
@usage print((xml.decode("<->")) – "<->"
@function decode
string | str string to be transformed @treturn string the decoded string |
Definition at line 884 of file LuaXML_lib.c.
References lua_tostring, luaL_checklstring(), and Xml_pushDecode().
Referenced by luaopen_LuaXML_lib().
|
static |
converts a string to XML encoding. This function transformsstr
by replacing any special characters with suitable XML encodings.
@usage print(xml.encode("<->")) – "<->"
@function encode
string | str string to be transformed @treturn string the XML-encoded string |
Definition at line 864 of file LuaXML_lib.c.
References luaL_checkstring, and Xml_pushEncode().
Referenced by luaopen_LuaXML_lib().
|
static |
parses an XML string into a Lua table. The table will contain a representation of the XML tag, attributes (and their values), and element content / subelements (either as strings or nested LuaXML "objects").
Note: Parsing "wide" strings or Unicode (UCS-2, UCS-4, UTF-16) currently is not supported. If needed, convert such xml
data to UTF-8 before passing it to eval()
. UTF-8 should be safe to use, and this function will also recognize and ignore a UTF-8 BOM (byte order mark) at the start of xml
.
@function eval
string|userdata | xml the XML to be converted. When passing a userdata type xml value, it must point to a C-style (NUL-terminated) string. |
?number | mode whitespace handling mode, one of the WS_* constants - see Fields. defaults to WS_TRIM (compatible to previous LuaXML versions) |
nil
in case of errors Definition at line 692 of file LuaXML_lib.c.
References Tokenizer_s::cdata, CLS, ESC, find(), Tokenizer_s::i, is_lead_token(), is_whitespace(), lua_gettop(), lua_isuserdata(), lua_newtable, lua_pop, lua_pushlstring(), lua_pushstring(), lua_pushvalue(), lua_rawlen, lua_rawset(), lua_rawseti(), lua_settop(), lua_touserdata(), luaL_checklstring(), luaL_error(), luaL_optint, make_xml_object(), OPN, push_TAG_key(), Tokenizer_delete(), Tokenizer_new(), Tokenizer_next(), WHITESPACE_NORMALIZE, WHITESPACE_TRIM, and Xml_pushDecode().
Referenced by luaopen_LuaXML_lib(), and Xml_load().
|
static |
recursively searches a Lua table for a subelement matching the provided tag and attribute. See the description of match
for the logic involved with testing fortag
, key
and value
.
@function find
var | the table to be searched in |
?string | tag the XML tag to be found |
?string | key the attribute key (= exact name) to be found |
value | (optional) the attribute value to be found |
nil
for no match Definition at line 1254 of file LuaXML_lib.c.
References find_on_match(), lua_call, lua_insert, lua_newtable, lua_pushboolean(), lua_pushcclosure(), lua_pushcfunction, lua_pushvalue(), lua_rawgeti(), lua_settop(), and Xml_iterate().
Referenced by luaopen_LuaXML_lib().
|
static |
iterates a LuaXML object, invoking a callback function for all matching (sub)elements.
The iteration starts with the variable var
itself (= default depth 0). A callback function cb
gets invoked for each match
, depending on the specified criteria. If the r
flag is set, the process will repeat recursively for the subelements of var
(at depth + 1). You can limit the scope by setting a maximum depth, or have the callback function explicitly request to stop the iteration (by returning false
).
@function iterate
var | the table (LuaXML object) to iterate |
function | cb callback function. callback(var, depth) will be called for each matching element.The function may return false to request a stop; if its result is any other value (including nil ), the iteration will continue. |
?string | tag XML tag to be matched |
?string | key attribute key to be matched |
value | (optional) attribute value to be matched |
?boolean | r recursive operation. If true , also iterate over the subelements of var |
?number | max maximum depth allowed |
?number | d initial depth value, defaults to 0 |
true
for an exhaustive iteration, and false
if was stopped from the callback.Definition at line 1182 of file LuaXML_lib.c.
References depth, lua_call, lua_equal(), lua_insert, lua_isnil, lua_pop, lua_pushboolean(), lua_pushcfunction, lua_pushinteger(), lua_pushvalue(), lua_rawgeti(), lua_settop(), LUA_TFUNCTION, lua_toboolean(), lua_tointeger, LUA_TTABLE, lua_type(), luaL_checktype(), luaL_optint, Xml_iterate(), and Xml_match().
Referenced by luaopen_LuaXML_lib(), Xml_find(), and Xml_iterate().
|
static |
loads XML data from a file and returns it as table. Basically, this is just calling eval
on the given file's content.
@function load
string | filename the name and path of the file to be loaded |
?number | mode whitespace handling mode, defaults to WS_TRIM |
nil
in case of errors Definition at line 791 of file LuaXML_lib.c.
References fread(), free, lua_pushlightuserdata(), lua_replace, luaL_checkstring, luaL_error(), malloc, and Xml_eval().
Referenced by luaopen_LuaXML_lib().
|
static |
match XML entity against given (optional) criteria. Passing nil
for one of thetag
, key
, or value
parameters means "don't
care" (i.e. match anything for that particular aspect). So for example var:match(nil, "text", nil) – or shorter, but identical: var:match(nil, "text") will look for an XML attribute (name) "text" to be present in var
, but won't consider its value or the tag of var
.
Note: If you want to test for a specific attribute value
, so also have to supply a key
- otherwise value
will be ignored.
@usage – each of these will either return x
, or nil
in case of no match
x:match("foo") – test for x:tag() == "foo" x:match(nil, "bar") – test if x has a "bar" attribute x:match(nil, "foo", "bar") – test if x has a "foo" attribute that equals "bar" x:match("foobar", "foo", "bar") – test for "foobar" tag, and attr "foo" == "bar"
@function match
var | the variable to test, normally a Lua table or LuaXML object. (If var is not a table type, the test always fails.) |
?string | tag If set, has to match the XML tag (i.e. must be equal to the tag(var, nil) result) |
?string | key If set, a corresponding attribute key needs to be present (exact name match). |
value | (optional) arbitrary Lua value. If set, the attribute value for key has to match it. |
nil
for no match; or the var
argument properly converted to a LuaXML object, equivalent to xml.new(var)
.This allows you to either make direct use of the matched LuaXML object, or to use the return value in a boolean test (if xml.match(...)
), which is a common Lua idiom.
Definition at line 1116 of file LuaXML_lib.c.
References lua_equal(), lua_isnil, lua_isnoneornil, lua_pop, lua_pushvalue(), lua_rawget(), lua_settop(), LUA_TSTRING, LUA_TTABLE, lua_type(), make_xml_object(), and push_TAG_key().
Referenced by luaopen_LuaXML_lib(), and Xml_iterate().
|
static |
creates a LuaXML "object", and optionally sets its tag. The function either sets the metatable of an existing Lua table, or creates a new (empty) "object". If you pass an optionaltag
string, it will be assigned to the result.
(It's also possible to call this as new(tag)
, which creates a new XML object with the given tag and is equivalent to new({}, tag)
.)
Note that it's not mandatory to use this function in order to treat a Lua table as LuaXML object. Setting the metatable just allows the usage of a more object-oriented syntax (e.g. xmlvar:str()
instead of xml.str(xmlvar)
). XML objects created by load
or eval
automatically offer the object-oriented syntax.
@function new
arg | (optional) _(1)_ a table to be converted to a LuaXML object, or _(2)_ the tag of the new LuaXML object |
?string | tag a tag value that will be assigned to the object |
arg
; optionally tagged as requested Definition at line 528 of file LuaXML_lib.c.
References lua_call, lua_insert, lua_istable, lua_newtable, lua_pushcfunction, lua_pushvalue(), lua_settop(), LUA_TSTRING, lua_type(), make_xml_object(), and Xml_tag().
Referenced by luaopen_LuaXML_lib(), and Xml_append().
|
static |
Definition at line 638 of file LuaXML_lib.c.
References do_gsub(), lua_call, lua_gettable(), lua_insert, lua_next(), lua_pop, lua_pushcfunction, lua_pushliteral, lua_pushlstring(), lua_pushnil(), lua_rawgeti(), LUA_REGISTRYINDEX, lua_tostring, s, sv_code_ref, and XMLencoding_replacement().
Referenced by Xml_decode(), and Xml_eval().
|
static |
Definition at line 575 of file LuaXML_lib.c.
References do_gsub(), index, lua_call, lua_getglobal, lua_gettop(), lua_next(), lua_pop, lua_pushnil(), lua_pushvalue(), lua_rawgeti(), LUA_REGISTRYINDEX, lua_replace, lua_tostring, LUA_TSTRING, lua_type(), luaL_addchar, luaL_addlstring(), luaL_buffinit(), luaL_pushresult(), s, snprintf, and sv_code_ref.
Referenced by Xml_encode(), and Xml_str().
|
static |
registers a custom code for the conversion between non-standard characters and XML character entities.
By default, only the most basic entities are known to LuaXML: " < > ' On top (and independent) of that, the ampersand sign always gets encoded / decoded separately: &
↔ &amp;
. Character codes above 127 are directly converted to an appropriate XML encoding, representing the character number (e.g. &#160;
). If other special encodings are needed, they can be registered using this function.
Note: LuaXML now manages these encodings in a (private) standard Lua table. This allows you to replace entries by calling registerCode()
again, using the same decoded
and a different encoded
. Encodings may even be removed later, by explictly registering a nil
value: registerCode(decoded, nil)
.
@function registerCode
string | decoded the character (sequence) to be used within Lua |
string | encoded the character entity to be used in XML |
Definition at line 836 of file LuaXML_lib.c.
References lua_insert, lua_isnoneornil, lua_rawgeti(), lua_rawset(), LUA_REGISTRYINDEX, lua_settop(), luaL_checkstring, and sv_code_ref.
Referenced by luaopen_LuaXML_lib().
|
static |
converts any Lua value to an XML string. @function str
value | the value to be converted, normally a table (LuaXML object). However this function will 'encapsulate' other Lua values (of arbitrary type) in a way that should make them valid XML. Note: Passing no value will cause the function to return nil . |
?number | indent indentation level for 'pretty' output. Mainly for internal use, defaults to 0. |
?string | tag the tag to be used in case value doesn't already have an 'implicit' tag. Mainly for internal use. |
@treturn string an XML string, or nil
in case of errors.
Definition at line 912 of file LuaXML_lib.c.
References lua_call, lua_concat(), lua_insert, lua_istable, lua_newtable, lua_next(), lua_pop, lua_pushcfunction, lua_pushfstring(), lua_pushinteger(), lua_pushliteral, lua_pushnil(), lua_pushvalue(), lua_rawget(), lua_rawgeti(), lua_rawlen, lua_rawseti(), lua_remove, lua_replace, lua_settop(), LUA_TNIL, lua_tointeger, lua_tostring, LUA_TSTRING, LUA_TTABLE, lua_type(), lua_typename(), luaL_addchar, luaL_addlstring(), luaL_addstring(), luaL_addvalue(), luaL_buffinit(), luaL_pushresult(), push_indentStr(), push_TAG_key(), Xml_pushEncode(), and Xml_str().
Referenced by luaopen_LuaXML_lib(), and Xml_str().
|
static |
sets or returns tag of a LuaXML object. This method is just "syntactic sugar" (using a typical Lua term) that allows the writing of clearer code. LuaXML stores the tag value of an XML statement at table index 0, hence it can be simply accessed or altered by var[0]
. However, writing var:tag()
for access or var:tag("newTag")
for altering may be more self explanatory (and future-proof in case LuaXML's tag handling should ever change).
@function tag
var | the variable whose tag should be accessed, a LuaXML object |
?string | tag the new tag to be set |
var
(with its tag changed); otherwise the result will be the current tag of var
(normally a string). Definition at line 483 of file LuaXML_lib.c.
References lua_istable, lua_pushvalue(), lua_rawget(), lua_rawset(), lua_settop(), LUA_TSTRING, lua_type(), and push_TAG_key().
Referenced by luaopen_LuaXML_lib(), and Xml_new().
|
static |
Definition at line 180 of file LuaXML_lib.c.
References lua_pushlstring(), lua_tostring, and NULL.
Referenced by Xml_pushDecode().
|
static |
Definition at line 463 of file LuaXML_lib.c.
Referenced by luaopen_LuaXML_lib(), Xml_pushDecode(), Xml_pushEncode(), and Xml_registerCode().