1 | /****************************************************************************
|
---|
2 | ** $Id: qglobal.cpp 8 2005-11-16 19:36:46Z dmik $
|
---|
3 | **
|
---|
4 | ** Global functions
|
---|
5 | **
|
---|
6 | ** Created : 920604
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qplatformdefs.h"
|
---|
39 |
|
---|
40 | #include "qasciidict.h"
|
---|
41 | #include <limits.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <limits.h>
|
---|
44 | #include <stdarg.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 |
|
---|
47 | #if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET) && !defined(Q_OS_TEMP)
|
---|
48 | #include <crtdbg.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #if defined(Q_OS_OS2)
|
---|
52 | #include "qt_os2.h"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | /*!
|
---|
57 | \relates QApplication
|
---|
58 |
|
---|
59 | Returns the Qt version number as a string, for example, "2.3.0" or
|
---|
60 | "3.0.5".
|
---|
61 |
|
---|
62 | The \c QT_VERSION define has the numeric value in the form:
|
---|
63 | 0xmmiibb (m = major, i = minor, b = bugfix). For example, Qt
|
---|
64 | 3.0.5's \c QT_VERSION is 0x030005.
|
---|
65 | */
|
---|
66 |
|
---|
67 | const char *qVersion()
|
---|
68 | {
|
---|
69 | return QT_VERSION_STR;
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool qSharedBuild()
|
---|
73 | {
|
---|
74 | #ifdef QT_SHARED
|
---|
75 | return TRUE;
|
---|
76 | #else
|
---|
77 | return FALSE;
|
---|
78 | #endif
|
---|
79 | }
|
---|
80 |
|
---|
81 | /*****************************************************************************
|
---|
82 | System detection routines
|
---|
83 | *****************************************************************************/
|
---|
84 |
|
---|
85 | static bool si_alreadyDone = FALSE;
|
---|
86 | static int si_wordSize;
|
---|
87 | static bool si_bigEndian;
|
---|
88 |
|
---|
89 | /*!
|
---|
90 | \relates QApplication
|
---|
91 |
|
---|
92 | Obtains information about the system.
|
---|
93 |
|
---|
94 | The system's word size in bits (typically 32) is returned in \a
|
---|
95 | *wordSize. The \a *bigEndian is set to TRUE if this is a big-endian
|
---|
96 | machine, or to FALSE if this is a little-endian machine.
|
---|
97 |
|
---|
98 | In debug mode, this function calls qFatal() with a message if the
|
---|
99 | computer is truly weird (i.e. different endianness for 16 bit and
|
---|
100 | 32 bit integers); in release mode it returns FALSE.
|
---|
101 | */
|
---|
102 |
|
---|
103 | bool qSysInfo( int *wordSize, bool *bigEndian )
|
---|
104 | {
|
---|
105 | #if defined(QT_CHECK_NULL)
|
---|
106 | Q_ASSERT( wordSize != 0 );
|
---|
107 | Q_ASSERT( bigEndian != 0 );
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | if ( si_alreadyDone ) { // run it only once
|
---|
111 | *wordSize = si_wordSize;
|
---|
112 | *bigEndian = si_bigEndian;
|
---|
113 | return TRUE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | si_wordSize = 0;
|
---|
117 | Q_ULONG n = (Q_ULONG)(~0);
|
---|
118 | while ( n ) { // detect word size
|
---|
119 | si_wordSize++;
|
---|
120 | n /= 2;
|
---|
121 | }
|
---|
122 | *wordSize = si_wordSize;
|
---|
123 |
|
---|
124 | if ( *wordSize != 64 &&
|
---|
125 | *wordSize != 32 &&
|
---|
126 | *wordSize != 16 ) { // word size: 16, 32 or 64
|
---|
127 | #if defined(QT_CHECK_RANGE)
|
---|
128 | qFatal( "qSysInfo: Unsupported system word size %d", *wordSize );
|
---|
129 | #endif
|
---|
130 | return FALSE;
|
---|
131 | }
|
---|
132 | if ( sizeof(Q_INT8) != 1 || sizeof(Q_INT16) != 2 || sizeof(Q_INT32) != 4 ||
|
---|
133 | sizeof(Q_ULONG)*8 != si_wordSize || sizeof(float) != 4 || sizeof(double) != 8 ) {
|
---|
134 | #if defined(QT_CHECK_RANGE)
|
---|
135 | qFatal( "qSysInfo: Unsupported system data type size" );
|
---|
136 | #endif
|
---|
137 | return FALSE;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool be16, be32; // determine byte ordering
|
---|
141 | short ns = 0x1234;
|
---|
142 | int nl = 0x12345678;
|
---|
143 |
|
---|
144 | unsigned char *p = (unsigned char *)(&ns); // 16-bit integer
|
---|
145 | be16 = *p == 0x12;
|
---|
146 |
|
---|
147 | p = (unsigned char *)(&nl); // 32-bit integer
|
---|
148 | if ( p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78 )
|
---|
149 | be32 = TRUE;
|
---|
150 | else
|
---|
151 | if ( p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12 )
|
---|
152 | be32 = FALSE;
|
---|
153 | else
|
---|
154 | be32 = !be16;
|
---|
155 |
|
---|
156 | if ( be16 != be32 ) { // strange machine!
|
---|
157 | #if defined(QT_CHECK_RANGE)
|
---|
158 | qFatal( "qSysInfo: Inconsistent system byte order" );
|
---|
159 | #endif
|
---|
160 | return FALSE;
|
---|
161 | }
|
---|
162 |
|
---|
163 | *bigEndian = si_bigEndian = be32;
|
---|
164 | si_alreadyDone = TRUE;
|
---|
165 | return TRUE;
|
---|
166 | }
|
---|
167 |
|
---|
168 | #if !defined(QWS) && defined(Q_OS_MAC)
|
---|
169 |
|
---|
170 | #include "qt_mac.h"
|
---|
171 |
|
---|
172 | // This function has descended from Apple Source Code (FSpLocationFromFullPath),
|
---|
173 | // but changes have been made. [Creates a minimal alias from the full pathname]
|
---|
174 | OSErr qt_mac_create_fsspec(const QString &file, FSSpec *spec)
|
---|
175 | {
|
---|
176 | FSRef fref;
|
---|
177 | QCString utfs = file.utf8();
|
---|
178 | OSErr ret = FSPathMakeRef((const UInt8 *)utfs.data(), &fref, NULL);
|
---|
179 | if(ret == noErr)
|
---|
180 | ret = FSGetCatalogInfo(&fref, kFSCatInfoNone, NULL, NULL, spec, NULL);
|
---|
181 | return ret;
|
---|
182 | }
|
---|
183 |
|
---|
184 | CFStringRef qstring2cfstring(const QString &str)
|
---|
185 | {
|
---|
186 | return CFStringCreateWithCharacters(0, (UniChar *)str.unicode(), str.length());
|
---|
187 | }
|
---|
188 |
|
---|
189 | QString cfstring2qstring(CFStringRef str)
|
---|
190 | {
|
---|
191 | if(!str)
|
---|
192 | return QString();
|
---|
193 |
|
---|
194 | CFIndex length = CFStringGetLength(str);
|
---|
195 | if(const UniChar *chars = CFStringGetCharactersPtr(str))
|
---|
196 | return QString((QChar *)chars, length);
|
---|
197 | UniChar *buffer = (UniChar*)malloc(length * sizeof(UniChar));
|
---|
198 | CFStringGetCharacters(str, CFRangeMake(0, length), buffer);
|
---|
199 | QString ret((QChar *)buffer, length);
|
---|
200 | free(buffer);
|
---|
201 | return ret;
|
---|
202 | }
|
---|
203 |
|
---|
204 | unsigned char * p_str(const char * c, int len=-1)
|
---|
205 | {
|
---|
206 | const int maxlen = 255;
|
---|
207 | if(len == -1)
|
---|
208 | len = qstrlen(c);
|
---|
209 | if(len > maxlen) {
|
---|
210 | qWarning( "p_str len must never exceed %d", maxlen );
|
---|
211 | len = maxlen;
|
---|
212 | }
|
---|
213 | unsigned char *ret = (unsigned char*)malloc(len+2);
|
---|
214 | *ret=len;
|
---|
215 | memcpy(((char *)ret)+1,c,len);
|
---|
216 | *(ret+len+1) = '\0';
|
---|
217 | return ret;
|
---|
218 | }
|
---|
219 |
|
---|
220 | unsigned char * p_str(const QString &s)
|
---|
221 | {
|
---|
222 | return p_str(s, s.length());
|
---|
223 | }
|
---|
224 |
|
---|
225 | QCString p2qstring(const unsigned char *c) {
|
---|
226 | char *arr = (char *)malloc(c[0] + 1);
|
---|
227 | memcpy(arr, c+1, c[0]);
|
---|
228 | arr[c[0]] = '\0';
|
---|
229 | QCString ret = arr;
|
---|
230 | delete arr;
|
---|
231 | return ret;
|
---|
232 | }
|
---|
233 |
|
---|
234 | int qMacVersion()
|
---|
235 | {
|
---|
236 | static int macver = Qt::MV_Unknown;
|
---|
237 | static bool first = TRUE;
|
---|
238 | if(first) {
|
---|
239 | first = FALSE;
|
---|
240 | long gestalt_version;
|
---|
241 | if(Gestalt(gestaltSystemVersion, &gestalt_version) == noErr) {
|
---|
242 | if(gestalt_version >= 0x1030 && gestalt_version < 0x1040)
|
---|
243 | macver = Qt::MV_10_DOT_3;
|
---|
244 | else if(gestalt_version >= 0x1020 && gestalt_version < 0x1030)
|
---|
245 | macver = Qt::MV_10_DOT_2;
|
---|
246 | else if(gestalt_version >= 0x1010 && gestalt_version < 0x1020)
|
---|
247 | macver = Qt::MV_10_DOT_1;
|
---|
248 | else if(gestalt_version >= 0x1000 && gestalt_version < 0x1010)
|
---|
249 | macver = Qt::MV_10_DOT_0;
|
---|
250 |
|
---|
251 | }
|
---|
252 | }
|
---|
253 | return macver;
|
---|
254 | }
|
---|
255 | Qt::MacintoshVersion qt_macver = (Qt::MacintoshVersion)qMacVersion();
|
---|
256 | #elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) || defined(Q_OS_TEMP)
|
---|
257 | bool qt_winunicode;
|
---|
258 | # ifdef Q_OS_TEMP
|
---|
259 | DWORD qt_cever = 0;
|
---|
260 | # endif // Q_OS_TEMP
|
---|
261 |
|
---|
262 | #include "qt_windows.h"
|
---|
263 |
|
---|
264 | int qWinVersion()
|
---|
265 | {
|
---|
266 | #ifndef VER_PLATFORM_WIN32s
|
---|
267 | #define VER_PLATFORM_WIN32s 0
|
---|
268 | #endif
|
---|
269 | #ifndef VER_PLATFORM_WIN32_WINDOWS
|
---|
270 | #define VER_PLATFORM_WIN32_WINDOWS 1
|
---|
271 | #endif
|
---|
272 | #ifndef VER_PLATFORM_WIN32_NT
|
---|
273 | #define VER_PLATFORM_WIN32_NT 2
|
---|
274 | #endif
|
---|
275 | #ifndef VER_PLATFORM_WIN32_CE
|
---|
276 | #define VER_PLATFORM_WIN32_CE 3
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | static int winver = Qt::WV_NT;
|
---|
280 | static int t=0;
|
---|
281 | if ( !t ) {
|
---|
282 | t=1;
|
---|
283 | #ifndef Q_OS_TEMP
|
---|
284 | OSVERSIONINFOA osver;
|
---|
285 | osver.dwOSVersionInfoSize = sizeof(osver);
|
---|
286 | GetVersionExA( &osver );
|
---|
287 | #else
|
---|
288 | OSVERSIONINFOW osver;
|
---|
289 | osver.dwOSVersionInfoSize = sizeof(osver);
|
---|
290 | GetVersionEx( &osver );
|
---|
291 | qt_cever = osver.dwMajorVersion * 100;
|
---|
292 | qt_cever += osver.dwMinorVersion * 10;
|
---|
293 | #endif
|
---|
294 | switch ( osver.dwPlatformId ) {
|
---|
295 | case VER_PLATFORM_WIN32s:
|
---|
296 | winver = Qt::WV_32s;
|
---|
297 | break;
|
---|
298 | case VER_PLATFORM_WIN32_WINDOWS:
|
---|
299 | // We treat Windows Me (minor 90) the same as Windows 98
|
---|
300 | if ( osver.dwMinorVersion == 90 )
|
---|
301 | winver = Qt::WV_Me;
|
---|
302 | else if ( osver.dwMinorVersion == 10 )
|
---|
303 | winver = Qt::WV_98;
|
---|
304 | else
|
---|
305 | winver = Qt::WV_95;
|
---|
306 | break;
|
---|
307 | case VER_PLATFORM_WIN32_CE:
|
---|
308 | #ifdef Q_OS_TEMP
|
---|
309 | if ( qt_cever >= 400 )
|
---|
310 | winver = Qt::WV_CENET;
|
---|
311 | else
|
---|
312 | #endif
|
---|
313 | winver = Qt::WV_CE;
|
---|
314 | break;
|
---|
315 | default: // VER_PLATFORM_WIN32_NT
|
---|
316 | if ( osver.dwMajorVersion < 5 ) {
|
---|
317 | winver = Qt::WV_NT;
|
---|
318 | } else if ( osver.dwMinorVersion == 0 ) {
|
---|
319 | winver = Qt::WV_2000;
|
---|
320 | } else if ( osver.dwMinorVersion == 1 ) {
|
---|
321 | winver = Qt::WV_XP;
|
---|
322 | } else if ( osver.dwMinorVersion == 2 ) {
|
---|
323 | winver = Qt::WV_2003;
|
---|
324 | } else {
|
---|
325 | qWarning("Untested Windows version detected!");
|
---|
326 | winver = Qt::WV_NT_based;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | #if defined(UNICODE)
|
---|
332 | if ( winver & Qt::WV_NT_based )
|
---|
333 | qt_winunicode = TRUE;
|
---|
334 | else
|
---|
335 | #endif
|
---|
336 | qt_winunicode = FALSE;
|
---|
337 |
|
---|
338 | return winver;
|
---|
339 | }
|
---|
340 |
|
---|
341 | Qt::WindowsVersion qt_winver = (Qt::WindowsVersion)qWinVersion();
|
---|
342 | #endif
|
---|
343 |
|
---|
344 |
|
---|
345 | /*****************************************************************************
|
---|
346 | Debug output routines
|
---|
347 | *****************************************************************************/
|
---|
348 |
|
---|
349 | /*!
|
---|
350 | \fn void qDebug( const char *msg, ... )
|
---|
351 |
|
---|
352 | \relates QApplication
|
---|
353 |
|
---|
354 | Prints a debug message \a msg, or calls the message handler (if it
|
---|
355 | has been installed).
|
---|
356 |
|
---|
357 | This function takes a format string and a list of arguments,
|
---|
358 | similar to the C printf() function.
|
---|
359 |
|
---|
360 | Example:
|
---|
361 | \code
|
---|
362 | qDebug( "my window handle = %x", myWidget->id() );
|
---|
363 | \endcode
|
---|
364 |
|
---|
365 | Under X11 or OS/2, the text is printed to stderr. Under Windows, the text
|
---|
366 | is sent to the debugger.
|
---|
367 |
|
---|
368 | \warning The internal buffer is limited to 8196 bytes (including
|
---|
369 | the '\0'-terminator).
|
---|
370 |
|
---|
371 | \warning Passing (const char *)0 as argument to qDebug might lead
|
---|
372 | to crashes on certain platforms due to the platforms printf implementation.
|
---|
373 |
|
---|
374 | \sa qWarning(), qFatal(), qInstallMsgHandler(),
|
---|
375 | \link debug.html Debugging\endlink
|
---|
376 | */
|
---|
377 |
|
---|
378 | /*!
|
---|
379 | \fn void qWarning( const char *msg, ... )
|
---|
380 |
|
---|
381 | \relates QApplication
|
---|
382 |
|
---|
383 | Prints a warning message \a msg, or calls the message handler (if
|
---|
384 | it has been installed).
|
---|
385 |
|
---|
386 | This function takes a format string and a list of arguments,
|
---|
387 | similar to the C printf() function.
|
---|
388 |
|
---|
389 | Example:
|
---|
390 | \code
|
---|
391 | void f( int c )
|
---|
392 | {
|
---|
393 | if ( c > 200 )
|
---|
394 | qWarning( "f: bad argument, c == %d", c );
|
---|
395 | }
|
---|
396 | \endcode
|
---|
397 |
|
---|
398 | Under X11 or OS/2, the text is printed to stderr. Under Windows, the text
|
---|
399 | is sent to the debugger.
|
---|
400 |
|
---|
401 | \warning The internal buffer is limited to 8196 bytes (including
|
---|
402 | the '\0'-terminator).
|
---|
403 |
|
---|
404 | \warning Passing (const char *)0 as argument to qWarning might lead
|
---|
405 | to crashes on certain platforms due to the platforms printf implementation.
|
---|
406 |
|
---|
407 | \sa qDebug(), qFatal(), qInstallMsgHandler(),
|
---|
408 | \link debug.html Debugging\endlink
|
---|
409 | */
|
---|
410 |
|
---|
411 | /*!
|
---|
412 | \fn void qFatal( const char *msg, ... )
|
---|
413 |
|
---|
414 | \relates QApplication
|
---|
415 |
|
---|
416 | Prints a fatal error message \a msg and exits, or calls the
|
---|
417 | message handler (if it has been installed).
|
---|
418 |
|
---|
419 | This function takes a format string and a list of arguments,
|
---|
420 | similar to the C printf() function.
|
---|
421 |
|
---|
422 | Example:
|
---|
423 | \code
|
---|
424 | int divide( int a, int b )
|
---|
425 | {
|
---|
426 | if ( b == 0 ) // program error
|
---|
427 | qFatal( "divide: cannot divide by zero" );
|
---|
428 | return a/b;
|
---|
429 | }
|
---|
430 | \endcode
|
---|
431 |
|
---|
432 | Under X11 or OS/2, the text is printed to stderr. Under Windows, the text
|
---|
433 | is sent to the debugger.
|
---|
434 |
|
---|
435 | \warning The internal buffer is limited to 8196 bytes (including
|
---|
436 | the '\0'-terminator).
|
---|
437 |
|
---|
438 | \warning Passing (const char *)0 as argument to qFatal might lead
|
---|
439 | to crashes on certain platforms due to the platforms printf implementation.
|
---|
440 |
|
---|
441 | \sa qDebug(), qWarning(), qInstallMsgHandler(),
|
---|
442 | \link debug.html Debugging\endlink
|
---|
443 | */
|
---|
444 |
|
---|
445 |
|
---|
446 | static QtMsgHandler handler = 0; // pointer to debug handler
|
---|
447 | static const int QT_BUFFER_LENGTH = 8196; // internal buffer length
|
---|
448 |
|
---|
449 |
|
---|
450 | #ifdef Q_CC_MWERKS
|
---|
451 |
|
---|
452 | #include "qt_mac.h"
|
---|
453 |
|
---|
454 | extern bool qt_is_gui_used;
|
---|
455 | static void mac_default_handler( const char *msg )
|
---|
456 | {
|
---|
457 | if ( qt_is_gui_used ) {
|
---|
458 | const unsigned char *p = p_str(msg);
|
---|
459 | DebugStr(p);
|
---|
460 | free((void*)p);
|
---|
461 | } else {
|
---|
462 | fprintf( stderr, msg );
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | #endif
|
---|
467 |
|
---|
468 |
|
---|
469 | void qDebug( const char *msg, ... )
|
---|
470 | {
|
---|
471 | char buf[QT_BUFFER_LENGTH];
|
---|
472 | va_list ap;
|
---|
473 | va_start( ap, msg ); // use variable arg list
|
---|
474 | if ( handler ) {
|
---|
475 | #if defined(QT_VSNPRINTF)
|
---|
476 | QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
|
---|
477 | #else
|
---|
478 | vsprintf( buf, msg, ap );
|
---|
479 | #endif
|
---|
480 | va_end( ap );
|
---|
481 | (*handler)( QtDebugMsg, buf );
|
---|
482 | } else {
|
---|
483 | vsprintf( buf, msg, ap ); // ### is there no vsnprintf()?
|
---|
484 | va_end( ap );
|
---|
485 | #if defined(Q_CC_MWERKS)
|
---|
486 | mac_default_handler(buf);
|
---|
487 | #elif defined(Q_OS_TEMP)
|
---|
488 | QString fstr( buf );
|
---|
489 | OutputDebugString( (fstr + "\n").ucs2() );
|
---|
490 | #else
|
---|
491 | fprintf( stderr, "%s\n", buf ); // add newline
|
---|
492 | #endif
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | // copied... this looks really bad.
|
---|
497 | void debug( const char *msg, ... )
|
---|
498 | {
|
---|
499 | char buf[QT_BUFFER_LENGTH];
|
---|
500 | va_list ap;
|
---|
501 | va_start( ap, msg ); // use variable arg list
|
---|
502 | if ( handler ) {
|
---|
503 | #if defined(QT_VSNPRINTF)
|
---|
504 | QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
|
---|
505 | #else
|
---|
506 | vsprintf( buf, msg, ap );
|
---|
507 | #endif
|
---|
508 | va_end( ap );
|
---|
509 | (*handler)( QtDebugMsg, buf );
|
---|
510 | } else {
|
---|
511 | vsprintf( buf, msg, ap ); // ### is there no vsnprintf()?
|
---|
512 | va_end( ap );
|
---|
513 | #if defined(Q_CC_MWERKS)
|
---|
514 | mac_default_handler(buf);
|
---|
515 | #elif defined(Q_OS_TEMP)
|
---|
516 | QString fstr( buf );
|
---|
517 | OutputDebugString( (fstr + "\n").ucs2() );
|
---|
518 | #else
|
---|
519 | fprintf( stderr, "%s\n", buf ); // add newline
|
---|
520 | #endif
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | void qWarning( const char *msg, ... )
|
---|
525 | {
|
---|
526 | char buf[QT_BUFFER_LENGTH];
|
---|
527 | va_list ap;
|
---|
528 | va_start( ap, msg ); // use variable arg list
|
---|
529 | if ( handler ) {
|
---|
530 | #if defined(QT_VSNPRINTF)
|
---|
531 | QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
|
---|
532 | #else
|
---|
533 | vsprintf( buf, msg, ap );
|
---|
534 | #endif
|
---|
535 | va_end( ap );
|
---|
536 | (*handler)( QtWarningMsg, buf );
|
---|
537 | } else {
|
---|
538 | vsprintf( buf, msg, ap ); // ### is there no vsnprintf()?
|
---|
539 | va_end( ap );
|
---|
540 | #if defined(Q_CC_MWERKS)
|
---|
541 | mac_default_handler(buf);
|
---|
542 | #elif defined(Q_OS_TEMP)
|
---|
543 | QString fstr( buf );
|
---|
544 | OutputDebugString( (fstr + "\n").ucs2() );
|
---|
545 | #else
|
---|
546 | fprintf( stderr, "%s\n", buf ); // add newline
|
---|
547 | #endif
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | // again, copied
|
---|
553 | void warning( const char *msg, ... )
|
---|
554 | {
|
---|
555 | char buf[QT_BUFFER_LENGTH];
|
---|
556 | va_list ap;
|
---|
557 | va_start( ap, msg ); // use variable arg list
|
---|
558 | if ( handler ) {
|
---|
559 | #if defined(QT_VSNPRINTF)
|
---|
560 | QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
|
---|
561 | #else
|
---|
562 | vsprintf( buf, msg, ap );
|
---|
563 | #endif
|
---|
564 | va_end( ap );
|
---|
565 | (*handler)( QtWarningMsg, buf );
|
---|
566 | } else {
|
---|
567 | vsprintf( buf, msg, ap ); // ### is there no vsnprintf()?
|
---|
568 | va_end( ap );
|
---|
569 | #if defined(Q_CC_MWERKS)
|
---|
570 | mac_default_handler(buf);
|
---|
571 | #elif defined(Q_OS_TEMP)
|
---|
572 | QString fstr( buf );
|
---|
573 | OutputDebugString( (fstr + "\n").ucs2() );
|
---|
574 | #else
|
---|
575 | fprintf( stderr, "%s\n", buf ); // add newline
|
---|
576 | #endif
|
---|
577 | }
|
---|
578 | }
|
---|
579 |
|
---|
580 | void qFatal( const char *msg, ... )
|
---|
581 | {
|
---|
582 | char buf[QT_BUFFER_LENGTH];
|
---|
583 | va_list ap;
|
---|
584 | va_start( ap, msg ); // use variable arg list
|
---|
585 | if ( handler ) {
|
---|
586 | #if defined(QT_VSNPRINTF)
|
---|
587 | QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
|
---|
588 | #else
|
---|
589 | vsprintf( buf, msg, ap );
|
---|
590 | #endif
|
---|
591 | va_end( ap );
|
---|
592 | (*handler)( QtFatalMsg, buf );
|
---|
593 | } else {
|
---|
594 | vsprintf( buf, msg, ap ); // ### is there no vsnprintf()?
|
---|
595 | va_end( ap );
|
---|
596 | #if defined(Q_CC_MWERKS)
|
---|
597 | mac_default_handler(buf);
|
---|
598 | #else
|
---|
599 | fprintf( stderr, "%s\n", buf ); // add newline
|
---|
600 | #endif
|
---|
601 | #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
|
---|
602 | abort(); // trap; generates core dump
|
---|
603 | #elif defined(Q_OS_TEMP) && defined(QT_DEBUG)
|
---|
604 | QString fstr;
|
---|
605 | fstr.sprintf( "%s:%s %s %s\n", __FILE__, __LINE__, QT_VERSION_STR, buf );
|
---|
606 | OutputDebugString( fstr.ucs2() );
|
---|
607 | #elif defined(_CRT_ERROR) && defined(_DEBUG)
|
---|
608 | _CrtDbgReport( _CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf );
|
---|
609 | #else
|
---|
610 | exit( 1 ); // goodbye cruel world
|
---|
611 | #endif
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 | // yet again, copied
|
---|
616 | void fatal( const char *msg, ... )
|
---|
617 | {
|
---|
618 | char buf[QT_BUFFER_LENGTH];
|
---|
619 | va_list ap;
|
---|
620 | va_start( ap, msg ); // use variable arg list
|
---|
621 | if ( handler ) {
|
---|
622 | #if defined(QT_VSNPRINTF)
|
---|
623 | QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
|
---|
624 | #else
|
---|
625 | vsprintf( buf, msg, ap );
|
---|
626 | #endif
|
---|
627 | va_end( ap );
|
---|
628 | (*handler)( QtFatalMsg, buf );
|
---|
629 | } else {
|
---|
630 | vsprintf( buf, msg, ap ); // ### is there no vsnprintf()?
|
---|
631 | va_end( ap );
|
---|
632 | #if defined(Q_CC_MWERKS)
|
---|
633 | mac_default_handler(buf);
|
---|
634 | #else
|
---|
635 | fprintf( stderr, "%s\n", buf ); // add newline
|
---|
636 | #endif
|
---|
637 | #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
|
---|
638 | abort(); // trap; generates core dump
|
---|
639 | #elif defined(Q_OS_TEMP) && defined(QT_DEBUG)
|
---|
640 | QString fstr;
|
---|
641 | fstr.sprintf( "%s:%s %s %s\n", __FILE__, __LINE__, QT_VERSION_STR, buf );
|
---|
642 | OutputDebugString( fstr.ucs2() );
|
---|
643 | #elif defined(_CRT_ERROR) && defined(_DEBUG)
|
---|
644 | _CrtDbgReport( _CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf );
|
---|
645 | #else
|
---|
646 | exit( 1 ); // goodbye cruel world
|
---|
647 | #endif
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | /*!
|
---|
652 | \relates QApplication
|
---|
653 |
|
---|
654 | Prints the message \a msg and uses \a code to get a system specific
|
---|
655 | error message. When \a code is -1 (the default), the system's last
|
---|
656 | error code will be used if possible. Use this method to handle
|
---|
657 | failures in platform specific API calls.
|
---|
658 |
|
---|
659 | Under OS/2, the value -1 for \a code should only be used after Presentation
|
---|
660 | Manager calls.
|
---|
661 |
|
---|
662 | This function does nothing when Qt is built with \c QT_NO_DEBUG
|
---|
663 | defined.
|
---|
664 | */
|
---|
665 | void qSystemWarning( const char* msg, int code )
|
---|
666 | {
|
---|
667 | #ifndef QT_NO_DEBUG
|
---|
668 | #if defined(Q_OS_WIN32)
|
---|
669 | if ( code == -1 )
|
---|
670 | code = GetLastError();
|
---|
671 |
|
---|
672 | if ( !code )
|
---|
673 | return;
|
---|
674 |
|
---|
675 | unsigned short *string;
|
---|
676 | QT_WA( {
|
---|
677 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
---|
678 | NULL,
|
---|
679 | code,
|
---|
680 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
681 | (LPTSTR)&string,
|
---|
682 | 0,
|
---|
683 | NULL );
|
---|
684 |
|
---|
685 | qWarning( "%s\n\tError code %d - %s", msg, code, QString::fromUcs2(string).latin1() );
|
---|
686 | }, {
|
---|
687 | FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
---|
688 | NULL,
|
---|
689 | code,
|
---|
690 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
691 | (char*)&string,
|
---|
692 | 0,
|
---|
693 | NULL );
|
---|
694 |
|
---|
695 | qWarning( "%s\n\tError code %d - %s", msg, code, (const char*)string );
|
---|
696 | } );
|
---|
697 | LocalFree( (HLOCAL)string );
|
---|
698 | #elif defined(Q_OS_OS2)
|
---|
699 | if ( code != -1 ) {
|
---|
700 | CHAR buf [256] = {0};
|
---|
701 | ULONG len = 0;
|
---|
702 | APIRET rc = DosGetMessage( NULL, 0, buf, sizeof(buf) - 1, code, "OSO001.MSG", &len );
|
---|
703 | if ( !rc ) {
|
---|
704 | buf[len] = '\0';
|
---|
705 | qWarning( "%s\n\tError %s", msg, buf );
|
---|
706 | } else {
|
---|
707 | if ( rc == ERROR_MR_MSG_TOO_LONG )
|
---|
708 | qWarning(
|
---|
709 | "%s\n\tError code SYS%04d, "
|
---|
710 | "type help %d in the command prompt to get more information",
|
---|
711 | msg, code, code );
|
---|
712 | else
|
---|
713 | qWarning( "%s\n\tError code %d (system error)", msg, code );
|
---|
714 | }
|
---|
715 | } else {
|
---|
716 | code = (int) WinGetLastError( 0 );
|
---|
717 | if ( code )
|
---|
718 | qWarning( "%s\n\tError code %08X (PM error)", msg, code );
|
---|
719 | else
|
---|
720 | qWarning( msg );
|
---|
721 | }
|
---|
722 | #else
|
---|
723 | if ( code != -1 )
|
---|
724 | qWarning( "%s\n\tError code %d - %s", msg, code, strerror( code ) );
|
---|
725 | else
|
---|
726 | qWarning( msg );
|
---|
727 | #endif
|
---|
728 | #else
|
---|
729 | Q_UNUSED( msg );
|
---|
730 | Q_UNUSED( code );
|
---|
731 | #endif
|
---|
732 | }
|
---|
733 |
|
---|
734 | /*!
|
---|
735 | \fn void Q_ASSERT( bool test )
|
---|
736 |
|
---|
737 | \relates QApplication
|
---|
738 |
|
---|
739 | Prints a warning message containing the source code file name and
|
---|
740 | line number if \a test is FALSE.
|
---|
741 |
|
---|
742 | This is really a macro defined in \c qglobal.h.
|
---|
743 |
|
---|
744 | Q_ASSERT is useful for testing pre- and post-conditions.
|
---|
745 |
|
---|
746 | Example:
|
---|
747 | \code
|
---|
748 | //
|
---|
749 | // File: div.cpp
|
---|
750 | //
|
---|
751 |
|
---|
752 | #include <qglobal.h>
|
---|
753 |
|
---|
754 | int divide( int a, int b )
|
---|
755 | {
|
---|
756 | Q_ASSERT( b != 0 ); // this is line 9
|
---|
757 | return a/b;
|
---|
758 | }
|
---|
759 | \endcode
|
---|
760 |
|
---|
761 | If \c b is zero, the Q_ASSERT statement will output the following
|
---|
762 | message using the qWarning() function:
|
---|
763 | \code
|
---|
764 | ASSERT: "b == 0" in div.cpp (9)
|
---|
765 | \endcode
|
---|
766 |
|
---|
767 | \sa qWarning(), \link debug.html Debugging\endlink
|
---|
768 | */
|
---|
769 |
|
---|
770 |
|
---|
771 | /*!
|
---|
772 | \fn void Q_CHECK_PTR( void *p )
|
---|
773 |
|
---|
774 | \relates QApplication
|
---|
775 |
|
---|
776 | If \a p is 0, prints a warning message containing the source code file
|
---|
777 | name and line number, saying that the program ran out of memory.
|
---|
778 |
|
---|
779 | This is really a macro defined in \c qglobal.h.
|
---|
780 |
|
---|
781 | Example:
|
---|
782 | \code
|
---|
783 | int *a;
|
---|
784 |
|
---|
785 | Q_CHECK_PTR( a = new int[80] ); // WRONG!
|
---|
786 |
|
---|
787 | a = new (nothrow) int[80]; // Right
|
---|
788 | Q_CHECK_PTR( a );
|
---|
789 | \endcode
|
---|
790 |
|
---|
791 | \sa qWarning(), \link debug.html Debugging\endlink
|
---|
792 | */
|
---|
793 |
|
---|
794 |
|
---|
795 | //
|
---|
796 | // The Q_CHECK_PTR macro calls this function to check if an allocation went ok.
|
---|
797 | //
|
---|
798 | #if (QT_VERSION-0 >= 0x040000)
|
---|
799 | #if defined(Q_CC_GNU)
|
---|
800 | #warning "Change Q_CHECK_PTR to '{if ((p)==0) qt_check_pointer(__FILE__,__LINE__);}'"
|
---|
801 | #warning "No need for qt_check_pointer() to return a value - make it void!"
|
---|
802 | #endif
|
---|
803 | #endif
|
---|
804 | bool qt_check_pointer( bool c, const char *n, int l )
|
---|
805 | {
|
---|
806 | if ( c )
|
---|
807 | qWarning( "In file %s, line %d: Out of memory", n, l );
|
---|
808 | return TRUE;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | static bool firstObsoleteWarning(const char *obj, const char *oldfunc )
|
---|
813 | {
|
---|
814 | static QAsciiDict<int> *obsoleteDict = 0;
|
---|
815 | if ( !obsoleteDict ) { // first time func is called
|
---|
816 | obsoleteDict = new QAsciiDict<int>;
|
---|
817 | #if defined(QT_DEBUG)
|
---|
818 | qDebug(
|
---|
819 | "You are using obsolete functions in the Qt library. Call the function\n"
|
---|
820 | "qSuppressObsoleteWarnings() to suppress obsolete warnings.\n"
|
---|
821 | );
|
---|
822 | #endif
|
---|
823 | }
|
---|
824 | QCString s( obj );
|
---|
825 | s += "::";
|
---|
826 | s += oldfunc;
|
---|
827 | if ( obsoleteDict->find(s.data()) == 0 ) {
|
---|
828 | obsoleteDict->insert( s.data(), (int*)1 ); // anything different from 0
|
---|
829 | return TRUE;
|
---|
830 | }
|
---|
831 | return FALSE;
|
---|
832 | }
|
---|
833 |
|
---|
834 | static bool suppressObsolete = FALSE;
|
---|
835 |
|
---|
836 | void qSuppressObsoleteWarnings( bool suppress )
|
---|
837 | {
|
---|
838 | suppressObsolete = suppress;
|
---|
839 | }
|
---|
840 |
|
---|
841 | void qObsolete( const char *obj, const char *oldfunc, const char *newfunc )
|
---|
842 | {
|
---|
843 | if ( suppressObsolete )
|
---|
844 | return;
|
---|
845 | if ( !firstObsoleteWarning(obj, oldfunc) )
|
---|
846 | return;
|
---|
847 | if ( obj )
|
---|
848 | qDebug( "%s::%s: This function is obsolete, use %s instead.",
|
---|
849 | obj, oldfunc, newfunc );
|
---|
850 | else
|
---|
851 | qDebug( "%s: This function is obsolete, use %s instead.",
|
---|
852 | oldfunc, newfunc );
|
---|
853 | }
|
---|
854 |
|
---|
855 | void qObsolete( const char *obj, const char *oldfunc )
|
---|
856 | {
|
---|
857 | if ( suppressObsolete )
|
---|
858 | return;
|
---|
859 | if ( !firstObsoleteWarning(obj, oldfunc) )
|
---|
860 | return;
|
---|
861 | if ( obj )
|
---|
862 | qDebug( "%s::%s: This function is obsolete.", obj, oldfunc );
|
---|
863 | else
|
---|
864 | qDebug( "%s: This function is obsolete.", oldfunc );
|
---|
865 | }
|
---|
866 |
|
---|
867 | void qObsolete( const char *message )
|
---|
868 | {
|
---|
869 | if ( suppressObsolete )
|
---|
870 | return;
|
---|
871 | if ( !firstObsoleteWarning( "Qt", message) )
|
---|
872 | return;
|
---|
873 | qDebug( "%s", message );
|
---|
874 | }
|
---|
875 |
|
---|
876 |
|
---|
877 | /*!
|
---|
878 | \relates QApplication
|
---|
879 |
|
---|
880 | Installs a Qt message handler \a h. Returns a pointer to the
|
---|
881 | message handler previously defined.
|
---|
882 |
|
---|
883 | The message handler is a function that prints out debug messages,
|
---|
884 | warnings and fatal error messages. The Qt library (debug version)
|
---|
885 | contains hundreds of warning messages that are printed when
|
---|
886 | internal errors (usually invalid function arguments) occur. If you
|
---|
887 | implement your own message handler, you get total control of these
|
---|
888 | messages.
|
---|
889 |
|
---|
890 | The default message handler prints the message to the standard
|
---|
891 | output under X11 and OS/2 or to the debugger under Windows. If it is a
|
---|
892 | fatal message, the application aborts immediately.
|
---|
893 |
|
---|
894 | Only one message handler can be defined, since this is usually
|
---|
895 | done on an application-wide basis to control debug output.
|
---|
896 |
|
---|
897 | To restore the message handler, call \c qInstallMsgHandler(0).
|
---|
898 |
|
---|
899 | Example:
|
---|
900 | \code
|
---|
901 | #include <qapplication.h>
|
---|
902 | #include <stdio.h>
|
---|
903 | #include <stdlib.h>
|
---|
904 |
|
---|
905 | void myMessageOutput( QtMsgType type, const char *msg )
|
---|
906 | {
|
---|
907 | switch ( type ) {
|
---|
908 | case QtDebugMsg:
|
---|
909 | fprintf( stderr, "Debug: %s\n", msg );
|
---|
910 | break;
|
---|
911 | case QtWarningMsg:
|
---|
912 | fprintf( stderr, "Warning: %s\n", msg );
|
---|
913 | break;
|
---|
914 | case QtFatalMsg:
|
---|
915 | fprintf( stderr, "Fatal: %s\n", msg );
|
---|
916 | abort(); // deliberately core dump
|
---|
917 | }
|
---|
918 | }
|
---|
919 |
|
---|
920 | int main( int argc, char **argv )
|
---|
921 | {
|
---|
922 | qInstallMsgHandler( myMessageOutput );
|
---|
923 | QApplication a( argc, argv );
|
---|
924 | ...
|
---|
925 | return a.exec();
|
---|
926 | }
|
---|
927 | \endcode
|
---|
928 |
|
---|
929 | \sa qDebug(), qWarning(), qFatal(), \link debug.html Debugging\endlink
|
---|
930 | */
|
---|
931 |
|
---|
932 | QtMsgHandler qInstallMsgHandler( QtMsgHandler h )
|
---|
933 | {
|
---|
934 | QtMsgHandler old = handler;
|
---|
935 | handler = h;
|
---|
936 | return old;
|
---|
937 | }
|
---|
938 |
|
---|
939 |
|
---|
940 | /*
|
---|
941 | Dijkstra's bisection algorithm to find the square root as an integer.
|
---|
942 | Deliberately not exported as part of the Qt API, but used in both
|
---|
943 | qsimplerichtext.cpp and qgfxraster_qws.cpp
|
---|
944 | */
|
---|
945 | unsigned int qt_int_sqrt( unsigned int n )
|
---|
946 | {
|
---|
947 | // n must be in the range 0...UINT_MAX/2-1
|
---|
948 | if ( n >= ( UINT_MAX>>2 ) ) {
|
---|
949 | unsigned int r = 2 * qt_int_sqrt( n / 4 );
|
---|
950 | unsigned int r2 = r + 1;
|
---|
951 | return ( n >= r2 * r2 ) ? r2 : r;
|
---|
952 | }
|
---|
953 | uint h, p= 0, q= 1, r= n;
|
---|
954 | while ( q <= n )
|
---|
955 | q <<= 2;
|
---|
956 | while ( q != 1 ) {
|
---|
957 | q >>= 2;
|
---|
958 | h= p + q;
|
---|
959 | p >>= 1;
|
---|
960 | if ( r >= h ) {
|
---|
961 | p += q;
|
---|
962 | r -= h;
|
---|
963 | }
|
---|
964 | }
|
---|
965 | return p;
|
---|
966 | }
|
---|
967 |
|
---|