diff options
| author | Patrick Storz <eduard.braun2@gmx.de> | 2019-07-22 20:33:48 +0000 |
|---|---|---|
| committer | Patrick Storz <eduard.braun2@gmx.de> | 2019-07-22 23:15:57 +0000 |
| commit | 624aeeae6ae43c2c298462cbd1108f2e5ad7fbef (patch) | |
| tree | dd202b8cbd2088698ce8281d9d024d91643b5e42 /src/preferences.cpp | |
| parent | Update jhbuild (diff) | |
| download | inkscape-624aeeae6ae43c2c298462cbd1108f2e5ad7fbef.tar.gz inkscape-624aeeae6ae43c2c298462cbd1108f2e5ad7fbef.zip | |
Preferences: add methods to set/get unsigned integers
Those should be used for RGBA color values, which would overflow
the signed type otherwise.
Replacement will be seamless and fully backwards-compatible, as we
can read the overflowed (i.e. negative) signed integers we used to
save and they will be properly unwrapped.
Also reading the unsigned ints as signed ints in older versions of
Inkscape will still work, as we always depended on the undefined
behavior of atoi in case of out of range numbers (which luckily is
means overflowing in gcc).
Diffstat (limited to 'src/preferences.cpp')
| -rw-r--r-- | src/preferences.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/preferences.cpp b/src/preferences.cpp index dc0c0d609..018ba7b01 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -391,6 +391,17 @@ void Preferences::setInt(Glib::ustring const &pref_path, int value) } /** + * Set an unsigned integer attribute of a preference. + * + * @param pref_path Path of the preference to modify. + * @param value The new value of the pref attribute. + */ +void Preferences::setUInt(Glib::ustring const &pref_path, unsigned int value) +{ + _setRawValue(pref_path, Glib::ustring::compose("%1",value)); +} + +/** * Set a floating point attribute of a preference. * * @param pref_path Path of the preference to modify. @@ -804,6 +815,26 @@ int Preferences::_extractInt(Entry const &v) } } +unsigned int Preferences::_extractUInt(Entry const &v) +{ + if (v.cached_uint) return v.value_uint; + v.cached_uint = true; + gchar const *s = static_cast<gchar const *>(v._value); + + // Note: 'strtoul' can also read overflowed (i.e. negative) signed int values that we used to save before we + // had the unsigned type, so this is fully backwards compatible and can be replaced seamlessly + unsigned int val = 0; + errno = 0; + val = (unsigned int)strtoul(s, nullptr, 0); + if (errno == ERANGE) { + g_warning("Unsigned integer preference out of range: '%s' (raw value: %s)", v._pref_path.c_str(), s); + val = 0; + } + + v.value_uint = val; + return v.value_uint; +} + double Preferences::_extractDouble(Entry const &v) { if (v.cached_double) return v.value_double; |
