OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / shared / proparser / ioutils.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "ioutils.h"
34
35 #include <QtCore/QDir>
36 #include <QtCore/QFile>
37
38 #ifdef Q_OS_WIN
39 #  include <windows.h>
40 #else
41 #  include <sys/types.h>
42 #  include <sys/stat.h>
43 #  include <unistd.h>
44 #endif
45
46 using namespace ProFileEvaluatorInternal;
47
48 IoUtils::FileType IoUtils::fileType(const QString &fileName)
49 {
50     Q_ASSERT(fileName.isEmpty() || isAbsolutePath(fileName));
51 #ifdef Q_OS_WIN
52     DWORD attr = GetFileAttributesW((WCHAR*)fileName.utf16());
53     if (attr == INVALID_FILE_ATTRIBUTES)
54         return FileNotFound;
55     return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FileIsDir : FileIsRegular;
56 #else
57     struct ::stat st;
58     if (::stat(fileName.toLocal8Bit().constData(), &st))
59         return FileNotFound;
60     return S_ISDIR(st.st_mode) ? FileIsDir : FileIsRegular;
61 #endif
62 }
63
64 bool IoUtils::isRelativePath(const QString &path)
65 {
66     if (path.startsWith(QLatin1Char('/')))
67         return false;
68 #ifdef Q_OS_WIN
69     if (path.startsWith(QLatin1Char('\\')))
70         return false;
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('\\')))
75         return false;
76 #endif
77     return true;
78 }
79
80 QStringRef IoUtils::fileName(const QString &fileName)
81 {
82     return fileName.midRef(fileName.lastIndexOf(QLatin1Char('/')) + 1);
83 }
84
85 QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)
86 {
87     if (fileName.isEmpty())
88         return QString();
89     if (isAbsolutePath(fileName))
90         return QDir::cleanPath(fileName);
91     return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
92 }
93
94 inline static bool isSpecialChar(ushort c)
95 {
96     // Chars that should be quoted (TM). This includes:
97 #ifdef Q_OS_WIN
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
104     };
105 #else
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 \'"$`<>|;&(){}*?#!~[]
110 #endif
111
112     return (c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7)));
113 }
114
115 inline static bool hasSpecialChars(const QString &arg)
116 {
117     for (int x = arg.length() - 1; x >= 0; --x)
118         if (isSpecialChar(arg.unicode()[x].unicode()))
119             return true;
120     return false;
121 }
122
123 QString IoUtils::shellQuote(const QString &arg)
124 {
125     if (!arg.length())
126         return QString::fromLatin1("\"\"");
127
128     QString ret(arg);
129     if (hasSpecialChars(ret)) {
130 #ifdef Q_OS_WIN
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('\\'))
140             --i;
141         ret.insert(i, QLatin1Char('"'));
142         ret.prepend(QLatin1Char('"'));
143 #else // Q_OS_WIN
144         ret.replace(QLatin1Char('\''), QLatin1String("'\\''"));
145         ret.prepend(QLatin1Char('\''));
146         ret.append(QLatin1Char('\''));
147 #endif // Q_OS_WIN
148     }
149     return ret;
150 }