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.
| 1504 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 1505 | // This can't really be used with "rt" because fseek size won't match read size. |
| 1506 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 1507 | { |
| 1508 | IM_ASSERT(filename && mode); |
| 1509 | if (out_file_size) |
| 1510 | *out_file_size = 0; |
| 1511 | |
| 1512 | ImFileHandle f; |
| 1513 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 1514 | return NULL; |
| 1515 | |
| 1516 | size_t file_size = (size_t)ImFileGetSize(f); |
| 1517 | if (file_size == (size_t)-1) |
| 1518 | { |
| 1519 | ImFileClose(f); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | |
| 1523 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 1524 | if (file_data == NULL) |
| 1525 | { |
| 1526 | ImFileClose(f); |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 1530 | { |
| 1531 | ImFileClose(f); |
| 1532 | IM_FREE(file_data); |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | if (padding_bytes > 0) |
| 1536 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 1537 | |
| 1538 | ImFileClose(f); |
| 1539 | if (out_file_size) |
| 1540 | *out_file_size = file_size; |
| 1541 | |
| 1542 | return file_data; |
| 1543 | } |
| 1544 | |
| 1545 | //----------------------------------------------------------------------------- |
| 1546 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected