#ifndef __ForSubCoreUtil_h
#define __ForSubCoreUtil_h

//----- TWIPS_xZkqqHx_iNmAjFfHiCkJiCoHiOmAiNmAjFfHiCnG_xHqqkZx_ -----
#define CCD_tw2mm(a) (25.4 * static_cast<double>(a) / 1440.0)

// Dynamic Draw _xZkqqHx_jGhLjBmMiCmMiDeDiDjDiDfOiDhEiDeGiBfLiDfIiBeC_xHqqkZx_
// _xZkqqHx_iClBiCmMiDeDiDjDiDfOiDhEiDeGiBfLiDfIiCpA_xHqqkZx_ PgDdConv234.dll _xZkqqHx_iCkJiCoHiOpDiCkPiOoGiCoJiBeC_xHqqkZx_
// Dynamic Draw _xZkqqHx_jGhLjBmMiLeAjEfMiCmNiBeBjBfDiCmEiClBiCmMiDeDiDjDiDfOiDhEiDeGiBfLiDfIiCkJiCoHiMmEiCnBiPgPiClHiBeC_xHqqkZx_
extern IMpgMolipBase*	FFGetMolipBaseI ();
#define FFUtilErrI      FFGetMolipBaseI()->GetUtilErrI()
#define FFUtilMathI     FFGetMolipBaseI()->GetUtilMathI()
#define FFUtilFileI     FFGetMolipBaseI()->GetUtilFileI()
#define FFUtilFontI     FFGetMolipBaseI()->GetUtilFontI()
#define FFUtilStrI      FFGetMolipBaseI()->GetUtilStrI()
#define FFUtilPrintI    FFGetMolipBaseI()->GetUtilPrintI()
#define FFUtilCoordI    FFGetMolipBaseI()->GetUtilCoordI()
#define FFUtilZlibPngI  FFGetMolipBaseI()->GetUtilZlibPngI()
#define FFUtilMiscI     FFGetMolipBaseI()->GetUtilMisc()
//----- 06.05.10 Fukushiro M. _xZkqqHx_jFmPiNfIiPeJ_xHqqkZx_ ()-----

//----- BYTEVector_xZkqqHx_iDfIiDgHiDiKiBfLiDiAiKnGjIeB_xHqqkZx_ -----
template <class T> void operator << (BYTEVector& vStream, const T data)
{
	vStream.insert(vStream.end(), (const BYTE*)(&data),
					(const BYTE*)(&data) + sizeof(data));
} // template <class T> void operator <<.

template <class T> void ReadData (T& data, const BYTE*& bypStream)
{
	memcpy(&data, bypStream, sizeof(data));
	bypStream += sizeof(data);
} // template <class T> void ReadData.

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	FFClassCreate
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	DynamicDraw_xZkqqHx_jGhLjBmMiDeOiDiJiDfIiDeJiDhFiDfHiDeGiDeOiDgHiCmMiNoMjAkMiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	T	:_xZkqqHx_iNoMjAkMiClHiCoJiDeOiDiJiDfIiDeJiDhFiDfHiDeGiDeOiDgHiCmMiMfOiCpAiOhHjCoIiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jFnEjCgM_xHqqkZx_>	_xZkqqHx_iDeOiDiJiDfIiDeJiDhFiDfHiDeGiDeOiDgHiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	06.05.10 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
template<class T> T* FFClassCreate (T*)
{
	return FFGetMolipBaseI()->GetClassInstanceI()->CreateDelete((T*)NULL);
}

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	FFClassDelete
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	FFClassCreate_xZkqqHx_iCmFiNoMjAkMiClFiClNiDeOiDiJiDfIiDeJiDhFiDfHiDeGiDeOiDgHiCpAiNoNiPjMiClHiCoJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	p	:_xZkqqHx_iNoNiPjMiClHiCoJiDeOiDiJiDfIiDeJiDhFiDfHiDeGiDeOiDgHiCpAiOhHjCoIiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	06.05.10 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
template<class T> void FFClassDelete (T* p)
{
	if (p == NULL)
		FFUtilErrI->ThrowMiscException(L"Error:FMpgClassDelete(NULL)");
	FFGetMolipBaseI()->GetClassInstanceI()->CreateDelete(p);
}

