bbi.cxx

Go to the documentation of this file.
00001 #ifdef _USE_WXWIDGETS_
00002 
00003 //==========================================================================
00004 // WITH WX
00005 //==========================================================================
00006 #include "bbtkObject.h"
00007 #include "bbtkInterpreter.h"
00008 #include "bbtkWxBlackBox.h"
00009 #include "bbtkWxGUIConsole.h"
00010 
00011 #include <wx/cmdline.h>
00012 #include <vector>
00013 #include <map>
00014 
00015 //==========================================================================
00016 // Command line options definition
00017 static const wxCmdLineEntryDesc cmdLineDesc[] =
00018 {
00019   { wxCMD_LINE_SWITCH, _T("h"), _T("help"),   _T("print this help or help on the application defined in input bbs file if any") },
00020   { wxCMD_LINE_SWITCH, _T("g"), _T("graphical-dialog"),   _T("prompt the input parameter values using graphical dialog") },
00021   { wxCMD_LINE_SWITCH, _T("t"), _T("text-dialog"),   _T("prompt the input parameter values in text mode") },
00022   { wxCMD_LINE_SWITCH, _T("c"), _T("console"), _T("open bbi console") },
00023   { wxCMD_LINE_SWITCH, _T("N"), _T("no-console"),   _T("never open bbi console even on error") },
00024   { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"),   _T("be quiet (='message max 0')") },
00025   { wxCMD_LINE_SWITCH, _T("d"), _T("debug"), _T("turn all messages on (='message all 9')") },
00026   { wxCMD_LINE_SWITCH, _T("D"), _T("Debug"), _T("memory debug on exit (='debug -D')") },
00027   { wxCMD_LINE_PARAM,  NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
00028   { wxCMD_LINE_NONE }
00029 };
00030 //==========================================================================
00031 
00032 //==========================================================================
00033 // Processes the command line parsing result
00034 struct WxProcessCmdLine
00035 {
00036   WxProcessCmdLine() {}
00037   void Process(wxCmdLineParser& parser);
00038 
00039   //  int argc;
00040   std::vector<std::string> argv;
00041   bool console;
00042   bool debug;
00043   bool quiet;
00044   bool help;
00045   bool graphical_dialog;
00046   bool text_dialog;
00047   bool no_console;
00048   bool proceed;
00049   std::map<std::string,std::string> param_map;
00050   std::vector<std::string> input_file;
00051   
00052 };
00053 //==========================================================================
00054 
00055 //==========================================================================
00056 void WxProcessCmdLine::Process(wxCmdLineParser& parser)
00057 {
00058   proceed = true;
00059   if (parser.Found(_T("D"))) 
00060     {
00061       bbtk::StaticInitTime::PrintObjectListInfo = true;
00062     }
00063   
00064   debug = ( parser.Found(_T("d")) );  
00065   quiet = ( parser.Found(_T("q")) );
00066   help = ( parser.Found(_T("h")) );
00067   graphical_dialog = ( parser.Found(_T("g")) );
00068   text_dialog = ( parser.Found(_T("t")) );
00069   no_console = ( parser.Found(_T("N")) );
00070   
00071   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
00072   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
00073 
00074   // parse the arguments and consider those which contain a "=" 
00075   // as set input commands, other as files
00076   int pargc = parser.GetParamCount();
00077   for (int i=0; i<pargc; ++i) 
00078     {
00079       std::string s = bbtk::wx2std(parser.GetParam(i));
00080       std::string::size_type pos = s.find_first_of("=");
00081       if (std::string::npos != pos) 
00082         {
00083           std::string left = s.substr(0,pos);
00084           std::string right = s.substr(pos+1,s.size());
00085           param_map[left]=right;
00086         }
00087       else 
00088         {
00089           input_file.push_back(s);
00090         }
00091     }
00092 
00093   bool usage = (help && (input_file.size()==0));
00094   if (usage) {
00095     std::cout << "BBI (The Black Box Interpreter) - bbtk "
00096               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
00097               << std::endl;
00098     parser.Usage();
00099     proceed = false;
00100   }
00101 
00102   console = ( parser.Found(_T("c")) || 
00103               ((input_file.size() == 0) && 
00104                (!no_console) &&
00105                (!usage) ) );
00106   
00107 }
00108 //==========================================================================
00109 
00110 
00111 //==========================================================================
00112 class wxBBIApp : public wxApp
00113 {
00114 public:
00115   bool OnInit( );
00116   int  OnExit() { 
00117     //    std::cout << "wxBBIApp::OnExit()"<<std::endl;
00118     // bbtk::Object::PrintObjectListInfo();
00119     return true; }
00120   void OnInitCmdLine(wxCmdLineParser& parser);
00121   bool OnCmdLineParsed(wxCmdLineParser& parser);
00122 
00123   WxProcessCmdLine cmd;
00124 };
00125 //==========================================================================
00126 
00127 
00128 //==========================================================================
00129 void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
00130 {
00131   parser.SetDesc(cmdLineDesc);
00132 }
00133 //==========================================================================
00134 
00135 //==========================================================================
00136 bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
00137 {
00138   cmd.Process(parser);
00139   // if (!cmd.proceed) return false;
00140   return true;
00141 }
00142 //==========================================================================
00143 
00144 
00145 
00146 
00147 
00148 
00149 //==========================================================================
00150 // The `main program' equivalent, creating the windows and returning the
00151 // main frame
00152 bool wxBBIApp::OnInit( )
00153 {
00154   //      std::cout << "OnInit"<<std::endl;
00155   wxApp::OnInit();
00156 #ifdef __WXGTK__
00157   //See http://www.wxwindows.org/faqgtk.htm#locale
00158   setlocale(LC_NUMERIC, "C");
00159 #endif
00160   
00161 
00162   if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
00163   if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
00164   
00165 
00166   bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
00167   SetTopWindow(I);  
00168   if (cmd.console) I->Show(true);
00169 
00170 
00171   I->SetInputs(cmd.param_map);
00172 
00173   bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
00174   if (help_on_script)     I->SetNoExecMode(true);
00175   if (cmd.graphical_dialog)     I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
00176   if (cmd.text_dialog)     I->SetDialogMode(bbtk::VirtualExec::TextDialog);
00177 
00178   std::vector<std::string>::const_iterator i;
00179   bool error = false;
00180 
00181   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
00182     {
00183       error = ! I->InterpretFile(*i);
00184       if (error) break;
00185     }
00186   bool show_on_error = error && ! cmd.no_console;
00187   if (show_on_error) I->Show();
00188 
00189   I->SetNoExecMode(false);
00190 
00191   if (help_on_script) 
00192     {
00193       std::string package; 
00194       I->GetInterpreter()->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",package,false);
00195     }
00196 
00197   /*
00198   std::cout << "soe="<<show_on_error <<std::endl;
00199   std::cout << "con="<<console<<std::endl;
00200   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
00201   */
00202   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowAlive() ))
00203     {
00204       I->Close();
00205       //      std::cout << "I->Close"<<std::endl;
00206     }
00207   else 
00208     {
00209       //      std::cout << "!I->Close"<<std::endl;
00210     }
00211         
00212   //    std::cout << "EO OnInit"<<std::endl;
00213 
00214   return true;
00215 
00216 }
00217 //==========================================================================
00218 
00219 
00220 #if defined(_WIN32) 
00221 //==========================================================================
00222 // WINDOWS
00223 //==========================================================================
00224 IMPLEMENT_APP(wxBBIApp);
00225 
00226 //  How to have a Console and wxWidgets
00227 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
00228 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
00229 //  (cout's to the console are visible) and has a wxWidgets GUI, 
00230 //  you need to use the linker option "/subsystem:console" and the following code:
00231 int main(int argc, char* argv[])
00232 {
00233     return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
00234 }
00235 
00236 #else 
00237 //==========================================================================
00238 // OTHER ( Linux... )
00239 //==========================================================================
00240 
00241 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
00242 
00243 
00244 int main(int argc, char* argv[])
00245 {       
00246   wxMessageOutput::Set( new wxMessageOutputBest );
00247 
00248   wxCmdLineParser parser(cmdLineDesc,argc,argv);
00249   int val = parser.Parse(false);
00250   if (val>0) 
00251     {  
00252       parser.Usage();
00253       return 0;
00254     }
00255   WxProcessCmdLine cmdline;
00256   cmdline.Process(parser);
00257 
00258   if (!cmdline.proceed) return 0;
00259 
00260   if (cmdline.no_console) 
00261     {
00262       //      std::cout << "main NC"<<std::endl;
00263       // Interpreter 
00264       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
00265       I->SetInputs(cmdline.param_map);
00266       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
00267       if (help_on_script) I->SetNoExecMode(true);
00268       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
00269       std::vector<std::string>::const_iterator i;
00270       bool error = false;
00271       for (i=cmdline.input_file.begin(); 
00272            i!=cmdline.input_file.end(); ++i) 
00273         {
00274           error = ! I->InterpretFile(*i);
00275           if (error) break;
00276         }
00277       if (help_on_script) 
00278         {
00279           I->SetNoExecMode(false);
00280           std::string package; 
00281           I->GetExecuter()->GetFactory()->PrintHelpDescriptor("workspace",
00282                                                               package,
00283                                                               false);
00284         }
00285       if (cmdline.input_file.size()==0)
00286         I->CommandLineInterpreter();
00287 
00288       //
00289     }
00290   else 
00291     {
00292       wxEntry(argc, argv);
00293     }
00294 
00295 }
00296 
00297 
00298 #endif // defined(_WIN32) 
00299 //==========================================================================
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308 #else
00309 //==========================================================================
00310 // WITHOUT WX
00311 //==========================================================================
00312 
00313 #include "bbtkInterpreter.h"
00314 
00315 // Processes the command line parsing result
00316 struct ProcessCmdLine
00317 {
00318   ProcessCmdLine() {}
00319   void Process(int argc_, char *argv_[]);
00320   bool Found(std::string value);
00321 
00322   int  argc;
00323   std::vector<std::string> argv;
00324   bool console;
00325   bool debug;
00326   bool quiet;
00327   bool help;
00328   bool graphical_dialog;
00329   bool text_dialog;
00330   bool no_console;
00331   bool proceed;
00332   std::map<std::string,std::string> param_map;
00333   std::vector<std::string> input_file;
00334   
00335 };
00336 
00337 bool ProcessCmdLine::Found(std::string value)
00338 {
00339         bool result=false;
00340         for (int i=1; i<argc; ++i) 
00341         {
00342                 if (value==argv[i]) 
00343                 {
00344                         result = true;
00345                 } 
00346         } // for
00347         return result;
00348 }
00349 
00350 void ProcessCmdLine::Process(int argc_, char *argv_[])
00351 {
00352    argc = argc_;
00353    for (int i=0; i<argc; ++i) 
00354     {
00355         argv.push_back(argv_[i]);
00356     } // for
00357 
00358 //EED  proceed = true;
00359   if (Found("D")) 
00360     {
00361       bbtk::StaticInitTime::PrintObjectListInfo = true;
00362     }
00363   
00364   debug                 = ( Found("d") );  
00365   quiet                 = ( Found("q") );
00366   help                  = ( Found("h") );
00367   graphical_dialog      = ( Found("g") );
00368   text_dialog           = ( Found("t") );
00369   no_console            = ( Found("N") );
00370   
00371   if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
00372   if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
00373 
00374   // parse the arguments and consider those which contain a "=" 
00375   // as set input commands, other as files
00376 //EED  int pargc = parser.GetParamCount();
00377   for (int i=1; i<argc; ++i) 
00378     {
00379       std::string s = argv[i];
00380       std::string::size_type pos = s.find_first_of("=");
00381       if (std::string::npos != pos) 
00382         {
00383           std::string left = s.substr(0,pos);
00384           std::string right = s.substr(pos+1,s.size());
00385           param_map[left]=right;
00386         }
00387       else 
00388         {
00389           input_file.push_back(s);
00390         }
00391     }// for
00392 
00393   bool usage = (help && (input_file.size()==0));
00394   if (usage) {
00395     std::cout << "BBI (The Black Box Interpreter) - bbtk "
00396               << bbtk::GetVersion() << " - (c) Creatis 2007-2008"
00397               << std::endl;
00398 //EED    parser.Usage();
00399     proceed = false;
00400   }
00401 
00402   console = ( Found("c") || 
00403               ((input_file.size() == 0) && 
00404                (!no_console) &&
00405                (!usage) ) );
00406   
00407 }
00408 //==========================================================================
00409 
00410 
00411 
00412 int main(int argc, char* argv[])
00413 {  
00414 //EED
00415 //  if (argc>2) 
00416 //    {
00417 //      std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
00418 //      return 0;
00419 //    }
00420 
00421   std::cout << "BBI (Black Box Interpreter) - bbtk "
00422             << bbtk::GetVersion()<< " - (c) Creatis 2007"
00423             << std::endl;
00424 
00425   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
00426   if (argc==1) 
00427   {
00428         I->CommandLineInterpreter();
00429   }  else     {
00430          
00431         ProcessCmdLine cmd;
00432         cmd.Process(argc,argv);
00433         I->SetInputs(cmd.param_map);
00434 
00435         std::string f(argv[1]);
00436         I->InterpretFile(f);
00437   }
00438   
00439   //  bbtk::Wx::LoopUntilAllWindowsClose(); 
00440      
00441   return 0;
00442 
00443 }
00444 
00445 // EOF
00446 #endif //#ifdef _USE_WXWIDGETS_
00447 
00448 
00449 

Generated on Thu May 31 14:12:02 2012 for BBTK by  doxygen 1.5.7.1