1 // Win32++ Version 7.3
\r
2 // Released: 30th November 2011
\r
6 // url: https://sourceforge.net/projects/win32-framework
\r
9 // Copyright (c) 2005-2011 David Nash
\r
11 // Permission is hereby granted, free of charge, to
\r
12 // any person obtaining a copy of this software and
\r
13 // associated documentation files (the "Software"),
\r
14 // to deal in the Software without restriction, including
\r
15 // without limitation the rights to use, copy, modify,
\r
16 // merge, publish, distribute, sublicense, and/or sell
\r
17 // copies of the Software, and to permit persons to whom
\r
18 // the Software is furnished to do so, subject to the
\r
19 // following conditions:
\r
21 // The above copyright notice and this permission notice
\r
22 // shall be included in all copies or substantial portions
\r
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
\r
26 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
\r
27 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
\r
28 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
\r
29 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
\r
30 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
\r
31 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
32 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
\r
33 // OR OTHER DEALINGS IN THE SOFTWARE.
\r
35 ////////////////////////////////////////////////////////
\r
38 #ifndef _WIN32XX_FILE_H_
\r
39 #define _WIN32XX_FILE_H_
\r
42 #include "wincore.h"
\r
51 CFile(HANDLE hFile);
\r
52 CFile(LPCTSTR pszFileName, UINT nOpenFlags);
\r
54 operator HANDLE() const;
\r
58 HANDLE GetHandle() const;
\r
59 ULONGLONG GetLength() const;
\r
60 const CString& GetFileName() const;
\r
61 const CString& GetFilePath() const;
\r
62 const CString& GetFileTitle() const;
\r
63 ULONGLONG GetPosition() const;
\r
64 BOOL LockRange(ULONGLONG Pos, ULONGLONG Count);
\r
65 BOOL Open(LPCTSTR pszFileName, UINT nOpenFlags);
\r
66 CString OpenFileDialog(LPCTSTR pszFilePathName = NULL,
\r
67 DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszFilter = NULL,
\r
68 CWnd* pOwnerWnd = NULL);
\r
69 UINT Read(void* pBuf, UINT nCount);
\r
70 static BOOL Remove(LPCTSTR pszFileName);
\r
71 static BOOL Rename(LPCTSTR pszOldName, LPCTSTR pszNewName);
\r
72 CString SaveFileDialog(LPCTSTR pszFilePathName = NULL,
\r
73 DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszFilter = NULL,
\r
74 LPCTSTR pszDefExt = NULL, CWnd* pOwnerWnd = NULL);
\r
75 ULONGLONG Seek(LONGLONG lOff, UINT nFrom);
\r
77 ULONGLONG SeekToEnd();
\r
78 void SetFilePath(LPCTSTR pszNewName);
\r
79 BOOL SetLength(ULONGLONG NewLen);
\r
80 BOOL UnlockRange(ULONGLONG Pos, ULONGLONG Count);
\r
81 BOOL Write(const void* pBuf, UINT nCount);
\r
84 CFile(const CFile&); // Disable copy construction
\r
85 CFile& operator = (const CFile&); // Disable assignment operator
\r
88 CString m_FileTitle;
\r
98 inline CFile::CFile() : m_hFile(0)
\r
102 inline CFile::CFile(HANDLE hFile) : m_hFile(hFile)
\r
106 inline CFile::CFile(LPCTSTR pszFileName, UINT nOpenFlags) : m_hFile(0)
\r
108 assert(pszFileName);
\r
109 Open(pszFileName, nOpenFlags);
\r
113 inline CFile::~CFile()
\r
118 inline CFile::operator HANDLE() const
\r
123 inline BOOL CFile::Close()
\r
124 // Closes the file associated with this object. Closed file can no longer be read or written to.
\r
126 BOOL bResult = TRUE;
\r
128 bResult = CloseHandle(m_hFile);
\r
134 inline BOOL CFile::Flush()
\r
135 // Causes any remaining data in the file buffer to be written to the file.
\r
138 return FlushFileBuffers(m_hFile);
\r
141 inline HANDLE CFile::GetHandle() const
\r
146 inline ULONGLONG CFile::GetLength( ) const
\r
147 // Returns the length of the file in bytes.
\r
151 LONG HighPosCur = 0;
\r
152 LONG HighPosEnd = 0;
\r
154 DWORD LowPosCur = SetFilePointer(m_hFile, 0, &HighPosCur, FILE_CURRENT);
\r
155 DWORD LowPosEnd = SetFilePointer(m_hFile, 0, &HighPosEnd, FILE_END);
\r
156 SetFilePointer(m_hFile, LowPosCur, &HighPosCur, FILE_BEGIN);
\r
158 ULONGLONG Result = ((ULONGLONG)HighPosEnd << 32) + LowPosEnd;
\r
162 inline const CString& CFile::GetFileName() const
\r
163 // Returns the filename of the file associated with this object.
\r
165 return (const CString&)m_FileName;
\r
168 inline const CString& CFile::GetFilePath() const
\r
169 // Returns the full filename including the directory of the file associated with this object.
\r
171 return (const CString&)m_FilePath;
\r
174 inline const CString& CFile::GetFileTitle() const
\r
175 // Returns the filename of the file associated with this object, excluding the path and the file extension
\r
177 return (const CString&)m_FileTitle;
\r
180 inline ULONGLONG CFile::GetPosition() const
\r
181 // Returns the current value of the file pointer, which can be used in subsequent calls to Seek.
\r
185 DWORD LowPos = SetFilePointer(m_hFile, 0, &High, FILE_CURRENT);
\r
187 ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;
\r
191 inline BOOL CFile::LockRange(ULONGLONG Pos, ULONGLONG Count)
\r
192 // Locks a range of bytes in and open file.
\r
196 DWORD dwPosHigh = (DWORD)(Pos >> 32);
\r
197 DWORD dwPosLow = (DWORD)(Pos & 0xFFFFFFFF);
\r
198 DWORD dwCountHigh = (DWORD)(Count >> 32);
\r
199 DWORD dwCountLow = (DWORD)(Count & 0xFFFFFFFF);
\r
201 return ::LockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);
\r
204 inline BOOL CFile::Open(LPCTSTR pszFileName, UINT nOpenFlags)
\r
205 // Prepares a file to be written to or read from.
\r
207 if (m_hFile) Close();
\r
209 m_hFile = ::CreateFile(pszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, nOpenFlags, FILE_ATTRIBUTE_NORMAL, NULL);
\r
211 if (INVALID_HANDLE_VALUE == m_hFile)
\r
213 TRACE(_T("Failed\n"));
\r
219 SetFilePath(pszFileName);
\r
222 return (m_hFile != 0);
\r
225 inline CString CFile::OpenFileDialog(LPCTSTR pszFilePathName, DWORD dwFlags, LPCTSTR pszFilter, CWnd* pOwnerWnd)
\r
226 // Displays the file open dialog.
\r
227 // Returns a CString containing either the selected file name or an empty CString.
\r
230 if (pszFilePathName)
\r
231 str = pszFilePathName;
\r
233 OPENFILENAME ofn = {0};
\r
234 ofn.lStructSize = sizeof(OPENFILENAME);
\r
236 #if defined OPENFILENAME_SIZE_VERSION_400
\r
237 if (GetWinVersion() < 2500)
\r
238 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
\r
241 ofn.hwndOwner = pOwnerWnd? pOwnerWnd->GetHwnd() : NULL;
\r
242 ofn.hInstance = GetApp()->GetInstanceHandle();
\r
243 ofn.lpstrFilter = pszFilter;
\r
244 ofn.lpstrTitle = _T("Open File");
\r
245 ofn.Flags = dwFlags;
\r
246 ofn.nMaxFile = _MAX_PATH;
\r
248 ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);
\r
249 ::GetOpenFileName(&ofn);
\r
250 str.ReleaseBuffer();
\r
255 inline UINT CFile::Read(void* pBuf, UINT nCount)
\r
256 // Reads from the file, storing the contents in the specified buffer.
\r
261 if (!::ReadFile(m_hFile, pBuf, nCount, &dwRead, NULL))
\r
267 inline BOOL CFile::Rename(LPCTSTR pszOldName, LPCTSTR pszNewName)
\r
268 // Renames the specified file.
\r
270 return ::MoveFile(pszOldName, pszNewName);
\r
273 inline BOOL CFile::Remove(LPCTSTR pszFileName)
\r
274 // Deletes the specified file.
\r
276 return ::DeleteFile(pszFileName);
\r
279 inline CString CFile::SaveFileDialog(LPCTSTR pszFilePathName, DWORD dwFlags, LPCTSTR pszFilter, LPCTSTR pszDefExt, CWnd* pOwnerWnd)
\r
280 // Displays the SaveFileDialog.
\r
281 // Returns a CString containing either the selected file name or an empty CString
\r
284 if (pszFilePathName)
\r
285 str = pszFilePathName;
\r
287 OPENFILENAME ofn = {0};
\r
288 ofn.lStructSize = sizeof(OPENFILENAME);
\r
290 #if defined OPENFILENAME_SIZE_VERSION_400
\r
291 if (GetWinVersion() < 2500)
\r
292 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
\r
295 ofn.hwndOwner = pOwnerWnd? pOwnerWnd->GetHwnd() : NULL;
\r
296 ofn.hInstance = GetApp()->GetInstanceHandle();
\r
297 ofn.lpstrFilter = pszFilter;
\r
298 ofn.lpstrFile = (LPTSTR)pszFilePathName;
\r
299 ofn.lpstrFileTitle = (LPTSTR)pszFilePathName;
\r
300 ofn.lpstrDefExt = pszDefExt;
\r
301 ofn.nMaxFile = lstrlen(pszFilePathName);
\r
302 ofn.lpstrTitle = _T("Save File");
\r
303 ofn.Flags = dwFlags;
\r
304 ofn.nMaxFile = _MAX_PATH;
\r
305 ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);
\r
306 ::GetSaveFileName(&ofn);
\r
307 str.ReleaseBuffer();
\r
312 inline ULONGLONG CFile::Seek(LONGLONG lOff, UINT nFrom)
\r
313 // Positions the current file pointer.
\r
314 // Permitted values for nFrom are: FILE_BEGIN, FILE_CURRENT, or FILE_END.
\r
317 assert(nFrom == FILE_BEGIN || nFrom == FILE_CURRENT || nFrom == FILE_END);
\r
319 LONG High = LONG(lOff >> 32);
\r
320 LONG Low = (LONG)(lOff & 0xFFFFFFFF);
\r
322 DWORD LowPos = SetFilePointer(m_hFile, Low, &High, nFrom);
\r
324 ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;
\r
328 inline void CFile::SeekToBegin()
\r
329 // Sets the current file pointer to the beginning of the file.
\r
332 Seek(0, FILE_BEGIN);
\r
335 inline ULONGLONG CFile::SeekToEnd()
\r
336 // Sets the current file pointer to the end of the file.
\r
339 return Seek(0, FILE_END);
\r
342 inline void CFile::SetFilePath(LPCTSTR pszFileName)
\r
343 // Specifies the full file name, including its path
\r
345 TCHAR* pFileName = NULL;
\r
346 int nBuffSize = ::GetFullPathName(pszFileName, 0, 0, 0);
\r
349 TCHAR* pBuff = m_FilePath.GetBuffer(nBuffSize);
\r
350 ::GetFullPathName(pszFileName, nBuffSize, pBuff, &pFileName);
\r
351 m_FilePath.ReleaseBuffer();
\r
352 m_FileName = pFileName;
\r
353 int nPos = m_FileName.ReverseFind(_T("."));
\r
355 m_FileTitle = m_FileName.Left(nPos);
\r
359 inline BOOL CFile::SetLength(ULONGLONG NewLen)
\r
360 // Changes the length of the file to the specified value.
\r
364 Seek(NewLen, FILE_BEGIN);
\r
365 return ::SetEndOfFile(m_hFile);
\r
368 inline BOOL CFile::UnlockRange(ULONGLONG Pos, ULONGLONG Count)
\r
369 // Unlocks a range of bytes in an open file.
\r
373 DWORD dwPosHigh = (DWORD)(Pos >> 32);
\r
374 DWORD dwPosLow = (DWORD)(Pos & 0xFFFFFFFF);
\r
375 DWORD dwCountHigh = (DWORD)(Count >> 32);
\r
376 DWORD dwCountLow = (DWORD)(Count & 0xFFFFFFFF);
\r
378 return ::UnlockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);
\r
381 inline BOOL CFile::Write(const void* pBuf, UINT nCount)
\r
382 // Writes the specified buffer to the file.
\r
385 DWORD dwWritten = 0;
\r
386 BOOL bResult = ::WriteFile(m_hFile, pBuf, nCount, &dwWritten, NULL);
\r
387 if (dwWritten != nCount)
\r
394 } // namespace Win32xx
\r