~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to qmake/library/ioutils.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the qmake application of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "ioutils.h"
 
43
 
 
44
#include <qdir.h>
 
45
#include <qfile.h>
 
46
 
 
47
#ifdef Q_OS_WIN
 
48
#  include <windows.h>
 
49
#else
 
50
#  include <sys/types.h>
 
51
#  include <sys/stat.h>
 
52
#  include <unistd.h>
 
53
#endif
 
54
 
 
55
QT_BEGIN_NAMESPACE
 
56
 
 
57
using namespace QMakeInternal;
 
58
 
 
59
IoUtils::FileType IoUtils::fileType(const QString &fileName)
 
60
{
 
61
    Q_ASSERT(fileName.isEmpty() || isAbsolutePath(fileName));
 
62
#ifdef Q_OS_WIN
 
63
    DWORD attr = GetFileAttributesW((WCHAR*)fileName.utf16());
 
64
    if (attr == INVALID_FILE_ATTRIBUTES)
 
65
        return FileNotFound;
 
66
    return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FileIsDir : FileIsRegular;
 
67
#else
 
68
    struct ::stat st;
 
69
    if (::stat(fileName.toLocal8Bit().constData(), &st))
 
70
        return FileNotFound;
 
71
    return S_ISDIR(st.st_mode) ? FileIsDir : FileIsRegular;
 
72
#endif
 
73
}
 
74
 
 
75
bool IoUtils::isRelativePath(const QString &path)
 
76
{
 
77
    if (path.startsWith(QLatin1Char('/')))
 
78
        return false;
 
79
#ifdef Q_OS_WIN
 
80
    if (path.startsWith(QLatin1Char('\\')))
 
81
        return false;
 
82
    // Unlike QFileInfo, this won't accept a relative path with a drive letter.
 
83
    // Such paths result in a royal mess anyway ...
 
84
    if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter()
 
85
        && (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\')))
 
86
        return false;
 
87
#endif
 
88
    return true;
 
89
}
 
90
 
 
91
QStringRef IoUtils::fileName(const QString &fileName)
 
92
{
 
93
    return fileName.midRef(fileName.lastIndexOf(QLatin1Char('/')) + 1);
 
94
}
 
95
 
 
96
QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)
 
97
{
 
98
    if (fileName.isEmpty())
 
99
        return QString();
 
100
    if (isAbsolutePath(fileName))
 
101
        return QDir::cleanPath(fileName);
 
102
    return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
 
103
}
 
104
 
 
105
inline static
 
106
bool hasSpecialChars(const QString &arg, const uchar (&iqm)[16])
 
107
{
 
108
    for (int x = arg.length() - 1; x >= 0; --x) {
 
109
        ushort c = arg.unicode()[x].unicode();
 
110
        if ((c < sizeof(iqm) * 8) && (iqm[c / 8] & (1 << (c & 7))))
 
111
            return true;
 
112
    }
 
113
    return false;
 
114
}
 
115
 
 
116
QString IoUtils::shellQuoteUnix(const QString &arg)
 
117
{
 
118
    // Chars that should be quoted (TM). This includes:
 
119
    static const uchar iqm[] = {
 
120
        0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0x00, 0xd8,
 
121
        0x00, 0x00, 0x00, 0x38, 0x01, 0x00, 0x00, 0x78
 
122
    }; // 0-32 \'"$`<>|;&(){}*?#!~[]
 
123
 
 
124
    if (!arg.length())
 
125
        return QString::fromLatin1("\"\"");
 
126
 
 
127
    QString ret(arg);
 
128
    if (hasSpecialChars(ret, iqm)) {
 
129
        ret.replace(QLatin1Char('\''), QLatin1String("'\\''"));
 
130
        ret.prepend(QLatin1Char('\''));
 
131
        ret.append(QLatin1Char('\''));
 
132
    }
 
133
    return ret;
 
134
}
 
135
 
 
136
QString IoUtils::shellQuoteWin(const QString &arg)
 
137
{
 
138
    // Chars that should be quoted (TM). This includes:
 
139
    // - control chars & space
 
140
    // - the shell meta chars "&()<>^|
 
141
    // - the potential separators ,;=
 
142
    static const uchar iqm[] = {
 
143
        0xff, 0xff, 0xff, 0xff, 0x45, 0x13, 0x00, 0x78,
 
144
        0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x10
 
145
    };
 
146
 
 
147
    if (!arg.length())
 
148
        return QString::fromLatin1("\"\"");
 
149
 
 
150
    QString ret(arg);
 
151
    if (hasSpecialChars(ret, iqm)) {
 
152
        // Quotes are escaped and their preceding backslashes are doubled.
 
153
        // It's impossible to escape anything inside a quoted string on cmd
 
154
        // level, so the outer quoting must be "suspended".
 
155
        ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\"\\1\\1\\^\"\""));
 
156
        // The argument must not end with a \ since this would be interpreted
 
157
        // as escaping the quote -- rather put the \ behind the quote: e.g.
 
158
        // rather use "foo"\ than "foo\"
 
159
        int i = ret.length();
 
160
        while (i > 0 && ret.at(i - 1) == QLatin1Char('\\'))
 
161
            --i;
 
162
        ret.insert(i, QLatin1Char('"'));
 
163
        ret.prepend(QLatin1Char('"'));
 
164
    }
 
165
    return ret;
 
166
}
 
167
 
 
168
QT_END_NAMESPACE