1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
40 #include <vlc_charset.h>
42 #include <lua.h> /* Low level lua C API */
43 #include <lauxlib.h> /* Higher level C API */
48 /*****************************************************************************
49 * String transformations
50 *****************************************************************************/
51 static int vlclua_decode_uri( lua_State *L )
53 int i_top = lua_gettop( L );
55 for( i = 1; i <= i_top; i++ )
57 const char *psz_cstring = luaL_checkstring( L, 1 );
58 char *psz_string = strdup( psz_cstring );
59 lua_remove( L, 1 ); /* remove elements to prevent being limited by
60 * the stack's size (this function will work with
61 * up to (stack size - 1) arguments */
62 decode_URI( psz_string );
63 lua_pushstring( L, psz_string );
69 static int vlclua_encode_uri_component( lua_State *L )
71 int i_top = lua_gettop( L );
73 for( i = 1; i <= i_top; i++ )
75 const char *psz_cstring = luaL_checkstring( L, 1 );
76 char *psz_string = encode_URI_component( psz_cstring );
78 lua_pushstring( L, psz_string );
84 static int vlclua_make_uri( lua_State *L )
86 const char *psz_input = luaL_checkstring( L, 1 );
87 const char *psz_scheme = luaL_optstring( L, 2, NULL );
88 char *psz_uri = make_URI( psz_input, psz_scheme );
89 lua_pushstring( L, psz_uri );
94 static int vlclua_resolve_xml_special_chars( lua_State *L )
96 int i_top = lua_gettop( L );
98 for( i = 1; i <= i_top; i++ )
100 const char *psz_cstring = luaL_checkstring( L, 1 );
101 char *psz_string = strdup( psz_cstring );
102 lua_remove( L, 1 ); /* remove elements to prevent being limited by
103 * the stack's size (this function will work with
104 * up to (stack size - 1) arguments */
105 resolve_xml_special_chars( psz_string );
106 lua_pushstring( L, psz_string );
112 static int vlclua_convert_xml_special_chars( lua_State *L )
114 int i_top = lua_gettop( L );
116 for( i = 1; i <= i_top; i++ )
118 char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
120 lua_pushstring( L, psz_string );
126 static int vlclua_from_charset( lua_State *L )
128 if( lua_gettop( L ) < 2 ) return vlclua_error( L );
131 const char *psz_input = luaL_checklstring( L, 2, &i_in_bytes );
132 if( i_in_bytes == 0 ) return vlclua_error( L );
134 const char *psz_charset = luaL_checkstring( L, 1 );
135 char *psz_output = FromCharset( psz_charset, psz_input, i_in_bytes );
136 lua_pushstring( L, psz_output ? psz_output : "" );
141 /*****************************************************************************
143 *****************************************************************************/
144 static const luaL_Reg vlclua_strings_reg[] = {
145 { "decode_uri", vlclua_decode_uri },
146 { "encode_uri_component", vlclua_encode_uri_component },
147 { "make_uri", vlclua_make_uri },
148 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
149 { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
150 { "from_charset", vlclua_from_charset },
154 void luaopen_strings( lua_State *L )
157 luaL_register( L, NULL, vlclua_strings_reg );
158 lua_setfield( L, -2, "strings" );