MCPcopy Create free account
hub / github.com/KebsCS/KBotExt / ImFileLoadToMemory

Function ImFileLoadToMemory

KBotExt/imgui/imgui.cpp:2043–2080  ·  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

2041// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
2042// This can't really be used with "rt" because fseek size won't match read size.
2043void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes)
2044{
2045 IM_ASSERT(filename && mode);
2046 if (out_file_size)
2047 *out_file_size = 0;
2048
2049 ImFileHandle f;
2050 if ((f = ImFileOpen(filename, mode)) == NULL)
2051 return NULL;
2052
2053 size_t file_size = (size_t)ImFileGetSize(f);
2054 if (file_size == (size_t)-1)
2055 {
2056 ImFileClose(f);
2057 return NULL;
2058 }
2059
2060 void* file_data = IM_ALLOC(file_size + padding_bytes);
2061 if (file_data == NULL)
2062 {
2063 ImFileClose(f);
2064 return NULL;
2065 }
2066 if (ImFileRead(file_data, 1, file_size, f) != file_size)
2067 {
2068 ImFileClose(f);
2069 IM_FREE(file_data);
2070 return NULL;
2071 }
2072 if (padding_bytes > 0)
2073 memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes);
2074
2075 ImFileClose(f);
2076 if (out_file_size)
2077 *out_file_size = file_size;
2078
2079 return file_data;
2080}
2081
2082//-----------------------------------------------------------------------------
2083// [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