• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版da5780459069bf50a233449aaf22df59aa189c05 (tree)
时间2018-04-30 20:55:44
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Added new overloads of make_temp_file() and make_unqiue_file() that take a QDir as parameter.

更改概述

差异

--- a/include/MUtils/Global.h
+++ b/include/MUtils/Global.h
@@ -30,6 +30,7 @@
3030
3131 //Forward Declarations
3232 class QProcess;
33+class QDir;
3334
3435 ///////////////////////////////////////////////////////////////////////////////
3536
@@ -157,6 +158,7 @@ namespace MUtils
157158 * \return If the function succeeds, it returns a QString holding the full path of the temporary file; otherwise it returns a default-constructed QString.
158159 */
159160 MUTILS_API QString make_temp_file(const QString &basePath, const QString &extension, const bool placeholder = false);
161+ MUTILS_API QString make_temp_file(const QDir &basePath, const QString &extension, const bool placeholder = false);
160162
161163 /**
162164 * \brief Generates a unique file name.
@@ -173,7 +175,8 @@ namespace MUtils
173175 *
174176 * \return If the function succeeds, it returns a QString holding the full path of the unique file; otherwise it returns a default-constructed QString.
175177 */
176- MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy = false);
178+ MUTILS_API QString make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy = false, const bool placeholder = false);
179+ MUTILS_API QString make_unique_file(const QDir &basePath, const QString &baseName, const QString &extension, const bool fancy = false, const bool placeholder = false);
177180
178181 /**
179182 * \brief Computes the *parity* of the given unsigned 32-Bit value
--- a/src/Global.cpp
+++ b/src/Global.cpp
@@ -187,9 +187,20 @@ QString MUtils::trim_left(const QString &str)
187187
188188 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
189189 {
190+ return make_temp_file(QDir(basePath), extension, placeholder);
191+}
192+
193+QString MUtils::make_temp_file(const QDir &basePath, const QString &extension, const bool placeholder)
194+{
195+ if (extension.isEmpty())
196+ {
197+ qWarning("Cannot generate temp file name with invalid parameters!");
198+ return QString();
199+ }
200+
190201 for(int i = 0; i < 4096; i++)
191202 {
192- const QString tempFileName = QString("%1/%2.%3").arg(basePath, next_rand_str(), extension);
203+ const QString tempFileName = basePath.absoluteFilePath(QString("%1.%2").arg(next_rand_str(), extension));
193204 if(!QFileInfo(tempFileName).exists())
194205 {
195206 if(placeholder)
@@ -212,21 +223,32 @@ QString MUtils::make_temp_file(const QString &basePath, const QString &extension
212223 return QString();
213224 }
214225
215-QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy)
226+QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy, const bool placeholder)
227+{
228+ return make_unique_file(QDir(basePath), baseName, extension, fancy);
229+}
230+
231+QString MUtils::make_unique_file(const QDir &basePath, const QString &baseName, const QString &extension, const bool fancy, const bool placeholder)
216232 {
233+ if (baseName.isEmpty() || extension.isEmpty())
234+ {
235+ qWarning("Cannot generate unique file name with invalid parameters!");
236+ return QString();
237+ }
238+
217239 quint32 n = fancy ? 2 : 0;
218- QString fileName = fancy ? QString("%1/%2.%3").arg(basePath, baseName, extension) : QString();
240+ QString fileName = fancy ? basePath.absoluteFilePath(QString("%1.%2").arg(baseName, extension)) : QString();
219241 while (fileName.isEmpty() || QFileInfo(fileName).exists())
220242 {
221243 if (n <= quint32(USHRT_MAX))
222244 {
223245 if (fancy)
224246 {
225- fileName = QString("%1/%2 (%3).%4").arg(basePath, baseName, QString::number(n++), extension);
247+ fileName = basePath.absoluteFilePath(QString("%1 (%2).%3").arg(baseName, QString::number(n++), extension));
226248 }
227249 else
228250 {
229- fileName = QString("%1/%2.%3.%4").arg(basePath, baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension);
251+ fileName = basePath.absoluteFilePath(QString("%1.%2.%3").arg(baseName, QString::number(n++, 16).rightJustified(4, QLatin1Char('0')), extension));
230252 }
231253 }
232254 else
@@ -235,6 +257,16 @@ QString MUtils::make_unique_file(const QString &basePath, const QString &baseNam
235257 return QString();
236258 }
237259 }
260+
261+ if (placeholder && (!fileName.isEmpty()))
262+ {
263+ QFile placeholder(fileName);
264+ if (placeholder.open(QIODevice::WriteOnly))
265+ {
266+ placeholder.close();
267+ }
268+ }
269+
238270 return fileName;
239271 }
240272
Show on old repository browser