summaryrefslogtreecommitdiff
path: root/src/libs/utils/stringutils.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@nokia.com>2009-11-25 12:34:56 +0100
committerFriedemann Kleint <Friedemann.Kleint@nokia.com>2009-11-25 12:34:56 +0100
commit7cbbada3f07b6084795bb5f4ea2e5551ed113d53 (patch)
treebf1dc0c8ed1c352219c281c49cccc3bb7dda08c6 /src/libs/utils/stringutils.cpp
parentefdd8ceaa0cefbfc8832a2a3d51ce77a8bf67f52 (diff)
downloadqt-creator-7cbbada3f07b6084795bb5f4ea2e5551ed113d53.tar.gz
Wizards: Improve list of files displayed.
Extract the common root directory and display in separate line. Use the routine from the cvs plugin for that, moving it to stringutils.h.
Diffstat (limited to 'src/libs/utils/stringutils.cpp')
-rw-r--r--src/libs/utils/stringutils.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/libs/utils/stringutils.cpp b/src/libs/utils/stringutils.cpp
new file mode 100644
index 0000000000..e809cb4786
--- /dev/null
+++ b/src/libs/utils/stringutils.cpp
@@ -0,0 +1,101 @@
+/**************************************************************************
+**
+** This file is part of Qt Creator
+**
+** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+**
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** Commercial Usage
+**
+** Licensees holding valid Qt Commercial licenses may use this file in
+** accordance with the Qt Commercial License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Nokia.
+**
+** GNU Lesser General Public License Usage
+**
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://qt.nokia.com/contact.
+**
+**************************************************************************/
+
+#include "stringutils.h"
+
+#include <QtCore/QString>
+#include <QtCore/QStringList>
+
+#include <limits.h>
+
+namespace Utils {
+
+QTCREATOR_UTILS_EXPORT QString settingsKey(const QString &category)
+{
+ QString rc(category);
+ const QChar underscore = QLatin1Char('_');
+ const int size = rc.size();
+ for (int i = 0; i < size; i++) {
+ const QChar c = rc.at(i);
+ if (!c.isLetterOrNumber() && c != underscore)
+ rc[i] = underscore;
+ }
+ return rc;
+}
+
+// Figure out length of common start of string ("C:\a", "c:\b" -> "c:\"
+static inline int commonPartSize(const QString &s1, const QString &s2)
+{
+ const int size = qMin(s1.size(), s2.size());
+ for (int i = 0; i < size; i++)
+ if (s1.at(i) != s2.at(i))
+ return i;
+ return size;
+}
+
+QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings)
+{
+ switch (strings.size()) {
+ case 0:
+ return QString();
+ case 1:
+ return strings.front();
+ default:
+ break;
+ }
+ // Figure out common string part: "C:\foo\bar1" "C:\foo\bar2" -> "C:\foo\bar"
+ int commonLength = INT_MAX;
+ const int last = strings.size() - 1;
+ for (int i = 0; i < last; i++)
+ commonLength = qMin(commonLength, commonPartSize(strings.at(i), strings.at(i + 1)));
+ if (!commonLength)
+ return QString();
+ return strings.at(0).left(commonLength);
+}
+
+QTCREATOR_UTILS_EXPORT QString commonPath(const QStringList &files)
+{
+ QString common = commonPrefix(files);
+ // Find common directory part: "C:\foo\bar" -> "C:\foo"
+ int lastSeparatorPos = common.lastIndexOf(QLatin1Char('/'));
+ if (lastSeparatorPos == -1)
+ lastSeparatorPos = common.lastIndexOf(QLatin1Char('\\'));
+ if (lastSeparatorPos == -1)
+ return QString();
+ if (lastSeparatorPos == -1)
+ return QString();
+#ifdef Q_OS_UNIX
+ if (lastSeparatorPos == 0) // Unix: "/a", "/b" -> '/'
+ lastSeparatorPos = 1;
+#endif
+ common.truncate(lastSeparatorPos);
+ return common;
+}
+
+} // namespace Utils