Qt 4.8
qfilesystementry.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module 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 "qfilesystementry_p.h"
43 
44 #include <QtCore/qdir.h>
45 #include <QtCore/qfile.h>
46 #include <QtCore/private/qfsfileengine_p.h>
47 #ifdef Q_OS_WIN
48 #include <QtCore/qstringbuilder.h>
49 #endif
50 
52 
53 #ifdef Q_OS_WIN
54 static bool isUncRoot(const QString &server)
55 {
56  QString localPath = QDir::toNativeSeparators(server);
57  if (!localPath.startsWith(QLatin1String("\\\\")))
58  return false;
59 
60  int idx = localPath.indexOf(QLatin1Char('\\'), 2);
61  if (idx == -1 || idx + 1 == localPath.length())
62  return true;
63 
64  localPath = localPath.right(localPath.length() - idx - 1).trimmed();
65  return localPath.isEmpty();
66 }
67 
68 static inline QString fixIfRelativeUncPath(const QString &path)
69 {
70  QString currentPath = QDir::currentPath();
71  if (currentPath.startsWith(QLatin1String("//")))
72  return currentPath % QChar(QLatin1Char('/')) % path;
73  return path;
74 }
75 #endif
76 
78  : m_lastSeparator(0),
79  m_firstDotInFileName(0),
80  m_lastDotInFileName(0)
81 {
82 }
83 
93  : m_filePath(QDir::fromNativeSeparators(filePath)),
94  m_lastSeparator(-2),
97 {
98 }
99 
109  : m_filePath(filePath),
110  m_lastSeparator(-2),
113 {
114 }
115 
121  : m_nativeFilePath(nativeFilePath),
122  m_lastSeparator(-2),
125 {
126 }
127 
129  : m_filePath(QDir::fromNativeSeparators(filePath)),
130  m_nativeFilePath(nativeFilePath),
131  m_lastSeparator(-2),
134 {
135 }
136 
138 {
139  resolveFilePath();
140  return m_filePath;
141 }
142 
144 {
146  return m_nativeFilePath;
147 }
148 
150 {
152 #if defined(QFILESYSTEMENTRY_NATIVE_PATH_IS_UTF16)
154 #ifdef Q_OS_WIN
155  if (m_filePath.startsWith(QLatin1String("//?/UNC/")))
157  if (m_filePath.startsWith(QLatin1String("//?/")))
159 #endif
160 #else
162 #endif
163  }
164 }
165 
167 {
169 #ifdef Q_OS_WIN
171  if (isRelative())
172  filePath = fixIfRelativeUncPath(m_filePath);
174 #elif defined(QFILESYSTEMENTRY_NATIVE_PATH_IS_UTF16)
176 #else
178 #endif
179  }
180 }
181 
183 {
185 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
186  if (m_lastSeparator == -1 && m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':'))
187  return m_filePath.mid(2);
188 #endif
189  return m_filePath.mid(m_lastSeparator + 1);
190 }
191 
193 {
195  if (m_lastSeparator == -1) {
196 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
197  if (m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':'))
198  return m_filePath.left(2);
199 #endif
200  return QString(QLatin1Char('.'));
201  }
202  if (m_lastSeparator == 0)
203  return QString(QLatin1Char('/'));
204 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
205  if (m_lastSeparator == 2 && m_filePath.at(1) == QLatin1Char(':'))
206  return m_filePath.left(m_lastSeparator + 1);
207 #endif
209 }
210 
212 {
214  int length = -1;
215  if (m_firstDotInFileName >= 0) {
216  length = m_firstDotInFileName;
217  if (m_lastSeparator != -1) // avoid off by one
218  length--;
219  }
220 #if defined(Q_OS_WIN)
221  if (m_lastSeparator == -1 && m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':'))
222  return m_filePath.mid(2, length - 2);
223 #endif
224  return m_filePath.mid(m_lastSeparator + 1, length);
225 }
226 
228 {
230  int length = -1;
231  if (m_firstDotInFileName >= 0) {
233  if (m_lastSeparator != -1) // avoid off by one
234  length--;
235  }
236 #if defined(Q_OS_WIN)
237  if (m_lastSeparator == -1 && m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':'))
238  return m_filePath.mid(2, length - 2);
239 #endif
240  return m_filePath.mid(m_lastSeparator + 1, length);
241 }
242 
244 {
246 
247  if (m_lastDotInFileName == -1)
248  return QString();
249 
251 }
252 
254 {
256  if (m_firstDotInFileName == -1)
257  return QString();
258 
260 }
261 
262 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
264 {
265  resolveFilePath();
266  return (m_filePath.isEmpty() || (!m_filePath.isEmpty() && (m_filePath[0].unicode() != '/')
267  && (!(m_filePath.length() >= 2 && m_filePath[1].unicode() == ':'))));
268 }
269 
271 {
272  resolveFilePath();
273  return (!m_filePath.isEmpty() && ((m_filePath.length() >= 3
274  && (m_filePath[0].isLetter() && m_filePath[1].unicode() == ':' && m_filePath[2].unicode() == '/'))
275 #ifdef Q_OS_WIN
276  || (m_filePath.length() >= 2 && (m_filePath.at(0) == QLatin1Char('/') && m_filePath.at(1) == QLatin1Char('/')))
277 #endif
278  ));
279 }
280 #else
281 bool QFileSystemEntry::isRelative() const
282 {
283  return !isAbsolute();
284 }
285 
286 bool QFileSystemEntry::isAbsolute() const
287 {
288  resolveFilePath();
289  return (!m_filePath.isEmpty() && (m_filePath[0].unicode() == '/'));
290 }
291 #endif
292 
293 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
295 {
296  resolveFilePath();
297  return (m_filePath.length() == 3
298  && m_filePath.at(0).isLetter() && m_filePath.at(1) == QLatin1Char(':')
299  && m_filePath.at(2) == QLatin1Char('/'));
300 }
301 #endif
302 
304 {
305  resolveFilePath();
306  if (m_filePath == QLatin1String("/")
307 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
308  || isDriveRoot()
309 #if defined(Q_OS_WIN)
311 #endif
312 #endif
313  )
314  return true;
315 
316  return false;
317 }
318 
320 {
322  return m_nativeFilePath.isEmpty();
323 }
324 
325 // private methods
326 
328 {
329  if (m_lastSeparator == -2) {
330  resolveFilePath();
331  m_lastSeparator = -1;
332  for (int i = m_filePath.size() - 1; i >= 0; --i) {
333  if (m_filePath[i].unicode() == '/') {
334  m_lastSeparator = i;
335  break;
336  }
337  }
338  }
339 }
340 
342 {
343  if (m_firstDotInFileName == -2) {
344  resolveFilePath();
345  int firstDotInFileName = -1;
346  int lastDotInFileName = -1;
347  int lastSeparator = m_lastSeparator;
348 
349  int stop;
350  if (lastSeparator < 0) {
351  lastSeparator = -1;
352  stop = 0;
353  } else {
354  stop = lastSeparator;
355  }
356 
357  int i = m_filePath.size() - 1;
358  for (; i >= stop; --i) {
359  if (m_filePath[i].unicode() == '.') {
360  firstDotInFileName = lastDotInFileName = i;
361  break;
362  } else if (m_filePath[i].unicode() == '/') {
363  lastSeparator = i;
364  break;
365  }
366  }
367 
368  if (lastSeparator != i) {
369  for (--i; i >= stop; --i) {
370  if (m_filePath[i].unicode() == '.')
371  firstDotInFileName = i;
372  else if (m_filePath[i].unicode() == '/') {
373  lastSeparator = i;
374  break;
375  }
376  }
377  }
378  m_lastSeparator = lastSeparator;
379  m_firstDotInFileName = firstDotInFileName == -1 ? -1 : firstDotInFileName - qMax(0, lastSeparator);
380  if (lastDotInFileName == -1)
381  m_lastDotInFileName = -1;
382  else if (firstDotInFileName == lastDotInFileName)
384  else
385  m_lastDotInFileName = lastDotInFileName - firstDotInFileName;
386  }
387 }
388 
390 {
391  resolveFilePath();
392  int dots = 0;
393  bool dotok = true; // checking for ".." or "." starts to relative paths
394  bool slashok = true;
395  for (QString::const_iterator iter = m_filePath.constBegin(); iter != m_filePath.constEnd(); iter++) {
396  if (*iter == QLatin1Char('/')) {
397  if (dots == 1 || dots == 2)
398  return false; // path contains "./" or "../"
399  if (!slashok)
400  return false; // path contains "//"
401  dots = 0;
402  dotok = true;
403  slashok = false;
404  } else if (dotok) {
405  slashok = true;
406  if (*iter == QLatin1Char('.')) {
407  dots++;
408  if (dots > 2)
409  dotok = false;
410  } else {
411  //path element contains a character other than '.', it's clean
412  dots = 0;
413  dotok = false;
414  }
415  }
416  }
417  return (dots != 1 && dots != 2); // clean if path doesn't end in . or ..
418 }
419 
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
void resolveNativeFilePath() const
bool isLetter() const
Returns true if the character is a letter (Letter_* categories); otherwise returns false...
Definition: qchar.cpp:653
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
void findLastSeparator() const
static QString fixIfRelativeUncPath(const QString &path)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isAbsolute() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString currentPath()
Returns the absolute path of the application&#39;s current directory.
Definition: qdir.cpp:1875
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static bool isUncRoot(const QString &server)
static QString longFileName(const QString &path)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
NativePath nativeFilePath() const
static QString decodeName(const QByteArray &localFileName)
This does the reverse of QFile::encodeName() using localFileName.
Definition: qfile.cpp:552
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first character in the string. ...
Definition: qstring.h:892
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
NativePath m_nativeFilePath
short qint16
Definition: qglobal.h:935
QString fileName() const
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qstring.h:898
QString right(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n rightmost characters of the string.
Definition: qstring.cpp:3682
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
static QAuServer & server()
Definition: qsound.cpp:79
QString trimmed(QString source)
Definition: generator.cpp:233
bool isDriveRoot() const
QString completeBaseName() const
QString baseName() const
void findFileNameSeparators() const
QString filePath() const
#define Q_OS_WIN
Definition: qglobal.h:270
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
bool isRelative() const
QString suffix() const
static QString fromNativeSeparators(const QString &pathName)
Returns pathName using &#39;/&#39; as file separator.
Definition: qdir.cpp:848
void resolveFilePath() const
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
QString completeSuffix() const
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QString path() const