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.
| 2440 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 2441 | // This can't really be used with "rt" because fseek size won't match read size. |
| 2442 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 2443 | { |
| 2444 | IM_ASSERT(filename && mode); |
| 2445 | if (out_file_size) |
| 2446 | *out_file_size = 0; |
| 2447 | |
| 2448 | ImFileHandle f; |
| 2449 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 2450 | return NULL; |
| 2451 | |
| 2452 | size_t file_size = (size_t)ImFileGetSize(f); |
| 2453 | if (file_size == (size_t)-1) |
| 2454 | { |
| 2455 | ImFileClose(f); |
| 2456 | return NULL; |
| 2457 | } |
| 2458 | |
| 2459 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 2460 | if (file_data == NULL) |
| 2461 | { |
| 2462 | ImFileClose(f); |
| 2463 | return NULL; |
| 2464 | } |
| 2465 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 2466 | { |
| 2467 | ImFileClose(f); |
| 2468 | IM_FREE(file_data); |
| 2469 | return NULL; |
| 2470 | } |
| 2471 | if (padding_bytes > 0) |
| 2472 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 2473 | |
| 2474 | ImFileClose(f); |
| 2475 | if (out_file_size) |
| 2476 | *out_file_size = file_size; |
| 2477 | |
| 2478 | return file_data; |
| 2479 | } |
| 2480 | |
| 2481 | //----------------------------------------------------------------------------- |
| 2482 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected