Internal ImLoadFileToMemory() return void* instead of taking void** + allow optional int* file_size

This commit is contained in:
ocornut 2015-09-11 16:01:54 +01:00
parent 4d83519244
commit 7b1168eb30
3 changed files with 14 additions and 15 deletions

View File

@ -1122,21 +1122,21 @@ void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float&
// Load file content into memory // Load file content into memory
// Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() // Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
bool ImLoadFileToMemory(const char* filename, const char* file_open_mode, void** out_file_data, int* out_file_size, int padding_bytes) void* ImLoadFileToMemory(const char* filename, const char* file_open_mode, int* out_file_size, int padding_bytes)
{ {
IM_ASSERT(filename && file_open_mode && out_file_data && out_file_size); IM_ASSERT(filename && file_open_mode);
*out_file_data = NULL; if (out_file_size)
*out_file_size = 0; *out_file_size = 0;
FILE* f; FILE* f;
if ((f = fopen(filename, file_open_mode)) == NULL) if ((f = fopen(filename, file_open_mode)) == NULL)
return false; return NULL;
long file_size_signed; long file_size_signed;
if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET))
{ {
fclose(f); fclose(f);
return false; return NULL;
} }
int file_size = (int)file_size_signed; int file_size = (int)file_size_signed;
@ -1144,23 +1144,22 @@ bool ImLoadFileToMemory(const char* filename, const char* file_open_mode, void**
if (file_data == NULL) if (file_data == NULL)
{ {
fclose(f); fclose(f);
return false; return NULL;
} }
if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size) if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size)
{ {
fclose(f); fclose(f);
ImGui::MemFree(file_data); ImGui::MemFree(file_data);
return false; return NULL;
} }
if (padding_bytes > 0) if (padding_bytes > 0)
memset((void *)(((char*)file_data) + file_size), 0, padding_bytes); memset((void *)(((char*)file_data) + file_size), 0, padding_bytes);
fclose(f); fclose(f);
*out_file_data = file_data;
if (out_file_size) if (out_file_size)
*out_file_size = file_size; *out_file_size = file_size;
return true; return file_data;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -2084,9 +2083,9 @@ static void LoadSettings()
if (!filename) if (!filename)
return; return;
char* file_data;
int file_size; int file_size;
if (!ImLoadFileToMemory(filename, "rb", (void**)&file_data, &file_size, 1)) char* file_data = (char*)ImLoadFileToMemory(filename, "rb", &file_size, 1);
if (!file_data)
return; return;
ImGuiIniData* settings = NULL; ImGuiIniData* settings = NULL;

View File

@ -1098,9 +1098,9 @@ ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)
ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)
{ {
void* data = NULL;
int data_size = 0; int data_size = 0;
if (!ImLoadFileToMemory(filename, "rb", (void**)&data, &data_size, 0)) void* data = ImLoadFileToMemory(filename, "rb", &data_size, 0);
if (!data)
{ {
IM_ASSERT(0); // Could not load file. IM_ASSERT(0); // Could not load file.
return NULL; return NULL;

View File

@ -83,7 +83,7 @@ IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, cons
// Helpers: Misc // Helpers: Misc
IMGUI_API ImU32 ImHash(const void* data, int data_size, ImU32 seed = 0); // Pass data_size==0 for zero-terminated strings IMGUI_API ImU32 ImHash(const void* data, int data_size, ImU32 seed = 0); // Pass data_size==0 for zero-terminated strings
IMGUI_API bool ImLoadFileToMemory(const char* filename, const char* file_open_mode, void** out_file_data, int* out_file_size = NULL, int padding_bytes = 0); IMGUI_API void* ImLoadFileToMemory(const char* filename, const char* file_open_mode, int* out_file_size = NULL, int padding_bytes = 0);
IMGUI_API bool ImIsPointInTriangle(const ImVec2& p, const ImVec2& a, const ImVec2& b, const ImVec2& c); IMGUI_API bool ImIsPointInTriangle(const ImVec2& p, const ImVec2& a, const ImVec2& b, const ImVec2& c);
static inline bool ImCharIsSpace(int c) { return c == ' ' || c == '\t' || c == 0x3000; } static inline bool ImCharIsSpace(int c) { return c == ' ' || c == '\t' || c == 0x3000; }
static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }