From 9fa2b5edbab76ad0cc849810759b1641fdee39c9 Mon Sep 17 00:00:00 2001 From: lvqcl Date: Sat, 18 Aug 2018 13:59:51 +0300 Subject: move CreateFile function outside of libFLAC --- src/flac/decode.c | 8 ++--- src/libFLAC/windows_unicode_filenames.c | 32 +------------------ src/share/grabbag/file.c | 54 +++++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/flac/decode.c b/src/flac/decode.c index de7773ec..a1c34721 100644 --- a/src/flac/decode.c +++ b/src/flac/decode.c @@ -263,11 +263,11 @@ FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__ void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred) { if(0 != d->fout && d->fout != stdout) { -#ifdef _WIN32 +#if defined _WIN32 && !defined __CYGWIN__ if(!error_occurred) { FLAC__off_t written_size = ftello(d->fout); if(written_size > 0) { - HANDLE fh = CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE fh = grabbag__CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(fh != INVALID_HANDLE_VALUE) { if(GetFileType(fh) == FILE_TYPE_DISK) { LARGE_INTEGER size; @@ -381,9 +381,9 @@ FLAC__bool DecoderSession_process(DecoderSession *d) } } -#ifdef _WIN32 +#if defined _WIN32 && !defined __CYGWIN__ if(!d->analysis_mode && !d->test_only && d->total_samples > 0 && d->fout != stdout) { - HANDLE fh = CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE fh = grabbag__CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(fh != INVALID_HANDLE_VALUE) { if (GetFileType(fh) == FILE_TYPE_DISK) { LARGE_INTEGER size; diff --git a/src/libFLAC/windows_unicode_filenames.c b/src/libFLAC/windows_unicode_filenames.c index 4c61558d..78550087 100644 --- a/src/libFLAC/windows_unicode_filenames.c +++ b/src/libFLAC/windows_unicode_filenames.c @@ -34,7 +34,7 @@ #endif #include -#include "share/compat.h" +#include #include "share/windows_unicode_filenames.h" /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */ @@ -183,33 +183,3 @@ int flac_internal_rename_utf8(const char *oldname, const char *newname) return ret; } } - -HANDLE WINAPI flac_internal_CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) -{ -#if _MSC_VER > 1900 && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) - wchar_t *wname; - HANDLE handle = INVALID_HANDLE_VALUE; - - if ((wname = wchar_from_utf8(lpFileName)) != NULL) { - - handle = CreateFile2(wname, dwDesiredAccess, dwShareMode, CREATE_ALWAYS, NULL); - free(wname); - - return handle; - } -#else - if (!utf8_filenames) { - return CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); - } else { - wchar_t *wname; - HANDLE handle = INVALID_HANDLE_VALUE; - - if ((wname = wchar_from_utf8(lpFileName)) != NULL) { - handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); - free(wname); - } - - return handle; - } -#endif -} diff --git a/src/share/grabbag/file.c b/src/share/grabbag/file.c index 3a972925..a7b2b62e 100644 --- a/src/share/grabbag/file.c +++ b/src/share/grabbag/file.c @@ -41,11 +41,6 @@ #include #include #include /* for strrchr() */ -#if defined _WIN32 && !defined __CYGWIN__ -// for GetFileInformationByHandle() etc -#include -#include -#endif #include "share/grabbag.h" @@ -116,9 +111,9 @@ FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only FLAC__bool grabbag__file_are_same(const char *f1, const char *f2) { -#if defined _MSC_VER || defined __MINGW32__ +#if defined _WIN32 && !defined __CYGWIN__ /* see - * http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0 + * http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp @@ -128,8 +123,8 @@ FLAC__bool grabbag__file_are_same(const char *f1, const char *f2) BY_HANDLE_FILE_INFORMATION info1, info2; HANDLE h1, h2; BOOL ok = 1; - h1 = CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - h2 = CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + h1 = grabbag__CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + h2 = grabbag__CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE) ok = 0; ok &= GetFileInformationByHandle(h1, &info1); @@ -185,3 +180,44 @@ FILE *grabbag__file_get_binary_stdout(void) return stdout; } + +#if defined _WIN32 && !defined __CYGWIN__ + +/* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */ +static wchar_t *wchar_from_utf8(const char *str) +{ + wchar_t *widestr; + int len; + + if (!str) + return NULL; + if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0) + return NULL; + if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL) + return NULL; + if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) { + free(widestr); + widestr = NULL; + } + + return widestr; +} + +HANDLE WINAPI grabbag__CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) +{ + if (!flac_internal_get_utf8_filenames()) { + return CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); + } else { + wchar_t *wname; + HANDLE handle = INVALID_HANDLE_VALUE; + + if ((wname = wchar_from_utf8(lpFileName)) != NULL) { + handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); + free(wname); + } + + return handle; + } +} + +#endif -- cgit v1.2.1