int main( int argc, char** argv ) { boost::scoped_ptr< comma::Multiplay > multiPlay; try { const boost::array< comma::signal_flag::signals, 2 > signals = { { comma::signal_flag::sigint, comma::signal_flag::sigterm } }; comma::signal_flag shutdownFlag( signals ); comma::command_line_options options( argc, argv ); if( options.exists( "--help,-h" ) ) { usage(); } options.assert_mutually_exclusive( "--speed,--slow,--slowdown" ); double speed = options.value( "--speed", 1.0 / options.value< double >( "--slow,--slowdown", 1.0 ) ); unsigned int precision = options.value( "--precision", 10u ); std::string from = options.value< std::string>( "--from", "" ); std::string to = options.value< std::string>( "--to", "" ); bool quiet = options.exists( "--quiet" ); bool flush = !options.exists( "--no-flush" ); std::vector< std::string > configstrings = options.unnamed("--quiet,--no-flush","--slow,--slowdown,--speed,--precision,--binary,--fields,--clients,--from,--to"); if( configstrings.empty() ) { configstrings.push_back( "-;-" ); } comma::csv::options csvoptions( argc, argv ); comma::name_value::parser nameValue("filename,output", ';', '=', false ); std::vector< comma::Multiplay::SourceConfig > sourceConfigs( configstrings.size() ); comma::Multiplay::SourceConfig defaultConfig( "-", options.value( "--clients", 0 ), csvoptions ); for( unsigned int i = 0U; i < configstrings.size(); ++i ) { sourceConfigs[i] = nameValue.get< comma::Multiplay::SourceConfig >( configstrings[i], defaultConfig ); } boost::posix_time::ptime fromtime; if( !from.empty() ) { fromtime = boost::posix_time::from_iso_string( from ); } boost::posix_time::ptime totime; if( !to.empty() ) { totime = boost::posix_time::from_iso_string( to ); } multiPlay.reset( new comma::Multiplay( sourceConfigs, 1.0 / speed, quiet, boost::posix_time::milliseconds(precision), fromtime, totime, flush ) ); while( multiPlay->read() && !shutdownFlag && std::cout.good() && !std::cout.bad() &&!std::cout.eof() ) { } multiPlay->close(); multiPlay.reset(); if( shutdownFlag ) { std::cerr << "csv-play: interrupted by signal" << std::endl; return -1; } return 0; } catch( std::exception& ex ) { std::cerr << "csv-play: " << ex.what() << std::endl; } catch( ... ) { std::cerr << "csv-play: unknown exception" << std::endl; } try { if( multiPlay ) { multiPlay->close(); } } catch ( ... ) {} // windows thing std::cerr << "reset multiplay" << std::endl; std::cerr << "done" << std::endl; return -1; }
int main( int argc, char** argv ) { boost::scoped_ptr< comma::Multiplay > multiplay; try { const boost::array< comma::signal_flag::signals, 2 > signals = { { comma::signal_flag::sigint, comma::signal_flag::sigterm } }; comma::signal_flag shutdown_flag( signals ); comma::command_line_options options( argc, argv ); if( options.exists( "--help,-h" ) ) { usage(); } options.assert_mutually_exclusive( "--speed,--slow,--slowdown" ); double speed = options.value( "--speed", 1.0 / options.value< double >( "--slow,--slowdown", 1.0 ) ); double resolution = options.value< double >( "--resolution", 0.01 ); std::string from = options.value< std::string>( "--from", "" ); std::string to = options.value< std::string>( "--to", "" ); bool quiet = options.exists( "--quiet" ); bool flush = !options.exists( "--no-flush" ); std::vector< std::string > configstrings = options.unnamed("--interactive,-i,--paused,--paused-at-start,--quiet,--flush,--no-flush","--slow,--slowdown,--speed,--resolution,--binary,--fields,--clients,--from,--to"); if( configstrings.empty() ) { configstrings.push_back( "-;-" ); } comma::csv::options csvoptions( argc, argv ); comma::name_value::parser name_value("filename,output", ';', '=', false ); std::vector< comma::Multiplay::SourceConfig > sourceConfigs( configstrings.size() ); comma::Multiplay::SourceConfig defaultConfig( "-", options.value( "--clients", 0 ), csvoptions ); for( unsigned int i = 0U; i < configstrings.size(); ++i ) { sourceConfigs[i] = name_value.get< comma::Multiplay::SourceConfig >( configstrings[i], defaultConfig ); } boost::posix_time::ptime fromtime; if( !from.empty() ) { fromtime = boost::posix_time::from_iso_string( from ); } boost::posix_time::ptime totime; if( !to.empty() ) { totime = boost::posix_time::from_iso_string( to ); } multiplay.reset( new comma::Multiplay( sourceConfigs, 1.0 / speed, quiet, boost::posix_time::microseconds( static_cast<unsigned int> (resolution * 1000000) ), fromtime, totime, flush ) ); key_press_handler_t key_press_handler( options.exists( "--interactive,-i" ), options.exists( "--paused,--paused-at-start" ) ); while( !shutdown_flag && std::cout.good() && !std::cout.bad() && !std::cout.eof() ) { key_press_handler.update( multiplay->now() ); if( key_press_handler.state() == key_press_handler_t::paused ) { boost::this_thread::sleep( boost::posix_time::millisec( 200 ) ); continue; } if( !multiplay->read() ) { break; } key_press_handler.has_read_once(); } multiplay->close(); multiplay.reset(); if( shutdown_flag ) { std::cerr << "csv-play: interrupted by signal" << std::endl; return -1; } return 0; } catch( std::exception& ex ) { std::cerr << "csv-play: " << ex.what() << std::endl; } catch( ... ) { std::cerr << "csv-play: unknown exception" << std::endl; } try { if( multiplay ) { multiplay->close(); } } catch ( ... ) {} // windows thing return 1; }