mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-24 02:42:48 +00:00
kdecore: search backwards for the template and stop at directory separator in KTemporaryFile::filePath()
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
49371e1370
commit
95a8d172be
2 changed files with 12 additions and 5 deletions
|
@ -79,7 +79,6 @@ void KTemporaryFile::setSuffix(const QString &suffix)
|
||||||
|
|
||||||
QString KTemporaryFile::filePath(const QString &pathtemplate)
|
QString KTemporaryFile::filePath(const QString &pathtemplate)
|
||||||
{
|
{
|
||||||
static QChar xchar = QChar::fromLatin1('X');
|
|
||||||
static QChar underscorechar = QChar::fromLatin1('_');
|
static QChar underscorechar = QChar::fromLatin1('_');
|
||||||
static const char tmpnamechars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
static const char tmpnamechars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
@ -94,10 +93,14 @@ QString KTemporaryFile::filePath(const QString &pathtemplate)
|
||||||
}
|
}
|
||||||
|
|
||||||
QString result = pathtemplate;
|
QString result = pathtemplate;
|
||||||
int xindex = result.indexOf(xchar);
|
int counter = result.size();
|
||||||
while (xindex != -1) {
|
while (counter) {
|
||||||
result.replace(xindex, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)]));
|
counter--;
|
||||||
xindex = result.indexOf(xchar, xindex + 1);
|
if (result.at(counter) == QLatin1Char('/')) {
|
||||||
|
break;
|
||||||
|
} else if (result.at(counter) == QLatin1Char('X')) {
|
||||||
|
result.replace(counter, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!QDir::isAbsolutePath(result)) {
|
if (!QDir::isAbsolutePath(result)) {
|
||||||
result.prepend(underscorechar);
|
result.prepend(underscorechar);
|
||||||
|
|
|
@ -109,4 +109,8 @@ void KTemporaryFileTest::testFilePath()
|
||||||
tmpfilepath = KTemporaryFile::filePath("/foo/bar/XXXXX.tmp");
|
tmpfilepath = KTemporaryFile::filePath("/foo/bar/XXXXX.tmp");
|
||||||
QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/bar/")));
|
QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/bar/")));
|
||||||
QVERIFY(!tmpfilepath.contains("XXXXX"));
|
QVERIFY(!tmpfilepath.contains("XXXXX"));
|
||||||
|
|
||||||
|
tmpfilepath = KTemporaryFile::filePath("/foo/X/bar/XXXXX.tmp");
|
||||||
|
QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/X/bar/")));
|
||||||
|
QVERIFY(!tmpfilepath.contains("XXXXX"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue