]> git.sesse.net Git - vlc/blobdiff - 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
index f1a1f3d4077379216bcee365b6850de7e8cbbb3e..a43e972a5112b3f15e5218b8e2384d079507cec1 100644 (file)
@@ -36,7 +36,6 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_meta.h>
-#include <vlc_charset.h>
 #include <vlc_aout.h>
 
 #include <lua.h>        /* Low level lua C API */
@@ -66,6 +65,21 @@ static int vlclua_decode_uri( lua_State *L )
     return i_top;
 }
 
+static int vlclua_encode_uri_component( lua_State *L )
+{
+    int i_top = lua_gettop( L );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+    {
+        const char *psz_cstring = luaL_checkstring( L, 1 );
+        char *psz_string = encode_URI_component( psz_cstring );
+        lua_remove( L,1 );
+        lua_pushstring( L, psz_string );
+        free( psz_string );
+    }
+    return i_top;
+}
+
 static int vlclua_resolve_xml_special_chars( lua_State *L )
 {
     int i_top = lua_gettop( L );
@@ -98,13 +112,43 @@ static int vlclua_convert_xml_special_chars( lua_State *L )
     return i_top;
 }
 
+static int vlclua_iconv( lua_State *L )
+{
+    int i_ret;
+    size_t i_out_bytes, i_in_bytes;
+    char *psz_output, *psz_original;
+
+    if( lua_gettop( L ) < 3 ) return vlclua_error( L );
+
+    const char *psz_input = luaL_checklstring( L, 3, &i_in_bytes );
+
+    if( i_in_bytes == 0 ) return vlclua_error( L );
+    vlc_iconv_t iconv_handle = vlc_iconv_open( luaL_checkstring(L, 1),
+                                             luaL_checkstring(L, 2) );
+
+    if( iconv_handle == (vlc_iconv_t)-1 )
+       return vlclua_error( L );
+    psz_output = psz_original = malloc( 4 * i_in_bytes );
+    i_out_bytes = 4 * i_in_bytes;
+    i_ret = vlc_iconv( iconv_handle, &psz_input ,
+                       &i_in_bytes, &psz_output, &i_out_bytes );
+    *psz_output = '\0';
+
+    lua_pushstring( L, psz_original );
+    vlc_iconv_close( iconv_handle );
+    free( psz_original );
+    return 1;
+}
+
 /*****************************************************************************
  *
  *****************************************************************************/
 static const luaL_Reg vlclua_strings_reg[] = {
     { "decode_uri", vlclua_decode_uri },
+    { "encode_uri_component", vlclua_encode_uri_component },
     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
     { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
+    { "iconv", vlclua_iconv },
     { NULL, NULL }
 };