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