Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qglobal.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qglobal.cpp,v 1.2 2003/07/10 02:40:11 llornkcor Exp $
00003 **
00004 ** Global functions
00005 **
00006 ** Created : 920604
00007 **
00008 ** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #include "qplatformdefs.h"
00039 
00040 #include "qasciidict.h"
00041 #include <limits.h>
00042 #include <stdio.h>
00043 #include <limits.h>
00044 #include <stdarg.h>
00045 #include <stdlib.h>
00046 
00047 
00059 const char *qVersion()
00060 {
00061     return QT_VERSION_STR;
00062 }
00063 
00064 
00065 /*****************************************************************************
00066   System detection routines
00067  *****************************************************************************/
00068 
00069 static bool si_alreadyDone = FALSE;
00070 static int  si_wordSize;
00071 static bool si_bigEndian;
00072 
00087 bool qSysInfo( int *wordSize, bool *bigEndian )
00088 {
00089 #if defined(QT_CHECK_NULL)
00090     Q_ASSERT( wordSize != 0 );
00091     Q_ASSERT( bigEndian != 0 );
00092 #endif
00093 
00094     if ( si_alreadyDone ) {                     // run it only once
00095         *wordSize  = si_wordSize;
00096         *bigEndian = si_bigEndian;
00097         return TRUE;
00098     }
00099 
00100     si_wordSize = 0;
00101     Q_ULONG n = (Q_ULONG)(~0);
00102     while ( n ) {                               // detect word size
00103         si_wordSize++;
00104         n /= 2;
00105     }
00106     *wordSize = si_wordSize;
00107 
00108     if ( *wordSize != 64 &&
00109          *wordSize != 32 &&
00110          *wordSize != 16 ) {                    // word size: 16, 32 or 64
00111 #if defined(QT_CHECK_RANGE)
00112         qFatal( "qSysInfo: Unsupported system word size %d", *wordSize );
00113 #endif
00114         return FALSE;
00115     }
00116     if ( sizeof(Q_INT8) != 1 || sizeof(Q_INT16) != 2 || sizeof(Q_INT32) != 4 ||
00117          sizeof(Q_ULONG)*8 != si_wordSize || sizeof(float) != 4 || sizeof(double) != 8 ) {
00118 #if defined(QT_CHECK_RANGE)
00119         qFatal( "qSysInfo: Unsupported system data type size" );
00120 #endif
00121         return FALSE;
00122     }
00123 
00124     bool  be16, be32;                           // determine byte ordering
00125     short ns = 0x1234;
00126     int   nl = 0x12345678;
00127 
00128     unsigned char *p = (unsigned char *)(&ns);  // 16-bit integer
00129     be16 = *p == 0x12;
00130 
00131     p = (unsigned char *)(&nl);                 // 32-bit integer
00132     if ( p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78 )
00133         be32 = TRUE;
00134     else
00135     if ( p[0] == 0x78 && p[1] == 0x56 && p[2] == 0x34 && p[3] == 0x12 )
00136         be32 = FALSE;
00137     else
00138         be32 = !be16;
00139 
00140     if ( be16 != be32 ) {                       // strange machine!
00141 #if defined(QT_CHECK_RANGE)
00142         qFatal( "qSysInfo: Inconsistent system byte order" );
00143 #endif
00144         return FALSE;
00145     }
00146 
00147     *bigEndian = si_bigEndian = be32;
00148     si_alreadyDone = TRUE;
00149     return TRUE;
00150 }
00151 
00152 #if !defined(QWS) && defined(Q_OS_MAC)
00153 
00154 #include "qt_mac.h"
00155 
00156 int qMacVersion()
00157 {
00158     static int macver = Qt::MV_Unknown;
00159     static bool first = TRUE;
00160     if(first) {
00161         first = FALSE;
00162         long gestalt_version;
00163         if(Gestalt(gestaltSystemVersion, &gestalt_version) == noErr) {
00164             if(gestalt_version >= 0x1020 && gestalt_version < 0x1030)
00165                 macver = Qt::MV_10_DOT_2;
00166             else if(gestalt_version >= 0x1010 && gestalt_version < 0x1020)
00167                 macver = Qt::MV_10_DOT_1;
00168         }
00169     }
00170     return macver;
00171 }
00172 Qt::MacintoshVersion qt_macver = (Qt::MacintoshVersion)qMacVersion();
00173 #elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN)
00174 bool qt_winunicode;
00175 
00176 #include "qt_windows.h"
00177 
00178 int qWinVersion()
00179 {
00180 #ifndef VER_PLATFORM_WIN32s
00181 #define VER_PLATFORM_WIN32s         0
00182 #endif
00183 #ifndef VER_PLATFORM_WIN32_WINDOWS
00184 #define VER_PLATFORM_WIN32_WINDOWS  1
00185 #endif
00186 #ifndef VER_PLATFORM_WIN32_NT
00187 #define VER_PLATFORM_WIN32_NT       2
00188 #endif
00189 
00190     static int winver = Qt::WV_NT;
00191     static int t=0;
00192     if ( !t ) {
00193         t=1;
00194 #ifdef Q_OS_TEMP
00195         OSVERSIONINFOW osver;
00196         osver.dwOSVersionInfoSize = sizeof(osver);
00197         GetVersionEx( &osver );
00198 #else
00199         OSVERSIONINFOA osver;
00200         osver.dwOSVersionInfoSize = sizeof(osver);
00201         GetVersionExA( &osver );
00202 #endif
00203         switch ( osver.dwPlatformId ) {
00204         case VER_PLATFORM_WIN32s:
00205             winver = Qt::WV_32s;
00206             break;
00207         case VER_PLATFORM_WIN32_WINDOWS:
00208             // We treat Windows Me (minor 90) the same as Windows 98
00209             if ( ( osver.dwMinorVersion == 10 ) || ( osver.dwMinorVersion == 90 ) )
00210                 winver = Qt::WV_98;
00211             else
00212                 winver = Qt::WV_95;
00213             break;
00214         default: // VER_PLATFORM_WIN32_NT
00215             if ( osver.dwMajorVersion < 5 ) {
00216                 winver = Qt::WV_NT;
00217             } else if ( osver.dwMinorVersion == 0 ) {
00218                 winver = Qt::WV_2000;
00219             } else {
00220                 winver = Qt::WV_XP;
00221             }
00222         }
00223     }
00224 
00225 #if defined(UNICODE)
00226     if ( winver & Qt::WV_NT_based )
00227         qt_winunicode = TRUE;
00228     else
00229 #endif
00230         qt_winunicode = FALSE;
00231 
00232     return winver;
00233 }
00234 
00235 Qt::WindowsVersion qt_winver = (Qt::WindowsVersion)qWinVersion();
00236 #endif
00237 
00238 
00239 /*****************************************************************************
00240   Debug output routines
00241  *****************************************************************************/
00242 
00340 static QtMsgHandler handler = 0;                // pointer to debug handler
00341 static const int QT_BUFFER_LENGTH = 8196;       // internal buffer length
00342 
00343 
00344 #ifdef Q_OS_MAC
00345 QString cfstring2qstring(CFStringRef str)
00346 {
00347     CFIndex length = CFStringGetLength(str); 
00348     if(const UniChar *chars = CFStringGetCharactersPtr(str)) 
00349         return QString((QChar *)chars, length);
00350     UniChar *buffer = (UniChar*)malloc(length * sizeof(UniChar)); 
00351     CFStringGetCharacters(str, CFRangeMake(0, length), buffer); 
00352     QString ret((QChar *)buffer, length);
00353     free(buffer); 
00354     return ret;
00355 }
00356 
00357 unsigned char * p_str(const char * c, int len=-1)
00358 {
00359     const int maxlen = 255;
00360     if(len == -1)
00361         len = qstrlen(c);
00362     if(len > maxlen) {
00363         qWarning( "p_str len must never exceed %d", maxlen );
00364         len = maxlen;
00365     }
00366     unsigned char *ret = (unsigned char*)malloc(len+2);
00367     *ret=len;
00368     memcpy(((char *)ret)+1,c,len);
00369     *(ret+len+1) = '\0';
00370     return ret;
00371 }
00372 
00373 unsigned char * p_str(const QString &s)
00374 {
00375     return p_str(s, s.length());
00376 }
00377 
00378 QCString p2qstring(const unsigned char *c) {
00379        char *arr = (char *)malloc(c[0] + 1);
00380        memcpy(arr, c+1, c[0]);
00381        arr[c[0]] = '\0';
00382        QCString ret = arr;
00383        delete arr;
00384        return ret;
00385 }
00386 #endif
00387 
00388 
00389 #ifdef Q_CC_MWERKS
00390 
00391 #include "qt_mac.h"
00392 
00393 extern bool qt_is_gui_used;
00394 static void mac_default_handler( const char *msg )
00395 {
00396     if ( qt_is_gui_used ) {
00397         const char *p = p_str(msg);
00398         DebugStr(p);
00399         free(p);
00400     } else {
00401         fprintf( stderr, msg );
00402     }
00403 }
00404 
00405 #endif
00406 
00407 
00408 void qDebug( const char *msg, ... )
00409 {
00410     char buf[QT_BUFFER_LENGTH];
00411     va_list ap;
00412     va_start( ap, msg );                        // use variable arg list
00413     if ( handler ) {
00414 #if defined(QT_VSNPRINTF)
00415         QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
00416 #else
00417         vsprintf( buf, msg, ap );
00418 #endif
00419         va_end( ap );
00420         (*handler)( QtDebugMsg, buf );
00421     } else {
00422 #if defined(Q_CC_MWERKS)
00423         vsprintf( buf, msg, ap );               // ### is there no vsnprintf()?
00424         va_end( ap );
00425         mac_default_handler(buf);
00426 #else
00427         vfprintf( stderr, msg, ap );
00428         va_end( ap );
00429         fprintf( stderr, "\n" );                // add newline
00430 #endif
00431     }
00432 }
00433 
00434 // copied... this looks really bad.
00435 void debug( const char *msg, ... )
00436 {
00437     char buf[QT_BUFFER_LENGTH];
00438     va_list ap;
00439     va_start( ap, msg );                        // use variable arg list
00440     if ( handler ) {
00441 #if defined(QT_VSNPRINTF)
00442         QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
00443 #else
00444         vsprintf( buf, msg, ap );
00445 #endif
00446         va_end( ap );
00447         (*handler)( QtDebugMsg, buf );
00448     } else {
00449 #ifdef Q_CC_MWERKS
00450         vsprintf( buf, msg, ap );               // ### is there no vsnprintf()?
00451         va_end( ap );
00452         mac_default_handler(buf);
00453 #else
00454         vfprintf( stderr, msg, ap );
00455         va_end( ap );
00456         fprintf( stderr, "\n" );                // add newline
00457 #endif
00458     }
00459 }
00460 
00461 void qWarning( const char *msg, ... )
00462 {
00463     char buf[QT_BUFFER_LENGTH];
00464     va_list ap;
00465     va_start( ap, msg );                        // use variable arg list
00466     if ( handler ) {
00467 #if defined(QT_VSNPRINTF)
00468         QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
00469 #else
00470         vsprintf( buf, msg, ap );
00471 #endif
00472         va_end( ap );
00473         (*handler)( QtWarningMsg, buf );
00474     } else {
00475 #ifdef Q_CC_MWERKS
00476         vsprintf( buf, msg, ap );               // ### is there no vsnprintf()?
00477         va_end( ap );
00478         mac_default_handler(buf);
00479 #else
00480         vfprintf( stderr, msg, ap );
00481         va_end( ap );
00482         fprintf( stderr, "\n" );                // add newline
00483 #endif
00484     }
00485 }
00486 
00487 
00488 // again, copied
00489 void warning( const char *msg, ... )
00490 {
00491     char buf[QT_BUFFER_LENGTH];
00492     va_list ap;
00493     va_start( ap, msg );                        // use variable arg list
00494     if ( handler ) {
00495 #if defined(QT_VSNPRINTF)
00496         QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
00497 #else
00498         vsprintf( buf, msg, ap );
00499 #endif
00500         va_end( ap );
00501         (*handler)( QtWarningMsg, buf );
00502     } else {
00503 #ifdef Q_CC_MWERKS
00504         vsprintf( buf, msg, ap );               // ### is there no vsnprintf()?
00505         va_end( ap );
00506         mac_default_handler(buf);
00507 #else
00508         vfprintf( stderr, msg, ap );
00509         va_end( ap );
00510         fprintf( stderr, "\n" );                // add newline
00511 #endif
00512     }
00513 }
00514 
00515 void qFatal( const char *msg, ... )
00516 {
00517     char buf[QT_BUFFER_LENGTH];
00518     va_list ap;
00519     va_start( ap, msg );                        // use variable arg list
00520     if ( handler ) {
00521 #if defined(QT_VSNPRINTF)
00522         QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
00523 #else
00524         vsprintf( buf, msg, ap );
00525 #endif
00526         va_end( ap );
00527         (*handler)( QtFatalMsg, buf );
00528     } else {
00529 #ifdef Q_CC_MWERKS
00530         vsprintf( buf, msg, ap );               // ### is there no vsnprintf()?
00531         va_end( ap );
00532         mac_default_handler(buf);
00533 #else
00534         vfprintf( stderr, msg, ap );
00535         va_end( ap );
00536         fprintf( stderr, "\n" );                // add newline
00537 #endif
00538 #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
00539         abort();                                // trap; generates core dump
00540 #else
00541         exit( 1 );                              // goodbye cruel world
00542 #endif
00543     }
00544 }
00545 
00546 // yet again, copied
00547 void fatal( const char *msg, ... )
00548 {
00549     char buf[QT_BUFFER_LENGTH];
00550     va_list ap;
00551     va_start( ap, msg );                        // use variable arg list
00552     if ( handler ) {
00553 #if defined(QT_VSNPRINTF)
00554         QT_VSNPRINTF( buf, QT_BUFFER_LENGTH, msg, ap );
00555 #else
00556         vsprintf( buf, msg, ap );
00557 #endif
00558         va_end( ap );
00559         (*handler)( QtFatalMsg, buf );
00560     } else {
00561 #ifdef Q_CC_MWERKS
00562         vsprintf( buf, msg, ap );               // ### is there no vsnprintf()?
00563         va_end( ap );
00564         mac_default_handler(buf);
00565 #else
00566         vfprintf( stderr, msg, ap );
00567         va_end( ap );
00568         fprintf( stderr, "\n" );                // add newline
00569 #endif
00570 #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
00571         abort();                                // trap; generates core dump
00572 #else
00573         exit( 1 );                              // goodbye cruel world
00574 #endif
00575     }
00576 }
00577 
00589 void qSystemWarning( const char* msg, int code )
00590 {
00591 #ifndef QT_NO_DEBUG
00592 #if defined(Q_OS_WIN32)
00593     if ( code == -1 )
00594         code = GetLastError();
00595 
00596     if ( !code )
00597         return;
00598 
00599 #ifdef Q_OS_TEMP
00600     unsigned short *string;
00601 
00602     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
00603                           NULL,
00604                           code,
00605                           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00606                           (LPTSTR)&string,
00607                           0,
00608                           NULL );
00609 
00610     qWarning( "%s\n\tError code %d - %s (###may need fixing in qglobal.h)", msg, code, (const char *)string );
00611 #else
00612     char* string;
00613 
00614     FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
00615                           NULL,
00616                           code,
00617                           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00618                           (char*)&string,
00619                           0,
00620                           NULL );
00621 
00622     qWarning( "%s\n\tError code %d - %s", msg, code, (const char*)string );
00623 #endif
00624 
00625     LocalFree( (HLOCAL)string );
00626 #else
00627     if ( code != -1 )
00628         qWarning( "%s\n\tError code %d - %s", msg, code, strerror( code ) );
00629     else
00630         qWarning( msg );
00631 #endif
00632 #endif
00633 }
00634 
00696 //
00697 // The Q_CHECK_PTR macro calls this function to check if an allocation went ok.
00698 //
00699 #if (QT_VERSION-0 >= 0x040000)
00700 #if defined(Q_CC_GNU)
00701 #warning "Change Q_CHECK_PTR to '{if ((p)==0) qt_check_pointer(__FILE__,__LINE__);}'"
00702 #warning "No need for qt_check_pointer() to return a value - make it void!"
00703 #endif
00704 #endif
00705 bool qt_check_pointer( bool c, const char *n, int l )
00706 {
00707     if ( c )
00708         qWarning( "In file %s, line %d: Out of memory", n, l );
00709     return TRUE;
00710 }
00711 
00712 
00713 static bool firstObsoleteWarning(const char *obj, const char *oldfunc )
00714 {
00715     static QAsciiDict<int> *obsoleteDict = 0;
00716     if ( !obsoleteDict ) {                      // first time func is called
00717         obsoleteDict = new QAsciiDict<int>;
00718 #if defined(QT_DEBUG)
00719         qDebug(
00720       "You are using obsolete functions in the Qt library. Call the function\n"
00721       "qSuppressObsoleteWarnings() to suppress obsolete warnings.\n"
00722              );
00723 #endif
00724     }
00725     QCString s( obj );
00726     s += "::";
00727     s += oldfunc;
00728     if ( obsoleteDict->find(s.data()) == 0 ) {
00729         obsoleteDict->insert( s.data(), (int*)1 );      // anything different from 0
00730         return TRUE;
00731     }
00732     return FALSE;
00733 }
00734 
00735 static bool suppressObsolete = FALSE;
00736 
00737 void qSuppressObsoleteWarnings( bool suppress )
00738 {
00739     suppressObsolete = suppress;
00740 }
00741 
00742 void qObsolete(  const char *obj, const char *oldfunc, const char *newfunc )
00743 {
00744     if ( suppressObsolete )
00745         return;
00746     if ( !firstObsoleteWarning(obj, oldfunc) )
00747         return;
00748     if ( obj )
00749         qDebug( "%s::%s: This function is obsolete, use %s instead.",
00750                obj, oldfunc, newfunc );
00751     else
00752         qDebug( "%s: This function is obsolete, use %s instead.",
00753                oldfunc, newfunc );
00754 }
00755 
00756 void qObsolete(  const char *obj, const char *oldfunc )
00757 {
00758     if ( suppressObsolete )
00759         return;
00760     if ( !firstObsoleteWarning(obj, oldfunc) )
00761         return;
00762     if ( obj )
00763         qDebug( "%s::%s: This function is obsolete.", obj, oldfunc );
00764     else
00765         qDebug( "%s: This function is obsolete.", oldfunc );
00766 }
00767 
00768 void qObsolete(  const char *message )
00769 {
00770     if ( suppressObsolete )
00771         return;
00772     if ( !firstObsoleteWarning( "Qt", message) )
00773         return;
00774     qDebug( "%s", message );
00775 }
00776 
00777 
00833 QtMsgHandler qInstallMsgHandler( QtMsgHandler h )
00834 {
00835     QtMsgHandler old = handler;
00836     handler = h;
00837     return old;
00838 }
00839 
00840 
00841 /*
00842     Dijkstra's bisection algorithm to find the square root as an integer.
00843     Deliberately not exported as part of the Qt API, but used in both
00844     qsimplerichtext.cpp and qgfxraster_qws.cpp
00845 */
00846 unsigned int qt_int_sqrt( unsigned int n )
00847 {
00848     // n must be in the range 0...UINT_MAX/2-1
00849     if ( n >= ( UINT_MAX>>2 ) ) {
00850         unsigned int r = 2 * qt_int_sqrt( n / 4 );
00851         unsigned int r2 = r + 1;
00852         return ( n >= r2 * r2 ) ? r2 : r;
00853     }
00854     uint h, p= 0, q= 1, r= n;
00855     while ( q <= n )
00856         q <<= 2;
00857     while ( q != 1 ) {
00858         q >>= 2;
00859         h= p + q;
00860         p >>= 1;
00861         if ( r >= h ) {
00862             p += q;
00863             r -= h;
00864         }
00865     }
00866     return p;
00867 }
00868 

Generated on Sat Nov 5 16:18:31 2005 for OPIE by  doxygen 1.4.2