--- /dev/null
+// Win32++ Version 7.3\r
+// Released: 30th November 2011\r
+//\r
+// David Nash\r
+// url: https://sourceforge.net/projects/win32-framework\r
+//\r
+//\r
+// Copyright (c) 2005-2011 David Nash\r
+//\r
+// Permission is hereby granted, free of charge, to\r
+// any person obtaining a copy of this software and\r
+// associated documentation files (the "Software"),\r
+// to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify,\r
+// merge, publish, distribute, sublicense, and/or sell\r
+// copies of the Software, and to permit persons to whom\r
+// the Software is furnished to do so, subject to the\r
+// following conditions:\r
+//\r
+// The above copyright notice and this permission notice\r
+// shall be included in all copies or substantial portions\r
+// of the Software.\r
+//\r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF\r
+// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\r
+// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\r
+// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\r
+// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR\r
+// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
+// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE\r
+// OR OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
+////////////////////////////////////////////////////////\r
+\r
+\r
+#ifndef _WIN32XX_FILE_H_\r
+#define _WIN32XX_FILE_H_\r
+\r
+\r
+#include "wincore.h"\r
+\r
+namespace Win32xx\r
+{\r
+\r
+ class CFile\r
+ {\r
+ public:\r
+ CFile();\r
+ CFile(HANDLE hFile);\r
+ CFile(LPCTSTR pszFileName, UINT nOpenFlags);\r
+ ~CFile();\r
+ operator HANDLE() const;\r
+\r
+ BOOL Close();\r
+ BOOL Flush();\r
+ HANDLE GetHandle() const;\r
+ ULONGLONG GetLength() const;\r
+ const CString& GetFileName() const;\r
+ const CString& GetFilePath() const;\r
+ const CString& GetFileTitle() const;\r
+ ULONGLONG GetPosition() const;\r
+ BOOL LockRange(ULONGLONG Pos, ULONGLONG Count);\r
+ BOOL Open(LPCTSTR pszFileName, UINT nOpenFlags);\r
+ CString OpenFileDialog(LPCTSTR pszFilePathName = NULL,\r
+ DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszFilter = NULL,\r
+ CWnd* pOwnerWnd = NULL);\r
+ UINT Read(void* pBuf, UINT nCount);\r
+ static BOOL Remove(LPCTSTR pszFileName);\r
+ static BOOL Rename(LPCTSTR pszOldName, LPCTSTR pszNewName);\r
+ CString SaveFileDialog(LPCTSTR pszFilePathName = NULL,\r
+ DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszFilter = NULL,\r
+ LPCTSTR pszDefExt = NULL, CWnd* pOwnerWnd = NULL);\r
+ ULONGLONG Seek(LONGLONG lOff, UINT nFrom);\r
+ void SeekToBegin();\r
+ ULONGLONG SeekToEnd();\r
+ void SetFilePath(LPCTSTR pszNewName);\r
+ BOOL SetLength(ULONGLONG NewLen);\r
+ BOOL UnlockRange(ULONGLONG Pos, ULONGLONG Count);\r
+ BOOL Write(const void* pBuf, UINT nCount);\r
+\r
+ private:\r
+ CFile(const CFile&); // Disable copy construction\r
+ CFile& operator = (const CFile&); // Disable assignment operator\r
+ CString m_FileName;\r
+ CString m_FilePath;\r
+ CString m_FileTitle;\r
+ HANDLE m_hFile;\r
+ };\r
+\r
+}\r
+\r
+\r
+\r
+namespace Win32xx\r
+{\r
+ inline CFile::CFile() : m_hFile(0)\r
+ {\r
+ }\r
+\r
+ inline CFile::CFile(HANDLE hFile) : m_hFile(hFile)\r
+ {\r
+ }\r
+\r
+ inline CFile::CFile(LPCTSTR pszFileName, UINT nOpenFlags) : m_hFile(0)\r
+ {\r
+ assert(pszFileName);\r
+ Open(pszFileName, nOpenFlags);\r
+ assert(m_hFile);\r
+ }\r
+ \r
+ inline CFile::~CFile()\r
+ {\r
+ Close();\r
+ }\r
+\r
+ inline CFile::operator HANDLE() const\r
+ {\r
+ return m_hFile;\r
+ }\r
+\r
+ inline BOOL CFile::Close()\r
+ // Closes the file associated with this object. Closed file can no longer be read or written to.\r
+ {\r
+ BOOL bResult = TRUE;\r
+ if (m_hFile)\r
+ bResult = CloseHandle(m_hFile);\r
+\r
+ m_hFile = 0;\r
+ return bResult;\r
+ }\r
+\r
+ inline BOOL CFile::Flush()\r
+ // Causes any remaining data in the file buffer to be written to the file.\r
+ {\r
+ assert(m_hFile);\r
+ return FlushFileBuffers(m_hFile);\r
+ }\r
+ \r
+ inline HANDLE CFile::GetHandle() const\r
+ {\r
+ return m_hFile;\r
+ }\r
+\r
+ inline ULONGLONG CFile::GetLength( ) const\r
+ // Returns the length of the file in bytes.\r
+ {\r
+ assert(m_hFile);\r
+\r
+ LONG HighPosCur = 0;\r
+ LONG HighPosEnd = 0;\r
+\r
+ DWORD LowPosCur = SetFilePointer(m_hFile, 0, &HighPosCur, FILE_CURRENT);\r
+ DWORD LowPosEnd = SetFilePointer(m_hFile, 0, &HighPosEnd, FILE_END);\r
+ SetFilePointer(m_hFile, LowPosCur, &HighPosCur, FILE_BEGIN);\r
+\r
+ ULONGLONG Result = ((ULONGLONG)HighPosEnd << 32) + LowPosEnd;\r
+ return Result;\r
+ }\r
+\r
+ inline const CString& CFile::GetFileName() const\r
+ // Returns the filename of the file associated with this object.\r
+ {\r
+ return (const CString&)m_FileName;\r
+ }\r
+\r
+ inline const CString& CFile::GetFilePath() const\r
+ // Returns the full filename including the directory of the file associated with this object.\r
+ {\r
+ return (const CString&)m_FilePath;\r
+ }\r
+\r
+ inline const CString& CFile::GetFileTitle() const\r
+ // Returns the filename of the file associated with this object, excluding the path and the file extension\r
+ {\r
+ return (const CString&)m_FileTitle;\r
+ }\r
+\r
+ inline ULONGLONG CFile::GetPosition() const\r
+ // Returns the current value of the file pointer, which can be used in subsequent calls to Seek.\r
+ {\r
+ assert(m_hFile);\r
+ LONG High = 0;\r
+ DWORD LowPos = SetFilePointer(m_hFile, 0, &High, FILE_CURRENT);\r
+\r
+ ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;\r
+ return Result;\r
+ }\r
+\r
+ inline BOOL CFile::LockRange(ULONGLONG Pos, ULONGLONG Count)\r
+ // Locks a range of bytes in and open file.\r
+ {\r
+ assert(m_hFile);\r
+\r
+ DWORD dwPosHigh = (DWORD)(Pos >> 32);\r
+ DWORD dwPosLow = (DWORD)(Pos & 0xFFFFFFFF);\r
+ DWORD dwCountHigh = (DWORD)(Count >> 32);\r
+ DWORD dwCountLow = (DWORD)(Count & 0xFFFFFFFF);\r
+\r
+ return ::LockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);\r
+ }\r
+\r
+ inline BOOL CFile::Open(LPCTSTR pszFileName, UINT nOpenFlags)\r
+ // Prepares a file to be written to or read from.\r
+ {\r
+ if (m_hFile) Close();\r
+\r
+ m_hFile = ::CreateFile(pszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, nOpenFlags, FILE_ATTRIBUTE_NORMAL, NULL);\r
+\r
+ if (INVALID_HANDLE_VALUE == m_hFile)\r
+ {\r
+ TRACE(_T("Failed\n"));\r
+ m_hFile = 0;\r
+ }\r
+\r
+ if (m_hFile)\r
+ {\r
+ SetFilePath(pszFileName);\r
+ }\r
+\r
+ return (m_hFile != 0);\r
+ }\r
+\r
+ inline CString CFile::OpenFileDialog(LPCTSTR pszFilePathName, DWORD dwFlags, LPCTSTR pszFilter, CWnd* pOwnerWnd)\r
+ // Displays the file open dialog. \r
+ // Returns a CString containing either the selected file name or an empty CString.\r
+ {\r
+ CString str;\r
+ if (pszFilePathName)\r
+ str = pszFilePathName;\r
+\r
+ OPENFILENAME ofn = {0};\r
+ ofn.lStructSize = sizeof(OPENFILENAME);\r
+\r
+#if defined OPENFILENAME_SIZE_VERSION_400\r
+ if (GetWinVersion() < 2500)\r
+ ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;\r
+#endif\r
+\r
+ ofn.hwndOwner = pOwnerWnd? pOwnerWnd->GetHwnd() : NULL;\r
+ ofn.hInstance = GetApp()->GetInstanceHandle();\r
+ ofn.lpstrFilter = pszFilter;\r
+ ofn.lpstrTitle = _T("Open File");\r
+ ofn.Flags = dwFlags;\r
+ ofn.nMaxFile = _MAX_PATH;\r
+ \r
+ ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);\r
+ ::GetOpenFileName(&ofn);\r
+ str.ReleaseBuffer();\r
+\r
+ return str;\r
+ }\r
+\r
+ inline UINT CFile::Read(void* pBuf, UINT nCount)\r
+ // Reads from the file, storing the contents in the specified buffer.\r
+ {\r
+ assert(m_hFile);\r
+ DWORD dwRead = 0;\r
+\r
+ if (!::ReadFile(m_hFile, pBuf, nCount, &dwRead, NULL))\r
+ dwRead = 0;\r
+\r
+ return dwRead;\r
+ }\r
+\r
+ inline BOOL CFile::Rename(LPCTSTR pszOldName, LPCTSTR pszNewName)\r
+ // Renames the specified file.\r
+ {\r
+ return ::MoveFile(pszOldName, pszNewName);\r
+ }\r
+\r
+ inline BOOL CFile::Remove(LPCTSTR pszFileName)\r
+ // Deletes the specified file.\r
+ {\r
+ return ::DeleteFile(pszFileName);\r
+ }\r
+\r
+ inline CString CFile::SaveFileDialog(LPCTSTR pszFilePathName, DWORD dwFlags, LPCTSTR pszFilter, LPCTSTR pszDefExt, CWnd* pOwnerWnd)\r
+ // Displays the SaveFileDialog.\r
+ // Returns a CString containing either the selected file name or an empty CString\r
+ {\r
+ CString str;\r
+ if (pszFilePathName)\r
+ str = pszFilePathName;\r
+\r
+ OPENFILENAME ofn = {0};\r
+ ofn.lStructSize = sizeof(OPENFILENAME);\r
+\r
+#if defined OPENFILENAME_SIZE_VERSION_400\r
+ if (GetWinVersion() < 2500)\r
+ ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;\r
+#endif\r
+\r
+ ofn.hwndOwner = pOwnerWnd? pOwnerWnd->GetHwnd() : NULL;\r
+ ofn.hInstance = GetApp()->GetInstanceHandle();\r
+ ofn.lpstrFilter = pszFilter;\r
+ ofn.lpstrFile = (LPTSTR)pszFilePathName;\r
+ ofn.lpstrFileTitle = (LPTSTR)pszFilePathName;\r
+ ofn.lpstrDefExt = pszDefExt;\r
+ ofn.nMaxFile = lstrlen(pszFilePathName);\r
+ ofn.lpstrTitle = _T("Save File");\r
+ ofn.Flags = dwFlags;\r
+ ofn.nMaxFile = _MAX_PATH;\r
+ ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);\r
+ ::GetSaveFileName(&ofn);\r
+ str.ReleaseBuffer();\r
+\r
+ return str;\r
+ }\r
+\r
+ inline ULONGLONG CFile::Seek(LONGLONG lOff, UINT nFrom)\r
+ // Positions the current file pointer.\r
+ // Permitted values for nFrom are: FILE_BEGIN, FILE_CURRENT, or FILE_END.\r
+ {\r
+ assert(m_hFile);\r
+ assert(nFrom == FILE_BEGIN || nFrom == FILE_CURRENT || nFrom == FILE_END);\r
+\r
+ LONG High = LONG(lOff >> 32);\r
+ LONG Low = (LONG)(lOff & 0xFFFFFFFF);\r
+\r
+ DWORD LowPos = SetFilePointer(m_hFile, Low, &High, nFrom);\r
+\r
+ ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;\r
+ return Result;\r
+ }\r
+\r
+ inline void CFile::SeekToBegin()\r
+ // Sets the current file pointer to the beginning of the file.\r
+ {\r
+ assert(m_hFile);\r
+ Seek(0, FILE_BEGIN);\r
+ }\r
+\r
+ inline ULONGLONG CFile::SeekToEnd()\r
+ // Sets the current file pointer to the end of the file.\r
+ {\r
+ assert(m_hFile);\r
+ return Seek(0, FILE_END);\r
+ }\r
+\r
+ inline void CFile::SetFilePath(LPCTSTR pszFileName)\r
+ // Specifies the full file name, including its path\r
+ {\r
+ TCHAR* pFileName = NULL;\r
+ int nBuffSize = ::GetFullPathName(pszFileName, 0, 0, 0);\r
+ if (nBuffSize > 0)\r
+ {\r
+ TCHAR* pBuff = m_FilePath.GetBuffer(nBuffSize);\r
+ ::GetFullPathName(pszFileName, nBuffSize, pBuff, &pFileName);\r
+ m_FilePath.ReleaseBuffer();\r
+ m_FileName = pFileName;\r
+ int nPos = m_FileName.ReverseFind(_T("."));\r
+ if (nPos >= 0)\r
+ m_FileTitle = m_FileName.Left(nPos);\r
+ }\r
+ }\r
+\r
+ inline BOOL CFile::SetLength(ULONGLONG NewLen)\r
+ // Changes the length of the file to the specified value.\r
+ {\r
+ assert(m_hFile);\r
+\r
+ Seek(NewLen, FILE_BEGIN);\r
+ return ::SetEndOfFile(m_hFile);\r
+ }\r
+\r
+ inline BOOL CFile::UnlockRange(ULONGLONG Pos, ULONGLONG Count)\r
+ // Unlocks a range of bytes in an open file.\r
+ {\r
+ assert(m_hFile);\r
+\r
+ DWORD dwPosHigh = (DWORD)(Pos >> 32);\r
+ DWORD dwPosLow = (DWORD)(Pos & 0xFFFFFFFF);\r
+ DWORD dwCountHigh = (DWORD)(Count >> 32);\r
+ DWORD dwCountLow = (DWORD)(Count & 0xFFFFFFFF);\r
+\r
+ return ::UnlockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);\r
+ }\r
+\r
+ inline BOOL CFile::Write(const void* pBuf, UINT nCount)\r
+ // Writes the specified buffer to the file.\r
+ {\r
+ assert(m_hFile);\r
+ DWORD dwWritten = 0;\r
+ BOOL bResult = ::WriteFile(m_hFile, pBuf, nCount, &dwWritten, NULL);\r
+ if (dwWritten != nCount)\r
+ bResult = FALSE;\r
+\r
+ return bResult;\r
+ }\r
+\r
+\r
+} // namespace Win32xx\r
+\r
+#endif\r