File: NstIoStream.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 (585 lines) | stat: -rw-r--r-- 12,370 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
////////////////////////////////////////////////////////////////////////////////////////
//
// 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 <iostream>
#include "NstIoFile.hpp"
#include "NstIoStream.hpp"

namespace Nestopia
{
	namespace Io
	{
		namespace Stream
		{
			class In::FileStream : public std::istream
			{
			public:

				class Buffer : public std::streambuf
				{
				public:

					explicit Buffer(const File&,bool=false);
					explicit Buffer(GenericString);
					~Buffer();

				protected:

					const File& file;

				private:

					const bool internal;

					int_type underflow();
					int_type uflow();
					std::streamsize xsgetn(char*,std::streamsize);
					std::streampos seekoff(std::streamoff,std::ios::seekdir,std::ios::openmode);
					std::streampos seekpos(std::streampos,std::ios::openmode);

					#if NST_MSVC >= 1400 // ugh
					std::streamsize _Xsgetn_s(char*,std::size_t,std::streamsize);
					#endif
				};

			private:

				Buffer buffer;

			public:

				explicit FileStream(const File& f)
				: std::istream(&buffer), buffer(f) {}

				explicit FileStream(GenericString filename)
				: std::istream(&buffer), buffer(filename) {}
			};

			class In::BufferStream : public std::istream
			{
				class Buffer : public std::streambuf
				{
				public:

					explicit Buffer(const void*,uint);

				private:

					uint pos;
					const char* const data;
					const uint size;

					int_type underflow();
					int_type uflow();
					std::streamsize xsgetn(char*,std::streamsize);
					std::streampos seekoff(std::streamoff,std::ios::seekdir,std::ios::openmode);
					std::streampos seekpos(std::streampos,std::ios::openmode);

					#if NST_MSVC >= 1400 // ugh
					std::streamsize _Xsgetn_s(char*,std::size_t,std::streamsize);
					#endif
				};

				Buffer buffer;

			public:

				explicit BufferStream(const void* data,uint size)
				: std::istream(&buffer), buffer(data,size) {}
			};

			In::In(const File& file)
			: stream(*new FileStream(file))
			{
			}

			In::In(GenericString filename)
			: stream(*new FileStream(filename))
			{
			}

			In::In(const Collection::Buffer& buffer)
			: stream(*new BufferStream(buffer.Ptr(),buffer.Size()))
			{
			}

			In::In(const void* data,uint size)
			: stream(*new BufferStream(data,size))
			{
			}

			In::~In()
			{
				delete &stream;
			}

			In::FileStream::Buffer::Buffer(const File& f,bool i)
			: file(f), internal(i)
			{
				setg( NULL, NULL, NULL );
			}

			In::FileStream::Buffer::Buffer(GenericString filename)
			: file(*new Io::File(filename,Io::File::READ|Io::File::EXISTING)), internal(true)
			{
				setg( NULL, NULL, NULL );
			}

