diff options
author | OctaneSnail <os@v12pwr.com> | 2009-01-18 14:23:57 +0300 |
---|---|---|
committer | OctaneSnail <os@v12pwr.com> | 2009-01-18 14:23:57 +0300 |
commit | 3af7c57491ab8082a71a9ffe17d453f83ee3fb08 (patch) | |
tree | 58cd2b3f47714a89504c84d1c98d230a5ecbc8d1 | |
parent | 847c4930cc8ef57513df52b86df0d8087581d56b (diff) |
Refactor code to remove duplication of SyncRead.
-rw-r--r-- | File.cpp | 92 | ||||
-rw-r--r-- | File.h | 1 | ||||
-rw-r--r-- | Mediatype.cpp | 104 | ||||
-rw-r--r-- | OutputPin.cpp | 101 | ||||
-rw-r--r-- | OutputPin.h | 1 |
5 files changed, 99 insertions, 200 deletions
@@ -18,6 +18,7 @@ #include <streams.h> #include "File.h" +#include "Utils.h" static int compare (const void *pos, const void *part) { @@ -46,3 +47,94 @@ int File::FindStartPart (LONGLONG position) return (int) (m_prev_part - array); } + +HRESULT File::SyncRead (LONGLONG llPosition, DWORD lLength, BYTE* pBuffer, LONG *cbActual) +{ + OVERLAPPED o; + LARGE_INTEGER offset; + DWORD to_read, read, acc = 0; + LONGLONG offset2; + int pos; +#ifdef _DEBUG + static int last_pos = -1; +#endif + + if (!pBuffer) + return E_POINTER; + + pos = FindStartPart (llPosition); + if (pos == -1) + { + DbgLog((LOG_TRACE, 2, L"FindStartPart bailed length = %lu, pos = %lld", lLength, llPosition)); + return S_FALSE; + } + +#ifdef _DEBUG + if (pos != last_pos) + { + DbgLog((LOG_TRACE, 2, L"Now reading volume %d.", pos)); + last_pos = pos; + } +#endif + FilePart *part = array + pos; + + offset2 = llPosition - part->in_file_offset; + offset.QuadPart = part->in_rar_offset + offset2; + + memset (&o, 0, sizeof (o)); + + if (!(o.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL))) + { + ErrorMsg (GetLastError (), L"CRFSOutputPin::SyncRead - CreateEvent)"); + return S_FALSE; + } + + while (true) + { + read = 0; + to_read = min (lLength, (DWORD) (part->size - offset2)); + + o.Offset = offset.LowPart; + o.OffsetHigh = offset.HighPart; + + if (!ReadFile (part->file, pBuffer + acc, to_read, NULL, &o)) + { + DWORD err = GetLastError (); + + if (err != ERROR_IO_PENDING) + { + ErrorMsg (err, L"CRFSOutputPin::SyncRead - ReadFile"); + break; + } + } + if (!GetOverlappedResult (part->file, &o, &read, TRUE)) + { + ErrorMsg (GetLastError (), L"CRFSOutputPin::SyncRead - GetOverlappedResult)"); + break; + } + lLength -= read; + acc += read; + + if (lLength == 0) + { + CloseHandle (o.hEvent); + if (cbActual) + *cbActual = acc; + return S_OK; + } + + pos ++; + + if (pos >= parts) + break; + + part ++; + offset2 = 0; + offset.QuadPart = part->in_rar_offset; + } + + CloseHandle (o.hEvent); + if (cbActual) + *cbActual = acc; + return S_FALSE; +} @@ -55,6 +55,7 @@ public: } int FindStartPart (LONGLONG position); + HRESULT SyncRead (LONGLONG llPosition, DWORD lLength, BYTE* pBuffer, LONG *cbActual); CMediaType media_type; LONGLONG size; diff --git a/Mediatype.cpp b/Mediatype.cpp index 2071aeb..23f89f1 100644 --- a/Mediatype.cpp +++ b/Mediatype.cpp @@ -33,7 +33,6 @@ // used to cast LONGLONG to LARGE_INTEGER #define CAST_LARGE_INTEGER(X) (*(LARGE_INTEGER *)&(X)) -static HRESULT SyncRead (File *file, LONGLONG llPosition, DWORD lLength, BYTE* pBuffer, LONG *cbActual); /* getNextToken extracts the current token from the string and and sets the starting point of the next token */ @@ -249,7 +248,7 @@ int checkFileForMediaType(File *file,List<MediaType> *mediaTypeList,MediaType ** return 0; } // read the necessary amount of bytes to compare to value (after masking) - ret = SyncRead(file,actOffset,cbg->checkBytes[i].byteCount,necessaryBytes,&lBytesRead); + ret = file->SyncRead(actOffset,cbg->checkBytes[i].byteCount,necessaryBytes,&lBytesRead); if(ret != S_OK) { matches = false; delete [] necessaryBytes; @@ -278,104 +277,3 @@ int checkFileForMediaType(File *file,List<MediaType> *mediaTypeList,MediaType ** *foundMediaType = NULL; return 1; } - -/* the next functions are copied from Outputpin.cpp with a few small modifications - - they should probably merged to reduce redundancy but I didn't want to change too much - of your classes */ - -static HRESULT SyncRead (File *file, LONGLONG llPosition, DWORD lLength, BYTE* pBuffer, LONG *cbActual) -{ - OVERLAPPED o; - LARGE_INTEGER offset; - DWORD to_read, read, acc = 0; - LONGLONG offset2; - int pos; -#ifdef _DEBUG - static int last_pos = -1; -#endif - - if (!file) - { - DbgLog((LOG_TRACE, 2, L"Mediatype.cpp - SyncRead called with no file loaded.")); - return E_UNEXPECTED; - } - - if (!pBuffer) - return E_POINTER; - - pos = file->FindStartPart (llPosition); - if (pos == -1) - { - DbgLog((LOG_TRACE, 2, L"Mediatype.cpp - FindStartPart bailed length = %lu, pos = %lld", lLength, llPosition)); - return ERROR_HANDLE_EOF; - } - -#ifdef _DEBUG - if (pos != last_pos) - { - DbgLog((LOG_TRACE, 2, L"Mediatype.cpp - Now reading volume %d.", pos)); - last_pos = pos; - } -#endif - FilePart *part = file->array + pos; - - offset2 = llPosition - part->in_file_offset; - offset.QuadPart = part->in_rar_offset + offset2; - - memset (&o, 0, sizeof (o)); - - if (!(o.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL))) - { - ErrorMsg (GetLastError (), L"Mediatype.cpp - SyncRead - CreateEvent)"); - return (S_FALSE); - } - - while (true) - { - read = 0; - to_read = min (lLength, part->size - offset2); - - o.Offset = offset.LowPart; - o.OffsetHigh = offset.HighPart; - - if (!ReadFile (part->file, pBuffer + acc, to_read, NULL, &o)) - { - DWORD err = GetLastError (); - - if (err != ERROR_IO_PENDING) - { - ErrorMsg (err, L"Mediatype.cpp - SyncRead - ReadFile"); - break; - } - } - if (!GetOverlappedResult (part->file, &o, &read, TRUE)) - { - ErrorMsg (GetLastError (), L"Mediatype.cpp - SyncRead - GetOverlappedResult)"); - break; - } - lLength -= read; - acc += read; - - if (lLength == 0) - { - CloseHandle (o.hEvent); - if (cbActual) - *cbActual = acc; - return S_OK; - } - - pos ++; - - if (pos >= file->parts) - break; - - part ++; - offset2 = 0; - offset.QuadPart = part->in_rar_offset; - } - - CloseHandle (o.hEvent); - if (cbActual) - *cbActual = acc; - return S_FALSE; -} diff --git a/OutputPin.cpp b/OutputPin.cpp index 5ff82bb..1534b9f 100644 --- a/OutputPin.cpp +++ b/OutputPin.cpp @@ -517,7 +517,7 @@ STDMETHODIMP CRFSOutputPin::SyncReadAligned (IMediaSample* pSample) if (!m_file) { - DbgLog((LOG_TRACE, 2, L"SyncRead called with no file loaded.")); + DbgLog((LOG_TRACE, 2, L"SyncReadAligned called with no file loaded.")); return E_UNEXPECTED; } @@ -535,7 +535,7 @@ STDMETHODIMP CRFSOutputPin::SyncReadAligned (IMediaSample* pSample) LONG cbActual = 0; - hr = SyncRead (llPosition, lLength, pBuffer, &cbActual); + hr = m_file->SyncRead (llPosition, lLength, pBuffer, &cbActual); pSample->SetActualDataLength (cbActual); @@ -544,107 +544,16 @@ STDMETHODIMP CRFSOutputPin::SyncReadAligned (IMediaSample* pSample) STDMETHODIMP CRFSOutputPin::SyncRead (LONGLONG llPosition, LONG lLength, BYTE* pBuffer) { - if (lLength < 0) - return E_UNEXPECTED; - - return SyncRead (llPosition, lLength, pBuffer, NULL); -} - -HRESULT CRFSOutputPin::SyncRead (LONGLONG llPosition, DWORD lLength, BYTE* pBuffer, LONG *cbActual) -{ - OVERLAPPED o; - LARGE_INTEGER offset; - DWORD to_read, read, acc = 0; - LONGLONG offset2; - int pos; -#ifdef _DEBUG - static int last_pos = -1; -#endif - if (!m_file) { DbgLog((LOG_TRACE, 2, L"SyncRead called with no file loaded.")); return E_UNEXPECTED; } - if (!pBuffer) - return E_POINTER; - - pos = m_file->FindStartPart (llPosition); - if (pos == -1) - { - DbgLog((LOG_TRACE, 2, L"FindStartPart bailed length = %lu, pos = %lld", lLength, llPosition)); - return S_FALSE; - } - -#ifdef _DEBUG - if (pos != last_pos) - { - DbgLog((LOG_TRACE, 2, L"Now reading volume %d.", pos)); - last_pos = pos; - } -#endif - FilePart *part = m_file->array + pos; - - offset2 = llPosition - part->in_file_offset; - offset.QuadPart = part->in_rar_offset + offset2; - - memset (&o, 0, sizeof (o)); - - if (!(o.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL))) - { - ErrorMsg (GetLastError (), L"CRFSOutputPin::SyncRead - CreateEvent)"); - return S_FALSE; - } - - while (true) - { - read = 0; - to_read = min (lLength, (DWORD) (part->size - offset2)); - - o.Offset = offset.LowPart; - o.OffsetHigh = offset.HighPart; - - if (!ReadFile (part->file, pBuffer + acc, to_read, NULL, &o)) - { - DWORD err = GetLastError (); - - if (err != ERROR_IO_PENDING) - { - ErrorMsg (err, L"CRFSOutputPin::SyncRead - ReadFile"); - break; - } - } - if (!GetOverlappedResult (part->file, &o, &read, TRUE)) - { - ErrorMsg (GetLastError (), L"CRFSOutputPin::SyncRead - GetOverlappedResult)"); - break; - } - lLength -= read; - acc += read; - - if (lLength == 0) - { - CloseHandle (o.hEvent); - if (cbActual) - *cbActual = acc; - return S_OK; - } - - pos ++; - - if (pos >= m_file->parts) - break; - - part ++; - offset2 = 0; - offset.QuadPart = part->in_rar_offset; - } + if (lLength < 0) + return E_UNEXPECTED; - CloseHandle (o.hEvent); - if (cbActual) - *cbActual = acc; - return S_FALSE; + return m_file->SyncRead (llPosition, lLength, pBuffer, NULL); } STDMETHODIMP CRFSOutputPin::Length (LONGLONG *pTotal, LONGLONG *pAvailable) diff --git a/OutputPin.h b/OutputPin.h index cf763f0..a31b6cd 100644 --- a/OutputPin.h +++ b/OutputPin.h @@ -96,7 +96,6 @@ private: List<ReadRequest> m_requests; CCritSec m_lock; - HRESULT SyncRead (LONGLONG llPosition, DWORD lLength, BYTE *pBuffer, LONG *cbActual); HRESULT ConvertSample (IMediaSample *sample, LONGLONG *pos, DWORD *length, BYTE **buffer); HRESULT DoFlush (IMediaSample **ppSample, DWORD_PTR *pdwUser); |