demux: wav: check implicit duration
[vlc.git] / modules / lua / libs / strings.c
blob66fb754c8fb326974d98eae73ce763e644b6dfd4
1 /*****************************************************************************
2 * strings.c
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifndef _GNU_SOURCE
29 # define _GNU_SOURCE
30 #endif
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_meta.h>
39 #include <vlc_charset.h>
41 #include "../vlc.h"
42 #include "../libs.h"
44 /*****************************************************************************
45 * String transformations
46 *****************************************************************************/
47 static int vlclua_decode_uri( lua_State *L )
49 int i_top = lua_gettop( L );
50 int i;
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 );
59 free( psz_string );
61 return i_top;
64 static int vlclua_encode_uri_component( lua_State *L )
66 int i_top = lua_gettop( L );
67 int i;
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 );
72 lua_remove( L,1 );
73 lua_pushstring( L, psz_string );
74 free( psz_string );
76 return i_top;
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 );
87 free( psz_uri );
89 else
90 lua_pushstring( L, psz_input );
91 return 1;
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 );
99 free( psz_path );
100 return 1;
103 int vlclua_url_parse( lua_State *L )
105 const char *psz_url = luaL_checkstring( L, 1 );
106 vlc_url_t url;
108 vlc_UrlParse( &url, psz_url );
110 lua_newtable( L );
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 );
128 return 1;
131 static int vlclua_resolve_xml_special_chars( lua_State *L )
133 int i_top = lua_gettop( L );
134 int i;
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 );
144 free( psz_string );
146 return i_top;
149 static int vlclua_convert_xml_special_chars( lua_State *L )
151 int i_top = lua_gettop( L );
152 int i;
153 for( i = 1; i <= i_top; i++ )
155 char *psz_string = vlc_xml_encode( luaL_checkstring(L,1) );
156 lua_remove( L, 1 );
157 lua_pushstring( L, psz_string );
158 free( psz_string );
160 return i_top;
163 static int vlclua_from_charset( lua_State *L )
165 if( lua_gettop( L ) < 2 ) return vlclua_error( L );
167 size_t i_in_bytes;
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 : "" );
174 free( psz_output );
175 return 1;
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 },
190 { NULL, NULL }
193 void luaopen_strings( lua_State *L )
195 lua_newtable( L );
196 luaL_register( L, NULL, vlclua_strings_reg );
197 lua_setfield( L, -2, "strings" );