Helper: Load file content into memory Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
| 1506 | // Helper: Load file content into memory |
| 1507 | // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() |
| 1508 | void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) |
| 1509 | { |
| 1510 | IM_ASSERT(filename && mode); |
| 1511 | if (out_file_size) |
| 1512 | *out_file_size = 0; |
| 1513 | |
| 1514 | ImFileHandle f; |
| 1515 | if ((f = ImFileOpen(filename, mode)) == NULL) |
| 1516 | return NULL; |
| 1517 | |
| 1518 | size_t file_size = (size_t)ImFileGetSize(f); |
| 1519 | if (file_size == (size_t)-1) |
| 1520 | { |
| 1521 | ImFileClose(f); |
| 1522 | return NULL; |
| 1523 | } |
| 1524 | |
| 1525 | void* file_data = IM_ALLOC(file_size + padding_bytes); |
| 1526 | if (file_data == NULL) |
| 1527 | { |
| 1528 | ImFileClose(f); |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | if (ImFileRead(file_data, 1, file_size, f) != file_size) |
| 1532 | { |
| 1533 | ImFileClose(f); |
| 1534 | IM_FREE(file_data); |
| 1535 | return NULL; |
| 1536 | } |
| 1537 | if (padding_bytes > 0) |
| 1538 | memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); |
| 1539 | |
| 1540 | ImFileClose(f); |
| 1541 | if (out_file_size) |
| 1542 | *out_file_size = file_size; |
| 1543 | |
| 1544 | return file_data; |
| 1545 | } |
| 1546 | |
| 1547 | //----------------------------------------------------------------------------- |
| 1548 | // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) |
no test coverage detected