MUtilities development repository
修订版 | da5780459069bf50a233449aaf22df59aa189c05 (tree) |
---|---|
时间 | 2018-04-30 20:55:44 |
作者 | ![]() |
Commiter | LoRd_MuldeR |
Added new overloads of make_temp_file() and make_unqiue_file() that take a QDir as parameter.
@@ -30,6 +30,7 @@ | ||
30 | 30 | |
31 | 31 | //Forward Declarations |
32 | 32 | class QProcess; |
33 | +class QDir; | |
33 | 34 | |
34 | 35 | /////////////////////////////////////////////////////////////////////////////// |
35 | 36 |
@@ -157,6 +158,7 @@ namespace MUtils | ||
157 | 158 | * \return If the function succeeds, it returns a QString holding the full path of the temporary file; otherwise it returns a default-constructed QString. |
158 | 159 | */ |
159 | 160 | 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); | |
160 | 162 | |
161 | 163 | /** |
162 | 164 | * \brief Generates a unique file name. |
@@ -173,7 +175,8 @@ namespace MUtils | ||
173 | 175 | * |
174 | 176 | * \return If the function succeeds, it returns a QString holding the full path of the unique file; otherwise it returns a default-constructed QString. |
175 | 177 | */ |
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); | |
177 | 180 | |
178 | 181 | /** |
179 | 182 | * \brief Computes the *parity* of the given unsigned 32-Bit value |
@@ -187,9 +187,20 @@ QString MUtils::trim_left(const QString &str) | ||
187 | 187 | |
188 | 188 | QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder) |
189 | 189 | { |
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 | + | |
190 | 201 | for(int i = 0; i < 4096; i++) |
191 | 202 | { |
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)); | |
193 | 204 | if(!QFileInfo(tempFileName).exists()) |
194 | 205 | { |
195 | 206 | if(placeholder) |
@@ -212,21 +223,32 @@ QString MUtils::make_temp_file(const QString &basePath, const QString &extension | ||
212 | 223 | return QString(); |
213 | 224 | } |
214 | 225 | |
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) | |
216 | 232 | { |
233 | + if (baseName.isEmpty() || extension.isEmpty()) | |
234 | + { | |
235 | + qWarning("Cannot generate unique file name with invalid parameters!"); | |
236 | + return QString(); | |
237 | + } | |
238 | + | |
217 | 239 | 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(); | |
219 | 241 | while (fileName.isEmpty() || QFileInfo(fileName).exists()) |
220 | 242 | { |
221 | 243 | if (n <= quint32(USHRT_MAX)) |
222 | 244 | { |
223 | 245 | if (fancy) |
224 | 246 | { |
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)); | |
226 | 248 | } |
227 | 249 | else |
228 | 250 | { |
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)); | |
230 | 252 | } |
231 | 253 | } |
232 | 254 | else |
@@ -235,6 +257,16 @@ QString MUtils::make_unique_file(const QString &basePath, const QString &baseNam | ||
235 | 257 | return QString(); |
236 | 258 | } |
237 | 259 | } |
260 | + | |
261 | + if (placeholder && (!fileName.isEmpty())) | |
262 | + { | |
263 | + QFile placeholder(fileName); | |
264 | + if (placeholder.open(QIODevice::WriteOnly)) | |
265 | + { | |
266 | + placeholder.close(); | |
267 | + } | |
268 | + } | |
269 | + | |
238 | 270 | return fileName; |
239 | 271 | } |
240 | 272 |