]> Creatis software - bbtk.git/blob - kernel/appli/bbi/bbi.cxx
3b5580b6b5381f35f0b6497354552f0df732045f
[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         printf("EED wxBBIApp::OnInit 1\n"); 
214 //Borrame
215 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED wxBBIApp::OnInit\n"); fclose(ff);
216
217   //      std::cout << "OnInit"<<std::endl;
218   wxApp::OnInit();
219 #ifdef __WXGTK__
220   //See http://www.wxwindows.org/faqgtk.htm#locale
221   setlocale(LC_NUMERIC, "C");
222 #endif
223   
224         printf("EED wxBBIApp::OnInit 2\n"); 
225
226   if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
227   if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
228   
229
230 //Borrame
231 //printf ("EED bbi wxBBIApp::OnInit .....................\n");
232 //cmd.input_file.push_back("/home/davila/Borrame/testwt.bbs");
233
234         printf("EED wxBBIApp::OnInit 2.1\n"); 
235
236   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
237         printf("EED wxBBIApp::OnInit 2.2\n"); 
238   SetTopWindow(I);  
239         printf("EED wxBBIApp::OnInit 2.3\n"); 
240   if (cmd.console) I->Show(true);
241
242 printf("EED wxBBIApp::OnInit 3\n"); 
243
244   I->SetInputs(cmd.param_map);
245
246   bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
247   if (help_on_script)         I->SetNoExecMode(true);
248   if (cmd.graphical_dialog)   I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
249   if (cmd.text_dialog)        I->SetDialogMode(bbtk::VirtualExec::TextDialog);
250
251   std::vector<std::string>::const_iterator i;
252   bool error = false;
253
254 printf("EED wxBBIApp::OnInit 4\n"); 
255
256   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
257     {
258       error = ! I->InterpretFile(*i);
259       if (error) break;
260     }
261   bool show_on_error = error && ! cmd.no_console;
262   if (show_on_error) I->Show();
263
264   I->SetNoExecMode(false);
265
266   if (help_on_script) 
267     {
268       std::string package; 
269       I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
270     }
271
272 printf("EED wxBBIApp::OnInit 5\n"); 
273
274   /*
275   std::cout << "soe="<<show_on_error <<std::endl;
276   std::cout << "con="<<console<<std::endl;
277   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
278   */
279   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
280     {
281 printf("EED wxBBIApp::OnInit 6\n"); 
282       I->Close();
283       //      std::cout << "I->Close"<<std::endl;
284     }
285   else 
286     {
287       //      std::cout << "!I->Close"<<std::endl;
288     }
289         
290   //    std::cout << "EO OnInit"<<std::endl;
291 printf("EED wxBBIApp::OnInit 7\n"); 
292
293   return true;
294
295 }
296 //==========================================================================
297
298
299 #if defined(_WIN32) 
300 //==========================================================================
301 // WINDOWS
302 //==========================================================================
303 IMPLEMENT_APP(wxBBIApp);
304
305 //  How to have a Console and wxWidgets
306 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
307 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
308 //  (cout's to the console are visible) and has a wxWidgets GUI, 
309 //  you need to use the linker option "/subsystem:console" and the following code:
310 int main(int argc, char* argv[])
311 {
312 printf("EED main cccc\n ");
313 //Borrame
314 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main C\n"); fclose(ff);
315
316         // EED 2018-07-16
317         char buffer[1500];
318         wcstombs(buffer , ::GetCommandLine() , 1500 );
319 printf("EED main cc%scc\n ",buffer);
320     return WinMain(::GetModuleHandle(NULL), NULL, buffer , SW_SHOWNORMAL);
321 //    return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
322 }
323
324
325 #else 
326 //==========================================================================
327 // OTHER ( Linux... )
328 //==========================================================================
329
330 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
331
332
333 int main(int argc, char* argv[])
334 {       
335 printf("EED bbi main BBBB\n");
336
337 //Borrame
338 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main A\n"); fclose(ff);
339
340   wxMessageOutput::Set( new wxMessageOutputBest );
341
342   wxCmdLineParser parser(cmdLineDesc,argc,argv);
343   int val = parser.Parse(false);
344   if (val>0) 
345     {  
346       parser.Usage();
347       return 0;
348     }
349   WxProcessCmdLine cmdline;
350   cmdline.Process(parser);
351
352   if (!cmdline.proceed) return 0;
353
354         if (cmdline.no_console) 
355     {
356       //      std::cout << "main NC"<<std::endl;
357       // Interpreter 
358       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
359       I->SetInputs(cmdline.param_map);
360       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
361       if (help_on_script) I->SetNoExecMode(true);
362       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
363       std::vector<std::string>::const_iterator i;
364       bool error = false;
365       for (i=cmdline.input_file.begin(); 
366            i!=cmdline.input_file.end(); ++i) 
367                 {
368                         error = ! I->InterpretFile(*i);
369                         if (error) break;
370                 }
371         if (help_on_script) 
372                 {
373                         I->SetNoExecMode(false);
374                         std::string package; 
375                         I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
376                                                               package,
377                                                               false);
378                 }
379         if (cmdline.input_file.size()==0)
380                 {
381                         I->CommandLineInterpreter();
382                 } // if cmdline.input_file.size
383       //
384     } else {
385       wxEntry(argc, argv);
386     } // if cmdline.no_console
387
388 }
389
390
391 #endif // defined(_WIN32) 
392 //==========================================================================
393
394
395
396
397
398
399
400
401 #else
402 //==========================================================================
403 // WITHOUT WX
404 //==========================================================================
405
406 #include "bbtkInterpreter.h"
407
408 // Processes the command line parsing result
409 struct ProcessCmdLine
410 {
411   ProcessCmdLine() {}
412   void Process(int argc_, char *argv_[]);
413   bool Found(std::string value);
414
415   int  argc;
416   std::vector<std::string> argv;
417   bool console;
418   bool debug;
419   bool quiet;
420   bool help;
421   bool graphical_dialog;
422   bool text_dialog;
423   bool no_console;
424   bool proceed;
425   std::map<std::string,std::string> param_map;
426   std::vector<std::string> input_file;
427   
428 };
429
430 bool ProcessCmdLine::Found(std::string value)
431 {
432         bool result=false;
433         for (int i=1; i<argc; ++i) 
434         {
435                 if (value==argv[i]) 
436                 {
437                         result = true;
438                 } 
439         } // for
440         return result;
441 }
442
443 void ProcessCmdLine::Process(int argc_, char *argv_[])
444 {
445    argc = argc_;
446    for (int i=0; i<argc; ++i) 
447     {
448         argv.push_back(argv_[i]);
449     } // for
450
451 //EED  proceed = true;
452   if (Found("D")) 
453     {
454       bbtk::StaticInitTime::PrintObjectListInfo = true;
455     }
456   
457   debug                 = ( Found("d") );  
458   quiet                 = ( Found("q") );
459   help                  = ( Found("h") );
460   graphical_dialog      = ( Found("g") );
461   text_dialog           = ( Found("t") );
462   no_console            = ( Found("N") );
463   
464   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
465   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
466
467   // parse the arguments and consider those which contain a "=" 
468   // as set input commands, other as files
469 //EED  int pargc = parser.GetParamCount();
470   for (int i=1; i<argc; ++i) 
471     {
472       std::string s = argv[i];
473       std::string::size_type pos = s.find_first_of("=");
474       if (std::string::npos != pos) 
475         {
476           std::string left = s.substr(0,pos);
477           std::string right = s.substr(pos+1,s.size());
478           param_map[left]=right;
479         }
480       else 
481         {
482           input_file.push_back(s);
483         }
484     }// for
485
486   bool usage = (help && (input_file.size()==0));
487   if (usage) {
488     std::cout << "BBI (The Black Box Interpreter) - bbtk "
489               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
490               << std::endl;
491 //EED    parser.Usage();
492     proceed = false;
493   }
494
495   console = ( Found("c") || 
496               ((input_file.size() == 0) && 
497                (!no_console) &&
498                (!usage) ) );
499   
500 }
501 //==========================================================================
502
503
504
505 int main(int argc, char* argv[])
506 {  
507 printf("EED bbi main AA\n");
508 //EED
509 //  if (argc>2) 
510 //    {
511 //      std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
512 //      return 0;
513 //    }
514
515   std::cout << "BBI (Black Box Interpreter) - bbtk "
516             << bbtk::GetVersion()<< " - (c) Creatis 2007"
517             << std::endl;
518 //Borrame
519 //FILE *ff; ff = fopen ("/tmp/wt.log","a+"); fprintf(ff,"EED main B\n"); fclose(ff);
520
521   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
522   if (argc==1) 
523   {
524         I->CommandLineInterpreter();
525   }  else     {
526          
527         ProcessCmdLine cmd;
528         cmd.Process(argc,argv);
529         I->SetInputs(cmd.param_map);
530
531         std::string f(argv[1]);
532         I->InterpretFile(f);
533   }
534   
535   //  bbtk::Wx::LoopUntilAllWindowsClose(); 
536      
537   return 0;
538
539 }
540
541 // EOF
542 #endif //#ifdef _USE_WXWIDGETS_
543
544
545