Zero-tolerance, no error reporting, cheap .ini parsing
| 2693 | |
| 2694 | // Zero-tolerance, no error reporting, cheap .ini parsing |
| 2695 | static void LoadIniSettingsFromMemory(const char* buf_readonly) |
| 2696 | { |
| 2697 | // For convenience and to make the code simpler, we'll write zero terminators inside the buffer. So let's create a writable copy. |
| 2698 | char* buf = ImStrdup(buf_readonly); |
| 2699 | char* buf_end = buf + strlen(buf); |
| 2700 | |
| 2701 | ImGuiContext& g = *GImGui; |
| 2702 | void* entry_data = NULL; |
| 2703 | const ImGuiSettingsHandler* entry_handler = NULL; |
| 2704 | |
| 2705 | char* line_end = NULL; |
| 2706 | for (char* line = buf; line < buf_end; line = line_end + 1) |
| 2707 | { |
| 2708 | // Skip new lines markers, then find end of the line |
| 2709 | while (*line == '\n' || *line == '\r') |
| 2710 | line++; |
| 2711 | line_end = line; |
| 2712 | while (line_end < buf_end && *line_end != '\n' && *line_end != '\r') |
| 2713 | line_end++; |
| 2714 | line_end[0] = 0; |
| 2715 | |
| 2716 | if (line[0] == '[' && line_end > line && line_end[-1] == ']') |
| 2717 | { |
| 2718 | // Parse "[Type][Name]". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code. |
| 2719 | line_end[-1] = 0; |
| 2720 | const char* name_end = line_end - 1; |
| 2721 | const char* type_start = line + 1; |
| 2722 | char* type_end = ImStrchrRange(type_start, name_end, ']'); |
| 2723 | const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL; |
| 2724 | if (!type_end || !name_start) |
| 2725 | { |
| 2726 | name_start = type_start; // Import legacy entries that have no type |
| 2727 | type_start = "Window"; |
| 2728 | } |
| 2729 | else |
| 2730 | { |
| 2731 | *type_end = 0; // Overwrite first ']' |
| 2732 | name_start++; // Skip second '[' |
| 2733 | } |
| 2734 | const ImGuiID type_hash = ImHash(type_start, 0, 0); |
| 2735 | entry_handler = ImGui::FindSettingsHandler(type_hash); |
| 2736 | entry_data = entry_handler ? entry_handler->ReadOpenFn(g, name_start) : NULL; |
| 2737 | } |
| 2738 | else if (entry_handler != NULL && entry_data != NULL) |
| 2739 | { |
| 2740 | // Let type handler parse the line |
| 2741 | entry_handler->ReadLineFn(g, entry_data, line); |
| 2742 | } |
| 2743 | } |
| 2744 | ImGui::MemFree(buf); |
| 2745 | } |
| 2746 | |
| 2747 | static void SaveIniSettingsToDisk(const char* ini_filename) |
| 2748 | { |
no test coverage detected