MCPcopy Create free account
hub / github.com/TheForceEngine/TheForceEngine / ImFileLoadToMemory

Function ImFileLoadToMemory

TheForceEngine/TFE_Ui/imGUI/imgui.cpp:2206–2243  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

2204// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
2205// This can't really be used with "rt" because fseek size won't match read size.
2206void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes)
2207{
2208 IM_ASSERT(filename && mode);
2209 if (out_file_size)
2210 *out_file_size = 0;
2211
2212 ImFileHandle f;
2213 if ((f = ImFileOpen(filename, mode)) == NULL)
2214 return NULL;
2215
2216 size_t file_size = (size_t)ImFileGetSize(f);
2217 if (file_size == (size_t)-1)
2218 {
2219 ImFileClose(f);
2220 return NULL;
2221 }
2222
2223 void* file_data = IM_ALLOC(file_size + padding_bytes);
2224 if (file_data == NULL)
2225 {
2226 ImFileClose(f);
2227 return NULL;
2228 }
2229 if (ImFileRead(file_data, 1, file_size, f) != file_size)
2230 {
2231 ImFileClose(f);
2232 IM_FREE(file_data);
2233 return NULL;
2234 }
2235 if (padding_bytes > 0)
2236 memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes);
2237
2238 ImFileClose(f);
2239 if (out_file_size)
2240 *out_file_size = file_size;
2241
2242 return file_data;
2243}
2244
2245//-----------------------------------------------------------------------------
2246// [SECTION] MISC HELPERS/UTILITIES (ImText* functions)

Callers 2

AddFontFromFileTTFMethod · 0.85

Calls 4

ImFileOpenFunction · 0.70
ImFileGetSizeFunction · 0.70
ImFileCloseFunction · 0.70
ImFileReadFunction · 0.70

Tested by

no test coverage detected