1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
35 #include <QtCore/QDir>
36 #include <QtCore/QFile>
41 # include <sys/types.h>
42 # include <sys/stat.h>
46 using namespace ProFileEvaluatorInternal;
48 IoUtils::FileType IoUtils::fileType(const QString &fileName)
50 Q_ASSERT(fileName.isEmpty() || isAbsolutePath(fileName));
52 DWORD attr = GetFileAttributesW((WCHAR*)fileName.utf16());
53 if (attr == INVALID_FILE_ATTRIBUTES)
55 return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FileIsDir : FileIsRegular;
58 if (::stat(fileName.toLocal8Bit().constData(), &st))
60 return S_ISDIR(st.st_mode) ? FileIsDir : FileIsRegular;
64 bool IoUtils::isRelativePath(const QString &path)
66 if (path.startsWith(QLatin1Char('/')))
69 if (path.startsWith(QLatin1Char('\\')))
71 // Unlike QFileInfo, this won't accept a relative path with a drive letter.
72 // Such paths result in a royal mess anyway ...
73 if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter()
74 && (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\')))
80 QStringRef IoUtils::fileName(const QString &fileName)
82 return fileName.midRef(fileName.lastIndexOf(QLatin1Char('/')) + 1);
85 QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)
87 if (fileName.isEmpty())
89 if (isAbsolutePath(fileName))
90 return QDir::cleanPath(fileName);
91 return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
94 inline static bool isSpecialChar(ushort c)
96 // Chars that should be quoted (TM). This includes:
98 // - control chars & space
99 // - the shell meta chars "&()<>^|
100 // - the potential separators ,;=
101 static const uchar iqm[] = {
102 0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78,
103 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10
106 static const uchar iqm[] = {
107 0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8,
108 0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78
109 }; // 0-32 \'"$`<>|;&(){}*?#!~[]
112 return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
115 inline static bool hasSpecialChars(const QString &arg)
117 for (int x = arg.length() - 1; x >= 0; --x)
118 if (isSpecialChar(arg.unicode()[x].unicode()))
123 QString IoUtils::shellQuote(const QString &arg)
126 return QString::fromLatin1("\"\"");
129 if (hasSpecialChars(ret)) {
131 // Quotes are escaped and their preceding backslashes are doubled.
132 // It's impossible to escape anything inside a quoted string on cmd
133 // level, so the outer quoting must be "suspended".
134 ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\"\\1\\1\\^\"\""));
135 // The argument must not end with a \ since this would be interpreted
136 // as escaping the quote -- rather put the \ behind the quote: e.g.
137 // rather use "foo"\ than "foo\"
138 int i = ret.length();
139 while (i > 0 && ret.at(i - 1) == QLatin1Char('\\'))
141 ret.insert(i, QLatin1Char('"'));
142 ret.prepend(QLatin1Char('"'));
144 ret.replace(QLatin1Char('\''), QLatin1String("'\\''"));
145 ret.prepend(QLatin1Char('\''));
146 ret.append(QLatin1Char('\''));