]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/stream.c
Clone video filter : fix potential memleak.
[vlc] / modules / misc / lua / libs / stream.c
1 /*****************************************************************************
2  * stream.c: stream functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_meta.h>
39 #include <vlc_charset.h>
40 #include <vlc_aout.h>
41
42 #include <lua.h>        /* Low level lua C API */
43 #include <lauxlib.h>    /* Higher level C API */
44
45 #include "../vlc.h"
46 #include "../libs.h"
47
48 /*****************************************************************************
49  * Stream handling
50  *****************************************************************************/
51 static int vlclua_stream_read( lua_State * );
52 static int vlclua_stream_readline( lua_State * );
53 static int vlclua_stream_delete( lua_State * );
54
55 static const luaL_Reg vlclua_stream_reg[] = {
56     { "read", vlclua_stream_read },
57     { "readline", vlclua_stream_readline },
58     { NULL, NULL }
59 };
60
61 static int vlclua_stream_new( lua_State *L )
62 {
63     vlc_object_t * p_this = vlclua_get_this( L );
64     stream_t * p_stream;
65     const char * psz_url;
66     psz_url = luaL_checkstring( L, 1 );
67     p_stream = stream_UrlNew( p_this, psz_url );
68     if( !p_stream )
69         return luaL_error( L, "Error when opening url: `%s'", psz_url );
70
71     stream_t **pp_stream = lua_newuserdata( L, sizeof( stream_t * ) );
72     *pp_stream = p_stream;
73
74     if( luaL_newmetatable( L, "stream" ) )
75     {
76         lua_newtable( L );
77         luaL_register( L, NULL, vlclua_stream_reg );
78         lua_setfield( L, -2, "__index" );
79         lua_pushcfunction( L, vlclua_stream_delete );
80         lua_setfield( L, -2, "__gc" );
81     }
82
83     lua_setmetatable( L, -2 );
84     return 1;
85 }
86
87 static int vlclua_stream_read( lua_State *L )
88 {
89     int i_read;
90     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
91     int n = luaL_checkint( L, 2 );
92     uint8_t *p_read = malloc( n );
93     if( !p_read ) return vlclua_error( L );
94     i_read = stream_Read( *pp_stream, p_read, n );
95     lua_pushlstring( L, (const char *)p_read, i_read );
96     free( p_read );
97     return 1;
98 }
99
100 static int vlclua_stream_readline( lua_State *L )
101 {
102     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
103     char *psz_line = stream_ReadLine( *pp_stream );
104     if( psz_line )
105     {
106         lua_pushstring( L, psz_line );
107         free( psz_line );
108     }
109     else
110         lua_pushnil( L );
111     return 1;
112 }
113
114 static int vlclua_stream_delete( lua_State *L )
115 {
116     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
117     stream_Delete( *pp_stream );
118     return 0;
119 }
120
121 /*****************************************************************************
122  *
123  *****************************************************************************/
124 void luaopen_stream( lua_State *L )
125 {
126     lua_pushcfunction( L, vlclua_stream_new );
127     lua_setfield( L, -2, "stream" );
128 }