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