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 "File.h"
4#include "FileInputStream.h"
5
6//Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
7//A new FileDescriptor object is created to represent this file connection.
8//First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument.
9//
10//If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading
11//then a FileNotFoundException is thrown.
12//
13//Parameters:
14//file - the file to be opened for reading.
15//Throws:
16//FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be
17//opened for reading.
18//SecurityException - if a security manager exists and its checkRead method denies read access to the file.
19FileInputStream::FileInputStream(const File &file)
20{
21 const char *pchFilename=wstringtofilename(file.getPath());
22#ifdef _UNICODE
23 m_fileHandle = CreateFile(
24 file.getPath().c_str(), // file name
25 GENERIC_READ, // access mode
26 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
27 NULL, // Unused
28 OPEN_EXISTING , // how to create // TODO 4J Stu - Assuming that the file already exists if we are opening to read from it
29 FILE_FLAG_SEQUENTIAL_SCAN, // file attributes
30 NULL // Unsupported
31 );
32#else
33 m_fileHandle = CreateFile(
34 pchFilename, // file name
35 GENERIC_READ, // access mode
36 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but...
37 NULL, // Unused
38 OPEN_EXISTING , // how to create // TODO 4J Stu - Assuming that the file already exists if we are opening to read from it
39 FILE_FLAG_SEQUENTIAL_SCAN, // file attributes
40 NULL // Unsupported
41 );
42#endif
43
44 if( m_fileHandle == INVALID_HANDLE_VALUE )
45 {
46 // TODO 4J Stu - Any form of error/exception handling
47 //__debugbreak();
48 app.FatalLoadError();
49 }
50}
51
52FileInputStream::~FileInputStream()
53{
54 if( m_fileHandle != INVALID_HANDLE_VALUE )
55 CloseHandle( m_fileHandle );
56}
57
58//Reads a byte of data from this input stream. This method blocks if no input is yet available.
59//Returns:
60//the next byte of data, or -1 if the end of the file is reached.
61int FileInputStream::read()
62{
63 byte byteRead = 0;
64 DWORD numberOfBytesRead;
65
66 BOOL bSuccess = ReadFile(
67 m_fileHandle, // handle to file
68 &byteRead, // data buffer
69 1, // number of bytes to read
70 &numberOfBytesRead, // number of bytes read
71 NULL // overlapped buffer
72 );
73
74 if( bSuccess==FALSE )
75 {
76 // TODO 4J Stu - Some kind of error handling
77 app.FatalLoadError();
78 //return -1;
79 }
80 else if( numberOfBytesRead == 0 )
81 {
82 // File pointer is past the end of the file
83 return -1;
84 }
85
86 return byteRead;
87}
88
89//Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
90//Parameters:
91//b - the buffer into which the data is read.
92//Returns:
93//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
94int FileInputStream::read(byteArray b)
95{
96 DWORD numberOfBytesRead;
97
98 BOOL bSuccess = ReadFile(
99 m_fileHandle, // handle to file
100 b.data, // data buffer
101 b.length, // number of bytes to read
102 &numberOfBytesRead, // number of bytes read
103 NULL // overlapped buffer
104 );
105
106 if( bSuccess==FALSE )
107 {
108 // TODO 4J Stu - Some kind of error handling
109 app.FatalLoadError();
110 //return -1;
111 }
112 else if( numberOfBytesRead == 0 )
113 {
114 // File pointer is past the end of the file
115 return -1;
116 }
117
118 return numberOfBytesRead;
119}
120
121//Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input
122//is available; otherwise, no bytes are read and 0 is returned.
123//Parameters:
124//b - the buffer into which the data is read.
125//off - the start offset in the destination array b
126//len - the maximum number of bytes read.
127//Returns:
128//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
129int FileInputStream::read(byteArray b, unsigned int offset, unsigned int length)
130{
131 // 4J Stu - We don't want to read any more than the array buffer can hold
132 assert( length <= ( b.length - offset ) );
133
134 DWORD numberOfBytesRead;
135
136 BOOL bSuccess = ReadFile(
137 m_fileHandle, // handle to file
138 &b[offset], // data buffer
139 length, // number of bytes to read
140 &numberOfBytesRead, // number of bytes read
141 NULL // overlapped buffer
142 );
143
144 if( bSuccess==FALSE )
145 {
146 // TODO 4J Stu - Some kind of error handling
147 app.FatalLoadError();
148 //return -1;
149 }
150 else if( numberOfBytesRead == 0 )
151 {
152 // File pointer is past the end of the file
153 return -1;
154 }
155
156 return numberOfBytesRead;
157}
158
159//Closes this file input stream and releases any system resources associated with the stream.
160//If this stream has an associated channel then the channel is closed as well.
161void FileInputStream::close()
162{
163 if(m_fileHandle==INVALID_HANDLE_VALUE)
164 {
165 //printf("\n\nFileInputStream::close - TRYING TO CLOSE AN INVALID FILE HANDLE\n\n");
166 return;
167 }
168
169 BOOL result = CloseHandle( m_fileHandle );
170
171 if( result == 0 )
172 {
173 // TODO 4J Stu - Some kind of error handling
174 }
175
176 // Stop the dtor from trying to close it again
177 m_fileHandle = INVALID_HANDLE_VALUE;
178}
179
180
181//Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned.
182//Overrides:
183//skip in class InputStream
184//Parameters:
185//n - the number of bytes to be skipped.
186//Returns:
187//the actual number of bytes skipped.
188__int64 FileInputStream::skip(__int64 n)
189{
190#ifdef _XBOX
191 LARGE_INTEGER li;
192 li.QuadPart = n;
193 li.LowPart = SetFilePointer(m_fileHandle, li.LowPart, &li.HighPart, FILE_CURRENT);
194
195 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
196 {
197 li.QuadPart = 0;
198 }
199
200 return li.QuadPart;
201#else
202 return 0;
203#endif
204}