00001 //=========================================================================== 00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh 00003 // 00004 //Redistribution and use in source and binary forms, with or without 00005 //modification, are permitted provided that the following conditions 00006 //are met: 00007 // 00008 //- Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // 00011 //- Redistributions in binary form must reproduce the above copyright 00012 // notice, this list of conditions and the following disclaimer in the 00013 // documentation and/or other materials provided with the distribution. 00014 // 00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 00016 // may be used to endorse or promote products derived from this software 00017 // without specific prior written permission. 00018 // 00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00022 //PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR 00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 //=========================================================================== 00031 #include "stdafx.h" 00032 #include ".\singlemediafilecache.h" 00033 00034 SingleMediaFileCache::SingleMediaFileCache(void) 00035 : mBytesWritten(0) 00036 , mIsComplete(false) 00037 , mReadPtr(0) 00038 { 00039 //debugLog.open("G:\\logs\\mediacache.log", ios_base::out); 00040 } 00041 00042 SingleMediaFileCache::~SingleMediaFileCache(void) 00043 { 00044 //debugLog.close(); 00045 } 00046 00047 bool SingleMediaFileCache::open(string inFileName) { 00048 mBytesWritten = 0; 00049 //debugLog<<"Opening "<<inFileName<<endl; 00050 mLocalFile.open(inFileName.c_str(), ios_base::in|ios_base::out|ios_base::binary|ios_base::trunc); 00051 00052 if (mLocalFile.is_open()) { 00053 //debugLog<<"File open...."<<endl; 00054 } else { 00055 //debugLog<<"File open FAILED"<<endl; 00056 } 00057 return mLocalFile.is_open(); 00058 } 00059 void SingleMediaFileCache::close() { 00060 mLocalFile.close(); 00061 00062 } 00063 bool SingleMediaFileCache::write(const unsigned char* inBuff, unsigned long inBuffSize) { 00064 //debugLog<<"Writeing "<<inBuffSize<<endl; 00065 //debugLog<<"Read Ptr = "<<mLocalFile.tellg(); 00066 //debugLog<<"Write Ptr = "<<mLocalFile.tellp(); 00067 mLocalFile.seekp(0, ios_base::end); 00068 if (inBuffSize != 0) { 00069 mLocalFile.write((const char*)inBuff, inBuffSize); 00070 mBytesWritten += inBuffSize; 00071 } 00072 00073 if (mLocalFile.fail()) { 00074 //debugLog<<"*** Write put into FAIL"<<endl; 00075 } 00076 return !(mLocalFile.fail()); 00077 } 00078 unsigned long SingleMediaFileCache::read(unsigned char* outBuff, unsigned long inBuffSize) { 00079 //debugLog<<"Read request for "<<inBuffSize<<" got "; 00080 //debugLog<<"Read Ptr = "<<mLocalFile.tellg(); 00081 //debugLog<<"Write Ptr = "<<mLocalFile.tellp(); 00082 mLocalFile.seekg(mReadPtr); 00083 unsigned long locBytesAvail = bytesAvail(); 00084 if (locBytesAvail >= inBuffSize) { 00085 mLocalFile.read((char*)outBuff, inBuffSize); 00086 //debugLog<<mLocalFile.gcount()<<endl; 00087 mReadPtr+=mLocalFile.gcount(); 00088 return mLocalFile.gcount(); 00089 } else if (locBytesAvail > 0) { 00090 mLocalFile.read((char*)outBuff, locBytesAvail); 00091 //debugLog<<locBytesAvail<<endl; 00092 mReadPtr += locBytesAvail; 00093 return locBytesAvail; 00094 } else { 00095 //debugLog << "NOTHING"<<endl; 00096 return 0; 00097 } 00098 00099 } 00100 bool SingleMediaFileCache::readSeek(unsigned long inSeekPos) { 00101 if (inSeekPos < mBytesWritten) { 00102 //debugLog<<"Seeking to "<<inSeekPos<<endl; 00103 mReadPtr = inSeekPos; 00104 return true; 00105 } else { 00106 return false; 00107 } 00108 } 00109 00110 unsigned long SingleMediaFileCache::totalBytes() { 00111 return mBytesWritten; 00112 } 00113 unsigned long SingleMediaFileCache::bytesAvail() { 00114 if (mLocalFile.fail()) { 00115 //debugLog<<"bytesAvail : File is in fail state"<<endl; 00116 } 00117 //debugLog<<"bytesAvail : Byteswritten = "<<mBytesWritten<<endl; 00118 //if ((!mLocalFile.fail()) && (mBytesWritten > 0)) { 00119 if (mBytesWritten > 0) { 00120 //debugLog<<"bytes Avail = "<<mBytesWritten - mReadPtr<<endl; 00121 return mBytesWritten - mReadPtr; 00122 } else { 00123 return 0; 00124 } 00125 }