📄 command.cpp
字号:
#include "command.h"
#include "Project.h"
bool execCommand(const string& cmd)
/* executing command. */
{
istrstream cmdStream( cmd.data(),cmd.size() );
string cmdPart;
cmdStream>>cmdPart;
if( cmdPart.size() == 0 ) { return true; }
if ( cmdPart == "quit" ) { exit(0); }
else if ( cmdPart == "help" ) {
if ( cmdStream>>cmdPart ) { helpCommand( cmdPart ); }
else { helpCommand( cmd ); }
}
else if ( cmdPart == "backup" ) {
if ( cmdStream>>cmdPart ) { backupCommand(); }
else { backupCommand(cmd); }
}
else if ( cmdPart == "find" ) { findCommand(cmd); }
else if ( cmdPart == "project" ){ projectCommand(cmd); }
else if ( cmdPart == "customer"){ customerCommand(cmd); }
else{
cout<<"Sorry, I don't understand your command, ";
cout<<"type help for more information."<<endl;
}
return true;
}
bool helpCommand(const string& cmd)
/* find and display help message about command (or keyword). */
{
string buf;
ifstream helpFile;
helpFile.open( "help.xml" );
if( !helpFile.is_open() ){
cerr<<"help document not found."<<endl;
return false;
}
while( getline( helpFile,buf,'>' ) ){
if( buf == cmd ){ // match keyword
getline( helpFile, buf, '<' );
cout<<buf;
return true;
}
getline( helpFile, buf, '<' ); // clear follow part
}
cout<<"sorry, I can't find help message about you keyword."<<endl;
return false;
}
bool backupCommand( )
{
string path;
cout << "Plear input backup path end with backup filename: ";
cin >> path;
return backupCommand( path );
}
bool backupCommand(const string& path)
/* backup database file, success return true, else return false. */
{
char ch;
ifstream dbFile;
ofstream backupStream;
if ( path.find( ".xml")==path.length()-4 ){ // path valid.
backupStream.open( path.data() );
if( dbFile.fail() ){
cout<< "create backup file false."<<endl;
return false;
}
dbFile.open( "customer.xml" );
if( dbFile.fail() ){
cout<< "open customer database file error."<<endl;
return false;
}
while( (ch=dbFile.get())!=EOF )
{ backupStream.put( ch ); }
return true;
}else{
cerr<<"path invalid,please end with '.xml'."<<endl;
return false;
}
}
bool findCommand( const string& cmd )
{
Customer customer;
string arg1,arg2;
unsigned id=0;
istrstream cmdStream( cmd.data(), cmd.size() );
typedef pair<string,string> str_Pair;
map<string,string> str_Map;
cmdStream>>arg1; // clear string "query"
while( cmdStream>>arg1 ){
if( arg1[0]=='-' ){
arg1.erase( arg1.begin() );
/* normal query */
if( ( arg1=="normal" || arg1== "n" ) && cmdStream>>arg2 )
{ return customer.display( arg2 ); }
/* fuzzy query */
else if( ( arg1== "fuzzy" || arg1=="f" ) && cmdStream>>arg2 )
{ return customer.displayf( arg2 ); }
/* high-level query */
else if( arg1 == "id" && cmdStream >> id )
{ return customer.display( id ); }
else if( arg1=="high" || arg1=="h" ){
while( cmdStream>>arg1 ){
if( arg1[0]=='-' && cmdStream>>arg2 ){
arg1.erase( arg1.begin() );
str_Map.insert( str_Pair(arg1,arg2) );
}
}
if( str_Map.size()!=0 )
{ return customer.display( str_Map ); }
}
}
}
cerr<<"query format invalid."<<endl;
return false;
}
bool projectCommand( const string& cmd )
/* manage project, manage success return true, else return false. */
{
istrstream istr( cmd.data(), cmd.size() );
Project project;
string arg1,arg2;
int id;
istr>>arg1;
if( istr>>arg1 ) {
if( ( arg1 == "-add" || arg1 == "-a" ) && istr >> arg2 )
{ return project.add( arg2 ); } // add a project
else if( ( arg1 == "-delete" || arg1 == "-d" ) && istr >> arg2 )
{ return project.del( arg2 ); } // delete a proejct
else if( arg1 == "-change" || arg1 == "-c" ){
if( istr >> arg1 >> arg2 )
{ return project.chg( arg1, arg2 );} // chagnge project's name
}
else if( istr >> arg2 ){
if( arg2 == "-join" || arg2 == "-j" ){
while( istr >> id ) // join customer to project
{ project.joinCustomer( arg1, id ); }
return true;
}
else if( arg2=="-remove" || arg2 == "-r" ){
while( istr >> id ) // remove customer from project
{ project.remCustomer( arg1, id ); }
return true;
}
}
else
{ return project.display( arg1 ); }
}
else
{ return project.display(); }
cerr << " invalid command format." << endl;
return true;
}
bool customerCommand( const string& cmd )
/* manage customers. success return true, else return false. */
{
istrstream istr( cmd.data(), cmd.size() );
Customer customer;
string arg1,arg2;
int id;
istr>>arg1; // clear first argument
if( istr>>arg1 ) {
if( '-' == arg1[0] ){ // condition command.
if( arg1 == "-add" )
{ return customer.add(); }
else if( arg1=="-del" ){
while( istr >> id )
{ customer.del( id ); }
return true;
}
else if( arg1 == "-change" && istr>>id )
{ return customer.chg( id ); }
}
else if ( istr>> id ){
return customer.display( id );
}
}
cerr << " invalid command format." << endl;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -