File indexing completed on 2024-04-28 04:52:48

0001 /*
0002     SPDX-FileCopyrightText: 2023 Julius Künzel <jk.kdedev@smartlab.uber.space>
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "qstringutils.h"
0007 
0008 #include <QFileInfo>
0009 #include <QString>
0010 #include <QStringList>
0011 
0012 QString QStringUtils::getUniqueName(const QStringList &names, const QString &name)
0013 {
0014     int i = 0;
0015     QString newName = name;
0016     while (names.contains(newName)) {
0017         // name is not unique, add a suffix
0018         newName = name + QString("-%1").arg(i);
0019         i++;
0020     }
0021     return newName;
0022 }
0023 
0024 QString QStringUtils::getUniqueFileName(const QStringList &names, const QString &name)
0025 {
0026     int i = 0;
0027     const QString baseName = QFileInfo(name).completeBaseName();
0028     const QString extension = QFileInfo(name).suffix();
0029     QString newName = name;
0030     while (names.contains(newName)) {
0031         // name is not unique, add a suffix
0032         newName = baseName + QString::asprintf("-%04d.", ++i) + extension;
0033     }
0034     return newName;
0035 }
0036 
0037 QString QStringUtils::appendToFilename(const QString &filename, const QString &appendix)
0038 {
0039     QString name = filename.section(QLatin1Char('.'), 0, -2);
0040     QString extension = filename.section(QLatin1Char('.'), -1);
0041     return name + appendix + QLatin1Char('.') + extension;
0042 }