profile.cpp

来自「A Model-View-Controller Framework that i」· C++ 代码 · 共 168 行

CPP
168
字号
#include "stdafx.h"
#include "Profile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

   
namespace SbjCore
{
	namespace Sys
	{
	
		struct SetSharedProfileStateImpl
		{
			CWinApp* pApp;
			LPCTSTR lpszOldProfile;
			CString sShared;
			bool bSharable;
			
			SetSharedProfileStateImpl(bool bIsSharable) :
				pApp(::AfxGetApp()),
				bSharable(bIsSharable),
				lpszOldProfile(NULL)
			{
				if (pApp != NULL)
				{
					if (bSharable)
					{
						lpszOldProfile = pApp->m_pszProfileName;
						CString	sOldProfile(lpszOldProfile);
						sShared = _T("Shared\\") + sOldProfile;
						pApp->m_pszProfileName = sShared;
					}
				}
			}
			
			virtual ~SetSharedProfileStateImpl()
			{
				if (pApp != NULL)
				{
					if (bSharable)
					{
						pApp->m_pszProfileName = lpszOldProfile;
					}
				}
			}
		};

		SetSharedProfileState::SetSharedProfileState(bool bIsSharable) :
			m_pImpl(new SetSharedProfileStateImpl(bIsSharable))		
		{
		}

		SetSharedProfileState::~SetSharedProfileState()
		{
			try
			{
				delete m_pImpl;
			}
			catch (...)
			{
				ASSERT(FALSE);
			}
		}
	
		/////////////////////////////////////////////////////////////////////////////////////
		
		struct SetAlternateProfileImpl
		{
			CWinApp* pApp;
			LPCTSTR lpszOldProfile;
			CString sAltProfile;

			SetAlternateProfileImpl(LPCTSTR lpszAltProfile) :
				pApp(::AfxGetApp()),
				sAltProfile(lpszAltProfile == NULL ? _T("") : lpszAltProfile),
				lpszOldProfile(NULL)
			{
				if (!sAltProfile.IsEmpty())
				{
					if (pApp != NULL)
					{
						lpszOldProfile = pApp->m_pszProfileName;
						pApp->m_pszProfileName = sAltProfile;
					}
				}
			}

			virtual ~SetAlternateProfileImpl()
			{
				if (!sAltProfile.IsEmpty())
				{
					if (pApp != NULL)
					{
						pApp->m_pszProfileName = lpszOldProfile;
					}
				}
			}
		};

		SetAlternateProfile::SetAlternateProfile(LPCTSTR lpszAltProfile) :
			m_pImpl(new SetAlternateProfileImpl(lpszAltProfile))		
		{
		}

		SetAlternateProfile::~SetAlternateProfile()
		{
			try
			{
				delete m_pImpl;
			}
			catch (...)
			{
				ASSERT(FALSE);
			}
		}


		/////////////////////////////////////////////////////////////////////////////////////	
		
		const CString GetProfileValue(
			LPCTSTR lpszSection, 
			LPCTSTR lpszEntry, 
			LPCTSTR lpszValue,
			bool bCreateEntry /*= true*/,
			bool bIsShareable /*= false*/)
		{
			CString s;
			CWinApp* pApp = ::AfxGetApp();

			if (pApp != NULL)
			{
				SetSharedProfileState theState(bIsShareable);
				s = pApp->GetProfileString(lpszSection, lpszEntry, lpszValue);
				if (bCreateEntry)
				{
					pApp->WriteProfileString(lpszSection, lpszEntry, s);
				}
			}

			return s;
		}

		BOOL WriteProfileValue(
			LPCTSTR lpszSection, 
			LPCTSTR lpszEntry, 
			LPCTSTR lpszValue,
			bool bIsShareable /*= false*/)
		{
			bool bRslt = false;
			CWinApp* pApp = ::AfxGetApp();

			if (pApp != NULL)
			{
				SetSharedProfileState theState(bIsShareable);
				bRslt = pApp->WriteProfileString(lpszSection, lpszEntry, lpszValue);
			}

			return bRslt; 
		}

	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?