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.
| 1614 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 1615 | // This can't really be used with "rt" because fseek size won't match read size. |
| 1616 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 1617 | { |
| 1618 | IM_ASSERT(filename && mode); |
| 1619 | if (out_file_size) |
| 1620 | *out_file_size = 0; |
| 1621 | |
| 1622 | ImFileHandle f; |
| 1623 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 1624 | return NULL; |
| 1625 | |
| 1626 | size_t file_size = (size_t)ImFileGetSize(f); |
| 1627 | if (file_size == (size_t)-1) |
| 1628 | { |
| 1629 | ImFileClose(f); |
| 1630 | return NULL; |
| 1631 | } |
| 1632 | |
| 1633 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 1634 | if (file_data == NULL) |
| 1635 | { |
| 1636 | ImFileClose(f); |
| 1637 | return NULL; |
| 1638 | } |
| 1639 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 1640 | { |
| 1641 | ImFileClose(f); |
| 1642 | IM_FREE(file_data); |
| 1643 | return NULL; |
| 1644 | } |
| 1645 | if (padding_bytes > 0) |
| 1646 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 1647 | |
| 1648 | ImFileClose(f); |
| 1649 | if (out_file_size) |
| 1650 | *out_file_size = file_size; |
| 1651 | |
| 1652 | return file_data; |
| 1653 | } |
| 1654 | |
| 1655 | //----------------------------------------------------------------------------- |
| 1656 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected