00001 // /// // Mx clientSDK 00002 // ///// //// Mx Crossplatform Client Library 00003 // /// XXX XXX /// 00004 // /// XXX XXX /// $RCSfile: mxcpcBinaryFileWriter.cpp,v $ 00005 // /// XXX /// $Revision: 1.1 $ 00006 // /// XXX XXX /// $Date: 2006/01/11 19:07:06 $ 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 <mxcpcBinaryFileWriter.h> 00021 00022 #include <unistd.h> 00023 #include <fcntl.h> 00024 00025 00026 00027 mxcpcBinaryFileWriter::mxcpcBinaryFileWriter(const char *filename) { 00028 00029 OutFile = open(filename, 00030 O_CREAT | O_WRONLY | O_TRUNC, 00031 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 00032 00033 if(OutFile != -1) { 00034 CloseFileUponDeletion = true; 00035 ErrorEncountered = false; 00036 } 00037 else { 00038 CloseFileUponDeletion = false; 00039 ErrorEncountered = true; 00040 } 00041 } 00042 00043 00044 mxcpcBinaryFileWriter::mxcpcBinaryFileWriter(int file_descriptor) { 00045 00046 OutFile = file_descriptor; 00047 00048 CloseFileUponDeletion = false; 00049 ErrorEncountered = false; 00050 } 00051 00052 00053 mxcpcBinaryFileWriter::~mxcpcBinaryFileWriter() { 00054 00055 if(CloseFileUponDeletion) close(OutFile); 00056 } 00057 00058 00059 00060 void mxcpcBinaryFileWriter::receiveStreamBytes(const mxcpc::u8 *bytes, 00061 int byte_num) { 00062 00063 int written, to_write; 00064 00065 if(ErrorEncountered || (OutFile < 0)) return; 00066 00067 // have to do it tricky to properly handle short writes... 00068 to_write = byte_num; 00069 while(to_write > 0) { 00070 00071 written = write(OutFile, bytes, to_write); 00072 if(written == -1) { 00073 ErrorEncountered = true; 00074 break; 00075 } 00076 00077 to_write -= written; 00078 bytes += written; 00079 } 00080 } 00081