From 3ed14183157702d9a155a2b60ee2c05dbfb492c5 Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Thu, 1 May 2014 17:45:12 +0000 Subject: [PATCH] ICU-10038 Add u_fopen variant that accepts UTF16 filename X-SVN-Rev: 35681 --- icu4c/source/common/unicode/urename.h | 2 ++ icu4c/source/io/ufile.c | 32 ++++++++++++++++++++++++++- icu4c/source/io/unicode/ustdio.h | 23 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/icu4c/source/common/unicode/urename.h b/icu4c/source/common/unicode/urename.h index 4a79d83647d..6dd3b9e7d5c 100644 --- a/icu4c/source/common/unicode/urename.h +++ b/icu4c/source/common/unicode/urename.h @@ -239,6 +239,7 @@ #define u_flushDefaultConverter U_ICU_ENTRY_POINT_RENAME(u_flushDefaultConverter) #define u_foldCase U_ICU_ENTRY_POINT_RENAME(u_foldCase) #define u_fopen U_ICU_ENTRY_POINT_RENAME(u_fopen) +#define u_fopen_u U_ICU_ENTRY_POINT_RENAME(u_fopen_u) #define u_forDigit U_ICU_ENTRY_POINT_RENAME(u_forDigit) #define u_formatMessage U_ICU_ENTRY_POINT_RENAME(u_formatMessage) #define u_formatMessageWithError U_ICU_ENTRY_POINT_RENAME(u_formatMessageWithError) @@ -413,6 +414,7 @@ #define u_vsprintf_u U_ICU_ENTRY_POINT_RENAME(u_vsprintf_u) #define u_vsscanf U_ICU_ENTRY_POINT_RENAME(u_vsscanf) #define u_vsscanf_u U_ICU_ENTRY_POINT_RENAME(u_vsscanf_u) +#define u_wfopen U_ICU_ENTRY_POINT_RENAME(u_wfopen) #define u_writeIdenticalLevelRun U_ICU_ENTRY_POINT_RENAME(u_writeIdenticalLevelRun) #define ubidi_addPropertyStarts U_ICU_ENTRY_POINT_RENAME(ubidi_addPropertyStarts) #define ubidi_close U_ICU_ENTRY_POINT_RENAME(ubidi_close) diff --git a/icu4c/source/io/ufile.c b/icu4c/source/io/ufile.c index 820071f1bcf..11a235e27fa 100644 --- a/icu4c/source/io/ufile.c +++ b/icu4c/source/io/ufile.c @@ -1,7 +1,7 @@ /* ****************************************************************************** * -* Copyright (C) 1998-2013, International Business Machines +* Copyright (C) 1998-2014, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** @@ -31,6 +31,7 @@ #include "unicode/uloc.h" #include "unicode/ures.h" #include "unicode/ucnv.h" +#include "unicode/ustring.h" #include "cstring.h" #include "cmemory.h" @@ -147,6 +148,35 @@ u_fopen(const char *filename, return result; /* not a file leak */ } +U_CAPI UFILE* U_EXPORT2 +u_fopen_u(const UChar *filename, + const char *perm, + const char *locale, + const char *codepage) +{ + UFILE *result; + char buffer[256]; + + u_austrcpy(buffer, filename); + + result = u_fopen(buffer, perm, locale, codepage); +#if U_PLATFORM_USES_ONLY_WIN32_API + /* Try Windows API _wfopen if the above fails. */ + if (!result) { + FILE *systemFile = _wfopen(filename, (UChar*)perm); + if (systemFile) { + result = finit_owner(systemFile, locale, codepage, TRUE); + } + if (!result) { + /* Something bad happened. + Maybe the converter couldn't be opened. */ + fclose(systemFile); + } + } +#endif + return result; /* not a file leak */ +} + U_CAPI UFILE* U_EXPORT2 u_fstropen(UChar *stringBuf, int32_t capacity, diff --git a/icu4c/source/io/unicode/ustdio.h b/icu4c/source/io/unicode/ustdio.h index 8fed38df785..b98ae6a74ce 100644 --- a/icu4c/source/io/unicode/ustdio.h +++ b/icu4c/source/io/unicode/ustdio.h @@ -241,6 +241,29 @@ u_fopen(const char *filename, const char *locale, const char *codepage); +/** + * Open a UFILE with a UChar* filename + * A UFILE is a wrapper around a FILE* that is locale and codepage aware. + * That is, data written to a UFILE will be formatted using the conventions + * specified by that UFILE's Locale; this data will be in the character set + * specified by that UFILE's codepage. + * @param filename The name of the file to open. + * @param perm The read/write permission for the UFILE; one of "r", "w", "rw" + * @param locale The locale whose conventions will be used to format + * and parse output. If this parameter is NULL, the default locale will + * be used. + * @param codepage The codepage in which data will be written to and + * read from the file. If this paramter is NULL the system default codepage + * will be used. + * @return A new UFILE, or NULL if an error occurred. + * @draft ICU 54 + */ +U_DRAFT UFILE* U_EXPORT2 +u_fopen_u(const UChar *filename, + const char *perm, + const char *locale, + const char *codepage); + /** * Open a UFILE on top of an existing FILE* stream. The FILE* stream * ownership remains with the caller. To have the UFILE take over