Commit b2bcaae1 authored by Ivan.Shulga's avatar Ivan.Shulga Committed by Alexander Trofimov

git-svn-id:...

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@52258 954022d7-b5bf-4e40-9824-e11837661b57
parent b948ad03
#ifndef ATLDEFINE_H_DEFINE
#define ATLDEFINE_H_DEFINE
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
//LINK_PROPERTY macros
#define LINK_PROPERTY_LONG(propName, memberName)\
STDMETHOD(get_##propName##)(long* pVal)\
{\
*pVal = ##memberName##;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(long newVal)\
{\
##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_SHORT(propName, memberName)\
STDMETHOD(get_##propName##)(SHORT* pVal)\
{\
*pVal = ##memberName##;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(SHORT newVal)\
{\
##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_BOOL(propName, memberName)\
STDMETHOD(get_##propName##)(VARIANT_BOOL* pVal)\
{\
*pVal = ##memberName## ? VARIANT_TRUE : VARIANT_FALSE;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(VARIANT_BOOL newVal)\
{\
##memberName## = (VARIANT_FALSE!=newVal);\
return S_OK;\
}
#define LINK_PROPERTY_VARIANT_BOOL(propName, memberName)\
STDMETHOD(get_##propName##)(VARIANT_BOOL* pVal)\
{\
*pVal = ##memberName## ? VARIANT_TRUE : VARIANT_FALSE;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(VARIANT_BOOL newVal)\
{\
##memberName## = (VARIANT_FALSE!=newVal)?VARIANT_TRUE : VARIANT_FALSE;\
return S_OK;\
}
#define LINK_PROPERTY_ULONG(propName, memberName)\
STDMETHOD(get_##propName##)(ULONG* pVal)\
{\
*pVal = ##memberName##;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(ULONG newVal)\
{\
##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_USHORT(propName, memberName)\
STDMETHOD(get_##propName##)(USHORT* pVal)\
{\
*pVal = ##memberName##;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(USHORT newVal)\
{\
##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_BSTR(propName, memberName)\
STDMETHOD(get_##propName##)(BSTR* pVal)\
{\
*pVal = ##memberName##.AllocSysString();\
return S_OK;\
}\
STDMETHOD(put_##propName##)(BSTR newVal)\
{\
##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_DOUBLE(propName, memberName)\
STDMETHOD(get_##propName##)(double* pVal)\
{\
*pVal = ##memberName##;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(double newVal)\
{\
##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_SAFEARRAY(propName, memberName)\
STDMETHOD(get_##propName##)(LPSAFEARRAY* pVal)\
{\
*pVal = NULL;\
if (NULL!=##memberName##)\
{\
SAFEARRAYBOUND rgsaBound[1];\
rgsaBound[0].lLbound = 0;\
rgsaBound[0].cElements = ##memberName##->rgsabound[0].cElements;\
*pVal = SafeArrayCreate(VT_UI1, 1, rgsaBound);\
memcpy((*pVal)->pvData, ##memberName##->pvData, ##memberName##->rgsabound[0].cElements);\
}\
return S_OK;\
}\
STDMETHOD(put_##propName##)(LPSAFEARRAY newVal)\
{\
RELEASEARRAY(##memberName##);\
if (NULL!=newVal)\
{\
SAFEARRAYBOUND rgsaBound[1];\
rgsaBound[0].lLbound = 0;\
rgsaBound[0].cElements = newVal->rgsabound[0].cElements;\
##memberName## = SafeArrayCreate(VT_UI1, 1, rgsaBound);\
if (NULL==##memberName##)\
return S_FALSE;\
memcpy(##memberName##->pvData, newVal->pvData, newVal->rgsabound->cElements);\
}\
return S_OK;\
}
#define LINK_PROPERTY_ARRAY_LONG(propName, arrayName, memberName)\
STDMETHOD(get_##propName##)(long Index, long* pVal)\
{\
if ((0>Index)||(Index>=(long)##arrayName##.GetCount()))\
return S_OK;\
*pVal = ##arrayName##[Index].##memberName##;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(long Index, long newVal)\
{\
if ((0>Index)||(Index>=(long)##arrayName##.GetCount()))\
return S_OK;\
##arrayName##[Index].##memberName## = newVal;\
return S_OK;\
}
#define LINK_PROPERTY_ARRAY_BOOL(propName, arrayName, memberName)\
STDMETHOD(get_##propName##)(long Index, VARIANT_BOOL* pVal)\
{\
if ((0>Index)||(Index>=(long)##arrayName##.GetCount()))\
return S_OK;\
*pVal = ##arrayName##[Index].##memberName## ? VARIANT_TRUE : VARIANT_FALSE;\
return S_OK;\
}\
STDMETHOD(put_##propName##)(long Index, VARIANT_BOOL newVal)\
{\
if ((0>Index)||(Index>=(long)##arrayName##.GetCount()))\
return S_OK;\
##arrayName##[Index].##memberName## = (VARIANT_FALSE!=newVal);\
return S_OK;\
}
// memory release macros
#define ADDREFINTERFACE(pinterface)\
{\
if (pinterface!=NULL)\
{\
pinterface->AddRef();\
}\
}
#define RELEASEINTERFACE(pinterface)\
{\
if (pinterface!=NULL)\
{\
pinterface->Release();\
pinterface=NULL;\
}\
}
#define RELEASEMEM(pobject)\
{\
if (pobject!=NULL)\
{\
free(pobject);\
pobject=NULL;\
}\
}
#define RELEASEOBJECT(pobject)\
{\
if (pobject!=NULL)\
{\
delete pobject;\
pobject=NULL;\
}\
}
#define RELEASEARRAYOBJECTS(pobject)\
{\
if (pobject!=NULL)\
{\
delete []pobject;\
pobject=NULL;\
}\
}
#define RELEASEHEAP(pmem)\
{\
if (pmem!=NULL)\
{\
HeapFree(GetProcessHeap(), 0, pmem);\
pmem=NULL;\
}\
}
#define RELEASEARRAY(parray)\
{\
if (parray!=NULL)\
{\
SafeArrayDestroy(parray);\
parray=NULL;\
}\
}
#define RELEASESYSSTRING(pstring)\
{\
if (pstring!=NULL)\
{\
SysFreeString(pstring);\
pstring=NULL;\
}\
}
#define RELEASEHANDLE(phandle)\
{\
if (phandle!=NULL)\
{\
CloseHandle(phandle);\
phandle=NULL;\
}\
}
#endif //ATLDEFINE
\ No newline at end of file
#pragma once
namespace NFileWriter
{
//------------------------------------------------------------------------------------------------------
// CBufferedFileWriter
//------------------------------------------------------------------------------------------------------
// ,
//------------------------------------------------------------------------------------------------------
class CFileWriter
{
public :
//
virtual VOID Write ( LPBYTE lpData, LONG64 lDataLength ) = 0;
// ( )
virtual VOID Flush () = 0;
//
virtual VOID Seek ( LONG64 lPosition, DWORD dwFrom = FILE_CURRENT ) = 0;
//
virtual VOID GetPosition(ULONGLONG& nPos) = 0;
//
virtual VOID GetSize(ULONGLONG& nLen) = 0;
public :
//
CFileWriter ()
{
}
//
virtual ~CFileWriter ()
{
}
};
//------------------------------------------------------------------------------------------------------
// CBufferedFileWriter
//------------------------------------------------------------------------------------------------------
// ,
//------------------------------------------------------------------------------------------------------
class CBufferedFileWriter : public CFileWriter
{
private :
HANDLE m_hFile; // ,
LONG64 m_lBufferSize; //
LPBYTE m_lpBuffer; //
LONG64 m_lWritePointer; //
public :
// ( , )
CBufferedFileWriter ( TCHAR *lpszFile, LONG64 lBufferSize = 10 * 1024 * 1024 ) : CFileWriter ()
{
// , , ,
m_hFile = CreateFile (lpszFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
//
if ( INVALID_HANDLE_VALUE == m_hFile )
{
//
throw 1;
}
//
m_lBufferSize = lBufferSize;
//
m_lWritePointer = 0;
// .
m_lpBuffer = ( LPBYTE ) ::HeapAlloc ( GetProcessHeap (), HEAP_ZERO_MEMORY, (size_t) m_lBufferSize );
// , Exception
if ( !m_lpBuffer )
{
//
throw 1;
}
// !!!
}
//
virtual ~CBufferedFileWriter ()
{
// ,
if ( m_hFile && m_hFile != INVALID_HANDLE_VALUE )
{
// ,
try
{
// , , -
Flush ();
}
catch ( ... )
{
}
CloseHandle ( m_hFile );
m_hFile = NULL;
}
//
if ( m_lpBuffer )
{
::HeapFree ( GetProcessHeap (), 0, m_lpBuffer );
m_lpBuffer = NULL;
}
}
//
virtual VOID Write ( LPBYTE lpData, LONG64 lDataLength )
{
//
while ( 0 < lDataLength )
{
//
LONG64 lBufferFreeLength = 0;
// ,
while ( 0 >= ( lBufferFreeLength = m_lBufferSize - m_lWritePointer ) )
{
// , ,
if ( FALSE == WriteBuffer ( m_lBufferSize ) )
throw 1;
}
// ,
if ( lBufferFreeLength > lDataLength )
lBufferFreeLength = lDataLength;
//
memcpy ( m_lpBuffer + m_lWritePointer, lpData, (size_t) lBufferFreeLength);
// ,
lDataLength -= lBufferFreeLength;
//
lpData = lpData + lBufferFreeLength;
//
m_lWritePointer += lBufferFreeLength;
}
}
// ( )
virtual VOID Flush ()
{
// ,
if ( 0 < m_lWritePointer )
{
// ,
if ( FALSE == WriteBuffer ( m_lWritePointer ) )
throw 1;
}
}
//
virtual VOID Seek ( LONG64 lPosition, DWORD dwFrom = FILE_CURRENT )
{
//
Flush ();
//
LARGE_INTEGER liOffset;
liOffset.QuadPart = lPosition;
DWORD nNewPos = ::SetFilePointer(m_hFile, liOffset.LowPart, &liOffset.HighPart, dwFrom);
if (nNewPos == INVALID_SET_FILE_POINTER)
{
HRESULT hr;
hr = AtlHresultFromLastError();
if (FAILED(hr))
throw 1;
}
}
//
virtual VOID GetPosition(ULONGLONG& nPos)
{
nPos=0;
if(m_hFile == NULL)return;
LARGE_INTEGER liOffset;
liOffset.QuadPart = 0;
liOffset.LowPart = ::SetFilePointer(m_hFile, 0, &liOffset.HighPart, FILE_CURRENT);
if (liOffset.LowPart == INVALID_SET_FILE_POINTER)
{
HRESULT hr;
hr = AtlHresultFromLastError();
if (FAILED(hr))
throw 1;
}
nPos = liOffset.QuadPart + m_lWritePointer;
}
//
virtual void GetSize(ULONGLONG& nLen)
{
nLen=0;
if(m_hFile == NULL)return;
LARGE_INTEGER liOffset;
ULARGE_INTEGER liFileSize;
liOffset.QuadPart = 0;
liOffset.LowPart = ::SetFilePointer(m_hFile, 0, &liOffset.HighPart, FILE_CURRENT);
liFileSize.LowPart = ::GetFileSize(m_hFile, &liFileSize.HighPart);
if (liFileSize.LowPart == INVALID_FILE_SIZE || liOffset.LowPart == INVALID_SET_FILE_POINTER)
{
HRESULT hr;
hr = AtlHresultFromLastError();
if (FAILED(hr))
throw 1;
}
nLen = liFileSize.QuadPart;
if (liFileSize.QuadPart==liOffset.QuadPart) //
{
nLen += m_lWritePointer;
}
}
private :
//
BOOL WriteBuffer ( LONG64 lSize )
{
//
if ( INVALID_HANDLE_VALUE == m_hFile || 0 == lSize )
return FALSE;
//
DWORD dwBytesWrite = 0;
//
if ( !WriteFile ( m_hFile, m_lpBuffer, ( DWORD ) lSize, &dwBytesWrite, NULL ) )
{
// - , FALSE
return FALSE;
}
// , ,
if ( ( LONG64 ) dwBytesWrite != lSize )
return FALSE;
// 0
m_lWritePointer = 0;
return TRUE;
}
};
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment