MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / ImFileLoadToMemory

Function ImFileLoadToMemory

extern/imgui/imgui.cpp:1980–2017  ·  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

1978// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
1979// This can't really be used with "rt" because fseek size won't match read size.
1980void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes)
1981{
1982 IM_ASSERT(filename && mode);
1983 if (out_file_size)
1984 *out_file_size = 0;
1985
1986 ImFileHandle f;
1987 if ((f = ImFileOpen(filename, mode)) == NULL)
1988 return NULL;
1989
1990 size_t file_size = (size_t)ImFileGetSize(f);
1991 if (file_size == (size_t)-1)
1992 {
1993 ImFileClose(f);
1994 return NULL;
1995 }
1996
1997 void* file_data = IM_ALLOC(file_size + padding_bytes);
1998 if (file_data == NULL)
1999 {
2000 ImFileClose(f);
2001 return NULL;
2002 }
2003 if (ImFileRead(file_data, 1, file_size, f) != file_size)
2004 {
2005 ImFileClose(f);
2006 IM_FREE(file_data);
2007 return NULL;
2008 }
2009 if (padding_bytes > 0)
2010 memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes);
2011
2012 ImFileClose(f);
2013 if (out_file_size)
2014 *out_file_size = file_size;
2015
2016 return file_data;
2017}
2018
2019//-----------------------------------------------------------------------------
2020// [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