From 7224c81664554540941bf41ac8d50646a8f0ae67 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 11 Jul 2023 12:49:36 +0300 Subject: [PATCH] UI: RegKey: Refactor setValue() --- src/ui/util/regkey.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/ui/util/regkey.cpp b/src/ui/util/regkey.cpp index 2c8909e7..bd5c01ed 100644 --- a/src/ui/util/regkey.cpp +++ b/src/ui/util/regkey.cpp @@ -58,7 +58,13 @@ bool RegKey::removeValue(const QString &name) bool RegKey::setValue(const QString &name, const QVariant &value, bool expand) { - const unsigned char *data = nullptr; + QString dataText; + union { + qint64 i64; + qint32 i32; + } dataNum; + const unsigned char *dataPtr; + DWORD size; DWORD type; @@ -66,33 +72,34 @@ bool RegKey::setValue(const QString &name, const QVariant &value, bool expand) case QMetaType::UnknownType: case QMetaType::Void: case QMetaType::Nullptr: { + dataPtr = nullptr; size = 0; type = REG_SZ; } break; case QMetaType::Bool: case QMetaType::Int: case QMetaType::UInt: { - const qint32 i32 = value.toInt(); - data = (const unsigned char *) &i32; + dataNum.i32 = value.toInt(); + dataPtr = (const unsigned char *) &dataNum.i32; size = sizeof(qint32); type = REG_DWORD; } break; case QMetaType::LongLong: case QMetaType::ULongLong: { - const qint64 i64 = value.toLongLong(); - data = (const unsigned char *) &i64; + dataNum.i64 = value.toLongLong(); + dataPtr = (const unsigned char *) &dataNum.i64; size = sizeof(qint64); type = REG_QWORD; } break; default: { - const QString text = value.toString(); - data = (const unsigned char *) text.utf16(); - size = DWORD(sizeof(wchar_t) * (text.size() + 1)); /* + terminating null character */ + dataText = value.toString(); + dataPtr = (const unsigned char *) dataText.utf16(); + size = DWORD(sizeof(wchar_t) * (dataText.size() + 1)); /* + terminating null character */ type = expand ? REG_EXPAND_SZ : REG_SZ; } break; } - return !RegSetValueEx((HKEY) handle(), (LPCWSTR) name.utf16(), 0, type, data, size); + return !RegSetValueEx((HKEY) handle(), (LPCWSTR) name.utf16(), 0, type, dataPtr, size); } QVariant RegKey::value(const QString &name, bool *expand) const