CommandRunner.cpp
Go to the documentation of this file.00001 #include <sstream>
00002 #include <cstdlib>
00003
00004 #include "CS240Exception.h"
00005 #include "StringUtil.h"
00006
00007 #include "CommandRunner.h"
00008
00009 using namespace std;
00010
00011 namespace CommandRunner
00012 {
00013 namespace
00014 {
00015 string InvalidCommandErrorMessage(const string & commandText) {
00016 return string("invalid command: ") + commandText;
00017 }
00018
00019 string CommandFailedErrorMessage(const string & commandText) {
00020 return string("command failed: ") + commandText;
00021 }
00022 }
00023
00024 void Run(const string & commandText)
00025 {
00026
00027 string commandTextCopy = commandText;
00028 StringUtil::Trim(commandTextCopy);
00029
00030 if (commandTextCopy.empty())
00031 {
00032 throw CS240Exception(InvalidCommandErrorMessage(commandTextCopy));
00033 }
00034
00035 if (::system(commandTextCopy.c_str()) != 0)
00036 {
00037 throw CS240Exception(CommandFailedErrorMessage(commandTextCopy));
00038 }
00039 }
00040
00041 }
00042