]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
Move some general usage methods to Utilities, to avoid dupplicate code :
[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/22 16:55:04 $
7   Version:   $Revision: 1.2 $
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   class BBTK_EXPORT Interpreter
41   {
42
43   private:
44     
45     /// The enumeration of command codes == Command name
46     typedef enum 
47     {
48       cNew, 
49       cDelete,
50       cConnect,
51       cExec,
52       cDefine,
53       cEndDefine,
54       cInput,
55       cOutput,
56       cSet,
57       cConfig,  // JPR
58       cReset,   // EED
59       cAuthor,
60       cDescription,
61       cHelp,
62       cMessage,
63       cInclude,
64       cQuit,
65       cLoad,
66       cUnload,
67       cGraph,
68       cPrint,
69       cWorkspace // LG 
70     } CommandCodeType;
71
72     /// The structure storing the informations on a command 
73     typedef struct 
74     {
75       std::string keyword;
76       int argmin, argmax;
77       CommandCodeType code;
78       std::string syntax;
79       std::string help;
80     } CommandInfoType;
81
82     /// The type of dictionnary of commands 
83     typedef std::map<std::string,CommandInfoType> CommandDictType;
84
85   public:
86     /// Constructor
87     Interpreter();
88  
89     /// Destructor
90     ~Interpreter();
91       
92    static Interpreter* mGlobalInterpreter;
93
94     /// Launches a command line interpreter (with a prompt)
95     void CommandLineInterpreter();
96     
97     /// Sets the inputs of the workspace : 
98     /// the map is passed as is to the Executer
99     void SetInputs(const std::map<std::string,std::string>& m)
100     { mExecuter->SetInputs(m); }
101  
102     /// Puts the executer in "no exec" mode, 
103     /// which creates but does not execute pipelines.
104     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
105
106     /// 
107     //typedef Executer::DialogModeType DialogModeType;
108     typedef VirtualExec::DialogModeType DialogModeType;
109
110     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
111
112     /// Runs the interpretation of a file
113     void InterpretFile( const std::string& filename, bool use_configuration_file=true, bool verbose=false);
114
115     /// Interprets a line (either from a file or typed interactively)
116     void InterpretLine( const std::string& line, bool &insideComment );
117
118     /// Reads a line from prompt
119     void GetLineFromPrompt( std::string& line );
120
121     /// Splits a line into words
122     void SplitLine ( const std::string& line,
123                      std::vector<std::string>& words );
124
125     /// Executes the right action depending on the command name  
126     void InterpretCommand( const std::vector<std::string>& words, 
127                            CommandInfoType& info );
128     
129     /// Switch to the interpretation of a file
130     void SwitchToFile( const std::string& filename, bool use_configuration_file=true, bool verbose=false );
131     
132     /// Closes the currently open file
133     void CloseCurrentFile();
134     
135     /// Closes all open files
136     void CloseAllFiles();
137
138     /// Displays help (entry point of any help)
139     void Help(const std::vector<std::string>& words);
140     
141     /// Displays help on all the commands
142     void HelpCommands();
143     
144     /// Displays help on a particular command 
145     void HelpCommand( const std::string& command );
146     
147     ///
148     void Graph(const std::vector<std::string>& words);
149     
150     ///
151     void Config(bool verbose) const; // JPR
152     /// 
153     void Print(const std::string&);
154
155     ///
156     void FindCommandsWithPrefix( char* buf,
157                                  int n,
158                                  std::vector<std::string>& commands );
159              
160     /// Sets the bool that indicates wether we are in command line context
161     void SetCommandLine(bool v = true) { mCommandLine = v; }
162
163   private: 
164     
165   private:
166   
167     //==================================================================
168     // ATTRIBUTES
169
170     /// The factory
171     //    bbtk::Factory* mFactory;
172
173     /// The command executer  
174    // bbtk::Executer* mExecuter;
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
207 }
208 #endif