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