]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkInterpreter.h,v $ $
5   Language:  C++
6   Date:      $Date: 2008/01/28 15:08:53 $
7   Version:   $Revision: 1.4 $
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   class BBTK_EXPORT Interpreter
40   {
41
42   private:
43     
44     /// The enumeration of command codes == Command name
45     typedef enum 
46     {
47       cNew, 
48       cDelete,
49       cConnect,
50       cExec,
51       cPackage,
52       cEndPackage,
53       cDefine,
54       cEndDefine,
55       cInput,
56       cOutput,
57       cSet,
58       cConfig,  // JPR
59       cReset,   // EED
60       cAuthor,
61       cDescription,
62       cHelp,
63       cMessage,
64       cInclude,
65       cQuit,
66       cLoad,
67       cUnload,
68       cGraph,
69       cPrint,
70       cWorkspace // LG 
71     } CommandCodeType;
72
73     /// The structure storing the informations on a command 
74     typedef struct 
75     {
76       std::string keyword;
77       int argmin, argmax;
78       CommandCodeType code;
79       std::string syntax;
80       std::string help;
81     } CommandInfoType;
82
83     /// The type of dictionnary of commands 
84     typedef std::map<std::string,CommandInfoType> CommandDictType;
85
86   public:
87     /// Constructor
88     Interpreter();
89  
90     /// Destructor
91     ~Interpreter();
92       
93    static Interpreter* mGlobalInterpreter;
94
95     /// Launches a command line interpreter (with a prompt)
96     void CommandLineInterpreter();
97     
98     /// Sets the inputs of the workspace : 
99     /// the map is passed as is to the Executer
100     void SetInputs(const std::map<std::string,std::string>& m)
101     { mExecuter->SetInputs(m); }
102  
103     /// Puts the executer in "no exec" mode, 
104     /// which creates but does not execute pipelines.
105     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
106
107     /// 
108     //typedef Executer::DialogModeType DialogModeType;
109     typedef VirtualExec::DialogModeType DialogModeType;
110
111     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
112
113     /// Runs the interpretation of a file
114     void InterpretFile( const std::string& filename, bool use_configuration_file=true, bool verbose=false);
115
116     /// Interprets a line (either from a file or typed interactively)
117     void InterpretLine( const std::string& line, bool &insideComment );
118
119     /// Reads a line from prompt
120     void GetLineFromPrompt( std::string& line );
121
122     /// Splits a line into words
123     void SplitLine ( const std::string& line,
124                      std::vector<std::string>& words );
125
126     /// Executes the right action depending on the command name  
127     void InterpretCommand( const std::vector<std::string>& words, 
128                            CommandInfoType& info );
129     
130     /// Switch to the interpretation of a file
131     void SwitchToFile( const std::string& filename, bool use_configuration_file=true, bool verbose=false );
132     
133     /// Closes the currently open file
134     void CloseCurrentFile();
135     
136     /// Closes all open files
137     void CloseAllFiles();
138
139     /// Displays help (entry point of any help)
140     void Help(const std::vector<std::string>& words);
141     
142     /// Displays help on all the commands
143     void HelpCommands();
144     
145     /// Displays help on a particular command 
146     void HelpCommand( const std::string& command );
147     
148     ///
149     void Graph(const std::vector<std::string>& words);
150     
151     ///
152     void Config(bool verbose) const; // JPR
153     /// 
154     void Print(const std::string&);
155
156     ///
157     void FindCommandsWithPrefix( char* buf,
158                                  int n,
159                                  std::vector<std::string>& commands );
160
161     /// Sets the bool that indicates wether we are in command line context
162     void SetCommandLine(bool v = true) { mCommandLine = v; }
163
164   private: 
165
166   private:
167
168     //==================================================================
169     // ATTRIBUTES
170
171     /// The factory
172     //    bbtk::Factory* mFactory;
173
174     /// The command executer
175     bbtk::VirtualExec* mExecuter;
176
177     /// Vector of opened files 
178     std::vector<std::ifstream*> mFile;
179
180     /// Vector of names of open files
181     std::vector<std::string> mFileName;
182
183     /// Stores the current line number in each open file
184     std::vector<int> mLine;
185
186     /// The dictionnary of commands
187     CommandDictType mCommandDict;
188
189     /// Are we in a command line context ?
190     bool mCommandLine;
191
192     /// The history of commands
193     std::deque< char* > mHistory;
194
195     bool verbose; // true -> displays the search results (for packages)
196   };
197
198
199   // The "Quit" exception
200   struct QuitException : public bbtk::Exception
201   {
202     QuitException() : bbtk::Exception("","","") {}
203   };
204
205 }
206 #endif