Load file content into memory Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
| 1147 | // Load file content into memory |
| 1148 | // Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() |
| 1149 | void* ImLoadFileToMemory(const char* filename, const char* file_open_mode, int* out_file_size, int padding_bytes) |
| 1150 | { |
| 1151 | IM_ASSERT(filename && file_open_mode); |
| 1152 | if (out_file_size) |
| 1153 | *out_file_size = 0; |
| 1154 | |
| 1155 | FILE* f; |
| 1156 | if ((f = fopen(filename, file_open_mode)) == NULL) |
| 1157 | return NULL; |
| 1158 | |
| 1159 | long file_size_signed; |
| 1160 | if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) |
| 1161 | { |
| 1162 | fclose(f); |
| 1163 | return NULL; |
| 1164 | } |
| 1165 | |
| 1166 | int file_size = (int)file_size_signed; |
| 1167 | void* file_data = ImGui::MemAlloc(file_size + padding_bytes); |
| 1168 | if (file_data == NULL) |
| 1169 | { |
| 1170 | fclose(f); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size) |
| 1174 | { |
| 1175 | fclose(f); |
| 1176 | ImGui::MemFree(file_data); |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | if (padding_bytes > 0) |
| 1180 | memset((void *)(((char*)file_data) + file_size), 0, padding_bytes); |
| 1181 | |
| 1182 | fclose(f); |
| 1183 | if (out_file_size) |
| 1184 | *out_file_size = file_size; |
| 1185 | |
| 1186 | return file_data; |
| 1187 | } |
| 1188 | |
| 1189 | //----------------------------------------------------------------------------- |
| 1190 | // ImGuiStorage |
no outgoing calls
no test coverage detected