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.
| 2598 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 2599 | // This can't really be used with "rt" because fseek size won't match read size. |
| 2600 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 2601 | { |
| 2602 | IM_ASSERT(filename && mode); |
| 2603 | if (out_file_size) |
| 2604 | *out_file_size = 0; |
| 2605 | |
| 2606 | ImFileHandle f; |
| 2607 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 2608 | return NULL; |
| 2609 | |
| 2610 | size_t file_size = (size_t)ImFileGetSize(f); |
| 2611 | if (file_size == (size_t)-1) |
| 2612 | { |
| 2613 | ImFileClose(f); |
| 2614 | return NULL; |
| 2615 | } |
| 2616 | |
| 2617 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 2618 | if (file_data == NULL) |
| 2619 | { |
| 2620 | ImFileClose(f); |
| 2621 | return NULL; |
| 2622 | } |
| 2623 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 2624 | { |
| 2625 | ImFileClose(f); |
| 2626 | IM_FREE(file_data); |
| 2627 | return NULL; |
| 2628 | } |
| 2629 | if (padding_bytes > 0) |
| 2630 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 2631 | |
| 2632 | ImFileClose(f); |
| 2633 | if (out_file_size) |
| 2634 | *out_file_size = file_size; |
| 2635 | |
| 2636 | return file_data; |
| 2637 | } |
| 2638 | |
| 2639 | //----------------------------------------------------------------------------- |
| 2640 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected