Load file content into memory Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
| 1360 | // Load file content into memory |
| 1361 | // Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() |
| 1362 | void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, int* out_file_size, int padding_bytes) |
| 1363 | { |
| 1364 | IM_ASSERT(filename && file_open_mode); |
| 1365 | if (out_file_size) |
| 1366 | *out_file_size = 0; |
| 1367 | |
| 1368 | FILE* f; |
| 1369 | if ((f = ImFileOpen(filename, file_open_mode)) == NULL) |
| 1370 | return NULL; |
| 1371 | |
| 1372 | long file_size_signed; |
| 1373 | if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) |
| 1374 | { |
| 1375 | fclose(f); |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | |
| 1379 | int file_size = (int)file_size_signed; |
| 1380 | void* file_data = ImGui::MemAlloc(file_size + padding_bytes); |
| 1381 | if (file_data == NULL) |
| 1382 | { |
| 1383 | fclose(f); |
| 1384 | return NULL; |
| 1385 | } |
| 1386 | if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size) |
| 1387 | { |
| 1388 | fclose(f); |
| 1389 | ImGui::MemFree(file_data); |
| 1390 | return NULL; |
| 1391 | } |
| 1392 | if (padding_bytes > 0) |
| 1393 | memset((void *)(((char*)file_data) + file_size), 0, padding_bytes); |
| 1394 | |
| 1395 | fclose(f); |
| 1396 | if (out_file_size) |
| 1397 | *out_file_size = file_size; |
| 1398 | |
| 1399 | return file_data; |
| 1400 | } |
| 1401 | |
| 1402 | //----------------------------------------------------------------------------- |
| 1403 | // ImGuiStorage |
no test coverage detected