/* STPipe Created by Stilianos Louca on 11/25/08 Last modified on 2011.09.05 Class for piping to shell. Namespace ST */ #ifndef ST_PIPE #define ST_PIPE #include #include #include #include #include using namespace std; namespace ST{ #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) #define PIPE_WRITE_MODE "w" #define PIPE_APPENDIX "" #elif defined(__APPLE__) #define PIPE_WRITE_MODE "r+" #define PIPE_APPENDIX " 2>&1" #elif defined(unix) || defined(__unix) || defined(__unix__) #define PIPE_WRITE_MODE "w" #define PIPE_APPENDIX " 2> /dev/null" #else #define PIPE_WRITE_MODE "w" #define PIPE_APPENDIX "" #endif class STPipe{ private: FILE *fout; // pointer to pipe. Will be NULL if pipe is not open bool requirePipe; // if true, failing to write to pipe returns an error. Otherwise, it's just silently accepted. string commandUsedToOpenPipe; // if pipe is open, it will have been opened using this exact command protected: ostream *logStream; // output everything also to this stream. This is not allocated (opened) by STPipe, but by whoever told STPipe to use it //implement vsprintf-like output for ostreams void vstreamprintf(ostream &streamOut, const char *format, va_list arg); public: STPipe(); STPipe(ostream *logStream); STPipe(string &command); STPipe(string &command, ostream *logStream); STPipe(const string &command, const string &directory); STPipe(const string &command, const string &directory, ostream *logStream); ~STPipe(); //copy constructor STPipe(const STPipe &original); //assignment operator STPipe &operator=(const STPipe &original); void setLogStream(ostream *logStream); void setRequirePipe(bool require); void open(const string &command); void open(const char *command); bool isOpen() const; void close(); virtual bool write(const char *format, va_list args); virtual bool write(const char *format, ...); virtual bool write(const string &message); bool pipeIsReady() const; }; //returns path to program (analogous to 'which' command) //Uses system's lookup tables, and does not guarantee file actually exists under the given path bool getProgramPath(const string &name, string &path); bool getCommandOutput(const string &command, bool suppressErrors, string &output); }//END OF NAMESPACE ST #endif