]> git.sesse.net Git - vlc/blob - modules/lua/libs/xml.c
V4L2: merge two switches
[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 <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 /*****************************************************************************
45  * XML
46  *****************************************************************************/
47 static int vlclua_xml_create_reader( lua_State * );
48
49 static const luaL_Reg vlclua_xml_reg[] = {
50     { "create_reader", vlclua_xml_create_reader },
51     { NULL, NULL }
52 };
53
54 static int vlclua_xml_delete( lua_State *L )
55 {
56     xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
57     xml_Delete( p_xml );
58     return 0;
59 }
60
61 static int vlclua_xml_create( lua_State *L )
62 {
63     xml_t *p_xml = xml_Create( vlclua_get_this( L ) );
64     if( !p_xml )
65         return luaL_error( L, "XML module creation failed." );
66
67     xml_t **pp_xml = lua_newuserdata( L, sizeof( xml_t * ) );
68     *pp_xml = p_xml;
69
70     if( luaL_newmetatable( L, "xml" ) )
71     {
72         lua_newtable( L );
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" );
77     }
78
79     lua_setmetatable( L, -2 );
80     return 1;
81 }
82
83 /*****************************************************************************
84  * XML Reader
85  *****************************************************************************/
86 static int vlclua_xml_reader_next_node( lua_State * );
87 static int vlclua_xml_reader_next_attr( lua_State * );
88
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 },
92     { NULL, NULL }
93 };
94
95 static int vlclua_xml_reader_delete( lua_State *L )
96 {
97     xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
98     xml_ReaderDelete( p_reader );
99     return 0;
100 }
101
102 static int vlclua_xml_create_reader( lua_State *L )
103 {
104     xml_t *p_xml = *(xml_t**)luaL_checkudata( L, 1, "xml" );
105     stream_t *p_stream = *(stream_t **)luaL_checkudata( L, 2, "stream" );
106
107     xml_reader_t *p_reader = xml_ReaderCreate( p_xml, p_stream );
108     if( !p_reader )
109         return luaL_error( L, "XML reader creation failed." );
110
111     xml_reader_t **pp_reader = lua_newuserdata( L, sizeof( xml_reader_t * ) );
112     *pp_reader = p_reader;
113
114     if( luaL_newmetatable( L, "xml_reader" ) )
115     {
116         lua_newtable( L );
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" );
121     }
122
123     lua_setmetatable( L, -2 );
124     return 1;
125 }
126
127 static int vlclua_xml_reader_next_node( lua_State *L )
128 {
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 );
132     if( i_type <= 0 )
133     {
134         lua_pushinteger( L, 0 );
135         return 1;
136     }
137
138     lua_pushinteger( L, i_type );
139     lua_pushstring( L, psz_name );
140     return 2;
141 }
142
143 static int vlclua_xml_reader_next_attr( lua_State *L )
144 {
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 );
148     if( !psz_name )
149         return 0;
150
151     lua_pushstring( L, psz_name );
152     lua_pushstring( L, psz_value );
153     return 2;
154 }
155
156 void luaopen_xml( lua_State *L )
157 {
158     lua_pushcfunction( L, vlclua_xml_create );
159     lua_setfield( L, -2, "xml" );
160 }