00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <mxcpcStreamFile.h>
00021
00022 #include <unistd.h>
00023 #include <fcntl.h>
00024
00025
00026
00027 mxcpcStreamFile::mxcpcStreamFile(const char *filename) {
00028
00029 InFile = open(filename, O_RDONLY);
00030
00031 if(InFile == -1) {
00032 CloseFileUponDestruction = false;
00033 StillUp = false;
00034 }
00035 else {
00036 CloseFileUponDestruction = true;
00037 StillUp = true;
00038 }
00039
00040 TimedBlockMode = false;
00041 BlockTimeout = 1000;
00042 }
00043
00044
00045 mxcpcStreamFile::mxcpcStreamFile(int fd) {
00046
00047 InFile = fd;
00048 CloseFileUponDestruction = false;
00049 StillUp = true;
00050
00051 TimedBlockMode = false;
00052 BlockTimeout = 1000;
00053 }
00054
00055
00056 mxcpcStreamFile::~mxcpcStreamFile() {
00057
00058 if(CloseFileUponDestruction) close(InFile);
00059 }
00060
00061
00062
00063 void mxcpcStreamFile::setTimedBlockModeEnabled(bool enabled, long timeout) {
00064
00065 TimedBlockMode = enabled;
00066
00067 if(TimedBlockMode) BlockTimeout = timeout;
00068 }
00069
00070
00071 int mxcpcStreamFile::fetchBytes(unsigned char *buffer, int num) {
00072
00073 int actually_read;
00074 struct timeval timeout;
00075 long usecs;
00076 fd_set file_descriptors;
00077
00078 if(!StillUp) return(0);
00079
00080 if(TimedBlockMode) {
00081
00082 usecs = BlockTimeout * 1000;
00083
00084 timeout.tv_sec = usecs / 1000000;
00085 timeout.tv_usec = usecs % 1000000;
00086
00087 FD_ZERO(&file_descriptors);
00088 FD_SET(InFile, &file_descriptors);
00089 if(!select(InFile + 1, &file_descriptors, 0, 0, &timeout)) return(0);
00090 }
00091
00092 actually_read = read(InFile, buffer, num);
00093
00094 if(actually_read <= 0) {
00095
00096 StillUp = false;
00097 return(0);
00098 }
00099
00100 return(actually_read);
00101 }
00102
00103
00104 bool mxcpcStreamFile::stillUp(void) {
00105
00106 return(StillUp);
00107 }
00108