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.
| 1637 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 1638 | // This can't really be used with "rt" because fseek size won't match read size. |
| 1639 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 1640 | { |
| 1641 | IM_ASSERT(filename && mode); |
| 1642 | if (out_file_size) |
| 1643 | *out_file_size = 0; |
| 1644 | |
| 1645 | ImFileHandle f; |
| 1646 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 1647 | return NULL; |
| 1648 | |
| 1649 | size_t file_size = (size_t)ImFileGetSize(f); |
| 1650 | if (file_size == (size_t)-1) |
| 1651 | { |
| 1652 | ImFileClose(f); |
| 1653 | return NULL; |
| 1654 | } |
| 1655 | |
| 1656 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 1657 | if (file_data == NULL) |
| 1658 | { |
| 1659 | ImFileClose(f); |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 1663 | { |
| 1664 | ImFileClose(f); |
| 1665 | IM_FREE(file_data); |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | if (padding_bytes > 0) |
| 1669 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 1670 | |
| 1671 | ImFileClose(f); |
| 1672 | if (out_file_size) |
| 1673 | *out_file_size = file_size; |
| 1674 | |
| 1675 | return file_data; |
| 1676 | } |
| 1677 | |
| 1678 | //----------------------------------------------------------------------------- |
| 1679 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected