File: NstIoFile.cpp

package info (click to toggle)
nestopia 1.52.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,140 kB
  • sloc: cpp: 127,444; xml: 27,234; ansic: 3,635; makefile: 949; sh: 19
file content (444 lines) | stat: -rw-r--r-- 9,277 bytes parent folder | download | duplicates (3)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#include "NstIoFile.hpp"
#include "NstCollectionVector.hpp"
#include <windows.h>

namespace Nestopia
{
	namespace Io
	{
		NST_COMPILE_ASSERT
		(
			File::BEGIN == FILE_BEGIN &&
			File::CURRENT == FILE_CURRENT &&
			File::END == FILE_END
		);

		File::File()
		: handle(NULL) {}

		File::File(const GenericString fileName,const uint mode)
		: handle(NULL)
		{
			Open( fileName, mode );
		}

		File::~File()
		{
			Close();
		}

		void File::Open(const GenericString n,const uint mode)
		{
			NST_ASSERT( (mode & (WRITE|READ)) && ((mode & WRITE) || !(mode & EMPTY)) && (mode & (RANDOM_ACCESS|SEQUENTIAL_ACCESS)) != (RANDOM_ACCESS|SEQUENTIAL_ACCESS) );

			Close();

			if (n.Empty())
				throw ERR_NOT_FOUND;

			handle = ::CreateFile
			(
				(name=n).Ptr(),
				(
					(mode & (READ|WRITE)) == (READ|WRITE) ? (GENERIC_READ|GENERIC_WRITE) :
					(mode & (READ|WRITE)) == (READ)       ? (GENERIC_READ) :
					(mode & (READ|WRITE)) == (WRITE)      ? (GENERIC_WRITE) :
															0
				),
				(
					(mode & (READ|WRITE)) == (WRITE) ? 0 : FILE_SHARE_READ
				),
				NULL,
				(
					(mode & (EMPTY|EXISTING)) == (EMPTY)          ? CREATE_ALWAYS :
					(mode & (EMPTY|EXISTING)) == (EMPTY|EXISTING) ? TRUNCATE_EXISTING :
					(mode & (EMPTY|EXISTING)) == (EXISTING)       ? OPEN_EXISTING :
					(mode & (WRITE))                              ? OPEN_ALWAYS :
																	OPEN_EXISTING
				),
				(
					(
						(mode & (WRITE|WRITE_THROUGH)) == (WRITE)               ? (FILE_ATTRIBUTE_NORMAL) :
						(mode & (WRITE|WRITE_THROUGH)) == (WRITE|WRITE_THROUGH) ? (FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH) : 0
					)
					|
					(
						(mode & (READ|RANDOM_ACCESS))     == (READ|RANDOM_ACCESS)     ? FILE_FLAG_RANDOM_ACCESS :
						(mode & (READ|SEQUENTIAL_ACCESS)) == (READ|SEQUENTIAL_ACCESS) ? FILE_FLAG_SEQUENTIAL_SCAN : 0
					)
				),
				NULL
			);

			if (handle && handle != INVALID_HANDLE_VALUE)
			{
				if (!(mode & EMPTY))
				{
					try
					{
						Size();
					}
					catch (...)
					{
						Close();
						throw;
					}
				}
			}
			else
			{
				handle = NULL;
				name.Clear();

				switch (::GetLastError())
				{
					case ERROR_FILE_NOT_FOUND:
					case ERROR_PATH_NOT_FOUND:

						throw ERR_NOT_FOUND;

					case ERROR_ALREADY_EXISTS:

						throw ERR_ALREADY_EXISTS;

					case ERROR_ACCESS_DENIED:

						if (mode & WRITE)
							throw ERR_READ_ONLY;

					default:

						throw ERR_OPEN;
				}
			}
		}

		void File::Close()
		{
			name.Clear();

			if (void* const tmp = handle)
			{
				handle = NULL;
				::CloseHandle( tmp );
			}
		}

		void File::Write(const void* const data,const uint size) const
		{
			NST_ASSERT( IsOpen() && bool(data) >= bool(size) );

			if (size)
			{
				DWORD written = 0;

				if (!::WriteFile( handle, data, size, &written, NULL ) || written != size)
					throw ERR_WRITE;
			}
		}

		uint File::WriteSome(const void* const data,const uint size) const
		{
			NST_ASSERT( IsOpen() && bool(data) >= bool(size) );

			if (size)
			{
				DWORD written = 0;

				if (!::WriteFile( handle, data, size, &written, NULL ))
					throw ERR_WRITE;

				return written;
			}

			return 0;
		}

		void File::Write8(uint data) const
		{
			const uchar buffer = data;
			Write( &buffer, 1 );
		}

		void File::Write16(uint data) const
		{
			const uchar buffer[] = { data & 0xFF, data >> 8 };
			Write( buffer, 2 );
		}

		void File::Write32(uint data) const
		{
			const uchar buffer[] = { data & 0xFF, data >> 8 & 0xFF, data >> 16 & 0xFF, data >> 24 };
			Write( buffer, 4 );
		}

		void File::Read(void* const data,const uint size) const
		{
			NST_ASSERT( IsOpen() && bool(data) >= bool(size) );

			if (size)
			{
				DWORD read = 0;

				if (!::ReadFile( handle, data, size, &read, NULL ) || read != size)
					throw ERR_READ;
			}
		}

		uint File::ReadSome(void* const data,uint size) const
		{
			NST_ASSERT( IsOpen() && bool(data) >= bool(size) );

			if (size)
			{
				DWORD read = 0;

				if (!::ReadFile( handle, data, size, &read, NULL ))
					throw ERR_READ;

				return read;
			}

			return 0;
		}

		void File::Peek(void* const data,const uint size) const
		{
			const uint pos = Position();
			Read( data, size );
			Position() = pos;
		}

		void File::Peek(const uint from,void* const data,const uint size) const
		{
			const uint pos = Position();
			Position() = from;
			Read( data, size );
			Position() = pos;
		}

		uint File::Peek8() const
		{
			uchar buffer;
			Peek( &buffer, 1 );
			return buffer;
		}

		uint File::Peek16() const
		{
			uchar buffer[2];
			Peek( buffer, 2 );
			return buffer[0] | uint(buffer[1]) << 8;
		}

		uint File::Peek32() const
		{
			uchar buffer[4];
			Peek( buffer, 4 );
			return buffer[0] | uint(buffer[1]) << 8 | uint(buffer[2]) << 16 | uint(buffer[3]) << 24;
		}

		uint File::Seek(const Offset offset,const int distance) const
		{
			NST_ASSERT( IsOpen() );

			const DWORD pos = ::SetFilePointer( handle, distance, NULL, offset );

			if (pos == INVALID_SET_FILE_POINTER && ::GetLastError() != NO_ERROR)
				throw ERR_SEEK;

			return pos;
		}

		uint File::Read8() const
		{
			uchar buffer;
			Read( &buffer, 1 );
			return buffer;
		}

		uint File::Read16() const
		{
			uchar buffer[2];
			Read( buffer, 2 );
			return buffer[0] | buffer[1] << 8;
		}

		uint File::Read32() const
		{
			uchar buffer[4];
			Read( buffer, 4 );
			return buffer[0] | buffer[1] << 8 | uint(buffer[2]) << 16 | uint(buffer[3]) << 24;
		}

		uint File::Size() const
		{
			NST_ASSERT( IsOpen() );

			DWORD words[2];

			words[1] = 0;
			words[0] = ::GetFileSize( handle, &words[1] );

			if (words[0] == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR)
				throw ERR_SEEK;

			if (words[1] || words[0] > MAX_SIZE)
				throw ERR_TOO_BIG;

			return words[0];
		}

		void File::Truncate() const
		{
			NST_ASSERT( IsOpen() );

			if (!::SetEndOfFile( handle ))
				throw ERR_SEEK;
		}

		void File::Truncate(const uint pos) const
		{
			Position() = pos;
			Truncate();
		}

		void File::Flush() const
		{
			NST_ASSERT( IsOpen() );
			::FlushFileBuffers( handle );
		}

		void File::ReadText(String::Heap<char>& string) const
		{
			string.Resize( Size() - Position() );

			if (string.Length())
				Read( string.Ptr(), string.Length() );
		}

		void File::ReadText(String::Heap<wchar_t>& string) const
		{
			String::Heap<char> buffer;
			ReadText( buffer );
			ParseText( buffer.Ptr(), buffer.Length(), string );
		}

		void File::WriteText(cstring const string,const uint length,bool) const
		{
			Write( string, length );
		}

		void File::WriteText(wcstring string,uint length,bool forceUnicode) const
		{
			if (length)
			{
				if (forceUnicode || String::Generic<wchar_t>(string,length).Wide())
				{
					Collection::Buffer buffer( length * 2 + 2 );
					char* NST_RESTRICT dst = buffer.Ptr();

					*dst++ = UTF16_LE & 0xFF;
					*dst++ = UTF16_LE >> 8;

					do
					{
						*dst++ = *string & 0xFF;
						*dst++ = *string++ >> 8;
					}
					while (--length);

					Write( buffer.Ptr(), buffer.Size() );
				}
				else
				{
					const String::Heap<char> tmp( string, length );
					Write( tmp.Ptr(), tmp.Length() );
				}
			}
		}

		void File::ParseText(cstring src,const uint length,String::Heap<char>& string)
		{
			string.Assign( src, length );
		}

		void File::ParseText(cstring src,const uint length,String::Heap<wchar_t>& string)
		{
			if (length)
			{
				const uint utf = (length >= 2) ? (src[0] | uint(src[1]) << 8) : 0;

				if (utf == UTF16_LE || utf == UTF16_BE)
				{
					NST_VERIFY( length > 2 && length % 2 == 0 );

					string.Resize( length / 2 - 1 );

					if (string.Length())
					{
						wchar_t* NST_RESTRICT dst = string.Ptr();
						const wchar_t* const end = dst + string.Length();

						if (utf == UTF16_LE)
						{
							do
							{
								src += 2;
								*dst++ = src[0] | src[1] << 8;
							}
							while (dst != end);
						}
						else
						{
							do
							{
								src += 2;
								*dst++ = src[1] | src[0] << 8;
							}
							while (dst != end);
						}
					}
				}
				else
				{
					string.Assign( src, length );
				}
			}
			else
			{
				string.Clear();
			}
		}

		bool File::Delete(wcstring const path)
		{
			NST_ASSERT( path && *path );
			return ::DeleteFile( path );
		}
	}
}