]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
af2b7fe15e7f67a6484f399b65520c58371a07d3
[bbtk.git] / kernel / appli / bbi / bbi.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #ifdef _USE_WXWIDGETS_
29
30 //==========================================================================
31 // WITH WX
32 //==========================================================================
33 #include "bbtkObject.h"
34 #include "bbtkInterpreter.h"
35 #include "bbtkWxBlackBox.h"
36 #include "bbtkWxGUIConsole.h"
37
38 #include <wx/cmdline.h>
39 #include <vector>
40 #include <map>
41
42 //==========================================================================
43 // Command line options definition
44 static const wxCmdLineEntryDesc cmdLineDesc[] =
45 {
46   { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
47   { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
48   { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
49   { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
50   { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
51   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
52   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
53   { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
54   { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
55   { wxCMD_LINE_NONE }
56 };
57 //==========================================================================
58
59 //==========================================================================
60 // Processes the command line parsing result
61 struct WxProcessCmdLine
62 {
63   WxProcessCmdLine() {}
64   void Process(wxCmdLineParser& parser);
65
66   //  int argc;
67   std::vector<std::string> argv;
68   bool console;
69   bool debug;
70   bool quiet;
71   bool help;
72   bool graphical_dialog;
73   bool text_dialog;
74   bool no_console;
75   bool proceed;
76   std::map<std::string,std::string> param_map;
77   std::vector<std::string> input_file;
78   
79 };
80 //==========================================================================
81
82 //==========================================================================
83 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
84 {
85   proceed = true;
86   if (parser.Found(_T("D"))) 
87     {
88       bbtk::StaticInitTime::PrintObjectListInfo = true;
89     }
90   
91   debug = ( parser.Found(_T("d")) );  
92   quiet = ( parser.Found(_T("q")) );
93   help = ( parser.Found(_T("h")) );
94   graphical_dialog = ( parser.Found(_T("g")) );
95   text_dialog = ( parser.Found(_T("t")) );
96   no_console = ( parser.Found(_T("N")) );
97   
98   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
99   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
100
101   // parse the arguments and consider those which contain a "=" 
102   // as set input commands, other as files
103   int pargc = parser.GetParamCount();
104   for (int i=0; i<pargc; ++i) 
105     {
106       std::string s = bbtk::wx2std(parser.GetParam(i));
107       std::string::size_type pos = s.find_first_of("=");
108       if (std::string::npos != pos) 
109         {
110           std::string left = s.substr(0,pos);
111           std::string right = s.substr(pos+1,s.size());
112           param_map[left]=right;
113         }
114       else 
115         {
116           input_file.push_back(s);
117         }
118     }
119
120   bool usage = (help && (input_file.size()==0));
121   if (usage) {
122     std::cout << "BBI (The Black Box Interpreter) - bbtk "
123               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
124               << std::endl;
125     parser.Usage();
126     proceed = false;
127   }
128
129   console = ( parser.Found(_T("c")) || 
130               ((input_file.size() == 0) && 
131                (!no_console) &&
132                (!usage) ) );
133   
134 }
135 //==========================================================================
136
137
138 //==========================================================================
139 class wxBBIApp : public wxApp
140 {
141 public:
142   bool OnInit( );
143   int  OnExit() { 
144     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
145     // bbtk::Object::PrintObjectListInfo();
146     return true; }
147   void OnInitCmdLine(wxCmdLineParser& parser);
148   bool OnCmdLineParsed(wxCmdLineParser& parser);
149
150   WxProcessCmdLine cmd;
151 };
152 //==========================================================================
153
154
155 //==========================================================================
156 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
157 {
158   parser.SetDesc(cmdLineDesc);
159 }
160 //==========================================================================
161
162 //==========================================================================
163 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
164 {
165   cmd.Process(parser);
166   // if (!cmd.proceed) return false;
167   return true;
168 }
169 //==========================================================================
170
171
172
173
174
175
176 //==========================================================================
177 // The `main program' equivalent, creating the windows and returning the
178 // main frame
179 bool wxBBIApp::OnInit( )
180 {
181   //      std::cout << "OnInit"<<std::endl;
182   wxApp::OnInit();
183 #ifdef __WXGTK__
184   //See http://www.wxwindows.org/faqgtk.htm#locale
185   setlocale(LC_NUMERIC, "C");
186 #endif
187   
188
189   if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
190   if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
191   
192
193   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
194   SetTopWindow(I);  
195   if (cmd.console) I->Show(true);
196
197
198   I->SetInputs(cmd.param_map);
199
200   bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
201   if (help_on_script)     I->SetNoExecMode(true);
202   if (cmd.graphical_dialog)     I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
203   if (cmd.text_dialog)     I->SetDialogMode(bbtk::VirtualExec::TextDialog);
204
205   std::vector<std::string>::const_iterator i;
206   bool error = false;
207
208   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
209     {
210       error = ! I->InterpretFile(*i);
211       if (error) break;
212     }
213   bool show_on_error = error && ! cmd.no_console;
214   if (show_on_error) I->Show();
215
216   I->SetNoExecMode(false);
217
218   if (help_on_script) 
219     {
220       std::string package; 
221       I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
222     }
223
224   /*
225   std::cout << "soe="<<show_on_error <<std::endl;
226   std::cout << "con="<<console<<std::endl;
227   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
228   */
229   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
230     {
231       I->Close();
232       //      std::cout << "I->Close"<<std::endl;
233     }
234   else 
235     {
236       //      std::cout << "!I->Close"<<std::endl;
237     }
238         
239   //    std::cout << "EO OnInit"<<std::endl;
240
241   return true;
242
243 }
244 //==========================================================================
245
246
247 #if defined(_WIN32) 
248 //==========================================================================
249 // WINDOWS
250 //==========================================================================
251 IMPLEMENT_APP(wxBBIApp);
252
253 //  How to have a Console and wxWidgets
254 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
255 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
256 //  (cout's to the console are visible) and has a wxWidgets GUI, 
257 //  you need to use the linker option "/subsystem:console" and the following code:
258 int main(int argc, char* argv[])
259 {
260     return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
261 }
262
263 #else 
264 //==========================================================================
265 // OTHER ( Linux... )
266 //==========================================================================
267
268 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
269
270
271 int main(int argc, char* argv[])
272 {       
273   wxMessageOutput::Set( new wxMessageOutputBest );
274
275   wxCmdLineParser parser(cmdLineDesc,argc,argv);
276   int val = parser.Parse(false);
277   if (val>0) 
278     {  
279       parser.Usage();
280       return 0;
281     }
282   WxProcessCmdLine cmdline;
283   cmdline.Process(parser);
284
285   if (!cmdline.proceed) return 0;
286
287   if (cmdline.no_console) 
288     {
289       //      std::cout << "main NC"<<std::endl;
290       // Interpreter 
291       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
292       I->SetInputs(cmdline.param_map);
293       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
294       if (help_on_script) I->SetNoExecMode(true);
295       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
296       std::vector<std::string>::const_iterator i;
297       bool error = false;
298       for (i=cmdline.input_file.begin(); 
299            i!=cmdline.input_file.end(); ++i) 
300         {
301           error = ! I->InterpretFile(*i);
302           if (error) break;
303         }
304       if (help_on_script) 
305         {
306           I->SetNoExecMode(false);
307           std::string package; 
308           I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
309                                                               package,
310                                                               false);
311         }
312       if (cmdline.input_file.size()==0)
313         I->CommandLineInterpreter();
314
315       //
316     }
317   else 
318     {
319       wxEntry(argc, argv);
320     }
321
322 }
323
324
325 #endif // defined(_WIN32) 
326 //==========================================================================
327
328
329
330
331
332
333
334
335 #else
336 //==========================================================================
337 // WITHOUT WX
338 //==========================================================================
339
340 #include "bbtkInterpreter.h"
341
342 // Processes the command line parsing result
343 struct ProcessCmdLine
344 {
345   ProcessCmdLine() {}
346   void Process(int argc_, char *argv_[]);
347   bool Found(std::string value);
348
349   int  argc;
350   std::vector<std::string> argv;
351   bool console;
352   bool debug;
353   bool quiet;
354   bool help;
355   bool graphical_dialog;
356   bool text_dialog;
357   bool no_console;
358   bool proceed;
359   std::map<std::string,std::string> param_map;
360   std::vector<std::string> input_file;
361   
362 };
363
364 bool ProcessCmdLine::Found(std::string value)
365 {
366         bool result=false;
367         for (int i=1; i<argc; ++i) 
368         {
369                 if (value==argv[i]) 
370                 {
371                         result = true;
372                 } 
373         } // for
374         return result;
375 }
376
377 void ProcessCmdLine::Process(int argc_, char *argv_[])
378 {
379    argc = argc_;
380    for (int i=0; i<argc; ++i) 
381     {
382         argv.push_back(argv_[i]);
383     } // for
384
385 //EED  proceed = true;
386   if (Found("D")) 
387     {
388       bbtk::StaticInitTime::PrintObjectListInfo = true;
389     }
390   
391   debug                 = ( Found("d") );  
392   quiet                 = ( Found("q") );
393   help                  = ( Found("h") );
394   graphical_dialog      = ( Found("g") );
395   text_dialog           = ( Found("t") );
396   no_console            = ( Found("N") );
397   
398   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
399   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
400
401   // parse the arguments and consider those which contain a "=" 
402   // as set input commands, other as files
403 //EED  int pargc = parser.GetParamCount();
404   for (int i=1; i<argc; ++i) 
405     {
406       std::string s = argv[i];
407       std::string::size_type pos = s.find_first_of("=");
408       if (std::string::npos != pos) 
409         {
410           std::string left = s.substr(0,pos);
411           std::string right = s.substr(pos+1,s.size());
412           param_map[left]=right;
413         }
414       else 
415         {
416           input_file.push_back(s);
417         }
418     }// for
419
420   bool usage = (help && (input_file.size()==0));
421   if (usage) {
422     std::cout << "BBI (The Black Box Interpreter) - bbtk "
423               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
424               << std::endl;
425 //EED    parser.Usage();
426     proceed = false;
427   }
428
429   console = ( Found("c") || 
430               ((input_file.size() == 0) && 
431                (!no_console) &&
432                (!usage) ) );
433   
434 }
435 //==========================================================================
436
437
438
439 int main(int argc, char* argv[])
440 {  
441 //EED
442 //  if (argc>2) 
443 //    {
444 //      std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
445 //      return 0;
446 //    }
447
448   std::cout << "BBI (Black Box Interpreter) - bbtk "
449             << bbtk::GetVersion()<< " - (c) Creatis 2007"
450             << std::endl;
451
452   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
453   if (argc==1) 
454   {
455         I->CommandLineInterpreter();
456   }  else     {
457          
458         ProcessCmdLine cmd;
459         cmd.Process(argc,argv);
460         I->SetInputs(cmd.param_map);
461
462         std::string f(argv[1]);
463         I->InterpretFile(f);
464   }
465   
466   //  bbtk::Wx::LoopUntilAllWindowsClose(); 
467      
468   return 0;
469
470 }
471
472 // EOF
473 #endif //#ifdef _USE_WXWIDGETS_
474
475
476