]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/configuration.c
Fix memleak in lua module.
[vlc] / modules / misc / lua / libs / configuration.c
1 /*****************************************************************************
2  * configuration.c: Generic lua<->vlc config interface
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
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
37 #include <lua.h>        /* Low level lua C API */
38 #include <lauxlib.h>    /* Higher level C API */
39
40 #include "../vlc.h"
41 #include "../libs.h"
42
43 /*****************************************************************************
44  * Config handling
45  *****************************************************************************/
46 static int vlclua_config_get( lua_State *L )
47 {
48     vlc_object_t * p_this = vlclua_get_this( L );
49     const char *psz_name;
50     psz_name = luaL_checkstring( L, 1 );
51     switch( config_GetType( p_this, psz_name ) )
52     {
53         case VLC_VAR_MODULE:
54         case VLC_VAR_STRING:
55         case VLC_VAR_FILE:
56         case VLC_VAR_DIRECTORY:
57         {
58             char *psz = config_GetPsz( p_this, psz_name );
59             lua_pushstring( L, psz );
60             free( psz );
61             break;
62         }
63
64         case VLC_VAR_INTEGER:
65             lua_pushinteger( L, config_GetInt( p_this, psz_name ) );
66             break;
67
68         case VLC_VAR_BOOL:
69             lua_pushboolean( L, config_GetInt( p_this, psz_name ) );
70             break;
71
72         case VLC_VAR_FLOAT:
73             lua_pushnumber( L, config_GetFloat( p_this, psz_name ) );
74             break;
75
76         default:
77             return vlclua_error( L );
78
79     }
80     return 1;
81 }
82
83 static int vlclua_config_set( lua_State *L )
84 {
85     vlc_object_t *p_this = vlclua_get_this( L );
86     const char *psz_name;
87     psz_name = luaL_checkstring( L, 1 );
88     switch( config_GetType( p_this, psz_name ) )
89     {
90         case VLC_VAR_MODULE:
91         case VLC_VAR_STRING:
92         case VLC_VAR_FILE:
93         case VLC_VAR_DIRECTORY:
94             config_PutPsz( p_this, psz_name, luaL_checkstring( L, 2 ) );
95             break;
96
97         case VLC_VAR_INTEGER:
98             config_PutInt( p_this, psz_name, luaL_checkint( L, 2 ) );
99             break;
100
101         case VLC_VAR_BOOL:
102             config_PutInt( p_this, psz_name, luaL_checkboolean( L, 2 ) );
103             break;
104
105         case VLC_VAR_FLOAT:
106             config_PutFloat( p_this, psz_name,
107                              luaL_checknumber( L, 2 ) );
108             break;
109
110         default:
111             return vlclua_error( L );
112     }
113     return 0;
114 }
115
116 /*****************************************************************************
117  *
118  *****************************************************************************/
119 static const luaL_Reg vlclua_config_reg[] = {
120     { "get", vlclua_config_get },
121     { "set", vlclua_config_set },
122     { NULL, NULL }
123 };
124
125 void luaopen_config( lua_State *L )
126 {
127     lua_newtable( L );
128     luaL_register( L, NULL, vlclua_config_reg );
129     lua_setfield( L, -2, "config" );
130 }