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>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int vlclua_xml_create_reader( lua_State * );
46 static const luaL_Reg vlclua_xml_reg[] = {
47 { "create_reader", vlclua_xml_create_reader },
51 static int vlclua_xml_delete( lua_State *L )
53 xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
58 static int vlclua_xml_create( lua_State *L )
60 xml_t *p_xml = xml_Create( vlclua_get_this( L ) );
62 return luaL_error( L, "XML module creation failed." );
64 xml_t **pp_xml = lua_newuserdata( L, sizeof( xml_t * ) );
67 if( luaL_newmetatable( L, "xml" ) )
70 luaL_register( L, NULL, vlclua_xml_reg );
71 lua_setfield( L, -2, "__index" );
72 lua_pushcfunction( L, vlclua_xml_delete );
73 lua_setfield( L, -2, "__gc" );
76 lua_setmetatable( L, -2 );
80 /*****************************************************************************
82 *****************************************************************************/
83 static int vlclua_xml_reader_next_node( lua_State * );
84 static int vlclua_xml_reader_next_attr( lua_State * );
86 static const luaL_Reg vlclua_xml_reader_reg[] = {
87 { "next_node", vlclua_xml_reader_next_node },
88 { "next_attr", vlclua_xml_reader_next_attr },
92 static int vlclua_xml_reader_delete( lua_State *L )
94 xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
95 xml_ReaderDelete( p_reader );
99 static int vlclua_xml_create_reader( lua_State *L )
101 xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
102 stream_t *p_stream = *(stream_t **)luaL_checkudata( L, 2, "stream" );
104 xml_reader_t *p_reader = xml_ReaderCreate( p_xml, p_stream );
106 return luaL_error( L, "XML reader creation failed." );
108 xml_reader_t **pp_reader = lua_newuserdata( L, sizeof( xml_reader_t * ) );
109 *pp_reader = p_reader;
111 if( luaL_newmetatable( L, "xml_reader" ) )
114 luaL_register( L, NULL, vlclua_xml_reader_reg );
115 lua_setfield( L, -2, "__index" );
116 lua_pushcfunction( L, vlclua_xml_reader_delete );
117 lua_setfield( L, -2, "__gc" );
120 lua_setmetatable( L, -2 );
124 static int vlclua_xml_reader_next_node( lua_State *L )
126 xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
127 const char *psz_name;
128 int i_type = xml_ReaderNextNode( p_reader, &psz_name );
131 lua_pushinteger( L, 0 );
135 lua_pushinteger( L, i_type );
136 lua_pushstring( L, psz_name );
140 static int vlclua_xml_reader_next_attr( lua_State *L )
142 xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
143 const char *psz_value;
144 const char *psz_name = xml_ReaderNextAttr( p_reader, &psz_value );
148 lua_pushstring( L, psz_name );
149 lua_pushstring( L, psz_value );
153 void luaopen_xml( lua_State *L )
155 lua_pushcfunction( L, vlclua_xml_create );
156 lua_setfield( L, -2, "xml" );