1 /*****************************************************************************
2 * xml.c: XML related functions
3 *****************************************************************************
4 * Copyright (C) 2010 Antoine Cellerier
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
38 #include <lua.h> /* Low level lua C API */
39 #include <lauxlib.h> /* Higher level C API */
44 /*****************************************************************************
46 *****************************************************************************/
47 static int vlclua_xml_create_reader( lua_State * );
49 static const luaL_Reg vlclua_xml_reg[] = {
50 { "create_reader", vlclua_xml_create_reader },
54 static int vlclua_xml_delete( lua_State *L )
56 xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
61 static int vlclua_xml_create( lua_State *L )
63 xml_t *p_xml = xml_Create( vlclua_get_this( L ) );
65 return luaL_error( L, "XML module creation failed." );
67 xml_t **pp_xml = lua_newuserdata( L, sizeof( xml_t * ) );
70 if( luaL_newmetatable( L, "xml" ) )
73 luaL_register( L, NULL, vlclua_xml_reg );
74 lua_setfield( L, -2, "__index" );
75 lua_pushcfunction( L, vlclua_xml_delete );
76 lua_setfield( L, -2, "__gc" );
79 lua_setmetatable( L, -2 );
83 /*****************************************************************************
85 *****************************************************************************/
86 static int vlclua_xml_reader_next_node( lua_State * );
87 static int vlclua_xml_reader_next_attr( lua_State * );
89 static const luaL_Reg vlclua_xml_reader_reg[] = {
90 { "next_node", vlclua_xml_reader_next_node },
91 { "next_attr", vlclua_xml_reader_next_attr },
95 static int vlclua_xml_reader_delete( lua_State *L )
97 xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
98 xml_ReaderDelete( p_reader );
102 static int vlclua_xml_create_reader( lua_State *L )
104 xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
105 stream_t *p_stream = *(stream_t **)luaL_checkudata( L, 2, "stream" );
107 xml_reader_t *p_reader = xml_ReaderCreate( p_xml, p_stream );
109 return luaL_error( L, "XML reader creation failed." );
111 xml_reader_t **pp_reader = lua_newuserdata( L, sizeof( xml_reader_t * ) );
112 *pp_reader = p_reader;
114 if( luaL_newmetatable( L, "xml_reader" ) )
117 luaL_register( L, NULL, vlclua_xml_reader_reg );
118 lua_setfield( L, -2, "__index" );
119 lua_pushcfunction( L, vlclua_xml_reader_delete );
120 lua_setfield( L, -2, "__gc" );
123 lua_setmetatable( L, -2 );
127 static int vlclua_xml_reader_next_node( lua_State *L )
129 xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
130 const char *psz_name;
131 int i_type = xml_ReaderNextNode( p_reader, &psz_name );
134 lua_pushinteger( L, 0 );
138 lua_pushinteger( L, i_type );
139 lua_pushstring( L, psz_name );
143 static int vlclua_xml_reader_next_attr( lua_State *L )
145 xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
146 const char *psz_value;
147 const char *psz_name = xml_ReaderNextAttr( p_reader, &psz_value );
151 lua_pushstring( L, psz_name );
152 lua_pushstring( L, psz_value );
156 void luaopen_xml( lua_State *L )
158 lua_pushcfunction( L, vlclua_xml_create );
159 lua_setfield( L, -2, "xml" );