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