⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filebackupe.h

📁 实时文件备份
💻 H
📖 第 1 页 / 共 5 页
字号:
		inline BOOL GetNextAvailableDriveBasePath(CParsedPathItem*& pDriveItem, CString& csBasePath, CParsedPathItem*& pItem)
		{
			TCHAR tDriveName;

			if ( (tDriveName = GetNextAvailableDrive(pDriveItem)) == NULL )
				return FALSE;
			return GetDriveBasePath(tDriveName, csBasePath, pItem);
		}

	protected:
		CParsedPathItem *						m_pRootParsedPathItem;
	};

	class CProfile
	{
	public:
		class CIncludeSrcPath
		{
		public:

			inline void Reset()
			{
				m_csPathname.Empty();
				m_csVolumeSerialNumber  = 0;

				m_fFilenameMasksAvailable = FALSE;
				m_csFilenameMasks.Empty();
				m_fIsIncludeFilenameMasks = FALSE;

				m_fFileAttributesMaskAvailable = FALSE;
				m_dwFileAttributesMask = 0;
				m_fIsIncludeFileAttributesMask = FALSE;
			}

			inline CIncludeSrcPath& operator =(const CIncludeSrcPath& obj)
			{
				m_csPathname = obj.m_csPathname;
				m_csVolumeSerialNumber = obj.m_csVolumeSerialNumber;
				m_fFilenameMasksAvailable = obj.m_fFilenameMasksAvailable;
				m_csFilenameMasks = obj.m_csFilenameMasks;
				m_fIsIncludeFilenameMasks = obj.m_fIsIncludeFilenameMasks;
				m_fFileAttributesMaskAvailable = obj.m_fFileAttributesMaskAvailable;
				m_dwFileAttributesMask = obj.m_dwFileAttributesMask;
				m_fIsIncludeFileAttributesMask = obj.m_fIsIncludeFileAttributesMask;
				return *this;
			}

			inline CIncludeSrcPath& operator =(const CString& csPathname)
			{
				if ( !csPathname.IsEmpty() )
				{
					int nIndex;

					Reset();
					if ( csPathname[1] != _T(':') || csPathname[2] != _T('\\') )
						AfxThrowNotSupportedException();
					if ( (nIndex = csPathname.FindOneOf(_T("*?"))) == -1 )
					{
						if ( csPathname.GetLength() > 3 && csPathname[csPathname.GetLength() - 1] == _T('\\') )
							m_csPathname = csPathname.Left(csPathname.GetLength() - 1);
						else
							m_csPathname = csPathname;
					}
					else if ( nIndex + 1 < csPathname.GetLength() && csPathname.Mid(nIndex + 1).Find(_T('\\')) != -1 )
						AfxThrowNotSupportedException();
					else
					{
						nIndex = csPathname.ReverseFind(_T('\\'));
						m_csPathname = csPathname.GetLength() > 3 ? csPathname.Left(nIndex) : csPathname.Left(nIndex + 1);
						m_fFilenameMasksAvailable = TRUE;
						m_csFilenameMasks = csPathname.Mid(nIndex + 1);
						m_fIsIncludeFilenameMasks = TRUE;
					}
				}
				return *this;
			}

		protected:
			CString					m_csPathname;
			DWORD					m_csVolumeSerialNumber;

			BOOL					m_fFilenameMasksAvailable;
			CString					m_csFilenameMasks;
			BOOL					m_fIsIncludeFilenameMasks;

			BOOL					m_fFileAttributesMaskAvailable;
			DWORD					m_dwFileAttributesMask;
			BOOL					m_fIsIncludeFileAttributesMask;
		};
		typedef CList<CIncludeSrcPath, CIncludeSrcPath&>	CIncludeSrcPaths;

		class CExcludeSrcPath
		{
		public:
			inline CExcludeSrcPath& operator =(const CExcludeSrcPath& obj)
			{
				m_csPathname = obj.m_csPathname;
				return *this;
			}

			inline CExcludeSrcPath& operator =(const CString& csPathname)
			{
				m_csPathname = csPathname;
				return *this;
			}

		protected:
			CString					m_csPathname;
		};
		typedef CList<CExcludeSrcPath, CExcludeSrcPath&>	CExcludeSrcPaths;

		class CVersionRelativePath
		{
		public:
			inline CVersionRelativePath& operator =(const CVersionRelativePath& obj)
			{
				m_csPathname = obj.m_csPathname;
				m_objCreateDateTime = obj.m_objCreateDateTime;
			}

		protected:
			CString					m_csPathname;
			CTime					m_objCreateDateTime;
		};
		typedef CList<CVersionRelativePath, CVersionRelativePath&>	CVersionRelativePaths;

	protected:
		CIncludeSrcPaths			m_objIncludeSrcPaths;
		CExcludeSrcPaths			m_objExcludeSrcPaths;
		CString						m_csBaseDestinationPath;
		CVersionRelativePaths		m_objVersionRelativePaths;
		DWORD						m_dwMaxVersions;
		CTimeSpan					m_objVersionIntervalTime;

		CString						m_csDatabasePathname;
		BOOL						m_fAbsolutePath;
		BOOL						m_fRelativeToDestinationPath;
	};

	class CLargeBufferBlockBank
	{
	public:
		class CLargeBufferPool
		{
		public:
			enum {
				BLOCK_SIZE				= 65536,
			};

			inline CLargeBufferPool() : m_pFreeBufferListHead(NULL)
			{
				InitializeCriticalSection(&m_objCriticalSection);
			}

			inline ~CLargeBufferPool()
			{
				LPVOID pPrevBlock;

				EnterCriticalSection(&m_objCriticalSection);
				while ( m_pFreeBufferListHead )
				{
					pPrevBlock = m_pFreeBufferListHead;
					m_pFreeBufferListHead = *(LPVOID*)m_pFreeBufferListHead;
					delete[] (LPBYTE)pPrevBlock;
				}
				DeleteCriticalSection(&m_objCriticalSection);
			}

			inline LPVOID Allocate()
			{
				EnterCriticalSection(&m_objCriticalSection);
				if ( m_pFreeBufferListHead )
				{
					LPVOID pBlock = m_pFreeBufferListHead;

					m_pFreeBufferListHead = *(LPVOID*)m_pFreeBufferListHead;
					LeaveCriticalSection(&m_objCriticalSection);
					return m_pFreeBufferListHead;
				}
				else
				{
					LeaveCriticalSection(&m_objCriticalSection);
					return new BYTE[BLOCK_SIZE];
				}
			}

			inline void Free(LPVOID pBlock)
			{
				if ( pBlock )
				{
					EnterCriticalSection(&m_objCriticalSection);
					*(LPVOID*)pBlock = m_pFreeBufferListHead;
					m_pFreeBufferListHead = pBlock;
					LeaveCriticalSection(&m_objCriticalSection);
				}
			}

			static inline int GetBufferSize() { return BLOCK_SIZE; }

		protected:
			LPVOID						m_pFreeBufferListHead;
			CRITICAL_SECTION			m_objCriticalSection;
		};

		inline static LPVOID Allocate() { return m_objLargeBufferPool.Allocate(); }
		inline static void Free(LPVOID pBlock) { m_objLargeBufferPool.Free(pBlock); }
		inline static int GetBlockSize() { return CLargeBufferPool::GetBufferSize(); }

	protected:
		static CLargeBufferPool			m_objLargeBufferPool;
	};

	class CMessageQueue
	{
	public:
		class CMessageBufferBlock;		//	pre-define
		class CMessageBufferPool;		//	pre-define
		class CMessage
		{
		public:
			friend class CMessageBufferBlock;
			friend class CMessageBufferPool;
			friend class CMessageQueue;

			inline CMessage() { Reset(); }

			inline void Reset()
			{
				m_nMessage = 0;
				m_wParam = NULL;
				m_lParam = NULL;
//				m_hFinishedEvent = NULL;
				m_pNext = NULL;
			}

		protected:
			//	Message
			UINT						m_nMessage;
			WPARAM						m_wParam;
			LPARAM						m_lParam;
			//	To implement SendMessage
//			HANDLE						m_hFinishedEvent;
			//	list
			CMessage*					m_pNext;
		};

		class CMessageBufferBlock
		{
		public:
			friend class CMessageBufferPool;

			inline CMessageBufferBlock() : m_pNext(NULL)
			{
				for ( int i = 0; i < sizeof(m_arrayMessageBank) / sizeof(m_arrayMessageBank[0]) - 1; i++ )
					m_arrayMessageBank[i].m_pNext = m_arrayMessageBank + i + 1;
			}

			inline ~CMessageBufferBlock()
			{
				if ( m_pNext )
					delete m_pNext;
			}

		protected:
			CMessage				m_arrayMessageBank[128];
			CMessageBufferBlock*	m_pNext;
		};

		class CMessageBufferPool
		{
		public:
			inline CMessageBufferPool() : m_pFirstMessageBlock(NULL), m_pFirstFreeMessage(NULL)
			{
				InitializeCriticalSection(&m_objCriticalSection);
			}

			inline ~CMessageBufferPool()
			{
				EnterCriticalSection(&m_objCriticalSection);
				if ( m_pFirstMessageBlock )
					delete m_pFirstMessageBlock;
				DeleteCriticalSection(&m_objCriticalSection);
			}

			inline CMessage* Allocate()
			{
				CMessage* pRet;

				EnterCriticalSection(&m_objCriticalSection);
				if ( m_pFirstFreeMessage )
				{
					pRet = m_pFirstFreeMessage;
					m_pFirstFreeMessage = m_pFirstFreeMessage->m_pNext;
				}
				else
				{
					CMessageBufferBlock* pBlock;

					LeaveCriticalSection(&m_objCriticalSection);
					pBlock = new CMessageBufferBlock;
					EnterCriticalSection(&m_objCriticalSection);
					pBlock->m_pNext = m_pFirstMessageBlock;
					m_pFirstMessageBlock = pBlock;
					pBlock->m_arrayMessageBank[sizeof(pBlock->m_arrayMessageBank) / sizeof(pBlock->m_arrayMessageBank[0]) - 1].m_pNext = m_pFirstFreeMessage;
					m_pFirstFreeMessage = pBlock->m_arrayMessageBank + 1;
					pRet = pBlock->m_arrayMessageBank;
				}
				LeaveCriticalSection(&m_objCriticalSection);
				ASSERT(pRet != NULL);
				pRet->Reset();
				return pRet;
			}

			inline void Free(CMessage* pMsg)
			{
				EnterCriticalSection(&m_objCriticalSection);
				pMsg->m_pNext = m_pFirstFreeMessage;
				m_pFirstFreeMessage = pMsg;
				LeaveCriticalSection(&m_objCriticalSection);
			}

		protected:
			CRITICAL_SECTION			m_objCriticalSection;
			CMessageBufferBlock*		m_pFirstMessageBlock;
			CMessage*					m_pFirstFreeMessage;
		};

		inline CMessageQueue() : m_pFirstQueuedMessage(NULL), m_ppLastQueuedMessage(&m_pFirstQueuedMessage)
		{
			InitializeCriticalSection(&m_objCriticalSection);
			if ( !(m_hMessageSemaphore = CreateSemaphore(NULL, 0, LONG_MAX, NULL)) )
				AfxThrowResourceException();
		}

		inline ~CMessageQueue()
		{
			EnterCriticalSection(&m_objCriticalSection);
			DeleteCriticalSection(&m_objCriticalSection);
			CloseHandle(m_hMessageSemaphore);
		}

		inline void PostMessage(UINT nMsg, WPARAM wParam, LPARAM lParam)
		{
			CMessage* pMsg = CMessageQueue::m_objMessageBufferPool.Allocate();

			pMsg->m_nMessage = nMsg;
			pMsg->m_wParam = wParam;
			pMsg->m_lParam = lParam;
			EnterCriticalSection(&m_objCriticalSection);
			*m_ppLastQueuedMessage = pMsg;
			m_ppLastQueuedMessage = &pMsg->m_pNext;
			LeaveCriticalSection(&m_objCriticalSection);
			ReleaseSemaphore(m_hMessageSemaphore, 1, NULL);
		}

//		inline void SendMessage(UINT nMsg, WPARAM wParam, LPARAM lParam) {}

		inline BOOL GetFirstMessage(UINT& nMsg, WPARAM& wParam, LPARAM& lParam)
		{
			CMessage* pMsg = NULL;

			EnterCriticalSection(&m_objCriticalSection);
			if ( (pMsg = m_pFirstQueuedMessage) )
			{
				if ( !(m_pFirstQueuedMessage = m_pFirstQueuedMessage->m_pNext) )
					m_ppLastQueuedMessage = &m_pFirstQueuedMessage;
				LeaveCriticalSection(&m_objCriticalSection);
				nMsg = pMsg->m_nMessage;
				wParam = pMsg->m_wParam;
				lParam = pMsg->m_lParam;
				CMessageQueue::m_objMessageBufferPool.Free(pMsg);
				return TRUE;
			}
			else
			{
				LeaveCriticalSection(&m_objCriticalSection);
				return FALSE;
			}
		}

		inline HANDLE GetSemaphore() const { return m_hMessageSemaphore; }

	protected:
		static CMessageBufferPool		m_objMessageBufferPool;

		CRITICAL_SECTION				m_objCriticalSection;
		CMessage*						m_pFirstQueuedMessage;
		CMessage**						m_ppLastQueuedMessage;
		HANDLE							m_hMessageSemaphore;
	};

	typedef struct
	{
		HANDLE							m_hFinishedEvent;
		DWORD							m_dwResult;
	} MESSAGE_RESULT;

	class CWorkerThreadWrapper
	{
	public:
		inline CWorkerThreadWrapper() : m_pThread(NULL) {}

		virtual BOOL InitInstance() = 0;
		virtual DWORD ExitInstance() = 0;

		inline BOOL CreateThread(AFX_THREADPROC pfnThreadProc, LPVOID lParam, int nPriority = THREAD_PRIORITY_NORMAL)
		{
/*			if ( !m_pThread )
			{
				if ( !InitInstance() )
					return FALSE;
				if ( !(m_pThread = AfxBeginThread(pfnThreadProc, lParam, nPriority)) )
					return FALSE;
			}
			return TRUE;
*/
			if ( !m_pThread )
			{
				m_pfnThreadProc = pfnThreadProc;
				m_lParam = lParam;
				if ( !InitInstance() )
					return FALSE;
				if ( !(m_pThread = AfxBeginThread((AFX_THREADPROC)DummyThreadProc, this, nPriority)) )
					return FALSE;
			}
			return TRUE;

⌨️ 快捷键说明

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