]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
Final (?) fix of "include *" pb
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================
2
3   Program:   bbtk
4   Module:    $RCSfile: bbtkInterpreter.h,v $ $
5   Language:  C++
6   Date:      $Date: 2008/03/14 14:58:53 $
7   Version:   $Revision: 1.14 $
8
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16
17 =========================================================================*/
18 /**
19  *  \file 
20  *  \brief class Interpreter : The bbtk language interpreter
21  */
22 /**
23  *  \class bbtk::Interpreter 
24  *  \brief The bbtk language interpreter
25  */
26
27 #ifndef __bbtkInterpreter_h__
28 #define __bbtkInterpreter_h__
29
30 #include "bbtkVirtualExec.h"
31 #include "bbtkExecuter.h"
32 #include "bbtkTranscriptor.h"
33
34 #include <fstream>
35 #include <deque>
36
37 namespace bbtk
38 {
39
40 #ifdef _USE_WXWIDGETS_
41   class WxConsole;
42 #endif
43   
44   class BBTK_EXPORT Interpreter
45   {
46
47   private:
48
49     /// The enumeration of command codes == Command name
50     typedef enum
51     {
52       cNew,
53       cDelete,
54       cConnect,
55       cExec,
56       cPackage,
57       cEndPackage,
58       cDefine,
59       cEndDefine,
60       cInput,
61       cOutput,
62       cSet,
63       cConfig,  // JPR
64       cReset,   // EED
65       cAuthor, 
66       cCategory, // JPR
67       cDescription,
68       cHelp,
69       cMessage,
70       cInclude,
71       cQuit,
72       cLoad,
73       cUnload,
74       cGraph,
75       cPrint,
76       cIndex,
77       cWorkspace // LG
78     } CommandCodeType;
79
80     /// The structure storing the informations on a command 
81     typedef struct 
82     {
83       std::string category;
84       int argmin, argmax;
85       CommandCodeType code;
86       std::string syntax;
87       std::string help;
88     } CommandInfoType;
89
90     /// The type of dictionnary of commands 
91     typedef std::map<std::string,CommandInfoType> CommandDictType;
92
93   public:
94     /// Constructor
95     Interpreter();
96
97     /// Destructor
98     ~Interpreter();
99
100     /// Launches a command line interpreter (with a prompt)
101     void CommandLineInterpreter();
102
103     /// Sets the inputs of the workspace :
104     /// the map is passed as is to the Executer
105     void SetInputs(const std::map<std::string,std::string>& m)
106     { mExecuter->SetInputs(m); }
107
108     /// Puts the executer in "no exec" mode,
109     /// which creates but does not execute pipelines.
110     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
111
112     ///
113     //typedef Executer::DialogModeType DialogModeType;
114     typedef VirtualExec::DialogModeType DialogModeType;
115
116     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
117
118     /// Runs the interpretation of a file
119     void InterpretFile( const std::string& filename);
120
121     /// Interprets a line (either from a file or typed interactively)
122     void InterpretLine( const std::string& line, bool &insideComment );
123
124     /// Reads a line from prompt
125     void GetLineFromPrompt( std::string& line );
126
127     /// Splits a line into words
128     void SplitLine ( const std::string& line,
129                      std::vector<std::string>& words );
130
131     /// Executes the right action depending on the command name
132     void InterpretCommand( const std::vector<std::string>& words,
133                            CommandInfoType& info );
134
135     /// Switch to the interpretation of a file
136     void SwitchToFile( const std::string& filename );
137
138    /// Switch to the interpretation of a stringstream
139     void SwitchToStream( std::stringstream* stream );
140
141     /// Closes the currently open file
142     void CloseCurrentFile();
143
144     /// Closes all open files
145     void CloseAllFiles();
146
147     /// Displays help (entry point of any help)
148     void Help(const std::vector<std::string>& words);
149
150     /// Displays help on all the commands
151     void HelpCommands();
152
153     /// Displays help on a particular command 
154     void HelpCommand( const std::string& command );
155
156     ///
157     void Graph(const std::vector<std::string>& words);
158
159     ///
160     void Config() const; 
161     /// 
162     void Print(const std::string&);
163
164     void Index(const std::string& filename, 
165                const std::string& type = "Initials");
166     ///
167     void FindCommandsWithPrefix( char* buf,
168                                  int n,
169                                  std::vector<std::string>& commands );
170
171     /// Sets the bool that indicates wether we are in command line context
172     void SetCommandLine(bool v = true) { mCommandLine = v; }
173
174 #ifdef _USE_WXWIDGETS_
175     /// Sets the WxConsole that created the interpreter (if any)
176     void SetWxConsole(WxConsole* c) { mWxConsole = c; }
177     /// Gets the WxConsole that created the interpreter (if any) 
178     WxConsole* GetWxConsole() { return mWxConsole; }
179     /// Gets the WxConsole that created the interpreter (if any) - const 
180     const WxConsole* GetWxConsole() const { return mWxConsole; }
181 #endif
182
183     /// Gets the Executer 
184     VirtualExec* GetExecuter() { return mExecuter; }
185     /// Gets the Executer (const)
186     const VirtualExec* GetExecuter() const { return mExecuter; }
187
188   private:
189
190     /// Opens the file fullPathScriptName 
191     /// includeScriptName is the name as given to the include command 
192     void LoadScript( std::string fullPathScriptName,
193                      std::string includeScriptName);
194
195   private:
196
197     //==================================================================
198     // ATTRIBUTES
199
200     /// The command executer used
201     bbtk::VirtualExec* mExecuter;
202
203 #ifdef _USE_WXWIDGETS_
204     /// The WxConsole which uses the interpreter (0 if none)
205     bbtk::WxConsole* mWxConsole;
206 #endif
207
208     /// Vector of open files / buffers (can be stringstream)
209     std::vector<std::istream*> mFile;
210
211     /// Vector of names of open files with full path (as open)
212     std::vector<std::string> mFileName;
213
214     /// Vector of names of open files as given to the include command
215     std::vector<std::string> mIncludeFileName;
216
217     /// Stores the current line number in each open file
218     std::vector<int> mLine;
219
220     /// The dictionnary of commands
221     CommandDictType mCommandDict;
222
223     /// Are we in a command line context ?
224     bool mCommandLine;
225
226     /// The history of commands
227     std::deque< char* > mHistory;
228
229    int bufferNb;
230   };
231   // EO class Interpreter
232
233
234   // The "Quit" exception
235   struct QuitException : public bbtk::Exception
236   {
237     QuitException() : bbtk::Exception("","","") {}
238   };
239
240 }
241 #endif