degate  0.1.2
DegateHelper.cc
Go to the documentation of this file.
00001 #include <degate.h>
00002 #include <DegateHelper.h>
00003 
00004 #include <vector>
00005 #include <string>
00006 #include <iostream>
00007 #include <fstream>
00008 
00009 #include <boost/foreach.hpp>
00010 
00011 #include <sys/wait.h>
00012 
00013 using namespace degate;
00014 using namespace std;
00015 
00016 std::vector<std::string> degate::tokenize(std::string const& str) {
00017 
00018   /* This implementation is more or less directly derived from
00019      this posting http://www.gamedev.net/community/forums/topic.asp?topic_id=320087
00020 
00021   */
00022   vector<string> result;
00023 
00024   string          item;
00025   stringstream    ss(str);
00026 
00027   while(ss >> item){
00028     if (item[0]=='"') {
00029       int lastItemPosition = item.length() - 1;
00030       if (item[lastItemPosition]!='"') {
00031         // read the rest of the double-quoted item
00032         string restOfItem;
00033         getline(ss, restOfItem, '"');
00034         item += restOfItem;
00035       }
00036       // otherwise, we had a single word that was quoted. In any case, we now
00037       // have the item in quotes; remove them
00038       item = item.substr(1, lastItemPosition-1);
00039     }
00040     // item is "fully cooked" now
00041     result.push_back(item);
00042   }
00043   return result;
00044 }
00045 
00046 std::string degate::write_string_to_temp_file(std::string const& dir,
00047                                               std::string const& content) {
00048 
00049   char filename[PATH_MAX];
00050   std::string pattern = generate_temp_file_pattern(dir);
00051   strncpy(filename, pattern.c_str(), sizeof(filename));
00052   if(!mktemp(filename)) // should never return NULL
00053     throw DegateRuntimeException("mktemp() failed");
00054 
00055   write_string_to_file(filename, content);
00056 
00057   return filename;
00058 }
00059 
00060 void degate::write_string_to_file(std::string const& path,
00061                                   std::string const& content) {
00062 
00063   std::ofstream file;
00064   file.open(path.c_str(), std::ios::trunc | std:: ios::out);
00065   file << content;
00066   file.close();
00067 }
00068 
00069 int degate::execute_command(std::string const& command, std::list<std::string> const& params) {
00070 
00071   pid_t pid = fork();
00072   if(pid == 0) {
00073     // child
00074     std::cout << "Execute command " << command << " ";
00075     char const ** argv = new char const *[params.size()+2];
00076     int i = 1;
00077     BOOST_FOREACH(std::string const& s, params) {
00078       argv[i] = s.c_str();
00079       i++;
00080       std::cout << s << " ";
00081     }
00082     argv[0] = command.c_str();
00083     argv[i] = NULL;
00084 
00085     std::cout << std::endl;
00086 
00087     if(execvp(command.c_str(), const_cast<char* const*>(argv)) == -1) {
00088       std::cout << "exec failed" << std::endl;
00089     }
00090     std::cout << "sth. failed" << std::endl;
00091     exit(0);
00092   }
00093   else if(pid < 0) {
00094     // fork failed
00095     throw SystemException("fork() failed");
00096   }
00097   else {
00098 
00099     // parent
00100     int exit_code;
00101     if(waitpid(pid, &exit_code, 0) != pid)
00102       throw SystemException("waitpid() failed");
00103     else {
00104       if(WEXITSTATUS(exit_code) != 0) {
00105         std::string errmsg("Failed to execute command: " + command);
00106         BOOST_FOREACH(std::string const& s, params) {
00107           errmsg += " ";
00108           errmsg += s;
00109         }
00110         errmsg += ". Error: ";
00111         errmsg += strerror(errno);
00112         throw SystemException(errmsg);
00113       }
00114       return  exit_code;    
00115     }
00116   }
00117 }
00118