Added function to generate unique file name, using an increasing counter.

This commit is contained in:
LoRd_MuldeR 2016-12-14 15:04:23 +01:00
parent ed6167b001
commit f9dd32c6e3
2 changed files with 30 additions and 3 deletions

View File

@ -84,8 +84,9 @@ namespace MUtils
MUTILS_API quint32 next_rand32(void); MUTILS_API quint32 next_rand32(void);
MUTILS_API quint64 next_rand64(void); MUTILS_API quint64 next_rand64(void);
//Temp 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);
MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy = false);
//Parity //Parity
MUTILS_API bool parity(quint32 value); MUTILS_API bool parity(quint32 value);

View File

@ -112,7 +112,7 @@ QString MUtils::rand_str(const bool &bLong)
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// GET TEMP FILE NAME // GENERATE FILE NAME
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder) QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
@ -138,10 +138,36 @@ QString MUtils::make_temp_file(const QString &basePath, const QString &extension
} }
} }
qWarning("Failed to generate unique temp file name!"); qWarning("Failed to generate temp file name!");
return QString(); return QString();
} }
QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
{
quint32 n = fancy ? 2 : 0;
QString fileName = fancy ? QString("%1/%2.%3").arg(basePath, baseName, extension) : QString();
while (fileName.isEmpty() || QFileInfo(fileName).exists())
{
if (n <= quint32(USHRT_MAX))
{
if (fancy)
{
fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
}
else
{
fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
}
}
else
{
qWarning("Failed to generate unique file name!");
return QString();
}
}
return fileName;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// COMPUTE PARITY // COMPUTE PARITY
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////