XTPPropExchange.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:61k
- // XTPPropExchange.cpp: implementation of the CXTPPropExchange class.
- //
- // This file is a part of the XTREME TOOLKIT PRO MFC class library.
- // (c)1998-2008 Codejock Software, All Rights Reserved.
- //
- // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
- // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
- // CONSENT OF CODEJOCK SOFTWARE.
- //
- // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
- // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
- // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
- // SINGLE COMPUTER.
- //
- // CONTACT INFORMATION:
- // support@codejock.com
- // http://www.codejock.com
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "XTPMacros.h"
- #include "XTPPropExchange.h"
- #include "XTPVC80Helpers.h"
- #include "XTPVC50Helpers.h"
- #include "XTPResourceManager.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #define new DEBUG_NEW
- #endif
- #pragma warning(disable : 4310)
- #ifdef _UNICODE
- #define CT2BSTR(x) (BSTR)(LPCWSTR)x
- #else
- #define CT2BSTR(x) (BSTR)XTP_CT2CW(x)
- #endif
- enum VARTYPE_EX
- {
- VT_EX_RECT = 100,
- VT_EX_SIZE = 101
- };
- LCID CXTPPropExchange::m_lcidDateTime = XTP_LOCALE_ISO8601;
- /////////////////////////////////////////////////////////////////////////////
- BOOL ParseDateTimeISO8601(COleDateTime& rDT, LPCTSTR pcszDateTime)
- {
- SYSTEMTIME st;
- ZeroMemory(&st, sizeof(SYSTEMTIME));
- int nResult = SCANF_S(pcszDateTime, _T("%hu-%hu-%huT%hu:%hu:%hu"),
- &st.wYear, &st.wDay, &st.wMonth, &st.wHour, &st.wMinute, &st.wSecond);
- if (nResult == 3 || nResult == 5 || nResult == 6)
- {
- COleDateTime dtTemp(st);
- if (dtTemp.GetStatus() != COleDateTime::valid)
- return FALSE;
- rDT = dtTemp;
- return TRUE;
- }
- ZeroMemory(&st, sizeof(SYSTEMTIME));
- nResult = SCANF_S(pcszDateTime, _T("%hu:%hu:%hu"),
- &st.wHour, &st.wMinute, &st.wSecond);
- if (nResult == 2 || nResult == 3)
- {
- double dblTime = (((long)st.wHour * 3600L) + // hrs in seconds
- ((long)st.wMinute * 60L) + // mins in seconds
- ((long)st.wSecond)) / 86400.;
- rDT = dblTime;
- return TRUE;
- }
- return FALSE;
- }
- CString FormatDateTimeISO8601(const COleDateTime& dt)
- {
- ASSERT(dt.m_status == COleDateTime::valid);
- CString strValue;
- if ((DWORD)dt == 0)
- {
- strValue = dt.Format(_T("%H:%M:%S"));
- }
- else if ((double)(DWORD)(dt) == (double)dt)
- {
- strValue = dt.Format(_T("%Y-%d-%m"));
- }
- else
- {
- strValue = dt.Format(_T("%Y-%d-%mT%H:%M:%S"));
- }
- return strValue;
- }
- //////////////////////////////////////////////////////////////////////////
- // CXTPPropExchangeEnumerator
- CXTPPropExchangeEnumerator::CXTPPropExchangeEnumerator(CXTPPropExchange* pContainer, LPCTSTR lpszSection)
- {
- m_pContainer = pContainer;
- m_strSectionName = lpszSection;
- m_nCount = 0;
- }
- CXTPPropExchangeEnumerator::~CXTPPropExchangeEnumerator()
- {
- }
- POSITION CXTPPropExchangeEnumerator::GetPosition(DWORD dwCount /*= 0*/, BOOL bCompactMode /*= TRUE*/)
- {
- m_nCount = dwCount;
- if (!m_pContainer)
- return NULL;
- if (!bCompactMode)
- {
- PX_DWord(m_pContainer, _T("Count"), m_nCount, 0);
- }
- else
- {
- if (m_pContainer->IsStoring())
- m_pContainer->WriteCount(m_nCount);
- else
- m_nCount = m_pContainer->ReadCount();
- }
- return m_nCount == 0 ? NULL : (POSITION)1;
- }
- CXTPPropExchange* CXTPPropExchangeEnumerator::GetNext(POSITION& pos)
- {
- CString strSection;
- strSection.Format(_T("%s%i"), (LPCTSTR)m_strSectionName, (DWORD)(DWORD_PTR)pos - 1);
- CXTPPropExchange* pSection = m_pContainer->GetSection(strSection);
- pos++;
- if ((DWORD)(DWORD_PTR)pos > m_nCount) pos = NULL;
- return pSection;
- }
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNAMIC(CXTPPropExchange, CCmdTarget)
- CXTPPropExchange::CXTPPropExchange()
- {
- m_bLoading = FALSE;
- m_dwData = 0;
- m_pOwner = NULL;
- m_nSchema = _XTP_SCHEMA_CURRENT;
- m_bChildSection = FALSE;
- m_bInitialized = FALSE;
- m_bEmptySection = FALSE;
- m_bUseDefaultOnLoad = TRUE;
- m_bUseDefaultOnSave = TRUE;
- }
- CXTPPropExchange::~CXTPPropExchange()
- {
- }
- void CXTPPropExchange::InitSection(CXTPPropExchange* pRootSection)
- {
- ASSERT(m_bLoading == pRootSection->m_bLoading);
- m_bChildSection = TRUE;
- m_nSchema = pRootSection->m_nSchema;
- m_dwData = pRootSection->m_dwData;
- m_bEmptySection = pRootSection->m_bEmptySection;
- m_bUseDefaultOnLoad = pRootSection->m_bUseDefaultOnLoad;
- m_bUseDefaultOnSave = pRootSection->m_bUseDefaultOnSave;
- }
- void CXTPPropExchange::EmptySection()
- {
- m_bEmptySection = TRUE;
- }
- DWORD CXTPPropExchange::GetSizeOfVarType(VARTYPE vt)
- {
- switch (vt)
- {
- case VT_BOOL: return sizeof(BOOL);
- case VT_UI1: return sizeof(BYTE);
- case VT_I2: return 2;
- case VT_I4: return 4;
- case VT_R4:
- ASSERT(sizeof(float) == 4);
- return 4;
- case VT_R8:
- ASSERT(sizeof(double) == 8);
- return 8;
- case VT_EX_RECT: return sizeof(RECT);
- case VT_EX_SIZE: return sizeof(SIZE);
- case VT_CY: return sizeof(CURRENCY);
- case VT_BSTR:return sizeof(BSTR);
- case VT_VARIANT: return sizeof(VARIANT);
- case VT_DATE: return sizeof(COleDateTime);
- }
- return 0;
- }
- void CXTPPropExchange::Write (LPCTSTR pszPropName, const void* lpBuf, UINT nCount)
- {
- ASSERT(IsStoring());
- ASSERT(lpBuf != NULL);
- ExchangeBlobProp(pszPropName, (BYTE*&)lpBuf, (DWORD&)nCount);
- }
- UINT CXTPPropExchange::Read (LPCTSTR pszPropName, void* lpBuf, UINT nCount)
- {
- ASSERT(IsLoading());
- ASSERT(lpBuf != NULL);
- if (!ExchangeBlobProp(pszPropName, (BYTE*&)lpBuf, (DWORD&)nCount))
- return 0;
- return nCount;
- }
- AFX_INLINE int REPLACEW_S(BSTR& strReplace, LPCWSTR lpszOld, LPCWSTR lpszNew)
- {
- int nOldLength = (strReplace == NULL) ? 0 : (int)wcslen(strReplace);
- int nSourceLen = (lpszOld == NULL) ? 0 : (int)wcslen(lpszOld);
- if (nSourceLen == 0 || nOldLength == 0)
- return 0;
- int nReplacementLen = (lpszNew == NULL) ? 0 : (int)wcslen(lpszNew);
- // loop once to figure out the size of the result string
- int nCount = 0;
- LPWSTR lpszStart = strReplace;
- LPWSTR lpszTarget;
- while ((lpszTarget = wcsstr(lpszStart, lpszOld)) != NULL)
- {
- nCount++;
- lpszStart = lpszTarget + nSourceLen;
- }
- // if any changes were made, make them
- if (nCount > 0)
- {
- int nNewLength = nOldLength + (nReplacementLen-nSourceLen)*nCount;
- ASSERT(nNewLength <= nOldLength); // not implemented
- lpszStart = strReplace;
- // loop again to actually do the work
- while ((lpszTarget = wcsstr(lpszStart, lpszOld)) != NULL)
- {
- int nBalance = nOldLength - int(lpszTarget - strReplace + nSourceLen);
- MEMMOVE_S(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
- nBalance * sizeof(WORD));
- MEMCPY_S(lpszTarget, lpszNew, nReplacementLen*sizeof(WORD));
- lpszStart = lpszTarget + nReplacementLen;
- lpszStart[nBalance] = ' ';
- nOldLength += (nReplacementLen - nSourceLen);
- }
- ASSERT(strReplace[nNewLength] == ' ');
- }
- return nCount;
- }
- void CXTPPropExchange::PreformatString(BSTR bstrValue)
- {
- ASSERT(bstrValue && m_bLoading);
- REPLACEW_S(bstrValue, L"\\", L"