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