|
@@ -2063,12 +2063,18 @@ ImFileHandle ImFileOpen(const char* filename, const char* mode)
|
|
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
|
|
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
|
|
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
|
|
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
|
|
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
|
|
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
|
|
- ImGuiContext& g = *GImGui;
|
|
|
|
- g.TempBuffer.reserve((filename_wsize + mode_wsize) * sizeof(wchar_t));
|
|
|
|
- wchar_t* buf = (wchar_t*)(void*)g.TempBuffer.Data;
|
|
|
|
- ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize);
|
|
|
|
- ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize);
|
|
|
|
- return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]);
|
|
|
|
|
|
+
|
|
|
|
+ // Use stack buffer if possible, otherwise heap buffer. Sizes include zero terminator.
|
|
|
|
+ // We don't rely on current ImGuiContext as this is implied to be a helper function which doesn't depend on it (see #7314).
|
|
|
|
+ wchar_t local_temp_stack[FILENAME_MAX];
|
|
|
|
+ ImVector<wchar_t> local_temp_heap;
|
|
|
|
+ if (filename_wsize + mode_wsize > IM_ARRAYSIZE(local_temp_stack))
|
|
|
|
+ local_temp_heap.resize(filename_wsize + mode_wsize);
|
|
|
|
+ wchar_t* filename_wbuf = local_temp_heap.Data ? local_temp_heap.Data : local_temp_stack;
|
|
|
|
+ wchar_t* mode_wbuf = filename_wbuf + filename_wsize;
|
|
|
|
+ ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, filename_wbuf, filename_wsize);
|
|
|
|
+ ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, mode_wbuf, mode_wsize);
|
|
|
|
+ return ::_wfopen(filename_wbuf, mode_wbuf);
|
|
#else
|
|
#else
|
|
return fopen(filename, mode);
|
|
return fopen(filename, mode);
|
|
#endif
|
|
#endif
|