File: commands.cpp

package info (click to toggle)
libopendbx 1.4.6-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,960 kB
  • ctags: 1,006
  • sloc: sh: 10,899; ansic: 7,283; xml: 1,837; cpp: 1,696; makefile: 362; sed: 16
file content (66 lines) | stat: -rw-r--r-- 1,561 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "commands.hpp"
#include <iostream>
#include <cstdlib>



Commands::Commands( OpenDBX::Conn& conn )
{
	m_conn = conn;

	m_cmds[".header"] = &Commands::header;
	m_cmds[".help"] = &Commands::help;
	m_cmds[".quit"] = &Commands::quit;
}



void Commands::exec( const string& cmdstr, struct format* fparam )
{
	string cmd, arg;
	string::size_type pos = cmdstr.find_first_of( " \n\t" );

	if( pos != string::npos ) {
		cmd = cmdstr.substr( 0, pos );
		arg = cmdstr.substr( pos, string::npos );
	} else {
		cmd = cmdstr;
	}

	if( m_cmds.find( cmd ) == m_cmds.end() ) {
		std::cout << gettext( "Unknown command, use .help to list available commands" ) << std::endl;
	} else {
		(this->*m_cmds[cmd])( arg, fparam );
	}
}



void Commands::header( const string& str, struct format* fparam )
{
	if( fparam->header != true ) {
		std::cout << gettext( "Printing column names is now enabled" ) << std::endl;
		fparam->header = true;
	} else{
		std::cout << gettext( "Printing column names is now disabled" ) << std::endl;
		fparam->header = false;
	}
}



void Commands::help( const string& str, struct format* fparam )
{
	std::cout << gettext( "Available commands:" ) << std::endl;
	std::cout << "    .header	" << gettext( "toggle printing column names for result sets" ) << std::endl;
	std::cout << "    .help	" << gettext( "print this help" ) << std::endl;
	std::cout << "    .quit	" << gettext( "exit program" ) << std::endl;
}



void Commands::quit( const string& str, struct format* fparam )
{
	std::cout << gettext( "Good bye" ) << std::endl;
	::exit( 0 );
}