/*************************************************************************
 * <_xZkqqHx_iNfMjBkCjBmM_xHqqkZx_>	FCResourceHandleBackup
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_iDiKiDfMiBfLiDfIiDgOiDjDiDgIiDiLiCpAiDgPiDgCiDeOiDeBiDgCiDhGiClFiBeBiOhHjCoIiClDiCoKiClNjCgMiCmJjFmPiNfIiClHiCoJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iJpAjAoA_xHqqkZx_>	_xZkqqHx_iDiKiDfMiBfLiDfIiDgOiDjDiDgIiDiLiCpAjFjMiMlDiClFiCmIiCkCiCmGiBeB_xHqqkZx_DD_xZkqqHx_jGhLjBmMiCmFiDiKiDfMiBfLiDfIiCkKiMmEiCnHiCmIiCkNiCmIiCoJiBeC_xHqqkZx_
 *			_xZkqqHx_iKgHjCkD_xHqqkZx_DLL_xZkqqHx_iCpAiOgHiCmBiCmEiCkCiCoJiClNiCnPiBeB_xHqqkZx_AfxSetResourceHandle_xZkqqHx_iKnGjAjEiCmNiLkEjCmKiBeC_xHqqkZx_
 *			_xZkqqHx_iClBiCoKiCmNiBeBiKgHjCkD_xHqqkZx_DLL_xZkqqHx_iCpAiOgHiCmBiCmEiCkCiClNiCmGiCkLiCmMjIgCiBeCjAmDjDeJiDiKiDjDiDeOiCpAiClFiCmEiCkCiCoKiCmOiKnGiMfHiCmIiCkCiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	06.05.10 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
struct FCResourceHandleBackup
{
	FCResourceHandleBackup (HINSTANCE hinstResource)
		{
			m_hinstResource = AfxGetResourceHandle();
			AfxSetResourceHandle(hinstResource);
		}

	~FCResourceHandleBackup ()
		{
			AfxSetResourceHandle(m_hinstResource);
		}

	HINSTANCE	m_hinstResource;
}; // struct FCResourceHandleBackup.

#if _MFC_VER > 0x0600
// MFC 7.0 _xZkqqHx_iImIiPoDiCmMiPoKiNiHiBeC_xHqqkZx_

class CFCMfcFile : public CFile
{
public:
	CFCMfcFile (MfcFile* pFile = NULL) : m_pMfcFile(pFile) {}
	virtual ULONGLONG GetPosition() const { return m_pMfcFile->GetPosition(); }
	virtual CString GetFileName() const { return (LPCTSTR)m_pMfcFile->GetFileName(); }
	virtual CString GetFileTitle() const { return (LPCTSTR)m_pMfcFile->GetFileTitle(); }
	virtual CString GetFilePath() const { return (LPCTSTR)m_pMfcFile->GetFilePath(); }
	virtual void SetFilePath(LPCTSTR lpszNewName) { m_pMfcFile->SetFilePath(lpszNewName); }

// Operations
	virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL)
				{ return m_pMfcFile->Open(lpszFileName, nOpenFlags, NULL); }

//	virtual CFile* Duplicate() const { return m_pMfcFile->Duplicate(); }

	virtual ULONGLONG Seek(LONGLONG lOff, UINT nFrom) { return m_pMfcFile->Seek((LONG)lOff, nFrom); }
	virtual void SetLength(ULONGLONG dwNewLen) { m_pMfcFile->SetLength((DWORD)dwNewLen); }
	virtual ULONGLONG GetLength() const { return m_pMfcFile->GetLength(); }

	virtual UINT Read(void* lpBuf, UINT nCount) { return m_pMfcFile->Read(lpBuf, nCount); }
	virtual void Write(const void* lpBuf, UINT nCount) { m_pMfcFile->Write(lpBuf, nCount); }

	virtual void LockRange(ULONGLONG dwPos, ULONGLONG dwCount) { m_pMfcFile->LockRange((DWORD)dwPos, (DWORD)dwCount); }
	virtual void UnlockRange(ULONGLONG dwPos, ULONGLONG dwCount) { m_pMfcFile->UnlockRange((DWORD)dwPos, (DWORD)dwCount); }

	virtual void Abort() { m_pMfcFile->Abort(); }
	virtual void Flush() { m_pMfcFile->Flush(); }
	virtual void Close() { m_pMfcFile->Close(); }

// Implementation
public:
	virtual ~CFCMfcFile() {}
	virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0, void** ppBufStart = NULL, void** ppBufMax = NULL)
		{ return m_pMfcFile->GetBufferPtr(nCommand, nCount, ppBufStart, ppBufMax); }

protected:
	MfcFile*	m_pMfcFile;
}; // class CFCMfcFile.

#else
// MFC 6.0 _xZkqqHx_iCmMiPoKiNiHiBeC_xHqqkZx_

class CFCMfcFile : public CFile
{
public:
	CFCMfcFile (MfcFile* pFile = NULL) : m_pMfcFile(pFile) {}
	virtual ULONG GetPosition() const { return m_pMfcFile->GetPosition(); }
	virtual CString GetFileName() const { return (LPCTSTR)m_pMfcFile->GetFileName(); }
	virtual CString GetFileTitle() const { return (LPCTSTR)m_pMfcFile->GetFileTitle(); }
	virtual CString GetFilePath() const { return (LPCTSTR)m_pMfcFile->GetFilePath(); }
	virtual void SetFilePath(LPCTSTR lpszNewName) { m_pMfcFile->SetFilePath(lpszNewName); }

// Operations
	virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL)
				{ return m_pMfcFile->Open(lpszFileName, nOpenFlags, NULL); }

//	virtual CFile* Duplicate() const { return m_pMfcFile->Duplicate(); }

	virtual ULONG Seek(LONGLONG lOff, UINT nFrom) { return m_pMfcFile->Seek((LONG)lOff, nFrom); }
	virtual void SetLength(ULONG dwNewLen) { m_pMfcFile->SetLength((DWORD)dwNewLen); }
	virtual ULONG GetLength() const { return m_pMfcFile->GetLength(); }

	virtual UINT Read(void* lpBuf, UINT nCount) { return m_pMfcFile->Read(lpBuf, nCount); }
	virtual void Write(const void* lpBuf, UINT nCount) { m_pMfcFile->Write(lpBuf, nCount); }

	virtual void LockRange(ULONG dwPos, ULONG dwCount) { m_pMfcFile->LockRange((DWORD)dwPos, (DWORD)dwCount); }
	virtual void UnlockRange(ULONG dwPos, ULONG dwCount) { m_pMfcFile->UnlockRange((DWORD)dwPos, (DWORD)dwCount); }

	virtual void Abort() { m_pMfcFile->Abort(); }
	virtual void Flush() { m_pMfcFile->Flush(); }
	virtual void Close() { m_pMfcFile->Close(); }

// Implementation
public:
	virtual ~CFCMfcFile() {}
	virtual UINT GetBufferPtr(UINT nCommand, UINT nCount = 0, void** ppBufStart = NULL, void** ppBufMax = NULL)
		{ return m_pMfcFile->GetBufferPtr(nCommand, nCount, ppBufStart, ppBufMax); }

protected:
	MfcFile*	m_pMfcFile;
}; // class CFCMfcFile.

#endif

template<class T> void Read (CArchive& ar, T& v)
{
	ar.Read(&v, sizeof(v));
}

template<class T> void Write (CArchive& ar, const T& v)
{
	ar.Write(&v, sizeof(v));
}

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	WriteString
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_jFlGiOjKjHpBjClHiCkK_xHqqkZx_WORD(2Byte)_xZkqqHx_iCmJiOpLiCnMiCoJjFlGiOjKjHpBiCmMiPgPjHmNiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	str	:_xZkqqHx_jFlGiOjKjHpBiCpAiOhHjCoIiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	01.01.03 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
extern void WriteString (CArchive& ar, const WString& str);

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	ReadString
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_jFlGiOjKjHpBjClHiCkK_xHqqkZx_WORD(2Byte)_xZkqqHx_iCmJiOpLiCnMiCoJjFlGiOjKjHpBiCmMjDpMjHmNiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	str	:_xZkqqHx_jFlGiOjKjHpBiCpAjFnEiClHiBeC_xHqqkZx_
 *			pError	:_xZkqqHx_iDeHiDiJiBfLiBgJjDmHiCnNiNjOiCnNiDgGiBfLiDfOjClHiCkKiOhHjCoIiCoGiCoIjCfKiCkCiPoKiNiHiCmIiCmHiBgKiCpA_xHqqkZx_
 *					 _xZkqqHx_jFnEiClHiBeC_xHqqkZx_
 *					 NULL _xZkqqHx_iCmMiPoKiNiHiCmNjHoBiKePiCpAjEkNjAlGiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	01.01.03 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *************************************************************************/
extern void ReadString (CArchive& ar, WString& str);

extern void operator << (CArchive& ar, const DBLPoint& mmpt);
extern void operator << (CArchive& ar, const DBLRect& mmrt);
extern void operator << (CArchive& ar, const GUID& guid);

extern void operator >> (CArchive& ar, DBLPoint& mmpt);
extern void operator >> (CArchive& ar, DBLRect& mmrt);
extern void operator >> (CArchive& ar, GUID& guid);


#define FOR_SUB_DEF_ONCE \
IMpgMolipBase* FFGetMolipBaseI () \
{ \
	return FMpgGetModule()->GetMolipBaseI(); \
} \
 \
void operator << (CArchive& ar, const DBLPoint& mmpt) \
{ \
	ar.Write(&mmpt, sizeof(mmpt)); \
} \
 \
void operator << (CArchive& ar, const DBLRect& mmrt) \
{ \
	ar.Write(&mmrt, sizeof(mmrt)); \
} \
 \
void operator << (CArchive& ar, const GUID& guid) \
{ \
	ar.Write(&guid, sizeof(guid)); \
} \
 \
void operator >> (CArchive& ar, DBLPoint& mmpt) \
{ \
	ar.Read(&mmpt, sizeof(mmpt)); \
} \
 \
void operator >> (CArchive& ar, DBLRect& mmrt) \
{ \
	ar.Read(&mmrt, sizeof(mmrt)); \
} \
 \
void operator >> (CArchive& ar, GUID& guid) \
{ \
	ar.Read(&guid, sizeof(guid)); \
} \
 \
void WriteString (CArchive& ar, const WString& str) \
{ \
	ar << WORD(str.length()); \
	ar.Write(str.c_str(), str.length() * sizeof(wchar_t)); \
} \
 \
void ReadString (CArchive& ar, WString& str) \
{ \
	WORD wLen; \
	ar.Read(&wLen, sizeof(wLen)); \
	str.assign(wLen + 1, L'0'); \
	ar.Read(&str[0], wLen * sizeof(wchar_t)); \
	str.resize(wLen); \
} \


#endif//__ForSubCoreUtil_h
