Merge branch 'unicode-file-support' of https://github.com/jrsmith17/stb into temp

This commit is contained in:
Sean Barrett 2019-02-07 05:07:14 -08:00
commit ae773aa438
2 changed files with 60 additions and 0 deletions

View File

@ -1143,12 +1143,40 @@ static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, in
#ifndef STBI_NO_STDIO
char* stbi_convert_wchar_to_utf8(wchar_t* input) {
#ifdef _WINDOWS_
int outputSizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &input[0], wcslen(input), NULL, 0, NULL, NULL);
char* temp = (char*)STBI_MALLOC(outputSizeNeeded);
int error = WideCharToMultiByte(65001, 0, input, -1, temp, outputSizeNeeded, NULL, NULL);
temp[outputSizeNeeded] = '\0';
return temp;
#else
return NULL;
#endif
}
static FILE *stbi__fopen(char const *filename, char const *mode)
{
FILE *f;
#if defined(_MSC_VER) && _MSC_VER >= 1400
#ifdef UNICODE
int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
wchar_t* wFilename = (wchar_t*)stbi__malloc(filenameLength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength);
int modeLength = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
wchar_t* wMode = (wchar_t*)stbi__malloc(modeLength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wMode, modeLength);
if (0 != _wfopen_s(&f, wFilename, wMode))
f = 0;
STBI_FREE(wFilename);
STBI_FREE(wMode);
#else
if (0 != fopen_s(&f, filename, mode))
f=0;
#endif
#else
f = fopen(filename, mode);
#endif

View File

@ -279,8 +279,17 @@ static int stbi__start_write_file(stbi__write_context *s, const char *filename)
{
FILE *f;
#ifdef STBI_MSC_SECURE_CRT
#ifdef UNICODE
int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
wchar_t* wFilename = (wchar_t*)STBIW_MALLOC(filenameLength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength);
if (0 != _wfopen_s(&f, wFilename, L"wb"))
f = NULL;
#else
if (fopen_s(&f, filename, "wb"))
f = NULL;
#endif
#else
f = fopen(filename, "wb");
#endif
@ -1112,8 +1121,19 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);
if (png == NULL) return 0;
#ifdef STBI_MSC_SECURE_CRT
#ifdef UNICODE
int filenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
wchar_t* wFilename = (wchar_t*)STBIW_MALLOC(filenameLength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wFilename, filenameLength);
if (0 != _wfopen_s(&f, wFilename, L"wb"))
f = NULL;
STBIW_FREE(wFilename);
#else
if (fopen_s(&f, filename, "wb"))
f = NULL;
#endif
#else
f = fopen(filename, "wb");
#endif
@ -1125,6 +1145,18 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
}
#endif
char* stbiw_convert_wchar_to_utf8(wchar_t* input) {
#ifdef _WINDOWS_
int outputSizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &input[0], wcslen(input), NULL, 0, NULL, NULL);
char* temp = (char*)STBIW_MALLOC(outputSizeNeeded);
int error = WideCharToMultiByte(65001, 0, input, -1, temp, outputSizeNeeded, NULL, NULL);
temp[outputSizeNeeded] = '\0';
return temp;
#else
return NULL;
#endif
}
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes)
{
int len;