interpreter.cpp
1/***************************************************************************
2 * interpreter.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "interpreter.h"
21#include "script.h"
22#include "../main/manager.h"
23#include "../main/scriptcontainer.h"
24
25#include <klibloader.h>
26
27extern "C"
28{
29 typedef int (*def_interpreter_func)(Kross::Api::InterpreterInfo*);
30}
31
32using namespace Kross::Api;
33
34/*************************************************************************
35 * InterpreterInfo
36 */
37
38InterpreterInfo::InterpreterInfo(const TQString& interpretername, const TQString& library, const TQString& wildcard, TQStringList mimetypes, Option::Map options)
39 : m_interpretername(interpretername)
40 , m_library(library)
41 , m_wildcard(wildcard)
42 , m_mimetypes(mimetypes)
43 , m_options(options)
44 , m_interpreter(0)
45{
46}
47
49{
50 for(Option::Map::Iterator it = m_options.begin(); it != m_options.end(); ++it)
51 delete it.data();
52
53 delete m_interpreter;
54 m_interpreter = 0;
55}
56
58{
59 return m_interpretername;
60}
61
63{
64 return m_wildcard;
65}
66
68{
69 return m_mimetypes;
70}
71
72bool InterpreterInfo::hasOption(const TQString& key)
73{
74 return m_options.contains(key);
75}
76
78{
79 return m_options[name];
80}
81
82const TQVariant InterpreterInfo::getOptionValue(const TQString name, TQVariant defaultvalue)
83{
84 Option* o = m_options[name];
85 return o ? o->value : defaultvalue;
86}
87
92
94{
95 if(m_interpreter) // buffered
96 return m_interpreter;
97
98 krossdebug( TQString("Loading the interpreter library for %1").arg(m_interpretername) );
99
100 // Load the krosspython library.
101 KLibLoader *libloader = KLibLoader::self();
102
103 KLibrary* library = libloader->globalLibrary( m_library.latin1() );
104 if(! library) {
105 /*
106 setException(
107 new Exception( TQString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(m_library).arg(m_interpretername) )
108 );
109 */
110 krosswarning( TQString("Could not load library \"%1\" for the \"%2\" interpreter.").arg(m_library).arg(m_interpretername) );
111 return 0;
112 }
113
114 // Get the extern "C" krosspython_instance function.
115 def_interpreter_func interpreter_func;
116 interpreter_func = (def_interpreter_func) library->symbol("krossinterpreter");
117 if(! interpreter_func) {
118 //setException( new Exception("Failed to load symbol in krosspython library.") );
119 krosswarning("Failed to load the 'krossinterpreter' symbol from the library.");
120 }
121 else {
122 // and execute the extern krosspython_instance function.
123 m_interpreter = (Interpreter*) (interpreter_func)(this);
124 if(! m_interpreter) {
125 krosswarning("Failed to load the Interpreter instance from library.");
126 }
127 else {
128 // Job done. The library is loaded and our Interpreter* points
129 // to the external Kross::Python::Interpreter* instance.
130 krossdebug("Successfully loaded Interpreter instance from library.");
131 }
132 }
133
134 // finally unload the library.
135 library->unload();
136
137 return m_interpreter;
138}
139
140/*************************************************************************
141 * Interpreter
142 */
143
145 : m_interpreterinfo(info)
146{
147}
148
Each interpreter is able to define options we could use to manipulate the interpreter behaviour.
Definition interpreter.h:50
TQVariant value
The value the option has.
Definition interpreter.h:76
TQMap< TQString, Option * > Map
Map of options.
Definition interpreter.h:56
While the Interpreter is the implemented interpreter this class is used to provide some abstract info...
Definition interpreter.h:42
const TQStringList getMimeTypes()
List of mimetypes this interpreter supports.
const TQString getWildcard()
Interpreter * getInterpreter()
const TQString getInterpretername()
InterpreterInfo(const TQString &interpretername, const TQString &library, const TQString &wildcard, TQStringList mimetypes, Option::Map options)
Constructor.
Option * getOption(const TQString name)
bool hasOption(const TQString &key)
const TQVariant getOptionValue(const TQString name, TQVariant defaultvalue=TQVariant())
Base class for interpreters.
virtual ~Interpreter()
Destructor.
Interpreter(InterpreterInfo *info)
Constructor.
The common Kross API used as common codebase.
Definition callable.h:31
void krossdebug(const TQString &s)
Debugging function.
void krosswarning(const TQString &s)
Warning function.