-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from myd7349/fopen-utf8
Patch edflib.c on Windows to enable opening UTF-8 encoded paths
- Loading branch information
Showing
5 changed files
with
101 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifdef _WIN32 | ||
|
||
#include "fopen_utf8.h" | ||
|
||
#include <stdlib.h> | ||
#include <windows.h> | ||
|
||
|
||
wchar_t* utf8_to_utf16(const char* str) | ||
{ | ||
wchar_t* wstr; | ||
|
||
int require = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); | ||
if (require == 0) | ||
return NULL; | ||
|
||
wstr = malloc(require * sizeof(wchar_t)); | ||
if (wstr == NULL) | ||
return NULL; | ||
|
||
if (MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, require) == 0) | ||
{ | ||
free(wstr); | ||
return NULL; | ||
} | ||
|
||
return wstr; | ||
} | ||
|
||
|
||
FILE* fopen_utf8(const char* filename, const char* mode) | ||
{ | ||
errno_t error; | ||
FILE* file; | ||
wchar_t* wfilename; | ||
wchar_t* wmode; | ||
|
||
wfilename = utf8_to_utf16(filename); | ||
if (wfilename == NULL) | ||
return NULL; | ||
|
||
wmode = utf8_to_utf16(mode); | ||
if (wmode == NULL) | ||
{ | ||
free(wfilename); | ||
return NULL; | ||
} | ||
|
||
error = _wfopen_s(&file, wfilename, wmode); | ||
|
||
free(wfilename); | ||
free(wmode); | ||
|
||
return error == 0 ? file : NULL; | ||
} | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef FOPEN_UTF8_H_ | ||
#define FOPEN_UTF8_H_ | ||
|
||
#include <stdio.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifdef _WIN32 | ||
FILE* fopen_utf8(const char* filename, const char* mode); | ||
#else | ||
#define fopen_utf8 fopen | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
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