Qt 4.8
qsystemerror.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 <qglobal.h>
43 #include "qsystemerror_p.h"
44 #if !defined(Q_OS_WINCE)
45 # include <errno.h>
46 # if defined(Q_CC_MSVC)
47 # include <crtdbg.h>
48 # endif
49 #else
50 # if (_WIN32_WCE >= 0x700)
51 # include <errno.h>
52 # endif
53 #endif
54 #ifdef Q_OS_WIN
55 #include <windows.h>
56 #endif
57 
59 
60 #if !defined(Q_OS_WIN) && !defined(QT_NO_THREAD) && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX) && \
61  defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L
62 namespace {
63  // There are two incompatible versions of strerror_r:
64  // a) the XSI/POSIX.1 version, which returns an int,
65  // indicating success or not
66  // b) the GNU version, which returns a char*, which may or may not
67  // be the beginning of the buffer we used
68  // The GNU libc manpage for strerror_r says you should use the the XSI
69  // version in portable code. However, it's impossible to do that if
70  // _GNU_SOURCE is defined so we use C++ overloading to decide what to do
71  // depending on the return type
72  static inline QString fromstrerror_helper(int, const QByteArray &buf)
73  {
74  return QString::fromLocal8Bit(buf);
75  }
76  static inline QString fromstrerror_helper(const char *str, const QByteArray &)
77  {
78  return QString::fromLocal8Bit(str);
79  }
80 }
81 #endif
82 
83 #ifdef Q_OS_WIN
84 static QString windowsErrorString(int errorCode)
85 {
86  QString ret;
87  wchar_t *string = 0;
88  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
89  NULL,
90  errorCode,
91  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
92  (LPWSTR)&string,
93  0,
94  NULL);
95  ret = QString::fromWCharArray(string);
96  LocalFree((HLOCAL)string);
97 
98  if (ret.isEmpty() && errorCode == ERROR_MOD_NOT_FOUND)
99  ret = QString::fromLatin1("The specified module could not be found.");
100  return ret;
101 }
102 #endif
103 
104 static QString standardLibraryErrorString(int errorCode)
105 {
106  const char *s = 0;
107  QString ret;
108  switch (errorCode) {
109  case 0:
110  break;
111  case EACCES:
112  s = QT_TRANSLATE_NOOP("QIODevice", "Permission denied");
113  break;
114  case EMFILE:
115  s = QT_TRANSLATE_NOOP("QIODevice", "Too many open files");
116  break;
117  case ENOENT:
118  s = QT_TRANSLATE_NOOP("QIODevice", "No such file or directory");
119  break;
120  case ENOSPC:
121  s = QT_TRANSLATE_NOOP("QIODevice", "No space left on device");
122  break;
123  default: {
124  #ifdef Q_OS_WINCE
125  ret = windowsErrorString(errorCode);
126  #else
127  #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX)
128  QByteArray buf(1024, '\0');
129  ret = fromstrerror_helper(strerror_r(errorCode, buf.data(), buf.size()), buf);
130  #else
131  ret = QString::fromLocal8Bit(strerror(errorCode));
132  #endif
133  #endif
134  break; }
135  }
136  if (s) {
137  // ######## this breaks moc build currently
138  // ret = QCoreApplication::translate("QIODevice", s);
139  ret = QString::fromLatin1(s);
140  }
141  return ret.trimmed();
142 }
143 
144 #ifdef Q_OS_SYMBIAN
145 static QString symbianErrorString(int errorCode)
146 {
147  switch (errorCode) {
148  case KErrNotFound:
149  return QLatin1String("not found");
150  case KErrCancel:
151  return QLatin1String("cancelled");
152  case KErrNoMemory:
153  return QLatin1String("out of memory");
154  case KErrNotSupported:
155  return QLatin1String("not supported");
156  case KErrBadHandle:
157  return QLatin1String("bad handle"); //KERN-EXEC 0 panic is more likely
158  case KErrAlreadyExists:
159  return QLatin1String("already exists");
160  case KErrPathNotFound:
161  return QLatin1String("path not found");
162  case KErrInUse:
163  return QLatin1String("in use");
164  case KErrNotReady:
165  return QLatin1String("not ready (e.g. FS dismounted, network down)");
166  case KErrCorrupt:
167  return QLatin1String("corrupt");
168  case KErrAccessDenied:
169  return QLatin1String("access denied");
170  case KErrLocked:
171  return QLatin1String("locked");
172  case KErrWrite:
173  return QLatin1String("incomplete write error");
174  case KErrDisMounted:
175  return QLatin1String("file system dismounted during operation"); //i.e. a forcible dismount was done while we had files open
176  case KErrEof:
177  return QLatin1String("end of file");
178  case KErrDiskFull:
179  return QLatin1String("no space in file system");
180  case KErrBadName:
181  return QLatin1String("invalid filename");
182  case KErrTimedOut:
183  return QLatin1String("timed out");
184  case KErrBadDescriptor:
185  return QLatin1String("bad descriptor (passed address on stack to async call?)");
186  case KErrAbort:
187  return QLatin1String("aborted");
188  case KErrTooBig:
189  return QLatin1String("too big"); //e.g. trying to open a >2GB file with 32 bit API
190  case KErrBadPower:
191  return QLatin1String("insufficient power");
192  case KErrDirFull:
193  return QLatin1String("no space in directory table");
194  case KErrHardwareNotAvailable:
195  return QLatin1String("hardware not available");
196  case KErrSessionClosed:
197  return QLatin1String("session closed");
198  case KErrPermissionDenied:
199  return QLatin1String("permission denied");
200  default:
201  return QString(QLatin1String("symbian error %1")).arg(errorCode);
202  }
203 }
204 #endif
205 
207 {
208  switch(errorScope) {
209  case NativeError:
210 #if defined (Q_OS_WIN)
212 #elif defined (Q_OS_SYMBIAN)
213  return symbianErrorString(errorCode);
214 #else
215  //unix: fall through as native and standard library are the same
216 #endif
219  default:
220  qWarning("invalid error scope");
221  //fall through
222  case NoError:
223  return QLatin1String("No error");
224  }
225 }
226 
228 
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static QString windowsErrorString(int errorCode)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
Definition: qglobal.h:2487
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString standardLibraryErrorString(int errorCode)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString trimmed() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end.
Definition: qstring.cpp:4506
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Q_CORE_EXPORT void qWarning(const char *,...)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QString toString()
ErrorScope errorScope