]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
ea8dd6cc5f504360e72013197270ecd90c7bc932
[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
44 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
45 #if wxMAJOR_VERSION <= 2
46         // Command line options definition
47         static const wxCmdLineEntryDesc cmdLineDesc[] =
48         {
49           { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
50           { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
51           { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
52           { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
53           { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
54           { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
55           { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
56           { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
57           { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
58           { wxCMD_LINE_NONE }
59         };
60 #else
61         // Command line options definition
62         static const wxCmdLineEntryDesc cmdLineDesc[] =
63         {
64           { wxCMD_LINE_SWITCH, "h", "help",   "print this help or help on the application defined in input bbs file if any" },
65           { wxCMD_LINE_SWITCH, "g", "graphical-dialog",   "prompt the input parameter values using graphical dialog" },
66           { wxCMD_LINE_SWITCH, "t", "text-dialog",   "prompt the input parameter values in text mode" },
67           { wxCMD_LINE_SWITCH, "c", "console", "open bbi console" },
68           { wxCMD_LINE_SWITCH, "N", "no-console",   "never open bbi console even on error" },
69           { wxCMD_LINE_SWITCH, "q", "quiet",   "be quiet (='message max 0')" },
70           { wxCMD_LINE_SWITCH, "d", "debug", "turn all messages on (='message all 9')" },
71           { wxCMD_LINE_SWITCH, "D", "Debug", "memory debug on exit (='debug -D')" },
72           { wxCMD_LINE_PARAM,  NULL, NULL, "file [file [...]]", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
73           { wxCMD_LINE_NONE }
74         };
75 #endif
76
77
78
79
80 //==========================================================================
81
82 //==========================================================================
83 // Processes the command line parsing result
84 struct WxProcessCmdLine
85 {
86   WxProcessCmdLine() {}
87   void Process(wxCmdLineParser& parser);
88
89   //  int argc;
90   std::vector<std::string> argv;
91   bool console;
92   bool debug;
93   bool quiet;
94   bool help;
95   bool graphical_dialog;
96   bool text_dialog;
97   bool no_console;
98   bool proceed;
99   std::map<std::string,std::string> param_map;
100   std::vector<std::string> input_file;
101   
102 };
103 //==========================================================================
104
105 //==========================================================================
106 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
107 {
108   proceed = true;
109   if (parser.Found(_T("D"))) 
110     {
111       bbtk::StaticInitTime::PrintObjectListInfo = true;
112     }
113   
114   debug = ( parser.Found(_T("d")) );  
115   quiet = ( parser.Found(_T("q")) );
116   help = ( parser.Found(_T("h")) );
117   graphical_dialog = ( parser.Found(_T("wxcommandlineg")) );
118   text_dialog = ( parser.Found(_T("t")) );
119   no_console = ( parser.Found(_T("N")) );
120   
121   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
122   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
123
124   // parse the arguments and consider those which contain a "=" 
125   // as set input commands, other as files
126   int pargc = parser.GetParamCount();
127   for (int i=0; i<pargc; ++i) 
128     {
129       std::string s = bbtk::wx2std(parser.GetParam(i));
130       std::string::size_type pos = s.find_first_of("=");
131       if (std::string::npos != pos) 
132         {
133           std::string left = s.substr(0,pos);
134           std::string right = s.substr(pos+1,s.size());
135           param_map[left]=right;
136         }
137       else 
138         {
139           input_file.push_back(s);
140         }
141     }
142
143   bool usage = (help && (input_file.size()==0));
144   if (usage) {
145     std::cout << "BBI (The Black Box Interpreter) - bbtk "
146               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
147               << std::endl;
148     parser.Usage();
149     proceed = false;
150   }
151
152   console = ( parser.Found(_T("c")) || 
153               ((input_file.size() == 0) && 
154                (!no_console) &&
155                (!usage) ) );
156   
157 }
158 //==========================================================================
159
160
161 //==========================================================================
162 class wxBBIApp : public wxApp
163 {
164 public:
165   bool OnInit( );
166   int  OnExit() { 
167     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
168     // bbtk::Object::PrintObjectListInfo();
169     return true; }
170   void OnInitCmdLine(wxCmdLineParser& parser);
171   bool OnCmdLineParsed(wxCmdLineParser& parser);
172
173   WxProcessCmdLine cmd;
174 };
175 //==========================================================================
176
177
178 //==========================================================================
179 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
180 {
181   parser.SetDesc(cmdLineDesc);
182 }
183 //==========================================================================
184
185 //==========================================================================
186 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
187 {
188   cmd.Process(parser);
189   // if (!cmd.proceed) return false;
190   return true;
191 }
192 //==========================================================================
193
194
195
196
197
198
199 //==========================================================================
200 // The `main program' equivalent, creating the windows and returning the
201 // main frame
202 bool wxBBIApp::OnInit( )
203 {
204 //Borrame
205 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED wxBBIApp::OnInit\n"); fclose(ff);
206
207   //      std::cout << "OnInit"<<std::endl;
208   wxApp::OnInit();
209 #ifdef __WXGTK__
210   //See http://www.wxwindows.org/faqgtk.htm#locale
211   setlocale(LC_NUMERIC, "C");
212 #endif
213   
214
215   if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
216   if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
217   
218
219 //Borrame
220 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
221 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
222
223
224   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
225   SetTopWindow(I);  
226   if (cmd.console) I->Show(true);
227
228
229   I->SetInputs(cmd.param_map);
230
231   bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
232   if (help_on_script)         I->SetNoExecMode(true);
233   if (cmd.graphical_dialog)   I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
234   if (cmd.text_dialog)        I->SetDialogMode(bbtk::VirtualExec::TextDialog);
235
236   std::vector<std::string>::const_iterator i;
237   bool error = false;
238
239   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
240     {
241       error = ! I->InterpretFile(*i);
242       if (error) break;
243     }
244   bool show_on_error = error && ! cmd.no_console;
245   if (show_on_error) I->Show();
246
247   I->SetNoExecMode(false);
248
249   if (help_on_script) 
250     {
251       std::string package; 
252       I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
253     }
254
255   /*
256   std::cout << "soe="<<show_on_error <<std::endl;
257   std::cout << "con="<<console<<std::endl;
258   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
259   */
260   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
261     {
262       I->Close();
263       //      std::cout << "I->Close"<<std::endl;
264     }
265   else 
266     {
267       //      std::cout << "!I->Close"<<std::endl;
268     }
269         
270   //    std::cout << "EO OnInit"<<std::endl;
271
272   return true;
273
274 }
275 //==========================================================================
276
277
278 #if defined(_WIN32) 
279 //==========================================================================
280 // WINDOWS
281 //==========================================================================
282 IMPLEMENT_APP(wxBBIApp);
283
284 //  How to have a Console and wxWidgets
285 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
286 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
287 //  (cout's to the console are visible) and has a wxWidgets GUI, 
288 //  you need to use the linker option "/subsystem:console" and the following code:
289 int main(int argc, char* argv[])
290 {
291
292 //Borrame
293 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
294
295     return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
296 }
297
298 #else 
299 //==========================================================================
300 // OTHER ( Linux... )
301 //==========================================================================
302
303 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
304
305
306 int main(int argc, char* argv[])
307 {       
308
309 //Borrame
310 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
311
312   wxMessageOutput::Set( new wxMessageOutputBest );
313
314   wxCmdLineParser parser(cmdLineDesc,argc,argv);
315   int val = parser.Parse(false);
316   if (val>0) 
317     {  
318       parser.Usage();
319       return 0;
320     }
321   WxProcessCmdLine cmdline;
322   cmdline.Process(parser);
323
324   if (!cmdline.proceed) return 0;
325
326   if (cmdline.no_console) 
327     {
328       //      std::cout << "main NC"<<std::endl;
329       // Interpreter 
330       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
331       I->SetInputs(cmdline.param_map);
332       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
333       if (help_on_script) I->SetNoExecMode(true);
334       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
335       std::vector<std::string>::const_iterator i;
336       bool error = false;
337       for (i=cmdline.input_file.begin(); 
338            i!=cmdline.input_file.end(); ++i) 
339         {
340           error = ! I->InterpretFile(*i);
341           if (error) break;
342         }
343       if (help_on_script) 
344         {
345           I->SetNoExecMode(false);
346           std::string package; 
347           I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
348                                                               package,
349                                                               false);
350         }
351       if (cmdline.input_file.size()==0)
352         I->CommandLineInterpreter();
353
354       //
355     }
356   else 
357     {
358       wxEntry(argc, argv);
359     }
360
361 }
362
363
364 #endif // defined(_WIN32) 
365 //==========================================================================
366
367
368
369
370
371
372
373
374 #else
375 //==========================================================================
376 // WITHOUT WX
377 //==========================================================================
378
379 #include "bbtkInterpreter.h"
380
381 // Processes the command line parsing result
382 struct ProcessCmdLine
383 {
384   ProcessCmdLine() {}
385   void Process(int argc_, char *argv_[]);
386   bool Found(std::string value);
387
388   int  argc;
389   std::vector<std::string> argv;
390   bool console;
391   bool debug;
392   bool quiet;
393   bool help;
394   bool graphical_dialog;
395   bool text_dialog;
396   bool no_console;
397   bool proceed;
398   std::map<std::string,std::string> param_map;
399   std::vector<std::string> input_file;
400   
401 };
402
403 bool ProcessCmdLine::Found(std::string value)
404 {
405         bool result=false;
406         for (int i=1; i<argc; ++i) 
407         {
408                 if (value==argv[i]) 
409                 {
410                         result = true;
411                 } 
412         } // for
413         return result;
414 }
415
416 void ProcessCmdLine::Process(int argc_, char *argv_[])
417 {
418    argc = argc_;
419    for (int i=0; i<argc; ++i) 
420     {
421         argv.push_back(argv_[i]);
422     } // for
423
424 //EED  proceed = true;
425   if (Found("D")) 
426     {
427       bbtk::StaticInitTime::PrintObjectListInfo = true;
428     }
429   
430   debug                 = ( Found("d") );  
431   quiet                 = ( Found("q") );
432   help                  = ( Found("h") );
433   graphical_dialog      = ( Found("g") );
434   text_dialog           = ( Found("t") );
435   no_console            = ( Found("N") );
436   
437   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
438   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
439
440   // parse the arguments and consider those which contain a "=" 
441   // as set input commands, other as files
442 //EED  int pargc = parser.GetParamCount();
443   for (int i=1; i<argc; ++i) 
444     {
445       std::string s = argv[i];
446       std::string::size_type pos = s.find_first_of("=");
447       if (std::string::npos != pos) 
448         {
449           std::string left = s.substr(0,pos);
450           std::string right = s.substr(pos+1,s.size());
451           param_map[left]=right;
452         }
453       else 
454         {
455           input_file.push_back(s);
456         }
457     }// for
458
459   bool usage = (help && (input_file.size()==0));
460   if (usage) {
461     std::cout << "BBI (The Black Box Interpreter) - bbtk "
462               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
463               << std::endl;
464 //EED    parser.Usage();
465     proceed = false;
466   }
467
468   console = ( Found("c") || 
469               ((input_file.size() == 0) && 
470                (!no_console) &&
471                (!usage) ) );
472   
473 }
474 //==========================================================================
475
476
477
478 int main(int argc, char* argv[])
479 {  
480 //EED
481 //  if (argc>2) 
482 //    {
483 //      std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
484 //      return 0;
485 //    }
486
487   std::cout << "BBI (Black Box Interpreter) - bbtk "
488             << bbtk::GetVersion()<< " - (c) Creatis 2007"
489             << std::endl;
490 //Borrame
491 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
492
493   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
494   if (argc==1) 
495   {
496         I->CommandLineInterpreter();
497   }  else     {
498          
499         ProcessCmdLine cmd;
500         cmd.Process(argc,argv);
501         I->SetInputs(cmd.param_map);
502
503         std::string f(argv[1]);
504         I->InterpretFile(f);
505   }
506   
507   //  bbtk::Wx::LoopUntilAllWindowsClose(); 
508      
509   return 0;
510
511 }
512
513 // EOF
514 #endif //#ifdef _USE_WXWIDGETS_
515
516
517