Move CreateFile_utf8 function to a more logical place
authorlvqcl <lvqcl.mail@gmail.com>
Sat, 25 Aug 2018 07:23:39 +0000 (10:23 +0300)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sat, 25 Aug 2018 08:55:30 +0000 (18:55 +1000)
FLAC-vs2005.sln
include/share/grabbag/file.h
include/share/win_utf8_io.h
src/flac/decode.c
src/share/grabbag/file.c
src/share/grabbag/grabbag_static.vcxproj
src/share/win_utf8_io/win_utf8_io.c

index 24fbacd..c35b07c 100644 (file)
@@ -55,6 +55,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "grabbag_static", "src\share
        ProjectSection(ProjectDependencies) = postProject\r
                {4CEFBC84-C215-11DB-8314-0800200C9A66} = {4CEFBC84-C215-11DB-8314-0800200C9A66}\r
                {4CEFBC89-C215-11DB-8314-0800200C9A66} = {4CEFBC89-C215-11DB-8314-0800200C9A66}\r
+               {4CEFBE02-C215-11DB-8314-0800200C9A66} = {4CEFBE02-C215-11DB-8314-0800200C9A66}\r
        EndProjectSection\r
 EndProject\r
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libFLAC_dynamic", "src\libFLAC\libFLAC_dynamic.vcproj", "{4CEFBC83-C215-11DB-8314-0800200C9A66}"\r
index 7bc851a..b3f4148 100644 (file)
@@ -58,11 +58,6 @@ FLAC__bool grabbag__file_remove_file(const char *filename);
 FILE *grabbag__file_get_binary_stdin(void);
 FILE *grabbag__file_get_binary_stdout(void);
 
-#if defined _WIN32 && !defined __CYGWIN__
-#include <windows.h>
-HANDLE WINAPI grabbag__CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
-#endif
-
 #ifdef __cplusplus
 }
 #endif
index 13fd118..6031b89 100644 (file)
@@ -50,6 +50,9 @@ int printf_utf8(const char *format, ...);
 int fprintf_utf8(FILE *stream, const char *format, ...);
 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr);
 
+#include <windows.h>
+HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index a1c3472..c26d3f6 100644 (file)
@@ -267,7 +267,7 @@ void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
                if(!error_occurred) {
                        FLAC__off_t written_size = ftello(d->fout);
                        if(written_size > 0) {
-                               HANDLE fh = grabbag__CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+                               HANDLE fh = 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;
@@ -383,7 +383,7 @@ FLAC__bool DecoderSession_process(DecoderSession *d)
 
 #if defined _WIN32 && !defined __CYGWIN__
        if(!d->analysis_mode && !d->test_only && d->total_samples > 0 && d->fout != stdout) {
-               HANDLE fh = grabbag__CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+               HANDLE fh = 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;
index a7b2b62..2c67beb 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h> /* for strrchr() */
+#if defined _WIN32 && !defined __CYGWIN__
+// for GetFileInformationByHandle() etc
+#include <windows.h>
+#include <winbase.h>
+#endif
 #include "share/grabbag.h"
+#include "share/compat.h"
 
 
 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
@@ -123,8 +129,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 = 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);
+       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);
        if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
                ok = 0;
        ok &= GetFileInformationByHandle(h1, &info1);
@@ -180,44 +186,3 @@ 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
index 8e7b6fd..8ae6a28 100644 (file)
       <Project>{4cefbc89-c215-11db-8314-0800200c9a66}</Project>\r
       <ReferenceOutputAssembly>false</ReferenceOutputAssembly>\r
     </ProjectReference>\r
+    <ProjectReference Include="..\win_utf8_io\win_utf8_io_static.vcxproj">\r
+      <Project>{4cefbe02-c215-11db-8314-0800200c9a66}</Project>\r
+      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>\r
+    </ProjectReference>\r
   </ItemGroup>\r
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\r
   <ImportGroup Label="ExtensionTargets">\r
index bb07ccd..bbb6a74 100644 (file)
@@ -149,6 +149,24 @@ int get_utf8_argv(int *argc, char ***argv)
        return ret;
 }
 
+/* similar to CreateFileW but accepts UTF-8 encoded lpFileName */
+HANDLE WINAPI 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;
+       }
+}
+
 /* return number of characters in the UTF-8 string */
 size_t strlen_utf8(const char *str)
 {