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 ProcessCmdLine
00035 {
00036   ProcessCmdLine() {}
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 ProcessCmdLine::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   ProcessCmdLine 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) 
00175     I->SetNoExecMode(true);
00176   if (cmd.graphical_dialog) 
00177     I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
00178   if (cmd.text_dialog) 
00179     I->SetDialogMode(bbtk::VirtualExec::TextDialog);
00180 
00181   std::vector<std::string>::const_iterator i;
00182   bool error = false;
00183 
00184   for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i) 
00185     {
00186       error = ! I->InterpretFile(*i);
00187       if (error) break;
00188     }
00189   bool show_on_error = error && ! cmd.no_console;
00190   if (show_on_error) I->Show();
00191 
00192   I->SetNoExecMode(false);
00193 
00194   if (help_on_script) 
00195     {
00196       std::string package; 
00197       I->GetInterpreter()->GetExecuter()->GetFactory()->HelpBlackBox("workspace",package,false);
00198     }
00199 
00200   /*
00201   std::cout << "soe="<<show_on_error <<std::endl;
00202   std::cout << "con="<<console<<std::endl;
00203   std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
00204   */
00205   if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowShown() ))
00206     {
00207       I->Close();
00208       //      std::cout << "I->Close"<<std::endl;
00209     }
00210   else 
00211     {
00212       //      std::cout << "!I->Close"<<std::endl;
00213     }
00214   return true;
00215 
00216 }
00217 //==========================================================================
00218 
00219 
00220 #if defined(_WIN32) 
00221 //==========================================================================
00222 // WINDOWS
00223 //==========================================================================
00224 IMPLEMENT_APP(wxBBIApp);
00225 //  How to have a Console and wxWidgets
00226 //  http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
00227 //   In Visual C++ 6 (7 should be similar), to create an application that is both a console application 
00228 //  (cout's to the console are visible) and has a wxWidgets GUI, 
00229 //  you need to use the linker option "/subsystem:console" and the following code:
00230 int main(int argc, char* argv[])
00231 {
00232   return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
00233 }
00234 
00235 #else 
00236 //==========================================================================
00237 // OTHER ( Linux... )
00238 //==========================================================================
00239 
00240 IMPLEMENT_APP_NO_MAIN(wxBBIApp);
00241 
00242 
00243 int main(int argc, char* argv[])
00244 {
00245   wxMessageOutput::Set( new wxMessageOutputBest );
00246 
00247   wxCmdLineParser parser(cmdLineDesc,argc,argv);
00248   int val = parser.Parse(false);
00249   if (val>0) 
00250     {  
00251       parser.Usage();
00252       return 0;
00253     }
00254   ProcessCmdLine cmdline;
00255   cmdline.Process(parser);
00256 
00257   if (!cmdline.proceed) return 0;
00258 
00259   if (cmdline.no_console) 
00260     {
00261       //      std::cout << "main NC"<<std::endl;
00262       // Interpreter 
00263       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
00264       I->SetInputs(cmdline.param_map);
00265       bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
00266       if (help_on_script) I->SetNoExecMode(true);
00267       if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
00268       std::vector<std::string>::const_iterator i;
00269       bool error = false;
00270       for (i=cmdline.input_file.begin(); 
00271            i!=cmdline.input_file.end(); ++i) 
00272         {
00273           error = ! I->InterpretFile(*i);
00274           if (error) break;
00275         }
00276       if (help_on_script) 
00277         {
00278           I->SetNoExecMode(false);
00279           std::string package; 
00280           I->GetExecuter()->GetFactory()->HelpBlackBox("workspace",
00281                                                        package,
00282                                                        false);
00283         }
00284       if (cmdline.input_file.size()==0)
00285         I->CommandLineInterpreter();
00286 
00287       //
00288     }
00289   else 
00290     {
00291       wxEntry(argc, argv);
00292     }
00293 
00294 }
00295 
00296 
00297 #endif // defined(_WIN32) 
00298 //==========================================================================
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 #else
00308 //==========================================================================
00309 // WITHOUT WX
00310 //==========================================================================
00311 
00312 #include "bbtkInterpreter.h"
00313 
00314 int main(int argc, char* argv[])
00315 {  
00316 
00317   if (argc>2) 
00318     {
00319       std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
00320       return 0;
00321     }
00322 
00323   std::cout << "BBI (Black Box Interpreter) - bbtk "
00324             << bbtk::GetVersion()<< " - (c) Creatis 2007"
00325             << std::endl;
00326 
00327   bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
00328   if (argc==1) 
00329     {
00330       I->CommandLineInterpreter();
00331     }
00332   else 
00333 
00334     {
00335       std::string f(argv[1]);
00336       I->InterpretFile(f);
00337     }
00338   
00339   bbtk::Wx::LoopUntilAllWindowsClose(); 
00340      
00341   return 0;
00342 
00343 }
00344 
00345 // EOF
00346 #endif //#ifdef _USE_WXWIDGETS_
00347 
00348 
00349 

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6