1 #include "commands.hpp"
2 #include <iostream>
3 #include <cstdlib>
4 
5 
6 
Commands(OpenDBX::Conn & conn)7 Commands::Commands( OpenDBX::Conn& conn )
8 {
9 	m_conn = conn;
10 
11 	m_cmds[".header"] = &Commands::header;
12 	m_cmds[".help"] = &Commands::help;
13 	m_cmds[".quit"] = &Commands::quit;
14 }
15 
16 
17 
exec(const string & cmdstr,struct format * fparam)18 void Commands::exec( const string& cmdstr, struct format* fparam )
19 {
20 	string cmd, arg;
21 	string::size_type pos = cmdstr.find_first_of( " \n\t" );
22 
23 	if( pos != string::npos ) {
24 		cmd = cmdstr.substr( 0, pos );
25 		arg = cmdstr.substr( pos, string::npos );
26 	} else {
27 		cmd = cmdstr;
28 	}
29 
30 	if( m_cmds.find( cmd ) == m_cmds.end() ) {
31 		std::cout << gettext( "Unknown command, use .help to list available commands" ) << std::endl;
32 	} else {
33 		(this->*m_cmds[cmd])( arg, fparam );
34 	}
35 }
36 
37 
38 
header(const string & str,struct format * fparam)39 void Commands::header( const string& str, struct format* fparam )
40 {
41 	if( fparam->header != true ) {
42 		std::cout << gettext( "Printing column names is now enabled" ) << std::endl;
43 		fparam->header = true;
44 	} else{
45 		std::cout << gettext( "Printing column names is now disabled" ) << std::endl;
46 		fparam->header = false;
47 	}
48 }
49 
50 
51 
help(const string & str,struct format * fparam)52 void Commands::help( const string& str, struct format* fparam )
53 {
54 	std::cout << gettext( "Available commands:" ) << std::endl;
55 	std::cout << "    .header	" << gettext( "toggle printing column names for result sets" ) << std::endl;
56 	std::cout << "    .help	" << gettext( "print this help" ) << std::endl;
57 	std::cout << "    .quit	" << gettext( "exit program" ) << std::endl;
58 }
59 
60 
61 
quit(const string & str,struct format * fparam)62 void Commands::quit( const string& str, struct format* fparam )
63 {
64 	std::cout << gettext( "Good bye" ) << std::endl;
65 	::exit( 0 );
66 }
67