summaryrefslogtreecommitdiffstats
path: root/mmc_updater/depends/win32cpp/file.h
blob: 4316dc94f12ae45c1697dfa3134db9351f7baba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Win32++   Version 7.2
// Released: 5th AUgust 2011
//
//      David Nash
//      email: dnash@bigpond.net.au
//      url: https://sourceforge.net/projects/win32-framework
//
//
// Copyright (c) 2005-2011  David Nash
//
// Permission is hereby granted, free of charge, to
// any person obtaining a copy of this software and
// associated documentation files (the "Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom
// the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
//
////////////////////////////////////////////////////////


#ifndef _WIN32XX_FILE_H_
#define _WIN32XX_FILE_H_


#include "wincore.h"

namespace Win32xx
{

	class CFile
	{
	public:
		CFile();
		CFile(HANDLE hFile);
		CFile(LPCTSTR pszFileName, UINT nOpenFlags);
		~CFile();
		operator HANDLE() const;

		BOOL Close();
		BOOL Flush();
		HANDLE GetHandle() const;
		ULONGLONG GetLength() const;
		const CString& GetFileName() const;
		const CString& GetFilePath() const;
		const CString& GetFileTitle() const;
		ULONGLONG GetPosition() const;
		BOOL LockRange(ULONGLONG Pos, ULONGLONG Count);
		BOOL Open(LPCTSTR pszFileName, UINT nOpenFlags);
		CString OpenFileDialog(LPCTSTR pszFilePathName = NULL,
						DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszFilter = NULL,
						CWnd* pOwnerWnd = NULL);
		UINT Read(void* pBuf, UINT nCount);
		static BOOL Remove(LPCTSTR pszFileName);
		static BOOL Rename(LPCTSTR pszOldName, LPCTSTR pszNewName);
		CString SaveFileDialog(LPCTSTR pszFilePathName = NULL,
						DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR pszFilter = NULL,
						LPCTSTR pszDefExt = NULL, CWnd* pOwnerWnd = NULL);
		ULONGLONG Seek(LONGLONG lOff, UINT nFrom);
		void SeekToBegin();
		ULONGLONG SeekToEnd();
		void SetFilePath(LPCTSTR pszNewName);
		BOOL SetLength(ULONGLONG NewLen);
		BOOL UnlockRange(ULONGLONG Pos, ULONGLONG Count);
		BOOL Write(const void* pBuf, UINT nCount);

	private:
		CFile(const CFile&);				// Disable copy construction
		CFile& operator = (const CFile&);	// Disable assignment operator
		CString m_FileName;
		CString m_FilePath;
		CString m_FileTitle;
		HANDLE m_hFile;
	};

}



namespace Win32xx
{
	inline CFile::CFile() : m_hFile(0)
	{
	}

	inline CFile::CFile(HANDLE hFile) : m_hFile(hFile)
	{
	}

	inline CFile::CFile(LPCTSTR pszFileName, UINT nOpenFlags) : m_hFile(0)
	{
		assert(pszFileName);
		Open(pszFileName, nOpenFlags);
		assert(m_hFile);
	}
	
	inline CFile::~CFile()
	{
		Close();
	}

	inline CFile::operator HANDLE() const
	{
		return m_hFile;
	}

	inline BOOL CFile::Close()
	// Closes the file associated with this object. Closed file can no longer be read or written to.
	{
		BOOL bResult = TRUE;
		if (m_hFile)
			bResult = CloseHandle(m_hFile);

		m_hFile = 0;
		return bResult;
	}

	inline BOOL CFile::Flush()
	// Causes any remaining data in the file buffer to be written to the file.
	{
		assert(m_hFile);
		return FlushFileBuffers(m_hFile);
	}
	
	inline HANDLE CFile::GetHandle() const
	{
		return m_hFile;
	}

	inline ULONGLONG CFile::GetLength( ) const
	// Returns the length of the file in bytes.
	{
		assert(m_hFile);

		LONG High = 0;
		DWORD LowPos = SetFilePointer(m_hFile, 0, &High, FILE_END);

		ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;
		return Result;
	}

