Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / include / file.h
1 // Win32++   Version 7.3\r
2 // Released: 30th November 2011\r
3 //\r
4 //      David Nash\r
5 //      email: [email protected]\r
6 //      url: https://sourceforge.net/projects/win32-framework\r
7 //\r
8 //\r
9 // Copyright (c) 2005-2011  David Nash\r
10 //\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
20 //\r
21 // The above copyright notice and this permission notice\r
22 // shall be included in all copies or substantial portions\r
23 // of the Software.\r
24 //\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
34 //\r
35 ////////////////////////////////////////////////////////\r
36 \r
37 \r
38 #ifndef _WIN32XX_FILE_H_\r
39 #define _WIN32XX_FILE_H_\r
40 \r
41 \r
42 #include "wincore.h"\r
43 \r
44 namespace Win32xx\r
45 {\r
46 \r
47         class CFile\r
48         {\r
49         public:\r
50                 CFile();\r
51                 CFile(HANDLE hFile);\r
52                 CFile(LPCTSTR pszFileName, UINT nOpenFlags);\r
53                 ~CFile();\r
54                 operator HANDLE() const;\r
55 \r
56                 BOOL Close();\r
57                 BOOL Flush();\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
76                 void SeekToBegin();\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
82 \r
83         private:\r
84                 CFile(const CFile&);                            // Disable copy construction\r
85                 CFile& operator = (const CFile&);       // Disable assignment operator\r
86                 CString m_FileName;\r
87                 CString m_FilePath;\r
88                 CString m_FileTitle;\r
89                 HANDLE m_hFile;\r
90         };\r
91 \r
92 }\r
93 \r
94 \r
95 \r
96 namespace Win32xx\r
97 {\r
98         inline CFile::CFile() : m_hFile(0)\r
99         {\r
100         }\r
101 \r
102         inline CFile::CFile(HANDLE hFile) : m_hFile(hFile)\r
103         {\r
104         }\r
105 \r
106         inline CFile::CFile(LPCTSTR pszFileName, UINT nOpenFlags) : m_hFile(0)\r
107         {\r
108                 assert(pszFileName);\r
109                 Open(pszFileName, nOpenFlags);\r
110                 assert(m_hFile);\r
111         }\r
112         \r
113         inline CFile::~CFile()\r
114         {\r
115                 Close();\r
116         }\r
117 \r
118         inline CFile::operator HANDLE() const\r
119         {\r
120                 return m_hFile;\r
121         }\r
122 \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
125         {\r
126                 BOOL bResult = TRUE;\r
127                 if (m_hFile)\r
128                         bResult = CloseHandle(m_hFile);\r
129 \r
130                 m_hFile = 0;\r
131                 return bResult;\r
132         }\r
133 \r
134         inline BOOL CFile::Flush()\r
135         // Causes any remaining data in the file buffer to be written to the file.\r
136         {\r
137                 assert(m_hFile);\r
138                 return FlushFileBuffers(m_hFile);\r
139         }\r
140         \r
141         inline HANDLE CFile::GetHandle() const\r
142         {\r
143                 return m_hFile;\r
144         }\r
145 \r
146         inline ULONGLONG CFile::GetLength( ) const\r
147         // Returns the length of the file in bytes.\r
148         {\r
149                 assert(m_hFile);\r
150 \r
151                 LONG HighPosCur = 0;\r
152                 LONG HighPosEnd = 0;\r
153 \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
157 \r
158                 ULONGLONG Result = ((ULONGLONG)HighPosEnd << 32) + LowPosEnd;\r
159                 return Result;\r
160         }\r
161 \r
162         inline const CString& CFile::GetFileName() const\r
163         // Returns the filename of the file associated with this object.\r
164         {\r
165                 return (const CString&)m_FileName;\r
166         }\r
167 \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
170         {\r
171                 return (const CString&)m_FilePath;\r
172         }\r
173 \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
176         {\r
177                 return (const CString&)m_FileTitle;\r
178         }\r
179 \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
182         {\r
183                 assert(m_hFile);\r
184                 LONG High = 0;\r
185                 DWORD LowPos = SetFilePointer(m_hFile, 0, &High, FILE_CURRENT);\r
186 \r
187                 ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;\r
188                 return Result;\r
189         }\r
190 \r
191         inline BOOL CFile::LockRange(ULONGLONG Pos, ULONGLONG Count)\r
192         // Locks a range of bytes in and open file.\r
193         {\r
194                 assert(m_hFile);\r
195 \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
200 \r
201                 return ::LockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);\r
202         }\r
203 \r
204         inline BOOL CFile::Open(LPCTSTR pszFileName, UINT nOpenFlags)\r
205         // Prepares a file to be written to or read from.\r
206         {\r
207                 if (m_hFile) Close();\r
208 \r
209                 m_hFile = ::CreateFile(pszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, nOpenFlags, FILE_ATTRIBUTE_NORMAL, NULL);\r
210 \r
211                 if (INVALID_HANDLE_VALUE == m_hFile)\r
212                 {\r
213                         TRACE(_T("Failed\n"));\r
214                         m_hFile = 0;\r
215                 }\r
216 \r
217                 if (m_hFile)\r
218                 {\r
219                         SetFilePath(pszFileName);\r
220                 }\r
221 \r
222                 return (m_hFile != 0);\r
223         }\r
224 \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
228         {\r
229                 CString str;\r
230                 if (pszFilePathName)\r
231                         str = pszFilePathName;\r
232 \r
233                 OPENFILENAME ofn = {0};\r
234                 ofn.lStructSize = sizeof(OPENFILENAME);\r
235 \r
236 #if defined OPENFILENAME_SIZE_VERSION_400\r
237                 if (GetWinVersion() < 2500)\r
238                         ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;\r
239 #endif\r
240 \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
247                 \r
248                 ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);\r
249                 ::GetOpenFileName(&ofn);\r
250                 str.ReleaseBuffer();\r
251 \r
252                 return str;\r
253         }\r
254 \r
255         inline UINT CFile::Read(void* pBuf, UINT nCount)\r
256         // Reads from the file, storing the contents in the specified buffer.\r
257         {\r
258                 assert(m_hFile);\r
259                 DWORD dwRead = 0;\r
260 \r
261                 if (!::ReadFile(m_hFile, pBuf, nCount, &dwRead, NULL))\r
262                         dwRead = 0;\r
263 \r
264                 return dwRead;\r
265         }\r
266 \r
267         inline BOOL CFile::Rename(LPCTSTR pszOldName, LPCTSTR pszNewName)\r
268         // Renames the specified file.\r
269         {\r
270                 return ::MoveFile(pszOldName, pszNewName);\r
271         }\r
272 \r
273         inline BOOL CFile::Remove(LPCTSTR pszFileName)\r
274         // Deletes the specified file.\r
275         {\r
276                 return ::DeleteFile(pszFileName);\r
277         }\r
278 \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
282         {\r
283                 CString str;\r
284                 if (pszFilePathName)\r
285                         str = pszFilePathName;\r
286 \r
287                 OPENFILENAME ofn = {0};\r
288                 ofn.lStructSize = sizeof(OPENFILENAME);\r
289 \r
290 #if defined OPENFILENAME_SIZE_VERSION_400\r
291                 if (GetWinVersion() < 2500)\r
292                         ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;\r
293 #endif\r
294 \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
308 \r
309                 return str;\r
310         }\r
311 \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
315         {\r
316                 assert(m_hFile);\r
317                 assert(nFrom == FILE_BEGIN || nFrom == FILE_CURRENT || nFrom == FILE_END);\r
318 \r
319                 LONG High = LONG(lOff >> 32);\r
320                 LONG Low = (LONG)(lOff & 0xFFFFFFFF);\r
321 \r
322                 DWORD LowPos = SetFilePointer(m_hFile, Low, &High, nFrom);\r
323 \r
324                 ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;\r
325                 return Result;\r
326         }\r
327 \r
328         inline void CFile::SeekToBegin()\r
329         // Sets the current file pointer to the beginning of the file.\r
330         {\r
331                 assert(m_hFile);\r
332                 Seek(0, FILE_BEGIN);\r
333         }\r
334 \r
335         inline ULONGLONG CFile::SeekToEnd()\r
336         // Sets the current file pointer to the end of the file.\r
337         {\r
338                 assert(m_hFile);\r
339                 return Seek(0, FILE_END);\r
340         }\r
341 \r
342         inline void CFile::SetFilePath(LPCTSTR pszFileName)\r
343         // Specifies the full file name, including its path\r
344         {\r
345                 TCHAR* pFileName = NULL;\r
346                 int nBuffSize = ::GetFullPathName(pszFileName, 0, 0, 0);\r
347                 if (nBuffSize > 0)\r
348                 {\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
354                         if (nPos >= 0)\r
355                                 m_FileTitle = m_FileName.Left(nPos);\r
356                 }\r
357         }\r
358 \r
359         inline BOOL CFile::SetLength(ULONGLONG NewLen)\r
360         // Changes the length of the file to the specified value.\r
361         {\r
362                 assert(m_hFile);\r
363 \r
364                 Seek(NewLen, FILE_BEGIN);\r
365                 return ::SetEndOfFile(m_hFile);\r
366         }\r
367 \r
368         inline BOOL CFile::UnlockRange(ULONGLONG Pos, ULONGLONG Count)\r
369         // Unlocks a range of bytes in an open file.\r
370         {\r
371                 assert(m_hFile);\r
372 \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
377 \r
378                 return ::UnlockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);\r
379         }\r
380 \r
381         inline BOOL CFile::Write(const void* pBuf, UINT nCount)\r
382         // Writes the specified buffer to the file.\r
383         {\r
384                 assert(m_hFile);\r
385                 DWORD dwWritten = 0;\r
386                 BOOL bResult = ::WriteFile(m_hFile, pBuf, nCount, &dwWritten, NULL);\r
387                 if (dwWritten != nCount)\r
388                         bResult = FALSE;\r
389 \r
390                 return bResult;\r
391         }\r
392 \r
393 \r
394 }       // namespace Win32xx\r
395 \r
396 #endif\r

UCC git Repository :: git.ucc.asn.au