From 6675b647b845d1b28e710ec4cf59bc088b345caa Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 27 Aug 2013 00:29:04 +0200 Subject: [PATCH] Some static/non-static fixes to procs/stream library, updated converter to make use of getOutputOf function instead of re-implementing this. --- lib/converter.cpp | 23 ++++------------------- lib/procs.cpp | 4 ++-- lib/procs.h | 4 ++-- lib/stream.cpp | 4 ++-- 4 files changed, 10 insertions(+), 25 deletions(-) diff --git a/lib/converter.cpp b/lib/converter.cpp index 295b9315..d03411cb 100644 --- a/lib/converter.cpp +++ b/lib/converter.cpp @@ -61,12 +61,9 @@ namespace Converter { } JSON::Value Converter::queryPath(std::string myPath){ - std::vector cmd; - cmd.reserve(3); + char const * cmd[3] = {0, 0, 0}; std::string mistPath = Util::getMyPath() + "MistInfo"; - cmd.push_back((char*)mistPath.c_str()); - cmd.push_back(NULL); - cmd.push_back(NULL); + cmd[0] = mistPath.c_str(); fprintf( stderr, "Querying %s\n", myPath.c_str()); JSON::Value result; DIR * Dirp = opendir(myPath.c_str()); @@ -82,20 +79,8 @@ namespace Converter { } std::string fileName = entry->d_name; std::string mijnPad = std::string(myPath + (myPath[myPath.size()-1] == '/' ? "" : "/") + entry->d_name); - cmd[1] = (char*)mijnPad.c_str(); - int outFD = -1; - Util::Procs::StartPiped("MistInfo", &cmd[0], 0, &outFD, 0); - while( Util::Procs::isActive("MistInfo")){ Util::sleep(10); } - FILE * outFile = fdopen( outFD, "r" ); - char * fileBuf = 0; - size_t fileBufLen = 0; - getline(&fileBuf, &fileBufLen, outFile); - std::string line = fileBuf; - result[fileName] = JSON::fromString(std::string(fileBuf)); - if ( !result[fileName]){ - result.removeMember(fileName); - } - fclose( outFile ); + cmd[1] = mijnPad.c_str(); + result[fileName] = JSON::fromString(Util::Procs::getOutputOf((char* const*)cmd)); } } return result; diff --git a/lib/procs.cpp b/lib/procs.cpp index 86295771..2293417e 100644 --- a/lib/procs.cpp +++ b/lib/procs.cpp @@ -153,7 +153,7 @@ void Util::Procs::childsig_handler(int signum){ /// Runs the given command and returns the stdout output as a string. -std::string Util::Procs::getOutputOf(char * argv[]){ +std::string Util::Procs::getOutputOf(char* const* argv){ std::string ret; int fin = 0, fout = -1, ferr = 0; StartPiped("output_getter", argv, &fin, &fout, &ferr); @@ -433,7 +433,7 @@ pid_t Util::Procs::Start(std::string name, std::string cmd, std::string cmd2, st /// \arg fdin Standard input file descriptor. If null, /dev/null is assumed. Otherwise, if arg contains -1, a new fd is automatically allocated and written into this arg. Then the arg will be used as fd. /// \arg fdout Same as fdin, but for stdout. /// \arg fdout Same as fdin, but for stderr. -pid_t Util::Procs::StartPiped(std::string name, char * argv[], int * fdin, int * fdout, int * fderr){ +pid_t Util::Procs::StartPiped(std::string name, char* const* argv, int * fdin, int * fdout, int * fderr){ if (isActive(name)){ #if DEBUG >= 1 std::cerr << name << " already active - skipping start" << std::endl; diff --git a/lib/procs.h b/lib/procs.h index 16f75a6d..8df712fc 100644 --- a/lib/procs.h +++ b/lib/procs.h @@ -23,12 +23,12 @@ namespace Util { static void runCmd(std::string & cmd); static void setHandler(); public: - static std::string getOutputOf(char * argv[]); + static std::string getOutputOf(char* const* argv); static std::string getOutputOf(std::string cmd); static pid_t Start(std::string name, std::string cmd); static pid_t Start(std::string name, std::string cmd, std::string cmd2); static pid_t Start(std::string name, std::string cmd, std::string cmd2, std::string cmd3); - static pid_t StartPiped(std::string name, char * argv[], int * fdin, int * fdout, int * fderr); + static pid_t StartPiped(std::string name, char* const* argv, int * fdin, int * fdout, int * fderr); static pid_t StartPiped(std::string name, std::string cmd, int * fdin, int * fdout, int * fderr); static pid_t StartPiped2(std::string name, std::string cmd1, std::string cmd2, int * fdin, int * fdout, int * fderr1, int * fderr2); static void Stop(std::string name); diff --git a/lib/stream.cpp b/lib/stream.cpp index 1ecd2363..b2bb3e5e 100644 --- a/lib/stream.cpp +++ b/lib/stream.cpp @@ -39,9 +39,9 @@ Socket::Connection Util::Stream::getLive(std::string streamname){ Socket::Connection Util::Stream::getVod(std::string filename){ std::string name = "MistPlayer " + filename; std::string player_bin = Util::getMyPath() + "MistPlayer"; - const char *argv[] = {player_bin.c_str(), filename.c_str(), NULL}; + char* const argv[] = {(char*)player_bin.c_str(), (char*)filename.c_str(), NULL}; int fdin = -1, fdout = -1, fderr = fileno(stderr); - Util::Procs::StartPiped(name, (char **)argv, &fdin, &fdout, &fderr); + Util::Procs::StartPiped(name, argv, &fdin, &fdout, &fderr); // if StartPiped fails then fdin and fdout will be unmodified (-1) return Socket::Connection(fdin, fdout); }