Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

mxcpcStreamFile.cpp

00001 //           ///          //                                        Mx clientSDK
00002 //          /////        ////                    Mx Crossplatform Client Library
00003 //         /// XXX     XXX ///
00004 //        ///    XXX XXX    ///     $RCSfile: mxcpcStreamFile.cpp,v $
00005 //       ///       XXX       ///     $Revision: 1.5 $
00006 //      ///      XXX XXX      ///     $Date: 2006/01/12 16:40:54 $
00007 //     ////    XXX     XXX    ////     $Author: cvs-kai $
00008 //    ////                     ////
00009 //   ////  M  O  B  O  T  I  X  ////////////////////////////////////////////////
00010 //  //// Security Vision Systems //////////////////////////////////////////////
00011 //                                                                          //
00012 //  Copyright (C) 2005 - 2006, MOBOTIX AG, Germany                         //
00013 //  This software is made available under the BSD licence. Please refer   //
00014 //  to the file LICENCE.TXT contained in this distribution for details.  //
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 

Generated on Mon Jan 30 15:13:08 2006 for mxcpc by  doxygen 1.4.4