source: trunk/src/tools/qglobal.cpp@ 138

Last change on this file since 138 was 133, checked in by dmik, 19 years ago

Global: Fixed qSystemWarning(): Do not try to get the error text for the DOS error code (OSO001.MSG is NOT for that purpose).

  • Property svn:keywords set to Id
File size: 24.5 KB
Line 
1/****************************************************************************
2** $Id: qglobal.cpp 133 2006-10-08 21:06:47Z 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
67const char *qVersion()
68{
69 return QT_VERSION_STR;
70}
71
72bool 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
85static bool si_alreadyDone = FALSE;
86static int si_wordSize;
87static 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
103bool 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]
174OSErr 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
184CFStringRef qstring2cfstring(const QString &str)
185{
186 return CFStringCreateWithCharacters(0, (UniChar *)str.unicode(), str.length());
187}
188
189QString 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
204unsigned 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
220unsigned char * p_str(const QString &s)
221{
222 return p_str(s, s.length());
223}
224
225QCString 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
234int 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}
255Qt::MacintoshVersion qt_macver = (Qt::MacintoshVersion)qMacVersion();
256#elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) || defined(Q_OS_TEMP)
257bool qt_winunicode;
258# ifdef Q_OS_TEMP
259 DWORD qt_cever = 0;
260# endif // Q_OS_TEMP
261
262#include "qt_windows.h"
263
264int 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
341Qt::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
446static QtMsgHandler handler = 0; // pointer to debug handler
447static const int QT_BUFFER_LENGTH = 8196; // internal buffer length
448
449
450#ifdef Q_CC_MWERKS
451
452#include "qt_mac.h"
453
454extern bool qt_is_gui_used;
455static 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
469void 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.
497void 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
524void 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
553void 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
580void 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
616void 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*/
665void 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 qWarning( "%s\n\tError code %d (system error)", msg, code );
701 } else {
702 code = (int) WinGetLastError( 0 );
703 if ( code )
704 qWarning( "%s\n\tError code %08X (PM error)", msg, code );
705 else
706 qWarning( msg );
707 }
708#else
709 if ( code != -1 )
710 qWarning( "%s\n\tError code %d - %s", msg, code, strerror( code ) );
711 else
712 qWarning( msg );
713#endif
714#else
715 Q_UNUSED( msg );
716 Q_UNUSED( code );
717#endif
718}
719
720/*!
721 \fn void Q_ASSERT( bool test )
722
723 \relates QApplication
724
725 Prints a warning message containing the source code file name and
726 line number if \a test is FALSE.
727
728 This is really a macro defined in \c qglobal.h.
729
730 Q_ASSERT is useful for testing pre- and post-conditions.
731
732 Example:
733 \code
734 //
735 // File: div.cpp
736 //
737
738 #include <qglobal.h>
739
740 int divide( int a, int b )
741 {
742 Q_ASSERT( b != 0 ); // this is line 9
743 return a/b;
744 }
745 \endcode
746
747 If \c b is zero, the Q_ASSERT statement will output the following
748 message using the qWarning() function:
749 \code
750 ASSERT: "b == 0" in div.cpp (9)
751 \endcode
752
753 \sa qWarning(), \link debug.html Debugging\endlink
754*/
755
756
757/*!
758 \fn void Q_CHECK_PTR( void *p )
759
760 \relates QApplication
761
762 If \a p is 0, prints a warning message containing the source code file
763 name and line number, saying that the program ran out of memory.
764
765 This is really a macro defined in \c qglobal.h.
766
767 Example:
768 \code
769 int *a;
770
771 Q_CHECK_PTR( a = new int[80] ); // WRONG!
772
773 a = new (nothrow) int[80]; // Right
774 Q_CHECK_PTR( a );
775 \endcode
776
777 \sa qWarning(), \link debug.html Debugging\endlink
778*/
779
780
781//
782// The Q_CHECK_PTR macro calls this function to check if an allocation went ok.
783//
784#if (QT_VERSION-0 >= 0x040000)
785#if defined(Q_CC_GNU)
786#warning "Change Q_CHECK_PTR to '{if ((p)==0) qt_check_pointer(__FILE__,__LINE__);}'"
787#warning "No need for qt_check_pointer() to return a value - make it void!"
788#endif
789#endif
790bool qt_check_pointer( bool c, const char *n, int l )
791{
792 if ( c )
793 qWarning( "In file %s, line %d: Out of memory", n, l );
794 return TRUE;
795}
796
797
798static bool firstObsoleteWarning(const char *obj, const char *oldfunc )
799{
800 static QAsciiDict<int> *obsoleteDict = 0;
801 if ( !obsoleteDict ) { // first time func is called
802 obsoleteDict = new QAsciiDict<int>;
803#if defined(QT_DEBUG)
804 qDebug(
805 "You are using obsolete functions in the Qt library. Call the function\n"
806 "qSuppressObsoleteWarnings() to suppress obsolete warnings.\n"
807 );
808#endif
809 }
810 QCString s( obj );
811 s += "::";
812 s += oldfunc;
813 if ( obsoleteDict->find(s.data()) == 0 ) {
814 obsoleteDict->insert( s.data(), (int*)1 ); // anything different from 0
815 return TRUE;
816 }
817 return FALSE;
818}
819
820static bool suppressObsolete = FALSE;
821
822void qSuppressObsoleteWarnings( bool suppress )
823{
824 suppressObsolete = suppress;
825}
826
827void qObsolete( const char *obj, const char *oldfunc, const char *newfunc )
828{
829 if ( suppressObsolete )
830 return;
831 if ( !firstObsoleteWarning(obj, oldfunc) )
832 return;
833 if ( obj )
834 qDebug( "%s::%s: This function is obsolete, use %s instead.",
835 obj, oldfunc, newfunc );
836 else
837 qDebug( "%s: This function is obsolete, use %s instead.",
838 oldfunc, newfunc );
839}
840
841void qObsolete( const char *obj, const char *oldfunc )
842{
843 if ( suppressObsolete )
844 return;
845 if ( !firstObsoleteWarning(obj, oldfunc) )
846 return;
847 if ( obj )
848 qDebug( "%s::%s: This function is obsolete.", obj, oldfunc );
849 else
850 qDebug( "%s: This function is obsolete.", oldfunc );
851}
852
853void qObsolete( const char *message )
854{
855 if ( suppressObsolete )
856 return;
857 if ( !firstObsoleteWarning( "Qt", message) )
858 return;
859 qDebug( "%s", message );
860}
861
862
863/*!
864 \relates QApplication
865
866 Installs a Qt message handler \a h. Returns a pointer to the
867 message handler previously defined.
868
869 The message handler is a function that prints out debug messages,
870 warnings and fatal error messages. The Qt library (debug version)
871 contains hundreds of warning messages that are printed when
872 internal errors (usually invalid function arguments) occur. If you
873 implement your own message handler, you get total control of these
874 messages.
875
876 The default message handler prints the message to the standard
877 output under X11 and OS/2 or to the debugger under Windows. If it is a
878 fatal message, the application aborts immediately.
879
880 Only one message handler can be defined, since this is usually
881 done on an application-wide basis to control debug output.
882
883 To restore the message handler, call \c qInstallMsgHandler(0).
884
885 Example:
886 \code
887 #include <qapplication.h>
888 #include <stdio.h>
889 #include <stdlib.h>
890
891 void myMessageOutput( QtMsgType type, const char *msg )
892 {
893 switch ( type ) {
894 case QtDebugMsg:
895 fprintf( stderr, "Debug: %s\n", msg );
896 break;
897 case QtWarningMsg:
898 fprintf( stderr, "Warning: %s\n", msg );
899 break;
900 case QtFatalMsg:
901 fprintf( stderr, "Fatal: %s\n", msg );
902 abort(); // deliberately core dump
903 }
904 }
905
906 int main( int argc, char **argv )
907 {
908 qInstallMsgHandler( myMessageOutput );
909 QApplication a( argc, argv );
910 ...
911 return a.exec();
912 }
913 \endcode
914
915 \sa qDebug(), qWarning(), qFatal(), \link debug.html Debugging\endlink
916*/
917
918QtMsgHandler qInstallMsgHandler( QtMsgHandler h )
919{
920 QtMsgHandler old = handler;
921 handler = h;
922 return old;
923}
924
925
926/*
927 Dijkstra's bisection algorithm to find the square root as an integer.
928 Deliberately not exported as part of the Qt API, but used in both
929 qsimplerichtext.cpp and qgfxraster_qws.cpp
930*/
931unsigned int qt_int_sqrt( unsigned int n )
932{
933 // n must be in the range 0...UINT_MAX/2-1
934 if ( n >= ( UINT_MAX>>2 ) ) {
935 unsigned int r = 2 * qt_int_sqrt( n / 4 );
936 unsigned int r2 = r + 1;
937 return ( n >= r2 * r2 ) ? r2 : r;
938 }
939 uint h, p= 0, q= 1, r= n;
940 while ( q <= n )
941 q <<= 2;
942 while ( q != 1 ) {
943 q >>= 2;
944 h= p + q;
945 p >>= 1;
946 if ( r >= h ) {
947 p += q;
948 r -= h;
949 }
950 }
951 return p;
952}
953
Note: See TracBrowser for help on using the repository browser.