-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Settings callbacks #993
Settings callbacks #993
Conversation
"Load callback is called twice, first time with buffer set to NULL to determine size, second time with a buffer to read content." Then isn't size_t size redundant? Since it must be the same as what was returned the 1st time, else it will cause a bug? |
Value returned describe preferred buffer size and parameter describe actual buffer size. If present it avoid creating implicit assumptions about buffer size and avoid buffer overflow errors. By omitting it code end up less secure prone to errors. Example implementation of Load() cuts corners, actually should look more like like this: io.LoadIniCb = [](char* buffer, size_t size) -> size_t
{
if (buffer)
memcpy(buffer, mySettings.data(), std::min(size, mySettings.size()));
return mySettings.size();
}; Rule of thumb is to never make an assumption about someone else allocating buffer big enough when you're then one putting stuff in it. Edit: io.LoadIniCb = [](char* buffer, size_t size) -> size_t
{
if (buffer)
{
const auto bytes_copied = std::min(size, mySettings.size());
memcpy(buffer, mySettings.data(), bytes_copied);
return bytes_copied;
}
return mySettings.size();
}; |
I just want to pin a comment from another issue so I don''t have to look for it next time. |
FYI I think they are slightly different issue, this issue (#993) is 80% done as if you dig in Issue #437 relates to using the .ini file as a general storage facility for the end-user. |
I pinned your comment because I vaguely remembered reading about changes in imgui_internal and couldn't find it quickly. And it touched topic of storing data by ImGui. : ) I'm looked at As for this PR I think I will close it as soon as public API appears. Thank you for clarification. |
Closing as solved, see #923 for details. |
This PR add callbacks for saving and loading INI settings file content.
Example usage:
Save callback is straightforward, it receive a text buffer and size to save.
Load callback is called twice, first time with buffer set to
NULL
to determine size, second time with a buffer to read content.