mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(settings): fixes some serious type conversion warnings from llvm code analysis
This commit is contained in:
parent
05bd55d8a5
commit
6c150a04c0
|
@ -67,18 +67,19 @@ float dataToFloat(QByteArray data)
|
|||
}
|
||||
|
||||
// Converts a string into PNet string data
|
||||
QByteArray stringToData(QString str)
|
||||
QByteArray stringToData(const QString& str)
|
||||
{
|
||||
QByteArray data(4,0);
|
||||
// Write the size in a Uint of variable lenght (8-32 bits)
|
||||
int i=0;
|
||||
uint num1 = (uint)str.toUtf8().size();
|
||||
int num1 = str.toUtf8().size();
|
||||
while (num1 >= 0x80)
|
||||
{
|
||||
data[i] = (unsigned char)(num1 | 0x80); i++;
|
||||
data[i] = static_cast<char>(num1 | 0x80);
|
||||
i++;
|
||||
num1 = num1 >> 7;
|
||||
}
|
||||
data[i]=num1;
|
||||
data[i] = static_cast<char>(num1);
|
||||
data.resize(i+1);
|
||||
data+=str.toUtf8();
|
||||
return data;
|
||||
|
@ -86,24 +87,24 @@ QByteArray stringToData(QString str)
|
|||
|
||||
QString dataToString(QByteArray data)
|
||||
{
|
||||
// Variable UInt32
|
||||
unsigned char num3;
|
||||
int num = 0;
|
||||
char num3;
|
||||
int strlen = 0;
|
||||
int num2 = 0;
|
||||
int i=0;
|
||||
do
|
||||
{
|
||||
num3 = data[i]; i++;
|
||||
num |= (num3 & 0x7f) << num2;
|
||||
num3 = data[i++];
|
||||
strlen |= (num3 & 0x7f) << num2;
|
||||
num2 += 7;
|
||||
} while ((num3 & 0x80) != 0);
|
||||
unsigned int strlen = (uint) num;
|
||||
|
||||
if (!strlen)
|
||||
if (strlen <= 0)
|
||||
return QString();
|
||||
|
||||
data = data.right(data.size()-i); // Remove the strlen
|
||||
// Remove the strlen
|
||||
data.remove(0, i - 1);
|
||||
data.truncate(strlen);
|
||||
|
||||
return QString(data);
|
||||
}
|
||||
|
||||
|
@ -113,35 +114,35 @@ float dataToRangedSingle(float min, float max, int numberOfBits, QByteArray data
|
|||
uint value=0;
|
||||
if (numberOfBits <= 8)
|
||||
{
|
||||
endvalue = (uchar)data[0];
|
||||
endvalue = static_cast<uchar>(data[0]);
|
||||
goto done;
|
||||
}
|
||||
value = (uchar)data[0];
|
||||
value = static_cast<uchar>(data[0]);
|
||||
numberOfBits -= 8;
|
||||
if (numberOfBits <= 8)
|
||||
{
|
||||
endvalue = (value | ((uint) ((uchar)data[1]) << 8));
|
||||
endvalue = value | (static_cast<uint>(data[1]) << 8);
|
||||
goto done;
|
||||
}
|
||||
value |= (uint) (((uchar)data[1]) << 8);
|
||||
value |= static_cast<uint>(data[1]) << 8;
|
||||
numberOfBits -= 8;
|
||||
if (numberOfBits <= 8)
|
||||
{
|
||||
uint num2 = (uint) (((uchar)data[2]) << 0x10);
|
||||
uint num2 = static_cast<uint>(data[2]) << 0x10;
|
||||
endvalue = (value | num2);
|
||||
goto done;
|
||||
}
|
||||
value |= (uint) (((uchar)data[2]) << 0x10);
|
||||
value |= static_cast<uint>(data[2]) << 0x10;
|
||||
numberOfBits -= 8;
|
||||
endvalue = (value | ((uint) (((uchar)data[3]) << 0x18)));
|
||||
endvalue = value | (static_cast<uint>(data[3]) << 0x18);
|
||||
goto done;
|
||||
|
||||
done:
|
||||
|
||||
float num = max - min;
|
||||
int num2 = (((int) 1) << numberOfBits) - 1;
|
||||
int num2 = (static_cast<int>(1) << numberOfBits) - 1;
|
||||
float num3 = endvalue;
|
||||
float num4 = num3 / ((float) num2);
|
||||
float num4 = num3 / num2;
|
||||
return (min + (num4 * num));
|
||||
}
|
||||
|
||||
|
@ -150,75 +151,90 @@ QByteArray rangedSingleToData(float value, float min, float max, int numberOfBit
|
|||
QByteArray data;
|
||||
float num = max - min;
|
||||
float num2 = (value - min) / num;
|
||||
int num3 = (((int) 1) << numberOfBits) - 1;
|
||||
uint source = num3 * num2;
|
||||
int num3 = (static_cast<int>(1) << numberOfBits) - 1;
|
||||
uint source = static_cast<uint>(num3 * num2);
|
||||
|
||||
if (numberOfBits <= 8)
|
||||
{
|
||||
data += (unsigned char)source;
|
||||
data += static_cast<char>(source);
|
||||
return data;
|
||||
}
|
||||
data += (unsigned char)source;
|
||||
data += static_cast<char>(source);
|
||||
numberOfBits -= 8;
|
||||
if (numberOfBits <= 8)
|
||||
{
|
||||
data += (unsigned char)(source>>8);
|
||||
data += static_cast<char>(source >> 8);
|
||||
return data;
|
||||
}
|
||||
data += (unsigned char)(source>>8);
|
||||
data += static_cast<char>(source >> 8);
|
||||
numberOfBits -= 8;
|
||||
if (numberOfBits <= 8)
|
||||
{
|
||||
data += (unsigned char)(source>>16);
|
||||
data += static_cast<char>(source >> 16);
|
||||
return data;
|
||||
}
|
||||
data += (unsigned char)(source>>16);
|
||||
data += (unsigned char)(source>>24);
|
||||
data += static_cast<char>(source >> 16);
|
||||
data += static_cast<char>(source >> 24);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
uint8_t dataToUint8(QByteArray data)
|
||||
uint8_t dataToUint8(const QByteArray& data)
|
||||
{
|
||||
return (uint8_t)data[0];
|
||||
return static_cast<uint8_t>(data[0]);
|
||||
}
|
||||
|
||||
uint16_t dataToUint16(QByteArray data)
|
||||
uint16_t dataToUint16(const QByteArray& data)
|
||||
{
|
||||
return ((uint16_t)(uint8_t)data[0])
|
||||
+(((uint16_t)(uint8_t)data[1])<<8);
|
||||
return static_cast<uint16_t>(data[0])
|
||||
| static_cast<uint16_t>(data[1] << 8);
|
||||
}
|
||||
|
||||
uint32_t dataToUint32(QByteArray data)
|
||||
uint32_t dataToUint32(const QByteArray& data)
|
||||
{
|
||||
return ((uint32_t)(uint8_t)data[0])
|
||||
+(((uint32_t)(uint8_t)data[1])<<8)
|
||||
+(((uint32_t)(uint8_t)data[2])<<16)
|
||||
+(((uint32_t)(uint8_t)data[3])<<24);
|
||||
return static_cast<uint32_t>(data[0])
|
||||
| (static_cast<uint32_t>(data[1]) << 8)
|
||||
| (static_cast<uint32_t>(data[2]) << 16)
|
||||
| (static_cast<uint32_t>(data[3]) << 24);
|
||||
}
|
||||
|
||||
uint64_t dataToUint64(QByteArray data)
|
||||
uint64_t dataToUint64(const QByteArray& data)
|
||||
{
|
||||
return ((uint64_t)(uint8_t)data[0])
|
||||
+(((uint64_t)(uint8_t)data[1])<<8)
|
||||
+(((uint64_t)(uint8_t)data[2])<<16)
|
||||
+(((uint64_t)(uint8_t)data[3])<<24)
|
||||
+(((uint64_t)(uint8_t)data[4])<<32)
|
||||
+(((uint64_t)(uint8_t)data[5])<<40)
|
||||
+(((uint64_t)(uint8_t)data[6])<<48)
|
||||
+(((uint64_t)(uint8_t)data[7])<<56);
|
||||
return static_cast<uint64_t>(data[0])
|
||||
| (static_cast<uint64_t>(data[1]) << 8)
|
||||
| (static_cast<uint64_t>(data[2]) << 16)
|
||||
| (static_cast<uint64_t>(data[3]) << 24)
|
||||
| (static_cast<uint64_t>(data[4]) << 32)
|
||||
| (static_cast<uint64_t>(data[5]) << 40)
|
||||
| (static_cast<uint64_t>(data[6]) << 48)
|
||||
| (static_cast<uint64_t>(data[7]) << 56);
|
||||
}
|
||||
|
||||
int dataToVInt(const QByteArray& data)
|
||||
{
|
||||
char num3;
|
||||
int num = 0;
|
||||
int num2 = 0;
|
||||
int i=0;
|
||||
do
|
||||
{
|
||||
num3 = data[i++];
|
||||
num |= static_cast<int>(num3 & 0x7f) << num2;
|
||||
num2 += 7;
|
||||
} while ((num3 & 0x80) != 0);
|
||||
return num;
|
||||
}
|
||||
|
||||
size_t dataToVUint(const QByteArray& data)
|
||||
{
|
||||
unsigned char num3;
|
||||
char num3;
|
||||
size_t num = 0;
|
||||
int num2 = 0;
|
||||
int i=0;
|
||||
do
|
||||
{
|
||||
num3 = data[i]; i++;
|
||||
num |= (num3 & 0x7f) << num2;
|
||||
num3 = data[i++];
|
||||
num |= static_cast<size_t>(num3 & 0x7f) << num2;
|
||||
num2 += 7;
|
||||
} while ((num3 & 0x80) != 0);
|
||||
return num;
|
||||
|
@ -227,52 +243,66 @@ size_t dataToVUint(const QByteArray& data)
|
|||
unsigned getVUint32Size(QByteArray data)
|
||||
{
|
||||
unsigned lensize=0;
|
||||
{
|
||||
unsigned char num3;
|
||||
|
||||
char num3;
|
||||
do {
|
||||
num3 = data[lensize];
|
||||
lensize++;
|
||||
} while ((num3 & 0x80) != 0);
|
||||
}
|
||||
|
||||
return lensize;
|
||||
}
|
||||
|
||||
QByteArray uint8ToData(uint8_t num)
|
||||
{
|
||||
QByteArray data(1,0);
|
||||
data[0] = (uint8_t)num;
|
||||
return data;
|
||||
return QByteArray(1, static_cast<char>(num));
|
||||
}
|
||||
|
||||
QByteArray uint16ToData(uint16_t num)
|
||||
{
|
||||
QByteArray data(2,0);
|
||||
data[0] = (uint8_t)(num & 0xFF);
|
||||
data[1] = (uint8_t)((num>>8) & 0xFF);
|
||||
data[0] = static_cast<char>(num & 0xFF);
|
||||
data[1] = static_cast<char>((num>>8) & 0xFF);
|
||||
return data;
|
||||
}
|
||||
|
||||
QByteArray uint32ToData(uint32_t num)
|
||||
{
|
||||
QByteArray data(4,0);
|
||||
data[0] = (uint8_t)(num & 0xFF);
|
||||
data[1] = (uint8_t)((num>>8) & 0xFF);
|
||||
data[2] = (uint8_t)((num>>16) & 0xFF);
|
||||
data[3] = (uint8_t)((num>>24) & 0xFF);
|
||||
data[0] = static_cast<char>(num & 0xFF);
|
||||
data[1] = static_cast<char>((num>>8) & 0xFF);
|
||||
data[2] = static_cast<char>((num>>16) & 0xFF);
|
||||
data[3] = static_cast<char>((num>>24) & 0xFF);
|
||||
return data;
|
||||
}
|
||||
|
||||
QByteArray uint64ToData(uint64_t num)
|
||||
{
|
||||
QByteArray data(8,0);
|
||||
data[0] = (uint8_t)(num & 0xFF);
|
||||
data[1] = (uint8_t)((num>>8) & 0xFF);
|
||||
data[2] = (uint8_t)((num>>16) & 0xFF);
|
||||
data[3] = (uint8_t)((num>>24) & 0xFF);
|
||||
data[4] = (uint8_t)((num>>32) & 0xFF);
|
||||
data[5] = (uint8_t)((num>>40) & 0xFF);
|
||||
data[6] = (uint8_t)((num>>48) & 0xFF);
|
||||
data[7] = (uint8_t)((num>>56) & 0xFF);
|
||||
data[0] = static_cast<char>(num & 0xFF);
|
||||
data[1] = static_cast<char>((num>>8) & 0xFF);
|
||||
data[2] = static_cast<char>((num>>16) & 0xFF);
|
||||
data[3] = static_cast<char>((num>>24) & 0xFF);
|
||||
data[4] = static_cast<char>((num>>32) & 0xFF);
|
||||
data[5] = static_cast<char>((num>>40) & 0xFF);
|
||||
data[6] = static_cast<char>((num>>48) & 0xFF);
|
||||
data[7] = static_cast<char>((num>>56) & 0xFF);
|
||||
return data;
|
||||
}
|
||||
|
||||
QByteArray vintToData(int num)
|
||||
{
|
||||
QByteArray data(sizeof(int), 0);
|
||||
// Write the size in a Uint of variable lenght (8-32 bits)
|
||||
int i=0;
|
||||
while (num >= 0x80)
|
||||
{
|
||||
data[i] = static_cast<char>(num | 0x80);
|
||||
i++;
|
||||
num = num >> 7;
|
||||
}
|
||||
data[i] = static_cast<char>(num);
|
||||
data.resize(i+1);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
@ -283,10 +313,11 @@ QByteArray vuintToData(size_t num)
|
|||
int i=0;
|
||||
while (num >= 0x80)
|
||||
{
|
||||
data[i] = (unsigned char)(num | 0x80); i++;
|
||||
data[i] = static_cast<char>(num | 0x80);
|
||||
i++;
|
||||
num = num >> 7;
|
||||
}
|
||||
data[i]=num;
|
||||
data[i] = static_cast<char>(num);
|
||||
data.resize(i+1);
|
||||
return data;
|
||||
}
|
||||
|
|
|
@ -27,21 +27,23 @@
|
|||
|
||||
QByteArray doubleToData(double num);
|
||||
QByteArray floatToData(float num);
|
||||
float dataToFloat(QByteArray data);
|
||||
QByteArray stringToData(QString str);
|
||||
float dataToFloat(const QByteArray& data);
|
||||
QByteArray stringToData(const QString& str);
|
||||
QString dataToString(QByteArray data);
|
||||
float dataToRangedSingle(float min, float max, int numberOfBits, QByteArray data);
|
||||
QByteArray rangedSingleToData(float value, float min, float max, int numberOfBits);
|
||||
uint8_t dataToUint8(QByteArray data);
|
||||
uint16_t dataToUint16(QByteArray data);
|
||||
uint32_t dataToUint32(QByteArray data);
|
||||
uint64_t dataToUint64(QByteArray data);
|
||||
uint8_t dataToUint8(const QByteArray& data);
|
||||
uint16_t dataToUint16(const QByteArray& data);
|
||||
uint32_t dataToUint32(const QByteArray& data);
|
||||
uint64_t dataToUint64(const QByteArray& data);
|
||||
int dataToVInt(const QByteArray& data);
|
||||
size_t dataToVUint(const QByteArray& data);
|
||||
unsigned getVUint32Size(QByteArray data);
|
||||
QByteArray uint8ToData(uint8_t num);
|
||||
QByteArray uint16ToData(uint16_t num);
|
||||
QByteArray uint32ToData(uint32_t num);
|
||||
QByteArray uint64ToData(uint64_t num);
|
||||
QByteArray vintToData(int num);
|
||||
QByteArray vuintToData(size_t num);
|
||||
|
||||
#endif // SERIALIZE_H
|
||||
|
|
|
@ -47,8 +47,6 @@
|
|||
#include <QThread>
|
||||
#include <QNetworkProxy>
|
||||
|
||||
#define SHOW_SYSTEM_TRAY_DEFAULT (bool) true
|
||||
|
||||
/**
|
||||
@var QHash<QString, QByteArray> Settings::widgetSettings
|
||||
@brief Assume all widgets have unique names
|
||||
|
@ -154,7 +152,7 @@ void Settings::loadGlobal()
|
|||
server.name = s.value("name").toString();
|
||||
server.userId = s.value("userId").toString();
|
||||
server.address = s.value("address").toString();
|
||||
server.port = s.value("port").toInt();
|
||||
server.port = static_cast<quint16>(s.value("port").toUInt());
|
||||
dhtServerList << server;
|
||||
}
|
||||
s.endArray();
|
||||
|
@ -168,14 +166,14 @@ void Settings::loadGlobal()
|
|||
s.beginGroup("General");
|
||||
enableIPv6 = s.value("enableIPv6", true).toBool();
|
||||
translation = s.value("translation", "en").toString();
|
||||
showSystemTray = s.value("showSystemTray", SHOW_SYSTEM_TRAY_DEFAULT).toBool();
|
||||
showSystemTray = s.value("showSystemTray", true).toBool();
|
||||
makeToxPortable = s.value("makeToxPortable", false).toBool();
|
||||
autostartInTray = s.value("autostartInTray", false).toBool();
|
||||
closeToTray = s.value("closeToTray", false).toBool();
|
||||
forceTCP = s.value("forceTCP", false).toBool();
|
||||
setProxyType(s.value("proxyType", static_cast<int>(ProxyType::ptNone)).toInt());
|
||||
proxyAddr = s.value("proxyAddr", "").toString();
|
||||
proxyPort = s.value("proxyPort", 0).toInt();
|
||||
proxyPort = static_cast<quint16>(s.value("proxyPort", 0).toUInt());
|
||||
if (currentProfile.isEmpty())
|
||||
{
|
||||
currentProfile = s.value("currentProfile", "").toString();
|
||||
|
@ -268,7 +266,7 @@ void Settings::loadGlobal()
|
|||
camVideoRes = s.value("camVideoRes", QRect()).toRect();
|
||||
screenRegion = s.value("screenRegion", QRect()).toRect();
|
||||
screenGrabbed = s.value("screenGrabbed", false).toBool();
|
||||
camVideoFPS = s.value("camVideoFPS", 0).toUInt();
|
||||
camVideoFPS = static_cast<quint16>(s.value("camVideoFPS", 0).toUInt());
|
||||
s.endGroup();
|
||||
|
||||
// Read the embedded DHT bootstrap nodes list if needed
|
||||
|
@ -285,7 +283,7 @@ void Settings::loadGlobal()
|
|||
server.name = rcs.value("name").toString();
|
||||
server.userId = rcs.value("userId").toString();
|
||||
server.address = rcs.value("address").toString();
|
||||
server.port = rcs.value("port").toInt();
|
||||
server.port = static_cast<quint16>(rcs.value("port").toUInt());
|
||||
dhtServerList << server;
|
||||
}
|
||||
rcs.endArray();
|
||||
|
@ -620,7 +618,7 @@ void Settings::savePersonal(QString profileName, const QString &password)
|
|||
uint32_t Settings::makeProfileId(const QString& profile)
|
||||
{
|
||||
QByteArray data = QCryptographicHash::hash(profile.toUtf8(), QCryptographicHash::Md5);
|
||||
const uint32_t* dwords = (uint32_t*)data.constData();
|
||||
const uint32_t* dwords = reinterpret_cast<const uint32_t*>(data.constData());
|
||||
return dwords[0] ^ dwords[1] ^ dwords[2] ^ dwords[3];
|
||||
}
|
||||
|
||||
|
@ -925,7 +923,7 @@ void Settings::deleteToxme()
|
|||
{
|
||||
setToxmeInfo("");
|
||||
setToxmeBio("");
|
||||
setToxmePriv("");
|
||||
setToxmePriv(false);
|
||||
setToxmePass("");
|
||||
}
|
||||
|
||||
|
@ -1016,7 +1014,9 @@ QNetworkProxy Settings::getProxy() const
|
|||
default:
|
||||
proxy.setType(QNetworkProxy::NoProxy);
|
||||
qWarning() << "Invalid Proxy type, setting to NoProxy";
|
||||
break;
|
||||
}
|
||||
|
||||
proxy.setHostName(Settings::getProxyAddr());
|
||||
proxy.setPort(Settings::getProxyPort());
|
||||
return proxy;
|
||||
|
@ -1049,13 +1049,13 @@ void Settings::setProxyAddr(const QString& newValue)
|
|||
proxyAddr = newValue;
|
||||
}
|
||||
|
||||
int Settings::getProxyPort() const
|
||||
quint16 Settings::getProxyPort() const
|
||||
{
|
||||
QMutexLocker locker{&bigLock};
|
||||
return proxyPort;
|
||||
}
|
||||
|
||||
void Settings::setProxyPort(int newValue)
|
||||
void Settings::setProxyPort(quint16 newValue)
|
||||
{
|
||||
QMutexLocker locker{&bigLock};
|
||||
proxyPort = newValue;
|
||||
|
|
|
@ -145,8 +145,8 @@ public:
|
|||
ProxyType getProxyType() const;
|
||||
void setProxyType(int newValue);
|
||||
|
||||
int getProxyPort() const;
|
||||
void setProxyPort(int newValue);
|
||||
quint16 getProxyPort() const;
|
||||
void setProxyPort(quint16 newValue);
|
||||
|
||||
bool getEnableLogging() const;
|
||||
void setEnableLogging(bool newValue);
|
||||
|
@ -390,7 +390,7 @@ private:
|
|||
|
||||
ProxyType proxyType;
|
||||
QString proxyAddr;
|
||||
int proxyPort;
|
||||
quint16 proxyPort;
|
||||
|
||||
QString currentProfile;
|
||||
uint32_t currentProfileId;
|
||||
|
|
|
@ -53,8 +53,6 @@ enum class RecordTag : uint8_t
|
|||
{
|
||||
|
||||
};
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
@var static const char magic[];
|
||||
@brief Little endian ASCII "QTOX" magic
|
||||
|
@ -68,7 +66,7 @@ QDataStream& writeStream(QDataStream& dataStream, const SettingsSerializer::Reco
|
|||
|
||||
QDataStream& writeStream(QDataStream& dataStream, const QByteArray& data)
|
||||
{
|
||||
QByteArray size = vuintToData(data.size());
|
||||
QByteArray size = vintToData(data.size());
|
||||
dataStream.writeRawData(size.data(), size.size());
|
||||
dataStream.writeRawData(data.data(), data.size());
|
||||
return dataStream;
|
||||
|
@ -81,18 +79,18 @@ QDataStream& writeStream(QDataStream& dataStream, const QString& str)
|
|||
|
||||
QDataStream& readStream(QDataStream& dataStream, SettingsSerializer::RecordTag& tag)
|
||||
{
|
||||
return dataStream.operator >>((uint8_t&)tag);
|
||||
return dataStream >> reinterpret_cast<quint8&>(tag) ;
|
||||
}
|
||||
|
||||
|
||||
QDataStream& readStream(QDataStream& dataStream, QByteArray& data)
|
||||
{
|
||||
unsigned char num3;
|
||||
size_t num = 0;
|
||||
char num3;
|
||||
int num = 0;
|
||||
int num2 = 0;
|
||||
do
|
||||
{
|
||||
dataStream.readRawData((char*)&num3, 1);
|
||||
dataStream.readRawData(&num3, 1);
|
||||
num |= (num3 & 0x7f) << num2;
|
||||
num2 += 7;
|
||||
} while ((num3 & 0x80) != 0);
|
||||
|
@ -130,10 +128,15 @@ void SettingsSerializer::endGroup()
|
|||
|
||||
int SettingsSerializer::beginReadArray(const QString &prefix)
|
||||
{
|
||||
auto index = find_if(begin(arrays), end(arrays), [=](const Array& a){return a.name==prefix;});
|
||||
if (index != end(arrays))
|
||||
auto index = std::find_if(std::begin(arrays), std::end(arrays),
|
||||
[=](const Array& a)
|
||||
{
|
||||
array = index-begin(arrays);
|
||||
return a.name==prefix;
|
||||
});
|
||||
|
||||
if (index != std::end(arrays))
|
||||
{
|
||||
array = static_cast<int>(index - std::begin(arrays));
|
||||
arrayIndex = -1;
|
||||
return index->size;
|
||||
}
|
||||
|
@ -148,20 +151,25 @@ int SettingsSerializer::beginReadArray(const QString &prefix)
|
|||
|
||||
void SettingsSerializer::beginWriteArray(const QString &prefix, int size)
|
||||
{
|
||||
auto index = find_if(begin(arrays), end(arrays), [=](const Array& a){return a.name==prefix;});
|
||||
if (index != end(arrays))
|
||||
auto index = std::find_if(std::begin(arrays), std::end(arrays),
|
||||
[=](const Array& a)
|
||||
{
|
||||
array = index-begin(arrays);
|
||||
return a.name==prefix;
|
||||
});
|
||||
|
||||
if (index != std::end(arrays))
|
||||
{
|
||||
array = static_cast<int>(index - std::begin(arrays));
|
||||
arrayIndex = -1;
|
||||
if (size > 0)
|
||||
index->size = max(index->size, (quint64)size);
|
||||
index->size = std::max(index->size, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (size < 0)
|
||||
size = 0;
|
||||
array = arrays.size();
|
||||
arrays.push_back({group, (uint64_t)size, prefix, {}});
|
||||
arrays.push_back({group, size, prefix, {}});
|
||||
arrayIndex = -1;
|
||||
}
|
||||
}
|
||||
|
@ -171,7 +179,7 @@ void SettingsSerializer::endArray()
|
|||
array = -1;
|
||||
}
|
||||
|
||||
void SettingsSerializer::setArrayIndex(unsigned i)
|
||||
void SettingsSerializer::setArrayIndex(int i)
|
||||
{
|
||||
arrayIndex = i;
|
||||
}
|
||||
|
@ -209,9 +217,13 @@ const SettingsSerializer::Value* SettingsSerializer::findValue(const QString& ke
|
|||
{
|
||||
if (a.group != group)
|
||||
continue;
|
||||
|
||||
for (int vi : a.values)
|
||||
if (values[vi].arrayIndex == arrayIndex && values[vi].key == key)
|
||||
return &values[vi];
|
||||
{
|
||||
const Value& v = values[vi];
|
||||
if (v.arrayIndex == arrayIndex && v.key == key)
|
||||
return &v;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -241,7 +253,7 @@ bool SettingsSerializer::isSerializedFormat(QString filePath)
|
|||
char fmagic[8];
|
||||
if (f.read(fmagic, sizeof(fmagic)) != sizeof(fmagic))
|
||||
return false;
|
||||
return !memcmp(fmagic, magic, 4) || tox_is_data_encrypted((uint8_t*)fmagic);
|
||||
return !memcmp(fmagic, magic, 4) || tox_is_data_encrypted(reinterpret_cast<uint8_t*>(fmagic));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -289,14 +301,15 @@ void SettingsSerializer::save()
|
|||
continue;
|
||||
writeStream(stream, RecordTag::ArrayStart);
|
||||
writeStream(stream, a.name.toUtf8());
|
||||
writeStream(stream, vuintToData(a.size));
|
||||
writeStream(stream, vintToData(a.size));
|
||||
|
||||
for (uint64_t vi : a.values)
|
||||
for (int vi : a.values)
|
||||
{
|
||||
const Value& v = values[vi];
|
||||
writeStream(stream, RecordTag::ArrayValue);
|
||||
writeStream(stream, vuintToData(values[vi].arrayIndex));
|
||||
writeStream(stream, values[vi].key.toUtf8());
|
||||
writePackedVariant(stream, values[vi].value);
|
||||
writeStream(stream, vintToData(values[vi].arrayIndex));
|
||||
writeStream(stream, v.key.toUtf8());
|
||||
writePackedVariant(stream, v.value);
|
||||
}
|
||||
writeStream(stream, RecordTag::ArrayEnd);
|
||||
}
|
||||
|
@ -346,7 +359,7 @@ void SettingsSerializer::readSerialized()
|
|||
f.close();
|
||||
|
||||
// Decrypt
|
||||
if (tox_is_data_encrypted((uint8_t*)data.data()))
|
||||
if (tox_is_data_encrypted(reinterpret_cast<uint8_t*>(data.data())))
|
||||
{
|
||||
if (password.isEmpty())
|
||||
{
|
||||
|
@ -413,8 +426,8 @@ void SettingsSerializer::readSerialized()
|
|||
qWarning("The personal save file is corrupted!");
|
||||
return;
|
||||
}
|
||||
quint64 size = dataToVUint(sizeData);
|
||||
arrays[array].size = max(size, arrays[array].size);
|
||||
int size = dataToVInt(sizeData);
|
||||
arrays[array].size = qMax(size, arrays[array].size);
|
||||
}
|
||||
else if (tag == RecordTag::ArrayValue)
|
||||
{
|
||||
|
@ -425,8 +438,7 @@ void SettingsSerializer::readSerialized()
|
|||
qWarning("The personal save file is corrupted!");
|
||||
return;
|
||||
}
|
||||
quint64 index = dataToVUint(indexData);
|
||||
setArrayIndex(index);
|
||||
setArrayIndex(dataToVInt(indexData));
|
||||
QByteArray key;
|
||||
QByteArray value;
|
||||
readStream(stream, key);
|
||||
|
@ -488,12 +500,12 @@ void SettingsSerializer::readIni()
|
|||
|
||||
// Find groups that only have 1 key
|
||||
std::unique_ptr<int[]> groupSizes{new int[groups.size()]};
|
||||
memset(groupSizes.get(), 0, groups.size()*sizeof(int));
|
||||
memset(groupSizes.get(), 0, static_cast<size_t>(groups.size()) * sizeof(int));
|
||||
for (const Value& v : values)
|
||||
{
|
||||
if (v.group < 0 || v.group > groups.size())
|
||||
continue;
|
||||
groupSizes[v.group]++;
|
||||
groupSizes[static_cast<size_t>(v.group)]++;
|
||||
}
|
||||
|
||||
// Find arrays, remove their size key from the values, and add them to `arrays`
|
||||
|
@ -503,7 +515,7 @@ void SettingsSerializer::readIni()
|
|||
const Value& v = values[i];
|
||||
if (v.group < 0 || v.group > groups.size())
|
||||
continue;
|
||||
if (groupSizes[v.group] != 1)
|
||||
if (groupSizes[static_cast<size_t>(v.group)] != 1)
|
||||
continue;
|
||||
if (v.key != "size")
|
||||
continue;
|
||||
|
@ -512,24 +524,24 @@ void SettingsSerializer::readIni()
|
|||
|
||||
Array a;
|
||||
a.size = v.value.toInt();
|
||||
int slashIndex = groups[v.group].lastIndexOf('/');
|
||||
int slashIndex = groups[static_cast<int>(v.group)].lastIndexOf('/');
|
||||
if (slashIndex == -1)
|
||||
{
|
||||
a.group = -1;
|
||||
a.name = groups[v.group];
|
||||
a.name = groups[static_cast<int>(v.group)];
|
||||
a.size = v.value.toInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
a.group = -1;
|
||||
for (int i=0; i<groups.size(); i++)
|
||||
if (groups[i] == groups[v.group].left(slashIndex))
|
||||
if (groups[i] == groups[static_cast<int>(v.group)].left(slashIndex))
|
||||
a.group = i;
|
||||
a.name = groups[v.group].mid(slashIndex+1);
|
||||
a.name = groups[static_cast<int>(v.group)].mid(slashIndex+1);
|
||||
|
||||
}
|
||||
groupSizes[v.group]--;
|
||||
groupsToKill.append(v.group);
|
||||
groupSizes[static_cast<size_t>(v.group)]--;
|
||||
groupsToKill.append(static_cast<int>(v.group));
|
||||
arrays.append(a);
|
||||
values.removeAt(i);
|
||||
}
|
||||
|
@ -540,7 +552,7 @@ void SettingsSerializer::readIni()
|
|||
Array& a = arrays[ai];
|
||||
QString arrayPrefix;
|
||||
if (a.group != -1)
|
||||
arrayPrefix += groups[a.group]+'/';
|
||||
arrayPrefix += groups[static_cast<int>(a.group)]+'/';
|
||||
arrayPrefix += a.name+'/';
|
||||
|
||||
// Find groups which represent each array index
|
||||
|
@ -549,7 +561,7 @@ void SettingsSerializer::readIni()
|
|||
if (!groups[g].startsWith(arrayPrefix))
|
||||
continue;
|
||||
bool ok;
|
||||
quint64 groupArrayIndex = groups[g].mid(arrayPrefix.size()).toInt(&ok);
|
||||
int groupArrayIndex = groups[g].mid(arrayPrefix.size()).toInt(&ok);
|
||||
if (!ok)
|
||||
continue;
|
||||
groupsToKill.append(g);
|
||||
|
@ -558,12 +570,12 @@ void SettingsSerializer::readIni()
|
|||
a.size = groupArrayIndex;
|
||||
|
||||
// Associate the values for this array index
|
||||
for (int vi=values.size()-1; vi>=0; vi--)
|
||||
for (int vi = values.size() - 1; vi >= 0; vi--)
|
||||
{
|
||||
Value& v = values[vi];
|
||||
if (v.group != g)
|
||||
continue;
|
||||
groupSizes[g]--;
|
||||
groupSizes[static_cast<size_t>(g)]--;
|
||||
v.group = a.group;
|
||||
v.array = ai;
|
||||
v.arrayIndex = groupArrayIndex;
|
||||
|
@ -573,10 +585,12 @@ void SettingsSerializer::readIni()
|
|||
}
|
||||
|
||||
// Clean up spurious array element groups
|
||||
sort(begin(groupsToKill), end(groupsToKill), std::greater_equal<int>());
|
||||
std::sort(std::begin(groupsToKill), std::end(groupsToKill),
|
||||
std::greater_equal<int>());
|
||||
|
||||
for (int g : groupsToKill)
|
||||
{
|
||||
if (groupSizes[g])
|
||||
if (groupSizes[static_cast<size_t>(g)])
|
||||
continue;
|
||||
|
||||
removeGroup(g);
|
||||
|
|
|
@ -41,7 +41,7 @@ public:
|
|||
int beginReadArray(const QString &prefix);
|
||||
void beginWriteArray(const QString &prefix, int size = -1);
|
||||
void endArray();
|
||||
void setArrayIndex(unsigned i);
|
||||
void setArrayIndex(int i);
|
||||
|
||||
void setValue(const QString &key, const QVariant &value);
|
||||
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
|
||||
|
@ -61,10 +61,11 @@ private:
|
|||
struct Value
|
||||
{
|
||||
Value() : group{-2},array{-2},key{QString()},value{}{}
|
||||
Value(qint64 group, qint64 array, qint64 arrayIndex, QString key, QVariant value)
|
||||
Value(qint64 group, qint64 array, int arrayIndex, QString key, QVariant value)
|
||||
: group{group}, array{array}, arrayIndex{arrayIndex}, key{key}, value{value} {}
|
||||
qint64 group;
|
||||
qint64 array, arrayIndex;
|
||||
qint64 array;
|
||||
int arrayIndex;
|
||||
QString key;
|
||||
QVariant value;
|
||||
};
|
||||
|
@ -72,9 +73,9 @@ private:
|
|||
struct Array
|
||||
{
|
||||
qint64 group;
|
||||
quint64 size;
|
||||
int size;
|
||||
QString name;
|
||||
QVector<quint64> values;
|
||||
QVector<int> values;
|
||||
};
|
||||
|
||||
private:
|
||||
|
|
Loading…
Reference in New Issue
Block a user