Created
May 14, 2013 20:44
-
-
Save exebetche/5579376 to your computer and use it in GitHub Desktop.
Test for utf-8/utf-16 encoded path support in lua for windows:
vlc/vlc/modules/lua/libs/strings.c -> add vlclua_io_open & vlclua_io_close functions
vlc/share/lua/extensions/utf16_win_test.lua -> lua test file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************************************************** | |
* strings.c | |
***************************************************************************** | |
* Copyright (C) 2007-2008 the VideoLAN team | |
* $Id: 715ad00aa2a4124ec4762c15262758b16bfc5d23 $ | |
* | |
* Authors: Antoine Cellerier <dionoea at videolan tod org> | |
* Pierre d'Herbemont <pdherbemont # videolan.org> | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. | |
*****************************************************************************/ | |
/***************************************************************************** | |
* Preamble | |
*****************************************************************************/ | |
#ifndef _GNU_SOURCE | |
# define _GNU_SOURCE | |
#endif | |
#ifdef HAVE_CONFIG_H | |
# include "config.h" | |
#endif | |
#include <vlc_common.h> | |
#include <vlc_plugin.h> | |
#include <vlc_meta.h> | |
#include <vlc_charset.h> | |
#include "../vlc.h" | |
#include "../libs.h" | |
/***************************************************************************** | |
* String transformations | |
*****************************************************************************/ | |
static int vlclua_decode_uri( 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 = strdup( psz_cstring ); | |
lua_remove( L, 1 ); /* remove elements to prevent being limited by | |
* the stack's size (this function will work with | |
* up to (stack size - 1) arguments */ | |
decode_URI( psz_string ); | |
lua_pushstring( L, psz_string ); | |
free( psz_string ); | |
} | |
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_make_uri( lua_State *L ) | |
{ | |
const char *psz_input = luaL_checkstring( L, 1 ); | |
const char *psz_scheme = luaL_optstring( L, 2, NULL ); | |
if( strstr( psz_input, "://" ) == NULL ) | |
{ | |
char *psz_uri = vlc_path2uri( psz_input, psz_scheme ); | |
lua_pushstring( L, psz_uri ); | |
free( psz_uri ); | |
} | |
else | |
lua_pushstring( L, psz_input ); | |
return 1; | |
} | |
static int vlclua_make_path( lua_State *L ) | |
{ | |
const char *psz_input = luaL_checkstring( L, 1 ); | |
char *psz_path = make_path( psz_input); | |
lua_pushstring( L, psz_path ); | |
free( psz_path ); | |
return 1; | |
} | |
static int vlclua_resolve_xml_special_chars( 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 = strdup( psz_cstring ); | |
lua_remove( L, 1 ); /* remove elements to prevent being limited by | |
* the stack's size (this function will work with | |
* up to (stack size - 1) arguments */ | |
resolve_xml_special_chars( psz_string ); | |
lua_pushstring( L, psz_string ); | |
free( psz_string ); | |
} | |
return i_top; | |
} | |
static int vlclua_convert_xml_special_chars( lua_State *L ) | |
{ | |
int i_top = lua_gettop( L ); | |
int i; | |
for( i = 1; i <= i_top; i++ ) | |
{ | |
char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) ); | |
lua_remove( L, 1 ); | |
lua_pushstring( L, psz_string ); | |
free( psz_string ); | |
} | |
return i_top; | |
} | |
static int vlclua_from_charset( lua_State *L ) | |
{ | |
if( lua_gettop( L ) < 2 ) return vlclua_error( L ); | |
size_t i_in_bytes; | |
const char *psz_input = luaL_checklstring( L, 2, &i_in_bytes ); | |
if( i_in_bytes == 0 ) return vlclua_error( L ); | |
const char *psz_charset = luaL_checkstring( L, 1 ); | |
char *psz_output = FromCharset( psz_charset, psz_input, i_in_bytes ); | |
lua_pushstring( L, psz_output ? psz_output : "" ); | |
free( psz_output ); | |
return 1; | |
} | |
#define vlclua_tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) | |
/* | |
** When creating file handles, always creates a `closed' file handle | |
** before opening the actual file; so, if there is a memory error, the | |
** file is not left opened. | |
*/ | |
static FILE **vlclua_newfile (lua_State *L) { | |
FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); | |
*pf = NULL; /* file handle is currently `closed' */ | |
luaL_getmetatable(L, LUA_FILEHANDLE); | |
lua_setmetatable(L, -2); | |
return pf; | |
} | |
static int vlclua_pushresult (lua_State *L, int i, const char *filename) { | |
int en = errno; /* calls to Lua API may change this value */ | |
if (i) { | |
lua_pushboolean(L, 1); | |
return 1; | |
} | |
else { | |
lua_pushnil(L); | |
if (filename) | |
lua_pushfstring(L, "%s: %s", filename, strerror(en)); | |
else | |
lua_pushfstring(L, "%s", strerror(en)); | |
lua_pushinteger(L, en); | |
return 3; | |
} | |
} | |
#ifdef WIN32 | |
static int vlclua_io_open (lua_State *L) { | |
const char *filename = luaL_checkstring(L, 1); | |
const char *mode = luaL_optstring(L, 2, "r"); | |
FILE **pf = vlclua_newfile(L); | |
wchar_t *wfilename = ToWide( filename); | |
wchar_t *wmode = ToWide( mode); | |
*pf = _wfopen(wfilename, wmode); | |
return (*pf == NULL) ? vlclua_pushresult(L, 0, filename) : 1; | |
} | |
#else | |
static int vlclua_io_open (lua_State *L) { | |
const char *filename = luaL_checkstring(L, 1); | |
const char *mode = luaL_optstring(L, 2, "r"); | |
FILE **pf = vlclua_newfile(L); | |
*pf = fopen(filename, mode); | |
return (*pf == NULL) ? vlclua_pushresult(L, 0, filename) : 1; | |
} | |
#endif | |
/* | |
** function to close regular files | |
*/ | |
static int vlclua_io_close (lua_State *L) { | |
FILE **p = vlclua_tofilep(L); | |
int ok = (fclose(*p) == 0); | |
*p = NULL; | |
return vlclua_pushresult(L, ok, NULL); | |
} | |
/***************************************************************************** | |
* | |
*****************************************************************************/ | |
static const luaL_Reg vlclua_strings_reg[] = { | |
{ "decode_uri", vlclua_decode_uri }, | |
{ "encode_uri_component", vlclua_encode_uri_component }, | |
{ "make_uri", vlclua_make_uri }, | |
{ "make_path", vlclua_make_path }, | |
{ "resolve_xml_special_chars", vlclua_resolve_xml_special_chars }, | |
{ "convert_xml_special_chars", vlclua_convert_xml_special_chars }, | |
{ "from_charset", vlclua_from_charset }, | |
{ "io_open", vlclua_io_open }, | |
{ "io_close", vlclua_io_close }, | |
{ NULL, NULL } | |
}; | |
void luaopen_strings( lua_State *L ) | |
{ | |
lua_newtable( L ); | |
luaL_register( L, NULL, vlclua_strings_reg ); | |
lua_setfield( L, -2, "strings" ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- /!\ The file should be utf-8 encoded (without BOM) otherwise use : | |
-- vlc.strings.from_charset(local_charset, path) | |
function descriptor() | |
return { | |
title = "Test utf-16", | |
capabilities = {} | |
} | |
end | |
function activate() | |
local path = vlc.config.homedir().."\\ザ.txt" | |
vlc.msg.dbg(path) | |
local file = vlc.strings.io_open(path, "wb") | |
if file then | |
file:write("ça a marché!") | |
vlc.strings.io_close(file) | |
local file2 = vlc.strings.io_open(path, "rb") | |
local str = file2:read("*a") | |
vlc.strings.io_close(file2) | |
vlc.msg.dbg(str) | |
else | |
vlc.msg.err("Pas bon.") | |
end | |
end | |
function deactivate() | |
vlc.msg.dbg("[VLsub] End") | |
end | |
function close() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment