]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
Recreated the complete cvs tree because the project architecture deeply changed
[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 15:02:00 $
7   Version:   $Revision: 1.1.1.1 $
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
31 #include "bbtkExecuter.h"
32 #include <fstream>
33 #include <deque>
34
35 namespace bbtk
36 {
37  
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
82
83     /// The type of dictionnary of commands 
84     typedef std::map<std::string,CommandInfoType> CommandDictType;
85
86   public:
87     /// Constructor
88     Interpreter();  
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
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     /// Splits the string in around the first dot encountered
155     void SplitAroundFirstDot( const std::string& in,
156                               std::string& left,
157                               std::string& right);
158     ///
159     void FindCommandsWithPrefix( char* buf,
160                                  int n,
161                                  std::vector<std::string>& commands );
162              
163     /// Sets the bool that indicates wether we are in command line context
164     void SetCommandLine(bool v = true) { mCommandLine = v; }
165
166   private:
167   /// the methods for include bbs
168   bool FileExists(std::string strFilename);
169   std::string ExtractScriptName(const std::string &name);
170   std::string ExpandLibName(const std::string &name, bool v);  
171   std::string MakeLibnameFromPath(std::string path, std::string pkgname);
172   bool IsAtRoot(std::string cwd);       
173     
174   private:
175   
176     //==================================================================
177     // ATTRIBUTES
178     /// The factory
179     //    bbtk::Factory* mFactory;
180     /// The command executer  
181     bbtk::Executer* mExecuter;
182     /// Vector of opened files  
183     std::vector<std::ifstream*> mFile; 
184     /// Vector of names of open files  
185     std::vector<std::string> mFileName; 
186     /// Stores the current line number in each open file
187     std::vector<int> mLine; 
188     /// The dictionnary of commands
189     CommandDictType mCommandDict;
190     /// Are we in a command line context ?
191     bool mCommandLine;
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