00001 // /// // Mx clientSDK 00002 // ///// //// Mx Crossplatform Client Library 00003 // /// XXX XXX /// 00004 // /// XXX XXX /// $RCSfile: mxcpcBinaryFileWriter_win.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 /*! 00028 * \file mxcpcBinaryFileWriter_win.cpp 00029 * 00030 * \brief Alternate implementation for the <tt>WINDOWS</tt> platform 00031 * 00032 * Differs from the original <tt>POSIX</tt>-dependent implementation in that 00033 * we explicitly have to request binary mode when doing an <tt>open()</tt> or 00034 * <tt>fopen()</tt> to avoid having our carriage returns (13s) implicitly 00035 * toasted. 00036 */ 00037 00038 00039 00040 mxcpcBinaryFileWriter::mxcpcBinaryFileWriter(const char *filename) { 00041 00042 OutFile = open(filename, 00043 O_CREAT | O_WRONLY | O_TRUNC | O_BINARY); 00044 00045 if(OutFile != -1) { 00046 CloseFileUponDeletion = true; 00047 ErrorEncountered = false; 00048 } 00049 else { 00050 CloseFileUponDeletion = false; 00051 ErrorEncountered = true; 00052 } 00053 00054 RestoreMode = -1; 00055 } 00056 00057 00058 mxcpcBinaryFileWriter::mxcpcBinaryFileWriter(int file_descriptor) { 00059 00060 OutFile = file_descriptor; 00061 00062 if((RestoreMode = setmode(file_descriptor, O_BINARY)) != -1) 00063 ErrorEncountered = false; 00064 else 00065 ErrorEncountered = true; 00066 00067 CloseFileUponDeletion = false; 00068 } 00069 00070 00071 mxcpcBinaryFileWriter::~mxcpcBinaryFileWriter() { 00072 00073 if(RestoreMode != -1) setmode(OutFile, RestoreMode); 00074 00075 if(CloseFileUponDeletion) close(OutFile); 00076 } 00077 00078 00079 00080 void mxcpcBinaryFileWriter::receiveStreamBytes(const mxcpc::u8 *bytes, 00081 int byte_num) { 00082 00083 int written, to_write; 00084 00085 if(ErrorEncountered || (OutFile < 0)) return; 00086 00087 // have to do it tricky to properly handle short writes... 00088 to_write = byte_num; 00089 while(to_write > 0) { 00090 00091 written = write(OutFile, bytes, to_write); 00092 if(written == -1) { 00093 ErrorEncountered = true; 00094 break; 00095 } 00096 00097 to_write -= written; 00098 bytes += written; 00099 } 00100 } 00101