| 2170 | #ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS |
| 2171 | |
| 2172 | ImFileHandle ImFileOpen(const char* filename, const char* mode) |
| 2173 | { |
| 2174 | #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(__CYGWIN__) && !defined(__GNUC__) |
| 2175 | // We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames. |
| 2176 | // Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32! |
| 2177 | const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); |
| 2178 | const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); |
| 2179 | |
| 2180 | // Use stack buffer if possible, otherwise heap buffer. Sizes include zero terminator. |
| 2181 | // We don't rely on current ImGuiContext as this is implied to be a helper function which doesn't depend on it (see #7314). |
| 2182 | wchar_t local_temp_stack[FILENAME_MAX]; |
| 2183 | ImVector<wchar_t> local_temp_heap; |
| 2184 | if (filename_wsize + mode_wsize > IM_ARRAYSIZE(local_temp_stack)) |
| 2185 | local_temp_heap.resize(filename_wsize + mode_wsize); |
| 2186 | wchar_t* filename_wbuf = local_temp_heap.Data ? local_temp_heap.Data : local_temp_stack; |
| 2187 | wchar_t* mode_wbuf = filename_wbuf + filename_wsize; |
| 2188 | ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, filename_wbuf, filename_wsize); |
| 2189 | ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, mode_wbuf, mode_wsize); |
| 2190 | return ::_wfopen(filename_wbuf, mode_wbuf); |
| 2191 | #else |
| 2192 | return fopen(filename, mode); |
| 2193 | #endif |
| 2194 | } |
| 2195 | |
| 2196 | // We should in theory be using fseeko()/ftello() with off_t and _fseeki64()/_ftelli64() with __int64, waiting for the PR that does that in a very portable pre-C++11 zero-warnings way. |
| 2197 | bool ImFileClose(ImFileHandle f) { return fclose(f) == 0; } |
no test coverage detected