Load file content into memory Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
| 1511 | // Load file content into memory |
| 1512 | // Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree() |
| 1513 | void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, int* out_file_size, int padding_bytes) |
| 1514 | { |
| 1515 | IM_ASSERT(filename && file_open_mode); |
| 1516 | if (out_file_size) |
| 1517 | *out_file_size = 0; |
| 1518 | |
| 1519 | FILE* f; |
| 1520 | if ((f = ImFileOpen(filename, file_open_mode)) == NULL) |
| 1521 | return NULL; |
| 1522 | |
| 1523 | long file_size_signed; |
| 1524 | if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) |
| 1525 | { |
| 1526 | fclose(f); |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | |
| 1530 | int file_size = (int)file_size_signed; |
| 1531 | void* file_data = ImGui::MemAlloc(file_size + padding_bytes); |
| 1532 | if (file_data == NULL) |
| 1533 | { |
| 1534 | fclose(f); |
| 1535 | return NULL; |
| 1536 | } |
| 1537 | if (fread(file_data, 1, (size_t)file_size, f) != (size_t)file_size) |
| 1538 | { |
| 1539 | fclose(f); |
| 1540 | ImGui::MemFree(file_data); |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | if (padding_bytes > 0) |
| 1544 | memset((void*)(((char*)file_data) + file_size), 0, padding_bytes); |
| 1545 | |
| 1546 | fclose(f); |
| 1547 | if (out_file_size) |
| 1548 | *out_file_size = file_size; |
| 1549 | |
| 1550 | return file_data; |
| 1551 | } |
| 1552 | |
| 1553 | //----------------------------------------------------------------------------- |
| 1554 | // ImGuiStorage |
no test coverage detected