Zero-tolerance, no error reporting, cheap .ini parsing
| 11594 | |
| 11595 | // Zero-tolerance, no error reporting, cheap .ini parsing |
| 11596 | void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) |
| 11597 | { |
| 11598 | ImGuiContext& g = *GImGui; |
| 11599 | IM_ASSERT(g.Initialized); |
| 11600 | //IM_ASSERT(!g.WithinFrameScope && "Cannot be called between NewFrame() and EndFrame()"); |
| 11601 | //IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0); |
| 11602 | |
| 11603 | // For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter). |
| 11604 | // For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy.. |
| 11605 | if (ini_size == 0) |
| 11606 | ini_size = strlen(ini_data); |
| 11607 | g.SettingsIniData.Buf.resize((int)ini_size + 1); |
| 11608 | char* const buf = g.SettingsIniData.Buf.Data; |
| 11609 | char* const buf_end = buf + ini_size; |
| 11610 | memcpy(buf, ini_data, ini_size); |
| 11611 | buf_end[0] = 0; |
| 11612 | |
| 11613 | // Call pre-read handlers |
| 11614 | // Some types will clear their data (e.g. dock information) some types will allow merge/override (window) |
| 11615 | for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) |
| 11616 | if (g.SettingsHandlers[handler_n].ReadInitFn) |
| 11617 | g.SettingsHandlers[handler_n].ReadInitFn(&g, &g.SettingsHandlers[handler_n]); |
| 11618 | |
| 11619 | void* entry_data = NULL; |
| 11620 | ImGuiSettingsHandler* entry_handler = NULL; |
| 11621 | |
| 11622 | char* line_end = NULL; |
| 11623 | for (char* line = buf; line < buf_end; line = line_end + 1) |
| 11624 | { |
| 11625 | // Skip new lines markers, then find end of the line |
| 11626 | while (*line == '\n' || *line == '\r') |
| 11627 | line++; |
| 11628 | line_end = line; |
| 11629 | while (line_end < buf_end && *line_end != '\n' && *line_end != '\r') |
| 11630 | line_end++; |
| 11631 | line_end[0] = 0; |
| 11632 | if (line[0] == ';') |
| 11633 | continue; |
| 11634 | if (line[0] == '[' && line_end > line && line_end[-1] == ']') |
| 11635 | { |
| 11636 | // Parse "[Type][Name]". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code. |
| 11637 | line_end[-1] = 0; |
| 11638 | const char* name_end = line_end - 1; |
| 11639 | const char* type_start = line + 1; |
| 11640 | char* type_end = (char*)(void*)ImStrchrRange(type_start, name_end, ']'); |
| 11641 | const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL; |
| 11642 | if (!type_end || !name_start) |
| 11643 | continue; |
| 11644 | *type_end = 0; // Overwrite first ']' |
| 11645 | name_start++; // Skip second '[' |
| 11646 | entry_handler = FindSettingsHandler(type_start); |
| 11647 | entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL; |
| 11648 | } |
| 11649 | else if (entry_handler != NULL && entry_data != NULL) |
| 11650 | { |
| 11651 | // Let type handler parse the line |
| 11652 | entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); |
| 11653 | } |
nothing calls this directly
no test coverage detected