MCPcopy Create free account
hub / github.com/VictorGordan/opengl-tutorials / ImFileLoadToMemory

Function ImFileLoadToMemory

ImGUI GLFW Tutorial/imgui/imgui.cpp:1570–1607  ·  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

1568// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
1569// This can't really be used with "rt" because fseek size won't match read size.
1570void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes)
1571{
1572 IM_ASSERT(filename && mode);
1573 if (out_file_size)
1574 *out_file_size = 0;
1575
1576 ImFileHandle f;
1577 if ((f = ImFileOpen(filename, mode)) == NULL)
1578 return NULL;
1579
1580 size_t file_size = (size_t)ImFileGetSize(f);
1581 if (file_size == (size_t)-1)
1582 {
1583 ImFileClose(f);
1584 return NULL;
1585 }
1586
1587 void* file_data = IM_ALLOC(file_size + padding_bytes);
1588 if (file_data == NULL)
1589 {
1590 ImFileClose(f);
1591 return NULL;
1592 }
1593 if (ImFileRead(file_data, 1, file_size, f) != file_size)
1594 {
1595 ImFileClose(f);
1596 IM_FREE(file_data);
1597 return NULL;
1598 }
1599 if (padding_bytes > 0)
1600 memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes);
1601
1602 ImFileClose(f);
1603 if (out_file_size)
1604 *out_file_size = file_size;
1605
1606 return file_data;
1607}
1608
1609//-----------------------------------------------------------------------------
1610// [SECTION] MISC HELPERS/UTILITIES (ImText* functions)

Callers 2

AddFontFromFileTTFMethod · 0.70

Calls 4

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

Tested by

no test coverage detected