source: vendor/trolltech/current/src/tools/qglobal.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

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