libkleo
chiasmuslibrary.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "chiasmuslibrary.h"
00035
00036 #include "chiasmusbackend.h"
00037
00038 #include "kleo/cryptoconfig.h"
00039
00040 #include <klibloader.h>
00041 #include <kdebug.h>
00042 #include <klocale.h>
00043
00044 #include <QFile>
00045
00046 #include <QByteArray>
00047
00048 #include <vector>
00049 #include <algorithm>
00050
00051 #include <cassert>
00052 #include <cstdlib>
00053 #include <cstring>
00054
00055 Kleo::ChiasmusLibrary * Kleo::ChiasmusLibrary::self = 0;
00056
00057 Kleo::ChiasmusLibrary::ChiasmusLibrary() : mXiaLibrary( 0 ) {
00058 self = this;
00059 }
00060
00061 Kleo::ChiasmusLibrary::~ChiasmusLibrary() {
00062
00063 }
00064
00065 Kleo::ChiasmusLibrary::main_func Kleo::ChiasmusLibrary::chiasmus( QString * reason ) const {
00066 assert( ChiasmusBackend::instance() );
00067 assert( ChiasmusBackend::instance()->config() );
00068 const CryptoConfigEntry * lib = ChiasmusBackend::instance()->config()->entry( "Chiasmus", "General", "lib" );
00069 assert( lib );
00070 const QString libfile = lib->urlValue().path();
00071 if ( !mXiaLibrary )
00072 mXiaLibrary = KLibLoader::self()->library( libfile );
00073 if ( !mXiaLibrary ) {
00074 if ( reason )
00075 *reason = i18n( "Failed to load %1: %2",
00076 libfile, KLibLoader::self()->lastErrorMessage() );
00077 kDebug(5150) <<"ChiasmusLibrary: loading \"" << libfile
00078 << "\" failed:" << KLibLoader::self()->lastErrorMessage();
00079 return 0;
00080 }
00081 KLibrary::void_function_ptr symbol = mXiaLibrary->resolveFunction( "Chiasmus" );
00082 if ( !symbol ) {
00083 if ( reason )
00084 *reason = i18n( "Failed to load %1: %2",
00085 libfile, i18n( "Library does not contain the symbol \"Chiasmus\"." ) );
00086 kDebug(5150) <<"ChiasmusLibrary: loading \"" << libfile
00087 << "\" failed: " << "Library does not contain the symbol \"Chiasmus\".";
00088 return 0;
00089 }
00090
00091 assert( symbol );
00092 return ( main_func )symbol;
00093 }
00094
00095 namespace {
00096 class ArgvProvider {
00097 char ** mArgv;
00098 int mArgc;
00099 public:
00100 ArgvProvider( const QVector<QByteArray> & args ) {
00101 mArgv = new char * [args.size()];
00102 for ( int i = 0 ; i < args.size() ; ++i )
00103 mArgv[i] = strdup( args[i].data() );
00104 }
00105 ~ArgvProvider() {
00106 std::for_each( mArgv, mArgv + mArgc, std::free );
00107 delete[] mArgv;
00108 }
00109 char ** argv() const { return mArgv; }
00110 };
00111 }
00112
00113 int Kleo::ChiasmusLibrary::perform( const QVector<QByteArray> & args ) const {
00114 if ( main_func func = chiasmus() )
00115 return func( args.size(), ArgvProvider( args ).argv() );
00116 else
00117 return -1;
00118 }