#include <vector>
#include <map>
-
+//==========================================================================
+// Command line options definition
static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("print this help or help on the application defined in input bbs file if any") },
{ wxCMD_LINE_PARAM, NULL, NULL, _T("file [file [...]]"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
{ wxCMD_LINE_NONE }
};
+//==========================================================================
-
-
-class wxBBIApp : public wxApp
+//==========================================================================
+// Processes the command line parsing result
+struct ProcessCmdLine
{
-public:
- bool OnInit( );
- int OnExit() {
- // std::cout << "wxBBIApp::OnExit()"<<std::endl;
- // bbtk::Object::PrintObjectListInfo();
- return true; }
- void OnInitCmdLine(wxCmdLineParser& parser);
- bool OnCmdLineParsed(wxCmdLineParser& parser);
- void Run(bbtk::Interpreter*);
+ ProcessCmdLine() {}
+ void Process(wxCmdLineParser& parser);
- bbtk::Interpreter* I;
- int argc;
+ // int argc;
std::vector<std::string> argv;
bool console;
bool debug;
bool graphical_dialog;
bool text_dialog;
bool no_console;
-
+ bool proceed;
std::map<std::string,std::string> param_map;
std::vector<std::string> input_file;
};
+//==========================================================================
-IMPLEMENT_APP(wxBBIApp);
-
-void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
-{
- // std::cout << "OnInitCmdLine"<<std::endl;
- parser.SetDesc(cmdLineDesc);
-}
-
-bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
+//==========================================================================
+void ProcessCmdLine::Process(wxCmdLineParser& parser)
{
-
+ proceed = true;
if (parser.Found(_T("D")))
{
bbtk::StaticInitTime::PrintObjectListInfo = true;
}
- debug = ( parser.Found(_T("d")) );
-
+ debug = ( parser.Found(_T("d")) );
quiet = ( parser.Found(_T("q")) );
help = ( parser.Found(_T("h")) );
graphical_dialog = ( parser.Found(_T("g")) );
text_dialog = ( parser.Found(_T("t")) );
no_console = ( parser.Found(_T("N")) );
+
+ if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
+ if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
// parse the arguments and consider those which contain a "="
// as set input commands, other as files
- int argc = parser.GetParamCount();
- for (int i=0; i<argc; ++i)
+ int pargc = parser.GetParamCount();
+ for (int i=0; i<pargc; ++i)
{
std::string s = bbtk::wx2std(parser.GetParam(i));
std::string::size_type pos = s.find_first_of("=");
std::string left = s.substr(0,pos);
std::string right = s.substr(pos+1,s.size());
param_map[left]=right;
- // std::cout << "'"<<left << "' = '"<<right<<"'"<<std::endl;
}
else
{
- // std::cout << "input file = ["<<s<<"]"<<std::endl;
input_file.push_back(s);
}
}
<< bbtk::GetVersion() << " - (c) Creatis 2007-2008"
<< std::endl;
parser.Usage();
+ proceed = false;
}
console = ( parser.Found(_T("c")) ||
((input_file.size() == 0) &&
(!no_console) &&
(!usage) ) );
+
+}
+//==========================================================================
+//==========================================================================
+class wxBBIApp : public wxApp
+{
+public:
+ bool OnInit( );
+ int OnExit() {
+ // std::cout << "wxBBIApp::OnExit()"<<std::endl;
+ // bbtk::Object::PrintObjectListInfo();
+ return true; }
+ void OnInitCmdLine(wxCmdLineParser& parser);
+ bool OnCmdLineParsed(wxCmdLineParser& parser);
+
+ ProcessCmdLine cmd;
+};
+//==========================================================================
+
+
+//==========================================================================
+void wxBBIApp::OnInitCmdLine(wxCmdLineParser& parser)
+{
+ parser.SetDesc(cmdLineDesc);
+}
+//==========================================================================
+//==========================================================================
+bool wxBBIApp::OnCmdLineParsed(wxCmdLineParser& parser)
+{
+ cmd.Process(parser);
+ // if (!cmd.proceed) return false;
return true;
}
+//==========================================================================
-// ----------------------------------------------------------------------------
+//==========================================================================
// The `main program' equivalent, creating the windows and returning the
// main frame
bool wxBBIApp::OnInit( )
#endif
- if (quiet) bbtk::MessageManager::SetMessageLevel("max",0);
- if (debug) bbtk::MessageManager::SetMessageLevel("all",9);
+ if (cmd.quiet) bbtk::MessageManager::SetMessageLevel("max",0);
+ if (cmd.debug) bbtk::MessageManager::SetMessageLevel("all",9);
bbtk::WxGUIConsole *I = new bbtk::WxGUIConsole(0,_T("bbi"),wxSize(800,600));
SetTopWindow(I);
- if (console) I->Show(true);
+ if (cmd.console) I->Show(true);
- I->SetInputs(param_map);
+ I->SetInputs(cmd.param_map);
- bool help_on_script = help && (input_file.size() > 0);
- if (help_on_script) I->SetNoExecMode(true);
-
- if (graphical_dialog) I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
- if (text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
+ bool help_on_script = cmd.help && (cmd.input_file.size() > 0);
+ if (help_on_script)
+ I->SetNoExecMode(true);
+ if (cmd.graphical_dialog)
+ I->SetDialogMode(bbtk::VirtualExec::GraphicalDialog);
+ if (cmd.text_dialog)
+ I->SetDialogMode(bbtk::VirtualExec::TextDialog);
std::vector<std::string>::const_iterator i;
bool error = false;
- for (i=input_file.begin(); i!=input_file.end(); ++i)
+ for (i=cmd.input_file.begin(); i!=cmd.input_file.end(); ++i)
{
error = ! I->InterpretFile(*i);
if (error) break;
}
- bool show_on_error = error && ! no_console;
+ bool show_on_error = error && ! cmd.no_console;
if (show_on_error) I->Show();
I->SetNoExecMode(false);
std::cout << "con="<<console<<std::endl;
std::cout << "iws="<<bbtk::Wx::IsSomeWindowShown()<<std::endl;
*/
- if (!(show_on_error || console || bbtk::Wx::IsSomeWindowShown() ))
+ if (!(show_on_error || cmd.console || bbtk::Wx::IsSomeWindowShown() ))
{
I->Close();
// std::cout << "I->Close"<<std::endl;
return true;
}
+//==========================================================================
#if defined(_WIN32)
-
+//==========================================================================
+// WINDOWS
+//==========================================================================
+IMPLEMENT_APP(wxBBIApp);
// How to have a Console and wxWidgets
// http://www.wxwidgets.org/wiki/index.php/MSVC_Setup_Guide
// In Visual C++ 6 (7 should be similar), to create an application that is both a console application
// you need to use the linker option "/subsystem:console" and the following code:
int main(int argc, char* argv[])
{
- return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
+ return WinMain(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOWNORMAL);
+}
+
+#else
+//==========================================================================
+// OTHER ( Linux... )
+//==========================================================================
+
+IMPLEMENT_APP_NO_MAIN(wxBBIApp);
+
+
+int main(int argc, char* argv[])
+{
+ wxMessageOutput::Set( new wxMessageOutputBest );
+
+ wxCmdLineParser parser(cmdLineDesc,argc,argv);
+ int val = parser.Parse(false);
+ if (val>0)
+ {
+ parser.Usage();
+ return 0;
+ }
+ ProcessCmdLine cmdline;
+ cmdline.Process(parser);
+
+ if (!cmdline.proceed) return 0;
+
+ if (cmdline.no_console)
+ {
+ // std::cout << "main NC"<<std::endl;
+ // Interpreter
+ bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
+ I->SetInputs(cmdline.param_map);
+ bool help_on_script = cmdline.help && (cmdline.input_file.size() > 0);
+ if (help_on_script) I->SetNoExecMode(true);
+ if (cmdline.text_dialog) I->SetDialogMode(bbtk::VirtualExec::TextDialog);
+ std::vector<std::string>::const_iterator i;
+ bool error = false;
+ for (i=cmdline.input_file.begin();
+ i!=cmdline.input_file.end(); ++i)
+ {
+ error = ! I->InterpretFile(*i);
+ if (error) break;
+ }
+ if (help_on_script)
+ {
+ I->SetNoExecMode(false);
+ std::string package;
+ I->GetExecuter()->GetFactory()->HelpBlackBox("workspace",
+ package,
+ false);
+ }
+ if (cmdline.input_file.size()==0)
+ I->CommandLineInterpreter();
+
+ //
+ }
+ else
+ {
+ // std::cout << "main C"<<std::endl;
+ wxEntry(argc, argv);
+ /*
+ // Create wxApp
+ wxBBIApp* app = new wxBBIApp();
+ app->SetCmdLine(cmdline);
+ if (wxApp::GetInstance()!=0)
+ {
+ std::cout << "WXAPP!!!"<<std::endl;
+ }
+ // wxApp::SetInstance(app);
+ // Start wx
+ wxInitialize();
+ std::cout << "Loop"<<std::endl;
+ app->MainLoop();
+ //bbtk::Wx::LoopUntilAllWindowsClose();
+ */
+ }
+
+ std::cout << "EO main"<<std::endl;
+
}
+
#endif // defined(_WIN32)
+//==========================================================================
+
+
+
+
+
+
#else
int main(int argc, char* argv[])
{
- if (argc>2) return 0;
+ if (argc>2)
+ {
+ std::cout << "usage : "<<argv[0]<<" [filename]"<<std::endl;
+ return 0;
+ }
std::cout << "BBI (Black Box Interpreter) - bbtk "
- << bbtk::GetVersion()<< " - (c) Creatis 2007"
- <<std::endl;
+ << bbtk::GetVersion()<< " - (c) Creatis 2007"
+ << std::endl;
- bbtk::Interpreter I;
+ bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
if (argc==1)
{
- I.CommandLineInterpreter();
+ I->CommandLineInterpreter();
}
else
+
{
std::string f(argv[1]);
- I.InterpretFile(f);
+ I->InterpretFile(f);
}
-
+
+ bbtk::Wx::LoopUntilAllWindowsClose();
+
return 0;
}