	inline const CString& CFile::GetFileName() const
	// Returns the filename of the file associated with this object.
	{
		return (const CString&)m_FileName;
	}

	inline const CString& CFile::GetFilePath() const
	// Returns the full filename including the directory of the file associated with this object.
	{
		return (const CString&)m_FilePath;
	}

	inline const CString& CFile::GetFileTitle() const
	// Returns the filename of the file associated with this object, excluding the path and the file extension
	{
		return (const CString&)m_FileTitle;
	}

	inline ULONGLONG CFile::GetPosition() const
	// Returns the current value of the file pointer, which can be used in subsequent calls to Seek.
	{
		assert(m_hFile);
		LONG High = 0;
		DWORD LowPos = SetFilePointer(m_hFile, 0, &High, FILE_CURRENT);

		ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;
		return Result;
	}

	inline BOOL CFile::LockRange(ULONGLONG Pos, ULONGLONG Count)
	// Locks a range of bytes in and open file.
	{
		assert(m_hFile);

		DWORD dwPosHigh = (DWORD)(Pos >> 32);
		DWORD dwPosLow = (DWORD)(Pos & 0xFFFFFFFF);
		DWORD dwCountHigh = (DWORD)(Count >> 32);
		DWORD dwCountLow = (DWORD)(Count & 0xFFFFFFFF);

		return ::LockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);
	}

	inline BOOL CFile::Open(LPCTSTR pszFileName, UINT nOpenFlags)
	// Prepares a file to be written to or read from.
	{
		if (m_hFile) Close();

		m_hFile = ::CreateFile(pszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, nOpenFlags, FILE_ATTRIBUTE_NORMAL, NULL);

		if (INVALID_HANDLE_VALUE == m_hFile)
		{
			TRACE(_T("Failed\n"));
			m_hFile = 0;
		}

		if (m_hFile)
		{
			SetFilePath(pszFileName);
		}

		return (m_hFile != 0);
	}

	inline CString CFile::OpenFileDialog(LPCTSTR pszFilePathName, DWORD dwFlags, LPCTSTR pszFilter, CWnd* pOwnerWnd)
	// Displays the file open dialog. 
	// Returns a CString containing either the selected file name or an empty CString.
	{
		CString str;
		if (pszFilePathName)
			str = pszFilePathName;

		OPENFILENAME ofn = {0};
		ofn.lStructSize = sizeof(OPENFILENAME);

#if defined OPENFILENAME_SIZE_VERSION_400
		if (GetWinVersion() < 2500)
			ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
#endif

		ofn.hwndOwner = pOwnerWnd? pOwnerWnd->GetHwnd() : NULL;
		ofn.hInstance = GetApp()->GetInstanceHandle();
		ofn.lpstrFilter = pszFilter;
		ofn.lpstrTitle = _T("Open File");
		ofn.Flags = dwFlags;
		ofn.nMaxFile = _MAX_PATH;
		
		ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);
		::GetOpenFileName(&ofn);
		str.ReleaseBuffer();

		return str;
	}

	inline UINT CFile::Read(void* pBuf, UINT nCount)
	// Reads from the file, storing the contents in the specified buffer.
	{
		assert(m_hFile);
		DWORD dwRead = 0;

		if (!::ReadFile(m_hFile, pBuf, nCount, &dwRead, NULL))
			dwRead = 0;

		return dwRead;
	}

	inline BOOL CFile::Rename(LPCTSTR pszOldName, LPCTSTR pszNewName)
	// Renames the specified file.
	{
		return ::MoveFile(pszOldName, pszNewName);
	}

	inline BOOL CFile::Remove(LPCTSTR pszFileName)
	// Deletes the specified file.
	{
		return ::DeleteFile(pszFileName);
	}

	inline CString CFile::SaveFileDialog(LPCTSTR pszFilePathName, DWORD dwFlags, LPCTSTR pszFilter, LPCTSTR pszDefExt, CWnd* pOwnerWnd)
	// Displays the SaveFileDialog.
	// Returns a CString containing either the selected file name or an empty CString
	{
		CString str;
		if (pszFilePathName)
			str = pszFilePathName;

		OPENFILENAME ofn = {0};
		ofn.lStructSize = sizeof(OPENFILENAME);

#if defined OPENFILENAME_SIZE_VERSION_400
		if (GetWinVersion() < 2500)
			ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
#endif

		ofn.hwndOwner = pOwnerWnd? pOwnerWnd->GetHwnd() : NULL;
		ofn.hInstance = GetApp()->GetInstanceHandle();
		ofn.lpstrFilter = pszFilter;
		ofn.lpstrFile = (LPTSTR)pszFilePathName;
		ofn.lpstrFileTitle = (LPTSTR)pszFilePathName;
		ofn.lpstrDefExt = pszDefExt;
		ofn.nMaxFile = lstrlen(pszFilePathName);
		ofn.lpstrTitle = _T("Save File");
		ofn.Flags = dwFlags;
		ofn.nMaxFile = _MAX_PATH;
		ofn.lpstrFile = (LPTSTR)str.GetBuffer(_MAX_PATH);
		::GetSaveFileName(&ofn);
		str.ReleaseBuffer();

		return str;
	}

	inline ULONGLONG CFile::Seek(LONGLONG lOff, UINT nFrom)
	// Positions the current file pointer.
	// Permitted values for nFrom are: FILE_BEGIN, FILE_CURRENT, or FILE_END.
	{
		assert(m_hFile);
		assert(nFrom == FILE_BEGIN || nFrom == FILE_CURRENT || nFrom == FILE_END);

		LONG High = LONG(lOff >> 32);
		LONG Low = (LONG)(lOff & 0xFFFFFFFF);

		DWORD LowPos = SetFilePointer(m_hFile, Low, &High, nFrom);

		ULONGLONG Result = ((ULONGLONG)High << 32) + LowPos;
		return Result;
	}

	inline void CFile::SeekToBegin()
	// Sets the current file pointer to the beginning of the file.
	{
		assert(m_hFile);
		Seek(0, FILE_BEGIN);
	}

	inline ULONGLONG CFile::SeekToEnd()
	// Sets the current file pointer to the end of the file.
	{
		assert(m_hFile);
		return Seek(0, FILE_END);
	}

	inline void CFile::SetFilePath(LPCTSTR pszFileName)
	// Specifies the full file name, including its path
	{
		TCHAR* pFileName = NULL;
		int nBuffSize = ::GetFullPathName(pszFileName, 0, 0, 0);
		if (nBuffSize > 0)
		{
			TCHAR* pBuff = m_FilePath.GetBuffer(nBuffSize);
			::GetFullPathName(pszFileName, nBuffSize, pBuff, &pFileName);
			m_FilePath.ReleaseBuffer();
			m_FileName = pFileName;
			int nPos = m_FileName.ReverseFind(_T("."));
			if (nPos >= 0)
				m_FileTitle = m_FileName.Left(nPos);
		}
	}

	inline BOOL CFile::SetLength(ULONGLONG NewLen)
	// Changes the length of the file to the specified value.
	{
		assert(m_hFile);

		Seek(NewLen, FILE_BEGIN);
		return ::SetEndOfFile(m_hFile);
	}

	inline BOOL CFile::UnlockRange(ULONGLONG Pos, ULONGLONG Count)
	// Unlocks a range of bytes in an open file.
	{
		assert(m_hFile);

		DWORD dwPosHigh = (DWORD)(Pos >> 32);
		DWORD dwPosLow = (DWORD)(Pos & 0xFFFFFFFF);
		DWORD dwCountHigh = (DWORD)(Count >> 32);
		DWORD dwCountLow = (DWORD)(Count & 0xFFFFFFFF);

		return ::UnlockFile(m_hFile, dwPosLow, dwPosHigh, dwCountLow, dwCountHigh);
	}

	inline BOOL CFile::Write(const void* pBuf, UINT nCount)
	// Writes the specified buffer to the file.
	{
		assert(m_hFile);
		DWORD dwWritten = 0;
		BOOL bResult = ::WriteFile(m_hFile, pBuf, nCount, &dwWritten, NULL);
		if (dwWritten != nCount)
			bResult = FALSE;

		return bResult;
	}


}	// namespace Win32xx

#endif