00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Luke Moore 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_ReadWritePipe.h (UT Library, C++) 00015 * 00016 * COMMENTS: This class spawns a program and allows bidirectional 00017 * communication between this process and the program by using 00018 * two pipes. The program only has to use stdin and stdout. 00019 * This process must use the pipe files. 00020 */ 00021 00022 #ifndef __UT_ReadWritePipe_h__ 00023 #define __UT_ReadWritePipe_h__ 00024 00025 #include "UT_API.h" 00026 #include "UT_Spawn.h" 00027 #include <stdio.h> 00028 00029 class UT_API UT_ReadWritePipe { 00030 public: 00031 // Note that the constructor does not open the pipe. Instead, you must 00032 // call open(). 00033 UT_ReadWritePipe(); 00034 00035 // If the pipe is still open when this object is destructed, the 00036 // child process will be killed. 00037 ~UT_ReadWritePipe(); 00038 00039 // If the command could not be run for any reason, open() will return 00040 // false. The reason for the failure can be determined with getErrno(). 00041 bool open(const char *cmd); 00042 int getErrno() const { return myErrno; } 00043 00044 // If the pipe was already closed, close() will return false. If the child 00045 // is still running, close() will kill the child with a SIGINT. 00046 bool close(); 00047 00048 // If close() succeeds, you can get the exit status of the child process 00049 // with this method. 00050 int getChildExitStatus() const { return myChildExitStatus; } 00051 00052 // Use these files to read from and write to the pipe. Note that if you 00053 // try to write to the pipe when the child has exited, you will get a 00054 // SIGPIPE. When you write, you should call fflush() to force the 00055 // pipe to be flushed. 00056 FILE *getReadFile() { return myReadFile; } 00057 FILE *getWriteFile() { return myWriteFile; } 00058 00059 // This method will return the process id of the command that was executed. 00060 pid_t getChildPid() const { return myChildPid; } 00061 00062 private: 00063 FILE *myReadFile; 00064 FILE *myWriteFile; 00065 00066 pid_t myChildPid; 00067 int myChildExitStatus; 00068 int myErrno; 00069 00070 #ifdef WIN32 00071 void *myReadPipe; 00072 void *myWritePipe; 00073 void *myHProcess; 00074 #endif 00075 }; 00076 00077 #endif
1.5.9