www.pudn.com > TVToolbar_demo > Chits.cpp


// CHits.cpp:  
// 
// Class for manipulation of ITS & CHM storage fills 
// Interfaces: 
// static const GUID CLSID_ITStorage = { 0x5d02926a, 0x212e, 0x11d0, { 0x9d, 0xf9, 0x0, 0xa0, 0xc9, 0x22, 0xe6, 0xec } }; 
// static const GUID IID_ITStorage = { 0x88cc31de, 0x27ab, 0x11d0, { 0x9d, 0xf9, 0x0, 0xa0, 0xc9, 0x22, 0xe6, 0xec} }; 
 
#include "stdafx.h" 
#include "chits.h" 
 
CHitsFile::CHitsFile() 
{ 
    m_pITStorage   = NULL; 
    m_pStorage     = NULL; 
    m_pwTmpFile     = NULL; 
} 
 
CHitsFile::~CHitsFile() 
{ 
    if (m_pStorage) 
        m_pStorage->Release(); 
    if (m_pITStorage) 
        m_pITStorage->Release(); 
    if (m_pwTmpFile) 
        LocalFree(m_pwTmpFile); 
} 
 
static const GUID CLSID_ITStorage = { 0x5d02926a, 0x212e, 0x11d0, { 0x9d, 0xf9, 0x0, 0xa0, 0xc9, 0x22, 0xe6, 0xec } }; 
static const GUID IID_ITStorage = { 0x88cc31de, 0x27ab, 0x11d0, { 0x9d, 0xf9, 0x0, 0xa0, 0xc9, 0x22, 0xe6, 0xec} }; 
 
HRESULT CHitsFile::OpenITS(PCSTR pszFile)  
{ 
    HRESULT hr = CoCreateInstance(CLSID_ITStorage, NULL, CLSCTX_INPROC_SERVER, 
        IID_ITStorage, (void **) &m_pITStorage); 
    if (!SUCCEEDED(hr))  
        return hr; 
 
    hr = m_pITStorage->StgOpenStorage(FileNameToWCHAR(pszFile), NULL,  
        STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &m_pStorage); 
    return hr; 
} 
     
WCHAR* CHitsFile::FileNameToWCHAR(PCSTR pszFile) 
{             
    if (!m_pwTmpFile) 
        m_pwTmpFile = (WCHAR*) LocalAlloc(LMEM_FIXED, MAX_PATH * sizeof(WCHAR)); 
    MultiByteToWideChar(CP_ACP, 0, pszFile, -1, m_pwTmpFile, MAX_PATH * sizeof(WCHAR)); 
    return m_pwTmpFile; 
} 
 
//////////////////////////////////////////// 
// CStringSubFile 
//////////////////////////////////////////// 
 
CStringSubFile::CStringSubFile(CHitsFile* pif) 
{ 
    m_pif = pif; 
    m_pStream = NULL; 
} 
 
CStringSubFile::~CStringSubFile() 
{ 
    if (m_pStream) 
        m_pStream->Release(); 
} 
 
bool CStringSubFile::GetString(DWORD offset, char* pszDst, int cbDst) 
{ 
    ASSERT(offset); 
    ASSERT(m_pif->GetStorage());    
 
    if (!m_pStream) { 
        if (FAILED(m_pif->OpenStream("#STRINGS", &m_pStream, m_pif->GetStorage(), STGM_READ))) { 
            *pszDst = 0;     
            return false;     
        }     
    } 
 
    LARGE_INTEGER liOffset; 
    liOffset.LowPart = (int) offset; 
    liOffset.HighPart = 0; 
    if (FAILED(m_pStream->Seek(liOffset, STREAM_SEEK_SET, NULL))) { 
        *pszDst = 0;     
        return false;     
    } 
 
    ULONG cbRead; 
    int cbTotalRead = 0; 
    const int CB_READ_BLOCK = 64; 
    while (cbTotalRead + CB_READ_BLOCK < cbDst && 
            SUCCEEDED(m_pStream->Read(pszDst, CB_READ_BLOCK, &cbRead))) { 
        if (!cbRead) { 
            *pszDst = 0; 
            return false;   
        }     
        for (ULONG iPos = 0; iPos < cbRead; iPos++) { 
            if (!pszDst[iPos])  
                return TRUE;    
        } 
        pszDst += cbRead; 
    }     
    *pszDst = 0; 
    return false;    
}