MCPcopy Create free account
hub / github.com/Wemino/EchoPatch / ImFileLoadToMemory

Function ImFileLoadToMemory

include/imgui/imgui.cpp:2533–2570  ·  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

2531// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
2532// This can't really be used with "rt" because fseek size won't match read size.
2533void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes)
2534{
2535 IM_ASSERT(filename && mode);
2536 if (out_file_size)
2537 *out_file_size = 0;
2538
2539 ImFileHandle f;
2540 if ((f = ImFileOpen(filename, mode)) == NULL)
2541 return NULL;
2542
2543 size_t file_size = (size_t)ImFileGetSize(f);
2544 if (file_size == (size_t)-1)
2545 {
2546 ImFileClose(f);
2547 return NULL;
2548 }
2549
2550 void* file_data = IM_ALLOC(file_size + padding_bytes);
2551 if (file_data == NULL)
2552 {
2553 ImFileClose(f);
2554 return NULL;
2555 }
2556 if (ImFileRead(file_data, 1, file_size, f) != file_size)
2557 {
2558 ImFileClose(f);
2559 IM_FREE(file_data);
2560 return NULL;
2561 }
2562 if (padding_bytes > 0)
2563 memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes);
2564
2565 ImFileClose(f);
2566 if (out_file_size)
2567 *out_file_size = file_size;
2568
2569 return file_data;
2570}
2571
2572//-----------------------------------------------------------------------------
2573// [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