/* * aql.cpp * * Created on: 1. 7. 2017 * Author: Jan Travnicek */ #include <istream> #include <iostream> template < class T, class U > std::istream& operator>> ( std::istream & in, std::pair < T, U > & value ) { std::string tmp; in >> tmp; size_t posEqual = tmp.find ( "=" ); if ( posEqual == std::string::npos ) in.setstate(std::ios_base::failbit); value.first = tmp.substr ( 0, posEqual ); value.second = tmp.substr ( posEqual + 1, tmp.size ( ) ); if ( value.first.empty ( ) || value.second.empty ( ) ) in.setstate(std::ios_base::failbit); return in; } #include <string> #include <measure> #include <tclap/CmdLine.h> #include <global/GlobalData.h> #include "prompt/Prompt.h" namespace TCLAP { template < class T, class U > struct ArgTraits < std::pair < T, U > > { typedef ValueLike ValueCategory; }; } /* namespace TCLAP */ int main ( int argc, char * argv[] ) { try { common::GlobalData::argc = argc; common::GlobalData::argv = argv; TCLAP::CmdLine cmd ( "Algorithms query language binary", ' ', "0.01" ); TCLAP::ValueArg < std::string > query ( "q", "query", "Query index", false, "", "query" ); cmd.add ( query ); TCLAP::SwitchArg measure ( "m", "measure", "Measure times", false ); cmd.add ( measure ); TCLAP::SwitchArg verbose ( "v", "verbose", "Be verbose", false ); cmd.add ( verbose ); TCLAP::MultiArg < std::pair < std::string, std::string > > params ( "p", "params", "Query index", false, "pair < string, string >" ); cmd.add ( params ); cmd.parse ( argc, argv ); if(verbose.isSet()) common::GlobalData::verbose = true; if(measure.isSet()) common::GlobalData::measure = true; measurements::start ( "Overal", measurements::Type::OVERALL ); cli::Environment environment; environment.set ( "stdin", "-" ); environment.set ( "stdout", "-" ); for ( const std::pair < std::string, std::string > & param : params.getValue ( ) ) { environment.set ( param.first, param.second ); } Prompt p ( std::move ( environment ) ); cli::Command::Result result = cli::Command::Result::OK; if ( query.isSet ( ) ) result = p.execute_line ( query.getValue ( ) ); else result = p.run ( ); measurements::end ( ); if ( measure.getValue ( ) ) std::cmeasure << measurements::results ( ) << std::endl; if ( result == cli::Command::Result::QUIT ) return 0; else return 4; } catch ( const exception::CommonException & exception ) { alib::XmlDataFactory::toStdout ( exception ); return 1; } catch ( const TCLAP::ArgException & exception ) { std::cout << exception.error ( ) << std::endl; return 2; } catch ( const std::exception & exception ) { std::cerr << "Exception caught: " << exception.what ( ) << std::endl; return 3; } catch ( ... ) { std::cerr << "Unknown exception caught." << std::endl; return 127; } }