Example #1
0
int main(int argc, char**argv)
{
    // Some objects used to redirect cout and cerr
    // Scope must be here, so this still works inside the catch block for logging exceptions
    std::streambuf* cout_rdbuf = std::cout.rdbuf ();
    std::streambuf* cerr_rdbuf = std::cerr.rdbuf ();

#if !(defined(_WIN32) && defined(_DEBUG))
    boost::iostreams::stream_buffer<Tee> coutsb;
    boost::iostreams::stream_buffer<Tee> cerrsb;
#endif

    std::ostream oldcout(cout_rdbuf);
    std::ostream oldcerr(cerr_rdbuf);

    boost::filesystem::ofstream logfile;

    std::auto_ptr<OMW::Engine> engine;

    int ret = 0;
    try
    {
        Files::ConfigurationManager cfgMgr;

#if defined(_WIN32) && defined(_DEBUG)
        // Redirect cout and cerr to VS debug output when running in debug mode
        boost::iostreams::stream_buffer<DebugOutput> sb;
        sb.open(DebugOutput());
        std::cout.rdbuf (&sb);
        std::cerr.rdbuf (&sb);
#else
        // Redirect cout and cerr to openmw.log
        logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log"));

        coutsb.open (Tee(logfile, oldcout));
        cerrsb.open (Tee(logfile, oldcerr));

        std::cout.rdbuf (&coutsb);
        std::cerr.rdbuf (&cerrsb);
#endif


#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
        // Unix crash catcher
        if ((argc == 2 && strcmp(argv[1], "--cc-handle-crash") == 0) || !is_debugger_attached())
        {
            int s[5] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGABRT };
            cc_install_handlers(argc, argv, 5, s, (cfgMgr.getLogPath() / "crash.log").string().c_str(), NULL);
            std::cout << "Installing crash catcher" << std::endl;
        }
        else
            std::cout << "Running in a debugger, not installing crash catcher" << std::endl;
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
        // set current dir to bundle path
        boost::filesystem::path bundlePath = boost::filesystem::path(Ogre::macBundlePath()).parent_path();
        boost::filesystem::current_path(bundlePath);
#endif

        engine.reset(new OMW::Engine(cfgMgr));

        if (parseOptions(argc, argv, *engine, cfgMgr))
        {
            engine->go();
        }
    }
    catch (std::exception &e)
    {
#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
        if (isatty(fileno(stdin)))
            std::cerr << "\nERROR: " << e.what() << std::endl;
        else
#endif
            SDL_ShowSimpleMessageBox(0, "OpenMW: Fatal error", e.what(), NULL);

        ret = 1;
    }

    // Restore cout and cerr
    std::cout.rdbuf(cout_rdbuf);
    std::cerr.rdbuf(cerr_rdbuf);

    return ret;
}
int main()
{
	Source src(100);
	Pipe pipe1(80);
	Valve valve1(on);

	Tank tank1(60);
	Switch switch1(&tank1,300);
	Switch switch2(&tank1, 50);

	Pipe pipe2(80);
	Sink sink1(30);
	Pipe pipe3(40);
	Valve valve2(on);

	Tank tank2(80);
	Switch switch3(&tank2, 250);
	Switch switch4(&tank2, 50);

	Sink sink2(20);

	while( !kbhit() )
	{
		src>=pipe1;
		pipe1>=valve1;
		valve1>=tank1;
		Tee(tank1,pipe2,pipe3);
		pipe2>=sink1;
		pipe3>=valve2;
		valve2>=tank2;
		tank2>=sink2;

		src.Tick();
		pipe1.Tick();
		valve1.Tick();
		tank1.Tick();
		switch1.Tick();
		switch2.Tick();
		pipe2.Tick();
		sink1.Tick();
		pipe3.Tick();
		valve2.Tick();
		tank2.Tick();
		switch3.Tick();
		switch4.Tick();
		sink2.Tick();
		if(valve1.Status()==on&& switch1.Status()==on)
			valve1.Status()=off;
		if (valve1.Status()==off && switch2.Status()==off)
			valve1.Status()=on;
		if(valve2.Status()==on && switch3.Status()==on)
			valve2.Status()=off;
		if(valve2.Status()==off && switch4.Status()==off)
			valve2.Status()=on;
		cout<<" Src=" <<setw(2)<<src.Flow();
		cout<<" p1="<<setw(2)<<pipe1.Flow();

		if(valve1.Status()==off)
			cout<<" v1=off";
		else
			cout<<" v1=on";

		cout<<" T1="<<setw(3)<<tank1.Contents();
		cout<<" p2="<<setw(2)<<pipe2.Flow();
		cout<<" Sink1= "<<setw(2)<<sink1.Flow();
		cout<<" p3="<<setw(2)<<pipe3.Flow();
		if(valve2.Status()==off)
			cout<<" v2=off";
		else
		cout<<" v2=on";
		cout<<" T2= "<<setw(3)<<tank2.Contents();
		cout<<" sink2= "<<setw(2)<<sink2.Flow();
		cout<<"\n";
	}
		return 0;
}