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

qlibrary.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qlibrary.cpp,v 1.2 2003/07/10 02:40:12 llornkcor Exp $
00003 **
00004 ** Implementation of QLibrary class
00005 **
00006 ** Created : 000101
00007 **
00008 ** Copyright (C) 2000-2003 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 #include <private/qlibrary_p.h>
00040 
00041 #ifndef QT_NO_LIBRARY
00042 
00043 // uncomment this to get error messages
00044 //#define QT_DEBUG_COMPONENT 1
00045 // uncomment this to get error and success messages
00046 //#define QT_DEBUG_COMPONENT 2
00047 
00048 #ifndef QT_DEBUG_COMPONENT
00049 # if defined(QT_DEBUG)
00050 #  define QT_DEBUG_COMPONENT 1
00051 # endif
00052 #endif
00053 
00054 #if defined(QT_DEBUG_COMPONENT)
00055 #include <qfile.h>
00056 #endif
00057 
00058 #if defined(Q_WS_WIN) && !defined(QT_MAKEDLL)
00059 #define QT_NO_LIBRARY_UNLOAD
00060 #endif
00061 
00062 QLibraryPrivate::QLibraryPrivate( QLibrary *lib )
00063     : pHnd( 0 ), library( lib )
00064 {
00065 }
00066 
00067 
00137 QLibrary::QLibrary( const QString& filename )
00138     : libfile( filename ), aunload( TRUE )
00139 {
00140     libfile.replace( '\\', '/' );
00141     d = new QLibraryPrivate( this );
00142 }
00143 
00153 QLibrary::~QLibrary()
00154 {
00155     if ( autoUnload() )
00156         unload();
00157 
00158     delete d;
00159 }
00160 
00177 void *QLibrary::resolve( const char* symb )
00178 {
00179     if ( !d->pHnd )
00180         load();
00181     if ( !d->pHnd )
00182         return 0;
00183 
00184     void *address = d->resolveSymbol( symb );
00185 
00186     return address;
00187 }
00188 
00221 void *QLibrary::resolve( const QString &filename, const char *symb )
00222 {
00223     QLibrary lib( filename );
00224     lib.setAutoUnload( FALSE );
00225     return lib.resolve( symb );
00226 }
00227 
00233 bool QLibrary::isLoaded() const
00234 {
00235     return d->pHnd != 0;
00236 }
00237 
00244 bool QLibrary::load()
00245 {
00246     return d->loadLibrary();
00247 }
00248 
00258 bool QLibrary::unload()
00259 {
00260     if ( !d->pHnd )
00261         return TRUE;
00262 
00263 #if !defined(QT_NO_LIBRARY_UNLOAD)
00264     if ( !d->freeLibrary() ) {
00265 # if defined(QT_DEBUG_COMPONENT)
00266         qWarning( "%s could not be unloaded", (const char*) QFile::encodeName(library()) );
00267 # endif
00268         return FALSE;
00269     }
00270 
00271 # if defined(QT_DEBUG_COMPONENT) && QT_DEBUG_COMPONENT == 2
00272     qWarning( "%s has been unloaded", (const char*) QFile::encodeName(library()) );
00273 # endif
00274     d->pHnd = 0;
00275 #endif
00276     return TRUE;
00277 }
00278 
00286 bool QLibrary::autoUnload() const
00287 {
00288     return (bool)aunload;
00289 }
00290 
00299 void QLibrary::setAutoUnload( bool enabled )
00300 {
00301     aunload = enabled;
00302 }
00303 
00315 QString QLibrary::library() const
00316 {
00317     if ( libfile.isEmpty() )
00318         return libfile;
00319 
00320     QString filename = libfile;
00321 
00322 #if defined(Q_WS_WIN)
00323     if ( filename.findRev( '.' ) <= filename.findRev( '/' ) )
00324         filename += ".dll";
00325 #elif defined(Q_OS_MACX)
00326     if ( filename.find( ".dylib" ) == -1 )
00327         filename += ".dylib";
00328 #else
00329     QString filter = ".so";
00330     if ( filename.find(filter) == -1 ) {
00331         const int x = filename.findRev( "/" );
00332         if ( x != -1 ) {
00333             QString path = filename.left( x + 1 );
00334             QString file = filename.right( filename.length() - x - 1 );
00335             filename = QString( "%1lib%2.%3" ).arg( path ).arg( file ).arg( filter );
00336         } else {
00337             filename = QString( "lib%1.%2" ).arg( filename ).arg( filter );
00338         }
00339     }
00340 #endif
00341 
00342     return filename;
00343 }
00344 #endif //QT_NO_LIBRARY

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