Chameleon

Chameleon Svn Source Tree

Root/branches/xZenu/src/util/doxygen/qtools/qglobal.cpp

Source at commit 1322 created 9 years 5 months ago.
By meklort, Add doxygen to utils folder
1/****************************************************************************
2**
3**
4** Global functions
5**
6** Created : 920604
7**
8** Copyright (C) 1992-2000 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 "qglobal.h"
39#include "qasciidict.h"
40#include "qstring.h"
41#include <stdio.h>
42#include <stdarg.h>
43#include <stdlib.h>
44
45// NOT REVISED
46
47/*!
48 \relates QApplication
49 Returns the Qt version number for the library, typically "1.30"
50 or "2.1.0".
51*/
52
53const char *qVersion()
54{
55 return QT_VERSION_STR;
56}
57
58
59/*****************************************************************************
60 System detection routines
61 *****************************************************************************/
62
63static bool si_alreadyDone = FALSE;
64static int si_wordSize;
65static bool si_bigEndian;
66
67/*!
68 \relates QApplication
69 Obtains information about the system.
70
71 The system's word size in bits (typically 32) is returned in \e *wordSize.
72 The \e *bigEndian is set to TRUE if this is a big-endian machine,
73 or to FALSE if this is a little-endian machine.
74
75 This function calls qFatal() with a message if the computer is truly weird
76 (i.e. different endianness for 16 bit and 32 bit integers).
77*/
78
79bool qSysInfo( int *wordSize, bool *bigEndian )
80{
81#if defined(CHECK_NULL)
82 ASSERT( wordSize != 0 );
83 ASSERT( bigEndian != 0 );
84#endif
85
86 if ( si_alreadyDone ) {// run it only once
87*wordSize = si_wordSize;
88*bigEndian = si_bigEndian;
89return TRUE;
90 }
91 si_alreadyDone = TRUE;
92
93 si_wordSize = 0;
94 uint n = (uint)(~0);
95 while ( n ) {// detect word size
96si_wordSize++;
97n /= 2;
98 }
99 *wordSize = si_wordSize;
100
101 if ( *wordSize != 64 &&
102 *wordSize != 32 &&
103 *wordSize != 16 ) {// word size: 16, 32 or 64
104#if defined(CHECK_RANGE)
105qFatal( "qSysInfo: Unsupported system word size %d", *wordSize );
106#endif
107return FALSE;
108 }
109 if ( sizeof(Q_INT8) != 1 || sizeof(Q_INT16) != 2 || sizeof(Q_INT32) != 4 ||
110 sizeof(float) != 4 || sizeof(double) != 8 ) {
111#if defined(CHECK_RANGE)
112qFatal( "qSysInfo: Unsupported system data type size" );
113#endif
114return FALSE;
115 }
116
117 bool be16, be32;// determine byte ordering
118 short ns = 0x1234;
119 int nl = 0x12345678;
120
121 unsigned char *p = (unsigned char *)(&ns);// 16-bit integer
122 be16 = *p == 0x12;
123
124 p = (unsigned char *)(&nl);// 32-bit integer
125 if ( p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78 )
126be32 = TRUE;
127 else
128 if ( p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12 )
129be32 = FALSE;
130 else
131be32 = !be16;
132
133 if ( be16 != be32 ) {// strange machine!
134#if defined(CHECK_RANGE)
135qFatal( "qSysInfo: Inconsistent system byte order" );
136#endif
137return FALSE;
138 }
139
140 *bigEndian = si_bigEndian = be32;
141 return TRUE;
142}
143
144
145/*****************************************************************************
146 Debug output routines
147 *****************************************************************************/
148
149/*!
150 \fn void qDebug( const char *msg, ... )
151
152 \relates QApplication
153 Prints a debug message, or calls the message handler (if it has been
154 installed).
155
156 This function takes a format string and a list of arguments, similar to
157 the C printf() function.
158
159 Example:
160 \code
161 qDebug( "my window handle = %x", myWidget->id() );
162 \endcode
163
164 Under X11, the text is printed to stderr. Under Windows, the text is
165 sent to the debugger.
166
167 \warning The internal buffer is limited to 8196 bytes (including the
168 0-terminator).
169
170 \sa qWarning(), qFatal(), qInstallMsgHandler(),
171 \link debug.html Debugging\endlink
172*/
173
174/*!
175 \fn void qWarning( const char *msg, ... )
176
177 \relates QApplication
178 Prints a warning message, or calls the message handler (if it has been
179 installed).
180
181 This function takes a format string and a list of arguments, similar to
182 the C printf() function.
183
184 Example:
185 \code
186 void f( int c )
187 {
188if ( c > 200 )
189 qWarning( "f: bad argument, c == %d", c );
190 }
191 \endcode
192
193 Under X11, the text is printed to stderr. Under Windows, the text is
194 sent to the debugger.
195
196 \warning The internal buffer is limited to 8196 bytes (including the
197 0-terminator).
198
199 \sa qDebug(), qFatal(), qInstallMsgHandler(),
200 \link debug.html Debugging\endlink
201*/
202
203/*!
204 \fn void qFatal( const char *msg, ... )
205
206 \relates QApplication
207 Prints a fatal error message and exits, or calls the message handler (if it
208 has been installed).
209
210 This function takes a format string and a list of arguments, similar to
211 the C printf() function.
212
213 Example:
214 \code
215 int divide( int a, int b )
216 {
217if ( b == 0 )// program error
218 qFatal( "divide: cannot divide by zero" );
219return a/b;
220 }
221 \endcode
222
223 Under X11, the text is printed to stderr. Under Windows, the text is
224 sent to the debugger.
225
226 \warning The internal buffer is limited to 8196 bytes (including the
227 0-terminator).
228
229 \sa qDebug(), qWarning(), qInstallMsgHandler(),
230 \link debug.html Debugging\endlink
231*/
232
233
234static msg_handler handler = 0;// pointer to debug handler
235
236
237#ifdef _OS_MAC_
238
239static FILE * mac_debug=0;
240
241void qDebug( const char *msg, ... )
242{
243 mac_debug=fopen( "debug.txt", "a+" );
244 if(mac_debug) {
245char buf[8196];
246va_list ap;
247va_start( ap, msg );// use variable arg list
248if ( handler ) {
249 vsprintf( buf, msg, ap );
250 va_end( ap );
251 (*handler)( QtDebugMsg, buf );
252} else {
253 vfprintf( mac_debug, msg, ap );
254 va_end( ap );
255 fprintf( mac_debug, "\n" );// add newline
256 fflush( mac_debug );
257}
258fclose(mac_debug);
259 } else {
260exit(0);
261 }
262}
263
264// copied... this looks really bad.
265void debug( const char *msg, ... )
266{
267 mac_debug=fopen( "debug.txt", "a+" );
268 if(mac_debug) {
269char buf[8196];
270va_list ap;
271va_start( ap, msg );// use variable arg list
272if ( handler ) {
273 vsprintf( buf, msg, ap );
274 va_end( ap );
275 (*handler)( QtDebugMsg, buf );
276} else {
277 vfprintf( mac_debug, msg, ap );
278 va_end( ap );
279 fprintf( mac_debug, "\n" );// add newline
280 fflush( mac_debug );
281}
282fclose(mac_debug);
283 }
284}
285
286void qWarning( const char *msg, ... )
287{
288 mac_debug=fopen( "debug.txt", "a+" );
289 if(mac_debug) {
290char buf[8196];
291va_list ap;
292va_start( ap, msg );// use variable arg list
293if ( handler ) {
294 vsprintf( buf, msg, ap );
295 va_end( ap );
296 (*handler)( QtDebugMsg, buf );
297} else {
298 vfprintf( mac_debug, msg, ap );
299 va_end( ap );
300 fprintf( mac_debug, "\n" );// add newline
301 fflush( mac_debug );
302}
303fclose(mac_debug);
304 }
305}
306
307// copied... this looks really bad.
308void warning( const char *msg, ... )
309{
310 mac_debug=fopen( "debug.txt", "a+" );
311 if(mac_debug) {
312char buf[8196];
313va_list ap;
314va_start( ap, msg );// use variable arg list
315if ( handler ) {
316 vsprintf( buf, msg, ap );
317 va_end( ap );
318 (*handler)( QtDebugMsg, buf );
319} else {
320 vfprintf( mac_debug, msg, ap );
321 va_end( ap );
322 fprintf( mac_debug, "\n" );// add newline
323 fflush( mac_debug );
324}
325fclose(mac_debug);
326 }
327}
328
329void qFatal( const char *msg, ... )
330{
331 mac_debug=fopen( "debug.txt", "a+");
332 if(mac_debug) {
333char buf[8196];
334va_list ap;
335va_start( ap, msg );// use variable arg list
336if ( handler ) {
337 vsprintf( buf, msg, ap );
338 va_end( ap );
339 (*handler)( QtDebugMsg, buf );
340} else {
341 vfprintf( mac_debug, msg, ap );
342 va_end( ap );
343 fprintf( mac_debug, "\n" );// add newline
344 fflush( mac_debug );
345}
346fclose(mac_debug);
347 }
348 exit(0);
349}
350
351// copied... this looks really bad.
352void fatal( const char *msg, ... )
353{
354 mac_debug=fopen( "debug.txt", "a+" );
355 if(mac_debug) {
356char buf[8196];
357va_list ap;
358va_start( ap, msg );// use variable arg list
359if ( handler ) {
360 vsprintf( buf, msg, ap );
361 va_end( ap );
362 (*handler)( QtDebugMsg, buf );
363} else {
364 vfprintf( mac_debug, msg, ap );
365 va_end( ap );
366 fprintf( mac_debug, "\n" );// add newline
367 fflush( mac_debug );
368}
369fclose(mac_debug);
370 }
371 exit(0);
372}
373
374#else
375
376void qDebug( const char *msg, ... )
377{
378 char buf[8196];
379 va_list ap;
380 va_start( ap, msg );// use variable arg list
381 if ( handler ) {
382vsprintf( buf, msg, ap );// ### vsnprintf would be great here
383va_end( ap );
384(*handler)( QtDebugMsg, buf );
385 } else {
386vfprintf( stderr, msg, ap );
387va_end( ap );
388fprintf( stderr, "\n" );// add newline
389 }
390}
391
392// copied... this looks really bad.
393void debug( const char *msg, ... )
394{
395 char buf[8196];
396 va_list ap;
397 va_start( ap, msg );// use variable arg list
398 if ( handler ) {
399vsprintf( buf, msg, ap );
400va_end( ap );
401(*handler)( QtDebugMsg, buf );
402 } else {
403vfprintf( stderr, msg, ap );
404va_end( ap );
405fprintf( stderr, "\n" );// add newline
406 }
407}
408
409void qWarning( const char *msg, ... )
410{
411 char buf[8196];
412 va_list ap;
413 va_start( ap, msg );// use variable arg list
414 if ( handler ) {
415vsprintf( buf, msg, ap );
416va_end( ap );
417(*handler)( QtWarningMsg, buf );
418 } else {
419vfprintf( stderr, msg, ap );
420va_end( ap );
421fprintf( stderr, "\n" );// add newline
422 }
423}
424
425
426// again, copied
427void warning( const char *msg, ... )
428{
429 char buf[8196];
430 va_list ap;
431 va_start( ap, msg );// use variable arg list
432 if ( handler ) {
433vsprintf( buf, msg, ap );
434va_end( ap );
435(*handler)( QtWarningMsg, buf );
436 } else {
437vfprintf( stderr, msg, ap );
438va_end( ap );
439fprintf( stderr, "\n" );// add newline
440 }
441}
442
443void qFatal( const char *msg, ... )
444{
445 char buf[8196];
446 va_list ap;
447 va_start( ap, msg );// use variable arg list
448 if ( handler ) {
449vsprintf( buf, msg, ap );
450va_end( ap );
451(*handler)( QtFatalMsg, buf );
452 } else {
453vfprintf( stderr, msg, ap );
454va_end( ap );
455fprintf( stderr, "\n" );// add newline
456#if defined(_OS_UNIX_) && defined(DEBUG)
457abort();// trap; generates core dump
458#else
459exit( 1 );// goodbye cruel world
460#endif
461 }
462}
463
464// yet again, copied
465void fatal( const char *msg, ... )
466{
467 char buf[8196];
468 va_list ap;
469 va_start( ap, msg );// use variable arg list
470 if ( handler ) {
471vsprintf( buf, msg, ap );
472va_end( ap );
473(*handler)( QtFatalMsg, buf );
474 } else {
475vfprintf( stderr, msg, ap );
476va_end( ap );
477fprintf( stderr, "\n" );// add newline
478#if defined(_OS_UNIX_) && defined(DEBUG)
479abort();// trap; generates core dump
480#else
481exit( 1 );// goodbye cruel world
482#endif
483 }
484}
485
486#endif
487
488
489/*!
490 \fn void ASSERT( bool test )
491 \relates QApplication
492 Prints a warning message containing the source code file name and line number
493 if \e test is FALSE.
494
495 This is really a macro defined in qglobal.h.
496
497 ASSERT is useful for testing required conditions in your program.
498
499 Example:
500 \code
501 //
502 // File: div.cpp
503 //
504
505 #include <qglobal.h>
506
507 int divide( int a, int b )
508 {
509ASSERT( b != 0 );// this is line 9
510return a/b;
511 }
512 \endcode
513
514 If \c b is zero, the ASSERT statement will output the following message
515 using the qWarning() function:
516 \code
517 ASSERT: "b == 0" in div.cpp (9)
518 \endcode
519
520 \sa qWarning(), \link debug.html Debugging\endlink
521*/
522
523
524/*!
525 \fn void CHECK_PTR( void *p )
526 \relates QApplication
527 If \e p is null, a fatal messages says that the program ran out of memory
528 and exits. If \e p is not null, nothing happens.
529
530 This is really a macro defined in qglobal.h.
531
532 Example:
533 \code
534 int *a;
535 CHECK_PTR( a = new int[80] );// never do this!
536 // do this instead:
537 a = new int[80];
538 CHECK_PTR( a );// this is fine
539 \endcode
540
541 \sa qFatal(), \link debug.html Debugging\endlink
542*/
543
544
545//
546// The CHECK_PTR macro calls this function to check if an allocation went ok.
547//
548
549bool qt_check_pointer( bool c, const char *n, int l )
550{
551 if ( c )
552qFatal( "In file %s, line %d: Out of memory", n, l );
553 return TRUE;
554}
555
556
557static bool firstObsoleteWarning(const char *obj, const char *oldfunc )
558{
559 static QAsciiDict<int> *obsoleteDict = 0;
560 if ( !obsoleteDict ) {// first time func is called
561obsoleteDict = new QAsciiDict<int>;
562#if defined(DEBUG)
563qDebug(
564 "You are using obsolete functions in the Qt library. Call the function\n"
565 "qSuppressObsoleteWarnings() to suppress obsolete warnings.\n"
566 );
567#endif
568 }
569 QCString s( obj );
570 s += "::";
571 s += oldfunc;
572 if ( obsoleteDict->find(s.data()) == 0 ) {
573obsoleteDict->insert( s.data(), (int*)1 );// anything different from 0
574return TRUE;
575 }
576 return FALSE;
577}
578
579static bool suppressObsolete = FALSE;
580
581void qSuppressObsoleteWarnings( bool suppress )
582{
583 suppressObsolete = suppress;
584}
585
586void qObsolete( const char *obj, const char *oldfunc, const char *newfunc )
587{
588 if ( suppressObsolete )
589return;
590 if ( !firstObsoleteWarning(obj, oldfunc) )
591return;
592 if ( obj )
593qDebug( "%s::%s: This function is obsolete, use %s instead.",
594 obj, oldfunc, newfunc );
595 else
596qDebug( "%s: This function is obsolete, use %s instead.",
597 oldfunc, newfunc );
598}
599
600void qObsolete( const char *obj, const char *oldfunc )
601{
602 if ( suppressObsolete )
603return;
604 if ( !firstObsoleteWarning(obj, oldfunc) )
605return;
606 if ( obj )
607qDebug( "%s::%s: This function is obsolete.", obj, oldfunc );
608 else
609qDebug( "%s: This function is obsolete.", oldfunc );
610}
611
612void qObsolete( const char *message )
613{
614 if ( suppressObsolete )
615return;
616 if ( !firstObsoleteWarning( "Qt", message) )
617return;
618 qDebug( "%s", message );
619}
620
621
622/*!
623 \relates QApplication
624 Installs a Qt message handler. Returns a pointer to the message handler
625 previously defined.
626
627 The message handler is a function that prints out debug messages,
628 warnings and fatal error messages. The Qt library (debug version)
629 contains hundreds of warning messages that are printed when internal
630 errors (usually invalid function arguments) occur. If you implement
631 your own message handler, you get total control of these messages.
632
633 The default message handler prints the message to the standard output
634 under X11 or to the debugger under Windows. If it is a fatal message,
635 the application aborts immediately.
636
637 Only one message handler can be defined, since this is usually done on
638 an application-wide basis to control debug output.
639
640 To restore the message handler, call \c qInstallMsgHandler(0).
641
642 Example:
643 \code
644 #include <qapplication.h>
645 #include <stdio.h>
646 #include <stdlib.h>
647
648 void myMessageOutput( QtMsgType type, const char *msg )
649 {
650switch ( type ) {
651 case QtDebugMsg:
652fprintf( stderr, "Debug: %s\n", msg );
653break;
654 case QtWarningMsg:
655fprintf( stderr, "Warning: %s\n", msg );
656break;
657 case QtFatalMsg:
658fprintf( stderr, "Fatal: %s\n", msg );
659abort();// dump core on purpose
660}
661 }
662
663 int main( int argc, char **argv )
664 {
665qInstallMsgHandler( myMessageOutput );
666QApplication a( argc, argv );
667...
668return a.exec();
669 }
670 \endcode
671
672 \sa qDebug(), qWarning(), qFatal(), \link debug.html Debugging\endlink
673*/
674
675msg_handler qInstallMsgHandler( msg_handler h )
676{
677 msg_handler old = handler;
678 handler = h;
679 return old;
680}
681
682
683#ifdef _WS_WIN_
684bool qt_winunicode=FALSE;
685#endif
686

Archive Download this file

Revision: 1322