the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2
3#include "BufferedReader.h"
4
5//Creates a buffering character-input stream that uses a default-sized input buffer.
6//Parameters:
7//in - A Reader
8BufferedReader::BufferedReader( Reader *in ) : reader( in ), readMark( 0 ), bufferedMark( 0 ), eofReached( false )
9{
10 bufferSize = 64;
11 buffer = new wchar_t[bufferSize];
12 memset( buffer,0,sizeof(wchar_t)*bufferSize);
13 bufferMore();
14}
15
16BufferedReader::~BufferedReader()
17{
18 delete[] buffer;
19}
20
21void BufferedReader::bufferMore()
22{
23 // Don't buffer more unless we are going to read at least twice as much as what is already left
24 if( bufferedMark - readMark > (BUFFER_MORE_AMOUNT / 2) )
25 return;
26
27 if( bufferSize < (bufferedMark + BUFFER_MORE_AMOUNT) )
28 {
29 // Enlarge the buffer
30 wchar_t *temp = new wchar_t[bufferSize * 2];
31 memset( temp,0,sizeof(wchar_t)*bufferSize*2);
32 std::copy( buffer, buffer+bufferSize, temp );
33
34 delete[] buffer;
35 buffer = temp;
36 bufferSize = bufferSize * 2;
37 }
38
39 int value = 0;
40 unsigned int newCharsBuffered = 0;
41 while( newCharsBuffered < BUFFER_MORE_AMOUNT && (value = reader->read() ) != -1 )
42 {
43 buffer[bufferedMark++] = value;
44 newCharsBuffered++;
45 }
46}
47
48//Closes the stream and releases any system resources associated with it. Once the stream has been closed,
49//further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
50void BufferedReader::close()
51{
52 reader->close();
53}
54
55//Reads a single character.
56//Returns:
57//The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
58int BufferedReader::read()
59{
60 // We should have buffered at least as much as we have read
61 assert( bufferedMark >= readMark );
62
63 if( bufferedMark == readMark )
64 {
65 int value = reader->read();
66 if( value == -1 )
67 return -1;
68
69 buffer[bufferedMark++] = value;
70
71 bufferMore();
72 }
73
74 return buffer[readMark++];
75}
76
77//Reads characters into a portion of an array.
78//This method implements the general contract of the corresponding read method of the Reader class.
79//As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read method
80//of the underlying stream. This iterated read continues until one of the following conditions becomes true:
81//
82//The specified number of characters have been read,
83//The read method of the underlying stream returns -1, indicating end-of-file, or
84//The ready method of the underlying stream returns false, indicating that further input requests would block.
85//If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1.
86//Otherwise this method returns the number of characters actually read.
87//Subclasses of this class are encouraged, but not required, to attempt to read as many characters as possible in the same fashion.
88//
89//Ordinarily this method takes characters from this stream's character buffer, filling it from the underlying stream as necessary.
90//If, however, the buffer is empty, the mark is not valid, and the requested length is at least as large as the buffer,
91//then this method will read characters directly from the underlying stream into the given array.
92//Thus redundant BufferedReaders will not copy data unnecessarily.
93//
94//Parameters:
95//cbuf - Destination buffer
96//off - Offset at which to start storing characters
97//len - Maximum number of characters to read
98//Returns:
99//The number of characters read, or -1 if the end of the stream has been reached
100int BufferedReader::read(wchar_t cbuf[], unsigned int off, unsigned int len)
101{
102 if( bufferSize < (bufferedMark + len) )
103 {
104 // Enlarge the buffer
105 wchar_t *temp = new wchar_t[bufferSize * 2];
106 memset( temp,0,sizeof(wchar_t)*bufferSize*2);
107 std::copy( buffer, buffer+bufferSize, temp );
108
109 delete[] buffer;
110 buffer = temp;
111 bufferSize = bufferSize * 2;
112 }
113
114 unsigned int charsRead = 0;
115 while( charsRead < len && readMark <= bufferedMark )
116 {
117 cbuf[off + charsRead] = buffer[ readMark++ ];
118 charsRead++;
119 }
120
121 int value = 0;
122 while( charsRead < len && (value = reader->read() ) != -1 )
123 {
124 buffer[bufferedMark++] = value;
125 cbuf[off+charsRead] = value;
126 charsRead++;
127 readMark++;
128 }
129
130 bufferMore();
131
132 return charsRead;
133}
134
135//Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'),
136//or a carriage return followed immediately by a linefeed.
137//Returns:
138//A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
139wstring BufferedReader::readLine()
140{
141 wstring output = L"";
142 bool newLineCharFound = false;
143
144 while( readMark < bufferedMark )
145 {
146 wchar_t value = buffer[readMark++];
147
148 if( !newLineCharFound )
149 {
150 if( ( value == '\n') || ( value == '\r') )
151 {
152 newLineCharFound = true;
153 }
154 else
155 {
156 output.push_back(value);
157 }
158 }
159 else
160 {
161 if( ( value != '\n') && ( value != '\r') )
162 {
163 readMark--; // Move back the read mark on char so we get this char again next time
164 break;
165 }
166 }
167
168 // This will only actually read more from the stream if we have less than half of the amount that
169 // will be added left to read
170 bufferMore();
171 }
172 return output;
173}