Added new overloads of make_temp_file() and make_unqiue_file() that take a QDir as parameter.
This commit is contained in:
parent
2c95becf74
commit
da57804590
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
//Forward Declarations
|
//Forward Declarations
|
||||||
class QProcess;
|
class QProcess;
|
||||||
|
class QDir;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@ -157,6 +158,7 @@ namespace MUtils
|
|||||||
* \return If the function succeeds, it returns a QString holding the full path of the temporary file; otherwise it returns a default-constructed QString.
|
* \return If the function succeeds, it returns a QString holding the full path of the temporary file; otherwise it returns a default-constructed QString.
|
||||||
*/
|
*/
|
||||||
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_temp_file(const QDir &basePath, const QString &extension, const bool placeholder = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Generates a unique file name.
|
* \brief Generates a unique file name.
|
||||||
@ -173,7 +175,8 @@ namespace MUtils
|
|||||||
*
|
*
|
||||||
* \return If the function succeeds, it returns a QString holding the full path of the unique file; otherwise it returns a default-constructed QString.
|
* \return If the function succeeds, it returns a QString holding the full path of the unique file; otherwise it returns a default-constructed QString.
|
||||||
*/
|
*/
|
||||||
MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy = false);
|
MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy = false, const bool placeholder = false);
|
||||||
|
MUTILS_API QString make_unique_file(const QDir &basePath, const QString &baseName, const QString &extension, const bool fancy = false, const bool placeholder = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Computes the *parity* of the given unsigned 32-Bit value
|
* \brief Computes the *parity* of the given unsigned 32-Bit value
|
||||||
|
@ -187,9 +187,20 @@ QString MUtils::trim_left(const QString &str)
|
|||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
return make_temp_file(QDir(basePath), extension, placeholder);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString MUtils::make_temp_file(const QDir &basePath, const QString &extension, const bool placeholder)
|
||||||
|
{
|
||||||
|
if (extension.isEmpty())
|
||||||
|
{
|
||||||
|
qWarning("Cannot generate temp file name with invalid parameters!");
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < 4096; i++)
|
for(int i = 0; i < 4096; i++)
|
||||||
{
|
{
|
||||||
const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
|
const QString tempFileName = basePath.absoluteFilePath(QString("%1.%2").arg(next_rand_str(), extension));
|
||||||
if(!QFileInfo(tempFileName).exists())
|
if(!QFileInfo(tempFileName).exists())
|
||||||
{
|
{
|
||||||
if(placeholder)
|
if(placeholder)
|
||||||
@ -212,21 +223,32 @@ QString MUtils::make_temp_file(const QString &basePath, const QString &extension
|
|||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
|
QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy, const bool placeholder)
|
||||||
{
|
{
|
||||||
|
return make_unique_file(QDir(basePath), baseName, extension, fancy);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString MUtils::make_unique_file(const QDir &basePath, const QString &baseName, const QString &extension, const bool fancy, const bool placeholder)
|
||||||
|
{
|
||||||
|
if (baseName.isEmpty() || extension.isEmpty())
|
||||||
|
{
|
||||||
|
qWarning("Cannot generate unique file name with invalid parameters!");
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
quint32 n = fancy ? 2 : 0;
|
quint32 n = fancy ? 2 : 0;
|
||||||
QString fileName = fancy ? QString("%1/%2.%3").arg(basePath, baseName, extension) : QString();
|
QString fileName = fancy ? basePath.absoluteFilePath(QString("%1.%2").arg(baseName, extension)) : QString();
|
||||||
while (fileName.isEmpty() || QFileInfo(fileName).exists())
|
while (fileName.isEmpty() || QFileInfo(fileName).exists())
|
||||||
{
|
{
|
||||||
if (n <= quint32(USHRT_MAX))
|
if (n <= quint32(USHRT_MAX))
|
||||||
{
|
{
|
||||||
if (fancy)
|
if (fancy)
|
||||||
{
|
{
|
||||||
fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
|
fileName = basePath.absoluteFilePath(QString("%1 (%2).%3").arg(baseName, QString::number(n++), extension));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
|
fileName = basePath.absoluteFilePath(QString("%1.%2.%3").arg(baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -235,6 +257,16 @@ QString MUtils::make_unique_file(const QString &basePath, const QString &baseNam
|
|||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (placeholder && (!fileName.isEmpty()))
|
||||||
|
{
|
||||||
|
QFile placeholder(fileName);
|
||||||
|
if (placeholder.open(QIODevice::WriteOnly))
|
||||||
|
{
|
||||||
|
placeholder.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user