00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stlib/os/UnixPipe.h>
00025
00026 #include <stlib/ByteArray.h>
00027 #include <stlib/ExternalReadStream.h>
00028 #include <stlib/ExternalWriteStream.h>
00029 #include <stlib/FileAccessor.h>
00030 #include <stlib/String.h>
00031
00032 #include <stlib/Error.h>
00033
00034 #include <stdio.h>
00035 #include <unistd.h>
00036
00037 namespace OS {
00038
00039 UnixPipe::UnixPipe(Core::FileAccessor *in, Core::FileAccessor *out)
00040 {
00041 input = in;
00042 output = out;
00043 }
00044
00045
00046 String *UnixPipe::className(void) const
00047 {
00048 return new String("UnixPipe");
00049 }
00050
00051
00052 UnixPipe *UnixPipe::openPair(void)
00053 {
00054 int fds[2];
00055
00056 if (pipe(fds) < 0) {
00057 perror(__PRETTY_FUNCTION__);
00058 (new Error(new String("Cannot create pipe"),
00059 new String(__PRETTY_FUNCTION__)))->raise();
00060 }
00061 return new UnixPipe(new FileAccessor(fds[1]), new FileAccessor(fds[0]));
00062 }
00063
00064
00065 void UnixPipe::close(void)
00066 {
00067 closeInput();
00068 closeOutput();
00069 }
00070
00071 void UnixPipe::closeInput(void)
00072 {
00073 input->close();
00074 }
00075
00076 void UnixPipe::closeOutput(void)
00077 {
00078 output->close();
00079 }
00080
00081
00082 Core::FileAccessor *UnixPipe::inputAccessor(void)
00083 {
00084 return input;
00085 }
00086
00087 Core::FileAccessor *UnixPipe::outputAccessor(void)
00088 {
00089 return output;
00090 }
00091
00092
00093 int UnixPipe::readInto(ByteArray *buffer, long startIndex, long count)
00094 {
00095 output->readInto(buffer, startIndex, count);
00096 }
00097
00098 int UnixPipe::writeFrom(ByteArray *buffer, long startIndex, long count)
00099 {
00100 input->writeFrom(buffer, startIndex, count);
00101 }
00102
00103
00104 void UnixPipe::seekTo(long position)
00105 {
00106 shouldNotImplement(__PRETTY_FUNCTION__);
00107 }
00108
00109
00110 ExternalReadStream *UnixPipe::readStream(void)
00111 {
00112 return output->readStream();
00113 }
00114
00115 ExternalWriteStream *UnixPipe::writeStream(void)
00116 {
00117 return input->writeStream();
00118 }
00119
00120 };