Internals: Settings: Added ReadCloseFn to allow handlers to sanitize data on a per-entry basis.

This commit is contained in:
omar 2018-02-27 22:32:45 +01:00
parent 29510fcb83
commit a761779b12
2 changed files with 13 additions and 3 deletions

View File

@ -3732,6 +3732,10 @@ static void LoadIniSettingsFromMemory(const char* buf_readonly)
if (line[0] == '[' && line_end > line && line_end[-1] == ']')
{
// Close last entry
if (entry_data && entry_handler && entry_handler->ReadCloseFn)
entry_handler->ReadCloseFn(&g, entry_handler, entry_data);
// Parse "[Type][Name]". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code.
line_end[-1] = 0;
const char* name_end = line_end - 1;
@ -3757,6 +3761,11 @@ static void LoadIniSettingsFromMemory(const char* buf_readonly)
entry_handler->ReadLineFn(&g, entry_handler, entry_data, line);
}
}
// Close last entry
if (entry_data && entry_handler && entry_handler->ReadCloseFn)
entry_handler->ReadCloseFn(&g, entry_handler, entry_data);
ImGui::MemFree(buf);
g.SettingsLoaded = true;
}

View File

@ -428,9 +428,10 @@ struct ImGuiSettingsHandler
{
const char* TypeName; // Short description stored in .ini file. Disallowed characters: '[' ']'
ImGuiID TypeHash; // == ImHash(TypeName, 0, 0)
void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name);
void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line);
void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf);
void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name); // Read: Called when entering into a new ini entry e.g. "[Window][Name]"
void (*ReadCloseFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry); // Read: Called when closing an existing entry, so code can validate overall data. [Optional]
void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); // Read: Called for every line of text within an ini entry
void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf); // Write: Output every entries into 'out_buf'
void* UserData;
ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); }