Zero-tolerance, no error reporting, cheap .ini parsing
| 12634 | |
| 12635 | // Zero-tolerance, no error reporting, cheap .ini parsing |
| 12636 | void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) |
| 12637 | { |
| 12638 | ImGuiContext& g = *GImGui; |
| 12639 | IM_ASSERT(g.Initialized); |
| 12640 | //IM_ASSERT(!g.WithinFrameScope && "Cannot be called between NewFrame() and EndFrame()"); |
| 12641 | //IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0); |
| 12642 | |
| 12643 | // For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter). |
| 12644 | // 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.. |
| 12645 | if (ini_size == 0) |
| 12646 | ini_size = strlen(ini_data); |
| 12647 | g.SettingsIniData.Buf.resize((int)ini_size + 1); |
| 12648 | char* const buf = g.SettingsIniData.Buf.Data; |
| 12649 | char* const buf_end = buf + ini_size; |
| 12650 | memcpy(buf, ini_data, ini_size); |
| 12651 | buf_end[0] = 0; |
| 12652 | |
| 12653 | // Call pre-read handlers |
| 12654 | // Some types will clear their data (e.g. dock information) some types will allow merge/override (window) |
| 12655 | for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) |
| 12656 | if (g.SettingsHandlers[handler_n].ReadInitFn) |
| 12657 | g.SettingsHandlers[handler_n].ReadInitFn(&g, &g.SettingsHandlers[handler_n]); |
| 12658 | |
| 12659 | void* entry_data = NULL; |
| 12660 | ImGuiSettingsHandler* entry_handler = NULL; |
| 12661 | |
| 12662 | char* line_end = NULL; |
| 12663 | for (char* line = buf; line < buf_end; line = line_end + 1) |
| 12664 | { |
| 12665 | // Skip new lines markers, then find end of the line |
| 12666 | while (*line == '\n' || *line == '\r') |
| 12667 | line++; |
| 12668 | line_end = line; |
| 12669 | while (line_end < buf_end && *line_end != '\n' && *line_end != '\r') |
| 12670 | line_end++; |
| 12671 | line_end[0] = 0; |
| 12672 | if (line[0] == ';') |
| 12673 | continue; |
| 12674 | if (line[0] == '[' && line_end > line && line_end[-1] == ']') |
| 12675 | { |
| 12676 | // Parse "[Type][Name]". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code. |
| 12677 | line_end[-1] = 0; |
| 12678 | const char* name_end = line_end - 1; |
| 12679 | const char* type_start = line + 1; |
| 12680 | char* type_end = (char*)(void*)ImStrchrRange(type_start, name_end, ']'); |
| 12681 | const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL; |
| 12682 | if (!type_end || !name_start) |
| 12683 | continue; |
| 12684 | *type_end = 0; // Overwrite first ']' |
| 12685 | name_start++; // Skip second '[' |
| 12686 | entry_handler = FindSettingsHandler(type_start); |
| 12687 | entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL; |
| 12688 | } |
| 12689 | else if (entry_handler != NULL && entry_data != NULL) |
| 12690 | { |
| 12691 | // Let type handler parse the line |
| 12692 | entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); |
| 12693 | } |
nothing calls this directly
no test coverage detected