Helper: Load file content into memory Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() This can't really be used with "rt" because fseek size won't match read size.
| 2079 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 2080 | // This can't really be used with "rt" because fseek size won't match read size. |
| 2081 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 2082 | { |
| 2083 | IM_ASSERT(filename && mode); |
| 2084 | if (out_file_size) |
| 2085 | *out_file_size = 0; |
| 2086 | |
| 2087 | ImFileHandle f; |
| 2088 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 2089 | return NULL; |
| 2090 | |
| 2091 | size_t file_size = (size_t)ImFileGetSize(f); |
| 2092 | if (file_size == (size_t)-1) |
| 2093 | { |
| 2094 | ImFileClose(f); |
| 2095 | return NULL; |
| 2096 | } |
| 2097 | |
| 2098 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 2099 | if (file_data == NULL) |
| 2100 | { |
| 2101 | ImFileClose(f); |
| 2102 | return NULL; |
| 2103 | } |
| 2104 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 2105 | { |
| 2106 | ImFileClose(f); |
| 2107 | IM_FREE(file_data); |
| 2108 | return NULL; |
| 2109 | } |
| 2110 | if (padding_bytes > 0) |
| 2111 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 2112 | |
| 2113 | ImFileClose(f); |
| 2114 | if (out_file_size) |
| 2115 | *out_file_size = file_size; |
| 2116 | |
| 2117 | return file_data; |
| 2118 | } |
| 2119 | |
| 2120 | //----------------------------------------------------------------------------- |
| 2121 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected