-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniqueFileName.h
37 lines (34 loc) · 1.11 KB
/
UniqueFileName.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef UNIQUEFILENAME_H
#define UNIQUEFILENAME_H
#include <QString>
#include <QFileInfo>
#include <QDir>
class UniqueFileName
{
public:
explicit UniqueFileName();
static inline QString newName(QString str)
{
int index=1;
QString fileName=str;
QFileInfo fileinfo;
fileinfo.setFile(fileName);
while(fileinfo.exists()){
QString completeName;
completeName=fileinfo.completeBaseName();
if(fileinfo.completeBaseName().endsWith( QString::number(index).prepend(" (").append(")") ))
{
completeName=completeName.left( completeName.lastIndexOf( QString::number(index).prepend(" (").append(")") ) );
index++;
}
fileName=fileinfo.path()
.append(QDir::separator())
.append( completeName )
.append( QString::number(index).prepend(" (").append(")") )
.append( fileinfo.suffix().prepend(".") );
fileinfo.setFile(fileName);
}
return fileName;
}
};
#endif // UNIQUEFILENAME_H