Renamed functions for consistency.

This commit is contained in:
LoRd_MuldeR 2016-12-16 19:23:35 +01:00
parent 83d1473a6b
commit 5dbe6b3a19
4 changed files with 28 additions and 27 deletions

View File

@ -80,9 +80,9 @@ namespace MUtils
//Random //Random
MUTILS_API void seed_rand(void); MUTILS_API void seed_rand(void);
MUTILS_API QString rand_str(const bool &bLong = false); MUTILS_API QString next_rand_str(const bool &bLong = false);
MUTILS_API quint32 next_rand32(void); MUTILS_API quint32 next_rand_u32(void);
MUTILS_API quint64 next_rand64(void); MUTILS_API quint64 next_rand_u64(void);
//File Name //File Name
MUTILS_API QString make_temp_file(const QString &basePath, const QString &extension, const bool placeholder = false); MUTILS_API QString make_temp_file(const QString &basePath, const QString &extension, const bool placeholder = false);

View File

@ -64,7 +64,7 @@ namespace MUtils
} }
for(int i = 0; i < 32; i++) for(int i = 0; i < 32; i++)
{ {
m_lockFile.reset(new QFile(QString("%1/~%2.lck").arg(m_dirPath, MUtils::rand_str()))); m_lockFile.reset(new QFile(QString("%1/~%2.lck").arg(m_dirPath, MUtils::next_rand_str())));
if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) if(m_lockFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered))
{ {
if(m_lockFile->write(testData) >= testData.size()) if(m_lockFile->write(testData) >= testData.size())

View File

@ -54,6 +54,10 @@
// Random Support // Random Support
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#ifndef _CRT_RAND_S
#define rand_s(X) (true)
#endif
//Robert Jenkins' 96 bit Mix Function //Robert Jenkins' 96 bit Mix Function
static unsigned int mix_function(const unsigned int x, const unsigned int y, const unsigned int z) static unsigned int mix_function(const unsigned int x, const unsigned int y, const unsigned int z)
{ {
@ -79,37 +83,34 @@ void MUtils::seed_rand(void)
qsrand(mix_function(clock(), time(NULL), _getpid())); qsrand(mix_function(clock(), time(NULL), _getpid()));
} }
quint32 MUtils::next_rand32(void) quint32 MUtils::next_rand_u32(void)
{ {
quint32 rnd = 0xDEADBEEF; quint32 rnd;
if (rand_s(&rnd))
#ifdef _CRT_RAND_S
if(rand_s(&rnd) == 0)
{ {
return rnd; for (size_t i = 0; i < sizeof(quint32); i += 2)
}
#endif //_CRT_RAND_S
for(size_t i = 0; i < sizeof(quint32); i++)
{ {
rnd = (rnd << 8) ^ qrand(); rnd = (rnd << 16) ^ qrand();
}
} }
return rnd; return rnd;
} }
quint64 MUtils::next_rand64(void) quint64 MUtils::next_rand_u64(void)
{ {
return (quint64(next_rand32()) << 32) | quint64(next_rand32()); return (quint64(next_rand_u32()) << 32) | quint64(next_rand_u32());
} }
QString MUtils::rand_str(const bool &bLong) QString MUtils::next_rand_str(const bool &bLong)
{ {
if(!bLong) if (bLong)
{ {
return QString::number(next_rand64(), 16).rightJustified(16, QLatin1Char('0')); return next_rand_str(false) + next_rand_str(false);
}
else
{
return QString::number(next_rand_u64(), 16).rightJustified(16, QLatin1Char('0'));
} }
return QString("%1%2").arg(rand_str(false), rand_str(false));
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -163,7 +164,7 @@ QString MUtils::make_temp_file(const QString &basePath, const QString &extension
{ {
for(int i = 0; i < 4096; i++) for(int i = 0; i < 4096; i++)
{ {
const QString tempFileName = QString("%1/%2.%3").arg(basePath, rand_str(), extension); const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
if(!QFileInfo(tempFileName).exists()) if(!QFileInfo(tempFileName).exists())
{ {
if(placeholder) if(placeholder)
@ -252,7 +253,7 @@ static QString try_create_subfolder(const QString &baseDir, const QString &postf
static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir) static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
{ {
const QString tempPath = try_create_subfolder(baseDir, MUtils::rand_str()); const QString tempPath = try_create_subfolder(baseDir, MUtils::next_rand_str());
if(!tempPath.isEmpty()) if(!tempPath.isEmpty())
{ {
for(int i = 0; i < 32; i++) for(int i = 0; i < 32; i++)

View File

@ -218,7 +218,7 @@ static QStringList buildRandomList(const char *const values[])
QStringList list; QStringList list;
for (int index = 0; values[index]; index++) for (int index = 0; values[index]; index++)
{ {
const int pos = next_rand32() % (index + 1); const int pos = next_rand_u32() % (index + 1);
list.insert(pos, QString::fromLatin1(values[index])); list.insert(pos, QString::fromLatin1(values[index]));
} }
return list; return list;
@ -455,7 +455,7 @@ bool UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString
bool success = false; bool success = false;
log("", "Trying mirror:", url); log("", "Trying mirror:", url);
const QString randPart = rand_str(); const QString randPart = next_rand_str();
const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart); const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart); const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);