From 569b6cb4296cc28dd4e84d7e5799c5eea3dcc7f5 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 8 Jan 2011 21:07:04 +0100 Subject: [PATCH] [io] Convert filenames to right encoding --- src/io/unix.c | 4 ++++ src/io/win32.c | 21 ++++++++++++++++----- src/string/api.c | 6 ++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/io/unix.c b/src/io/unix.c index 72ee4dba4b..eee82c8a4a 100644 --- a/src/io/unix.c +++ b/src/io/unix.c @@ -161,6 +161,10 @@ Parrot_io_open_unix(PARROT_INTERP, ARGIN(STRING *path), INTVAL flags) PIOHANDLE fd; char *spath; + if (path->encoding != Parrot_ascii_encoding_ptr + && path->encoding != Parrot_utf8_encoding_ptr) + path = Parrot_utf8_encoding_ptr->to_encoding(interp, path); + oflags = convert_flags_to_unix(flags); spath = Parrot_str_to_cstring(interp, path); diff --git a/src/io/win32.c b/src/io/win32.c index e237c4cb44..34ec0c46b9 100644 --- a/src/io/win32.c +++ b/src/io/win32.c @@ -206,8 +206,9 @@ PIOHANDLE Parrot_io_open_win32(PARROT_INTERP, ARGIN(STRING *path), INTVAL flags) { ASSERT_ARGS(Parrot_io_open_win32) - DWORD fAcc, fShare, fCreat; - PIOHANDLE fd; + DWORD fAcc, fShare, fCreat; + PIOHANDLE fd; + char *spath; # if 0 if ((Interp_flags_TEST(interp, PARROT_DEBUG_FLAG)) != 0) { @@ -219,12 +220,22 @@ Parrot_io_open_win32(PARROT_INTERP, ARGIN(STRING *path), INTVAL flags) /* add ? and ! for block/non-block */ convert_flags_to_win32(flags, &fAcc, &fShare, &fCreat); - { /* enclosing scope for temporary C string */ - char * const spath = Parrot_str_to_cstring(interp, path); + if (path->encoding == Parrot_ascii_encoding_ptr) { + spath = Parrot_str_to_cstring(interp, path); fd = CreateFile(spath, fAcc, fShare, NULL, fCreat, FILE_ATTRIBUTE_NORMAL, NULL); - Parrot_str_free_cstring(spath); } + else { + if (path->encoding != Parrot_ucs2_encoding_ptr + && path->encoding != Parrot_utf16_encoding_ptr) + path = Parrot_utf16_encoding_ptr->to_encoding(interp, path); + + spath = Parrot_str_to_cstring(interp, path); + fd = CreateFileW((LPCWSTR)spath, fAcc, fShare, NULL, fCreat, + FILE_ATTRIBUTE_NORMAL, NULL); + } + + Parrot_str_free_cstring(spath); return fd; } diff --git a/src/string/api.c b/src/string/api.c index f964ec556b..b4be1b90a5 100644 --- a/src/string/api.c +++ b/src/string/api.c @@ -2092,9 +2092,11 @@ string_to_cstring_nullable(SHIM_INTERP, ARGIN_NULLOK(const STRING *s)) if (STRING_IS_NULL(s)) return NULL; else { - char * const p = (char*)mem_internal_allocate(s->bufused + 1); + char * const p = (char*)mem_internal_allocate(s->bufused + 2); memcpy(p, s->strstart, s->bufused); - p[s->bufused] = '\0'; + /* Two trailing NULs for wide char strings */ + p[s->bufused] = '\0'; + p[s->bufused+1] = '\0'; return p; } }