			In::FileStream::Buffer::~Buffer()
			{
				if (internal)
					delete &file;
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("t", on)
			#endif

			In::FileStream::Buffer::int_type In::FileStream::Buffer::underflow()
			{
				try
				{
					return file.Peek8();
				}
				catch (...)
				{
					return traits_type::eof();
				}
			}

			In::FileStream::Buffer::int_type In::FileStream::Buffer::uflow()
			{
				try
				{
					return file.Read8();
				}
				catch (...)
				{
					return traits_type::eof();
				}
			}

			std::streamsize In::FileStream::Buffer::xsgetn(char* output,std::streamsize count)
			{
				NST_ASSERT( count >= 0 );

				try
				{
					return file.ReadSome( output, count );
				}
				catch (...)
				{
					return 0;
				}
			}

			#if NST_MSVC >= 1400
			std::streamsize In::FileStream::Buffer::_Xsgetn_s(char* output,std::size_t,std::streamsize count)
			{
				return Buffer::xsgetn( output, count );
			}
			#endif

			std::streampos In::FileStream::Buffer::seekoff(std::streamoff off,std::ios::seekdir dir,std::ios::openmode)
			{
				NST_ASSERT( (dir == std::ios::beg || dir == std::ios::cur || dir == std::ios::end) );

				try
				{
					return file.Seek
					(
						dir == std::ios::beg ? File::BEGIN :
						dir == std::ios::end ? File::END :
                                               File::CURRENT,
						off
					);
				}
				catch (...)
				{
					return std::streamoff(-1);
				}
			}

			std::streampos In::FileStream::Buffer::seekpos(std::streampos p,std::ios::openmode mode)
			{
				return seekoff( p, std::ios::beg, mode );
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("", on)
			#endif

			In::BufferStream::Buffer::Buffer(const void* d,uint s)
			: pos(0), data(static_cast<const char*>(d)), size(s)
			{
				setg( NULL, NULL, NULL );
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("t", on)
			#endif

			In::BufferStream::Buffer::int_type In::BufferStream::Buffer::underflow()
			{
				NST_VERIFY( pos <= size );
				return pos < size ? data[pos] : traits_type::eof();
			}

			In::BufferStream::Buffer::int_type In::BufferStream::Buffer::uflow()
			{
				NST_VERIFY( pos <= size );
				return pos < size ? data[pos++] : traits_type::eof();
			}

			std::streamsize In::BufferStream::Buffer::xsgetn(char* output,std::streamsize count)
			{
				NST_ASSERT( count >= 0 );
				NST_VERIFY( count + pos <= size + 1 );

				if (count + pos > size)
					count = pos < size ? size - pos : 0;

				std::memcpy( output, data + pos, count );
				pos += count;

				return count;
			}

			#if NST_MSVC >= 1400
			std::streamsize In::BufferStream::Buffer::_Xsgetn_s(char* output,std::size_t,std::streamsize count)
			{
				return Buffer::xsgetn( output, count );
			}
			#endif

			std::streampos In::BufferStream::Buffer::seekoff(std::streamoff off,std::ios::seekdir dir,std::ios::openmode mode)
			{
				NST_ASSERT( (mode == std::ios::in) && (dir == std::ios::beg || dir == std::ios::cur || dir == std::ios::end) );

				if (dir == std::ios::cur)
				{
					off += int(pos);
				}
				else if (dir == std::ios::end)
				{
					off += int(size);
				}

				NST_VERIFY( off >= 0 && off <= size );

				if (off >= 0 && off <= size)
				{
					pos = off;
					return off;
				}
				else
				{
					return std::streamoff(-1);
				}
			}

			std::streampos In::BufferStream::Buffer::seekpos(std::streampos p,std::ios::openmode mode)
			{
				return seekoff( p, std::ios::beg, mode );
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("", on)
			#endif

			class Out::FileStream : public std::ostream
			{
			public:

				class Buffer : public In::FileStream::Buffer
				{
				public:

					explicit Buffer(const File&,bool=false);
					explicit Buffer(GenericString);

				private:

					int_type overflow(int_type);
					std::streamsize xsputn(const char*,std::streamsize);
				};

			private:

				Buffer buffer;

			public:

				explicit FileStream(const File& f)
				: std::ostream(&buffer), buffer(f) {}

				explicit FileStream(GenericString filename)
				: std::ostream(&buffer), buffer(filename) {}
			};

			class Out::BufferStream : public std::ostream
			{
				class Buffer : public std::streambuf
				{
				public:

					explicit Buffer(Collection::Buffer&);

				private:

					uint pos;
					Collection::Buffer& data;

					int_type overflow(int_type);
					std::streamsize xsputn(const char*,std::streamsize);
					std::streampos seekoff(std::streamoff,std::ios::seekdir,std::ios::openmode);
					std::streampos seekpos(std::streampos,std::ios::openmode);
				};

				Buffer buffer;

			public:

				explicit BufferStream(Collection::Buffer& b)
				: std::ostream(&buffer), buffer(b) {}
			};

			Out::Out(const File& file)
			: stream(*new FileStream(file))
			{
			}

			Out::Out(GenericString filename)
			: stream(*new FileStream(filename))
			{
			}

			Out::Out(Collection::Buffer& buffer)
			: stream(*new BufferStream(buffer))
			{
			}

			Out::~Out()
			{
				delete &stream;
			}

			Out::FileStream::Buffer::Buffer(const File& file,bool i)
			: In::FileStream::Buffer(file,i)
			{
				setp( NULL, NULL );
			}

			Out::FileStream::Buffer::Buffer(GenericString filename)
			: In::FileStream::Buffer(*new Io::File(filename,Io::File::WRITE|Io::File::EMPTY),true)
			{
				setp( NULL, NULL );
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("t", on)
			#endif

			Out::FileStream::Buffer::int_type Out::FileStream::Buffer::overflow(int_type c)
			{
				if (!traits_type::eq_int_type(traits_type::eof(),c))
				{
					try
					{
						file.Write8( c );
					}
					catch (...)
					{
						c = traits_type::eof();
					}
				}

				return c;
			}

			std::streamsize Out::FileStream::Buffer::xsputn(const char* input,std::streamsize count)
			{
				NST_ASSERT( count >= 0 );

				try
				{
					return file.WriteSome( input, count );
				}
				catch (...)
				{
					return 0;
				}
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("", on)
			#endif

			Out::BufferStream::Buffer::Buffer(Collection::Buffer& buffer)
			: pos(0), data(buffer)
			{
				setp( NULL, NULL );
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("t", on)
			#endif

			Out::BufferStream::Buffer::int_type Out::BufferStream::Buffer::overflow(int_type c)
			{
				if (!traits_type::eq_int_type(traits_type::eof(),c))
				{
					if (data.Size() == pos)
					{
						++pos;
						data.PushBack( c );
					}
					else if (data.Size() > pos)
					{
						data[pos++] = c;
					}
					else
					{
						c = traits_type::eof();
					}
				}

				return c;
			}

			std::streamsize Out::BufferStream::Buffer::xsputn(const char* input,std::streamsize count)
			{
				NST_ASSERT( count >= 0 );

				const uint cur = pos;
				pos += count;

				if (pos > data.Capacity())
					data.Reserve( pos * 2 );

				if (pos > data.Size())
					data.SetTo( pos );

				std::memcpy( data.Ptr() + cur, input, count );

				return count;
			}

			std::streampos Out::BufferStream::Buffer::seekoff(std::streamoff off,std::ios::seekdir dir,std::ios::openmode mode)
			{
				NST_ASSERT( (mode == std::ios::out) && (dir == std::ios::beg || dir == std::ios::cur || dir == std::ios::end) );

				if (dir == std::ios::cur)
				{
					off += int(pos);
				}
				else if (dir == std::ios::end)
				{
					off += int(data.Size());
				}

				NST_VERIFY( off >= 0 && off <= data.Size() );

				if (off >= 0 && off <= data.Size())
				{
					pos = off;
					return off;
				}
				else
				{
					return std::streamoff(-1);
				}
			}

			std::streampos Out::BufferStream::Buffer::seekpos(std::streampos p,std::ios::openmode mode)
			{
				return seekoff( p, std::ios::beg, mode );
			}

			#ifdef NST_MSVC_OPTIMIZE
			#pragma optimize("", on)
			#endif

			class InOut::FileStream : public std::iostream
			{
				class Buffer : public Out::FileStream::Buffer
				{
				public:

					explicit Buffer(const File& file)
					: Out::FileStream::Buffer(file) {}

					explicit Buffer(GenericString filename,uint flags)
					: Out::FileStream::Buffer
					(
						*new Io::File
						(
							filename,
							((flags &     READ) ? Io::File::READ  : 0) |
							((flags &    WRITE) ? Io::File::WRITE : 0) |
							((flags & TRUNCATE) ? Io::File::EMPTY : 0)
						),
						true
					)
					{}
				};

				Buffer buffer;

			public:

				explicit FileStream(const File& file)
				: std::iostream(&buffer), buffer(file) {}

				explicit FileStream(GenericString filename,uint flags)
				: std::iostream(&buffer), buffer(filename,flags) {}
			};

			InOut::InOut(const File& file)
			: stream(*new FileStream(file))
			{
			}

			InOut::InOut(GenericString filename,uint flags)
			: stream(*new FileStream(filename,flags))
			{
			}

			InOut::~InOut()
			{
				delete &stream;
			}

			InOut::operator std::istream& ()
			{
				return stream;
			}

			InOut::operator std::ostream& ()
			{
				return stream;
			}
		}
	}
}