]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/strings.c
lua: add vlc.strings.iconv to use vlc_iconv, shouldn't eat kittens
[vlc] / modules / misc / lua / libs / strings.c
1 /*****************************************************************************
2  * strings.c
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_aout.h>
40
41 #include <lua.h>        /* Low level lua C API */
42 #include <lauxlib.h>    /* Higher level C API */
43
44 #include "../vlc.h"
45 #include "../libs.h"
46
47 /*****************************************************************************
48  * String transformations
49  *****************************************************************************/
50 static int vlclua_decode_uri( lua_State *L )
51 {
52     int i_top = lua_gettop( L );
53     int i;
54     for( i = 1; i <= i_top; i++ )
55     {
56         const char *psz_cstring = luaL_checkstring( L, 1 );
57         char *psz_string = strdup( psz_cstring );
58         lua_remove( L, 1 ); /* remove elements to prevent being limited by
59                              * the stack's size (this function will work with
60                              * up to (stack size - 1) arguments */
61         decode_URI( psz_string );
62         lua_pushstring( L, psz_string );
63         free( psz_string );
64     }
65     return i_top;
66 }
67
68 static int vlclua_encode_uri_component( lua_State *L )
69 {
70     int i_top = lua_gettop( L );
71     int i;
72     for( i = 1; i <= i_top; i++ )
73     {
74         const char *psz_cstring = luaL_checkstring( L, 1 );
75         char *psz_string = encode_URI_component( psz_cstring );
76         lua_remove( L,1 );
77         lua_pushstring( L, psz_string );
78         free( psz_string );
79     }
80     return i_top;
81 }
82
83 static int vlclua_resolve_xml_special_chars( lua_State *L )
84 {
85     int i_top = lua_gettop( L );
86     int i;
87     for( i = 1; i <= i_top; i++ )
88     {
89         const char *psz_cstring = luaL_checkstring( L, 1 );
90         char *psz_string = strdup( psz_cstring );
91         lua_remove( L, 1 ); /* remove elements to prevent being limited by
92                              * the stack's size (this function will work with
93                              * up to (stack size - 1) arguments */
94         resolve_xml_special_chars( psz_string );
95         lua_pushstring( L, psz_string );
96         free( psz_string );
97     }
98     return i_top;
99 }
100
101 static int vlclua_convert_xml_special_chars( lua_State *L )
102 {
103     int i_top = lua_gettop( L );
104     int i;
105     for( i = 1; i <= i_top; i++ )
106     {
107         char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
108         lua_remove( L, 1 );
109         lua_pushstring( L, psz_string );
110         free( psz_string );
111     }
112     return i_top;
113 }
114
115 static int vlclua_iconv( lua_State *L )
116 {
117     int i_ret;
118     size_t i_out_bytes, i_in_bytes;
119     char *psz_output, *psz_original;
120
121     if( lua_gettop( L ) < 3 ) return vlclua_error( L );
122
123     const char *psz_input = luaL_checklstring( L, 3, &i_in_bytes );
124
125     if( i_in_bytes == 0 ) return vlclua_error( L );
126     vlc_iconv_t iconv_handle = vlc_iconv_open( luaL_checkstring(L, 1),
127                                              luaL_checkstring(L, 2) );
128
129     if( iconv_handle == (vlc_iconv_t)-1 )
130        return vlclua_error( L );
131     psz_output = psz_original = malloc( 4 * i_in_bytes );
132     i_out_bytes = 4 * i_in_bytes;
133     i_ret = vlc_iconv( iconv_handle, &psz_input ,
134                        &i_in_bytes, &psz_output, &i_out_bytes );
135     *psz_output = '\0';
136
137     lua_pushstring( L, psz_original );
138     vlc_iconv_close( iconv_handle );
139     free( psz_original );
140     return 1;
141 }
142
143 /*****************************************************************************
144  *
145  *****************************************************************************/
146 static const luaL_Reg vlclua_strings_reg[] = {
147     { "decode_uri", vlclua_decode_uri },
148     { "encode_uri_component", vlclua_encode_uri_component },
149     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
150     { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
151     { "iconv", vlclua_iconv },
152     { NULL, NULL }
153 };
154
155 void luaopen_strings( lua_State *L )
156 {
157     lua_newtable( L );
158     luaL_register( L, NULL, vlclua_strings_reg );
159     lua_setfield( L, -2, "strings" );
160 }