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>
39 #include <vlc_charset.h>
44 /*****************************************************************************
45 * String transformations
46 *****************************************************************************/
47 static int vlclua_decode_uri( lua_State
*L
)
49 int i_top
= lua_gettop( L
);
51 for( i
= 1; i
<= i_top
; i
++ )
53 const char *psz_cstring
= luaL_checkstring( L
, 1 );
54 char *psz_string
= vlc_uri_decode_duplicate( psz_cstring
);
55 lua_remove( L
, 1 ); /* remove elements to prevent being limited by
56 * the stack's size (this function will work with
57 * up to (stack size - 1) arguments */
58 lua_pushstring( L
, psz_string
);
64 static int vlclua_encode_uri_component( lua_State
*L
)
66 int i_top
= lua_gettop( L
);
68 for( i
= 1; i
<= i_top
; i
++ )
70 const char *psz_cstring
= luaL_checkstring( L
, 1 );
71 char *psz_string
= vlc_uri_encode( psz_cstring
);
73 lua_pushstring( L
, psz_string
);
79 static int vlclua_make_uri( lua_State
*L
)
81 const char *psz_input
= luaL_checkstring( L
, 1 );
82 const char *psz_scheme
= luaL_optstring( L
, 2, NULL
);
83 if( strstr( psz_input
, "://" ) == NULL
)
85 char *psz_uri
= vlc_path2uri( psz_input
, psz_scheme
);
86 lua_pushstring( L
, psz_uri
);
90 lua_pushstring( L
, psz_input
);
94 static int vlclua_make_path( lua_State
*L
)
96 const char *psz_input
= luaL_checkstring( L
, 1 );
97 char *psz_path
= vlc_uri2path( psz_input
);
98 lua_pushstring( L
, psz_path
);
103 int vlclua_url_parse( lua_State
*L
)
105 const char *psz_url
= luaL_checkstring( L
, 1 );
108 vlc_UrlParse( &url
, psz_url
);
111 lua_pushstring( L
, url
.psz_protocol
);
112 lua_setfield( L
, -2, "protocol" );
113 lua_pushstring( L
, url
.psz_username
);
114 lua_setfield( L
, -2, "username" );
115 lua_pushstring( L
, url
.psz_password
);
116 lua_setfield( L
, -2, "password" );
117 lua_pushstring( L
, url
.psz_host
);
118 lua_setfield( L
, -2, "host" );
119 lua_pushinteger( L
, url
.i_port
);
120 lua_setfield( L
, -2, "port" );
121 lua_pushstring( L
, url
.psz_path
);
122 lua_setfield( L
, -2, "path" );
123 lua_pushstring( L
, url
.psz_option
);
124 lua_setfield( L
, -2, "option" );
126 vlc_UrlClean( &url
);
131 static int vlclua_resolve_xml_special_chars( lua_State
*L
)
133 int i_top
= lua_gettop( L
);
135 for( i
= 1; i
<= i_top
; i
++ )
137 const char *psz_cstring
= luaL_checkstring( L
, 1 );
138 char *psz_string
= strdup( psz_cstring
);
139 lua_remove( L
, 1 ); /* remove elements to prevent being limited by
140 * the stack's size (this function will work with
141 * up to (stack size - 1) arguments */
142 vlc_xml_decode( psz_string
);
143 lua_pushstring( L
, psz_string
);
149 static int vlclua_convert_xml_special_chars( lua_State
*L
)
151 int i_top
= lua_gettop( L
);
153 for( i
= 1; i
<= i_top
; i
++ )
155 char *psz_string
= vlc_xml_encode( luaL_checkstring(L
,1) );
157 lua_pushstring( L
, psz_string
);
163 static int vlclua_from_charset( lua_State
*L
)
165 if( lua_gettop( L
) < 2 ) return vlclua_error( L
);
168 const char *psz_input
= luaL_checklstring( L
, 2, &i_in_bytes
);
169 if( i_in_bytes
== 0 ) return vlclua_error( L
);
171 const char *psz_charset
= luaL_checkstring( L
, 1 );
172 char *psz_output
= FromCharset( psz_charset
, psz_input
, i_in_bytes
);
173 lua_pushstring( L
, psz_output
? psz_output
: "" );
178 /*****************************************************************************
180 *****************************************************************************/
181 static const luaL_Reg vlclua_strings_reg
[] = {
182 { "decode_uri", vlclua_decode_uri
},
183 { "encode_uri_component", vlclua_encode_uri_component
},
184 { "make_uri", vlclua_make_uri
},
185 { "make_path", vlclua_make_path
},
186 { "url_parse", vlclua_url_parse
},
187 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars
},
188 { "convert_xml_special_chars", vlclua_convert_xml_special_chars
},
189 { "from_charset", vlclua_from_charset
},
193 void luaopen_strings( lua_State
*L
)
196 luaL_register( L
, NULL
, vlclua_strings_reg
);
197 lua_setfield( L
, -2, "strings" );