]> git.sesse.net Git - vlc/blob - modules/lua/libs/xml.c
Lua: revert should_die to using vlc_object_alive()
[vlc] / modules / lua / libs / xml.c
1 /*****************************************************************************
2  * xml.c: XML related functions
3  *****************************************************************************
4  * Copyright (C) 2010 Antoine Cellerier
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_xml.h>
37
38 #include "../vlc.h"
39 #include "../libs.h"
40
41 /*****************************************************************************
42  * XML
43  *****************************************************************************/
44 static int vlclua_xml_create_reader( lua_State * );
45
46 static const luaL_Reg vlclua_xml_reg[] = {
47     { "create_reader", vlclua_xml_create_reader },
48     { NULL, NULL }
49 };
50
51 static int vlclua_xml_delete( lua_State *L )
52 {
53     xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
54     xml_Delete( p_xml );
55     return 0;
56 }
57
58 static int vlclua_xml_create( lua_State *L )
59 {
60     xml_t *p_xml = xml_Create( vlclua_get_this( L ) );
61     if( !p_xml )
62         return luaL_error( L, "XML module creation failed." );
63
64     xml_t **pp_xml = lua_newuserdata( L, sizeof( xml_t * ) );
65     *pp_xml = p_xml;
66
67     if( luaL_newmetatable( L, "xml" ) )
68     {
69         lua_newtable( L );
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" );
74     }
75
76     lua_setmetatable( L, -2 );
77     return 1;
78 }
79
80 /*****************************************************************************
81  * XML Reader
82  *****************************************************************************/
83 static int vlclua_xml_reader_next_node( lua_State * );
84 static int vlclua_xml_reader_next_attr( lua_State * );
85
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 },
89     { NULL, NULL }
90 };
91
92 static int vlclua_xml_reader_delete( lua_State *L )
93 {
94     xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
95     xml_ReaderDelete( p_reader );
96     return 0;
97 }
98
99 static int vlclua_xml_create_reader( lua_State *L )
100 {
101     xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
102     stream_t *p_stream = *(stream_t **)luaL_checkudata( L, 2, "stream" );
103
104     xml_reader_t *p_reader = xml_ReaderCreate( p_xml, p_stream );
105     if( !p_reader )
106         return luaL_error( L, "XML reader creation failed." );
107
108     xml_reader_t **pp_reader = lua_newuserdata( L, sizeof( xml_reader_t * ) );
109     *pp_reader = p_reader;
110
111     if( luaL_newmetatable( L, "xml_reader" ) )
112     {
113         lua_newtable( L );
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" );
118     }
119
120     lua_setmetatable( L, -2 );
121     return 1;
122 }
123
124 static int vlclua_xml_reader_next_node( lua_State *L )
125 {
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 );
129     if( i_type <= 0 )
130     {
131         lua_pushinteger( L, 0 );
132         return 1;
133     }
134
135     lua_pushinteger( L, i_type );
136     lua_pushstring( L, psz_name );
137     return 2;
138 }
139
140 static int vlclua_xml_reader_next_attr( lua_State *L )
141 {
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 );
145     if( !psz_name )
146         return 0;
147
148     lua_pushstring( L, psz_name );
149     lua_pushstring( L, psz_value );
150     return 2;
151 }
152
153 void luaopen_xml( lua_State *L )
154 {
155     lua_pushcfunction( L, vlclua_xml_create );
156     lua_setfield( L, -2, "xml" );
157 }