/* * aql.cpp * * Created on: 1. 7. 2017 * Author: Jan Travnicek */ #include <version.hpp> #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 <alib/measure> #include <alib/exception> #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[] ) { alib::ExceptionHandler::addHandler < 2 > ( [] ( std::ostream & out, const TCLAP::ArgException & exception ) { out << exception.error ( ) << std::endl; } ); try { common::GlobalData::argc = argc; common::GlobalData::argv = argv; TCLAP::CmdLine cmd ( "Algorithms query language binary", ' ', ALIB_VERSION_INFO ); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall) TCLAP::MultiArg < std::string > queries ( "q", "query", "Query string", false, "string" ); cmd.add ( queries ); 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 ); Prompt & p = Prompt::getPrompt ( ); cli::Environment & environment = p.getEnvironment ( ); environment.setBinding ( "stdin", "-" ); environment.setBinding ( "stdout", "-" ); for ( const std::pair < std::string, std::string > & param : params.getValue ( ) ) { environment.setBinding ( param.first, param.second ); } cli::Command::Result result = cli::Command::Result::OK; if ( queries.getValue ( ).empty ( ) ) { result = p.run ( ); } else { for ( const std::string & query : queries.getValue ( ) ) { result = p.execute_line ( cli::StringCharSequence ( query ), false ); if ( result != cli::Command::Result::OK ) break; } if ( result == cli::Command::Result::OK ) result = cli::Command::Result::QUIT; } measurements::end ( ); if ( measure.getValue ( ) ) common::Streams::measure << measurements::results ( ) << std::endl; if ( result == cli::Command::Result::QUIT ) return p.getEnvironment ( ).getResult ( ); else return 4; } catch ( ... ) { return alib::ExceptionHandler::handle ( common::Streams::err ); } }