[vlc-commits] commit: lua: add vlc.strings.iconv to use vlc_iconv, shouldn't eat kittens (Ilkka Ollakka )

git at videolan.org git at videolan.org
Tue Jun 22 21:48:58 CEST 2010


vlc | branch: master | Ilkka Ollakka <ileoo at videolan.org> | Tue Jun 22 22:18:45 2010 +0300| [91e56a1ea8059552d6de1b0497939c8b43eca6e4] | committer: Ilkka Ollakka 

lua: add vlc.strings.iconv to use vlc_iconv, shouldn't eat kittens

Not sure if I remembered to do all the checks needed for errors, but
works for me ok.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=91e56a1ea8059552d6de1b0497939c8b43eca6e4
---

 modules/misc/lua/libs/strings.c |   29 +++++++++++++++++++++++++++++
 share/lua/README.txt            |    2 ++
 2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/modules/misc/lua/libs/strings.c b/modules/misc/lua/libs/strings.c
index 5511af1..a43e972 100644
--- a/modules/misc/lua/libs/strings.c
+++ b/modules/misc/lua/libs/strings.c
@@ -112,6 +112,34 @@ 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;
+}
+
 /*****************************************************************************
  *
  *****************************************************************************/
@@ -120,6 +148,7 @@ static const luaL_Reg vlclua_strings_reg[] = {
     { "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 }
 };
 
diff --git a/share/lua/README.txt b/share/lua/README.txt
index fd06b66..73dc2d5 100644
--- a/share/lua/README.txt
+++ b/share/lua/README.txt
@@ -359,6 +359,8 @@ strings.resolve_xml_special_chars( [str1, [str2, [...]]] ): Resolve XML
   variables as it had arguments.
 strings.convert_xml_special_chars( [str1, [str2, [...]]] ): Do the inverse
   operation.
+strings.iconv( str1 to, str2 from, str ): use vlc_iconv to convert string
+  from encoding to another
 
 Variables
 ---------



More information about the vlc-commits mailing list