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 "ConsoleSaveFile.h"
5#include "ConsoleSaveFileInputStream.h"
6
7ConsoleSaveFileInputStream::ConsoleSaveFileInputStream(ConsoleSaveFile *saveFile, const ConsoleSavePath &file)
8{
9 m_saveFile = saveFile;
10 m_file = m_saveFile->createFile( file );
11
12 m_saveFile->setFilePointer( m_file, 0, NULL, FILE_BEGIN );
13}
14
15ConsoleSaveFileInputStream::ConsoleSaveFileInputStream(ConsoleSaveFile *saveFile, FileEntry *file)
16{
17 m_saveFile = saveFile;
18 m_file = file;
19
20 m_saveFile->setFilePointer( m_file, 0, NULL, FILE_BEGIN );
21}
22
23//Reads a byte of data from this input stream. This method blocks if no input is yet available.
24//Returns:
25//the next byte of data, or -1 if the end of the file is reached.
26int ConsoleSaveFileInputStream::read()
27{
28 byte byteRead = 0;
29 DWORD numberOfBytesRead;
30
31 BOOL result = m_saveFile->readFile(
32 m_file,
33 &byteRead, // data buffer
34 1, // number of bytes to read
35 &numberOfBytesRead // number of bytes read
36 );
37
38 if( result == 0 )
39 {
40 // TODO 4J Stu - Some kind of error handling
41 return -1;
42 }
43 else if( numberOfBytesRead == 0 )
44 {
45 // File pointer is past the end of the file
46 return -1;
47 }
48
49 return byteRead;
50}
51
52//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.
53//Parameters:
54//b - the buffer into which the data is read.
55//Returns:
56//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.
57int ConsoleSaveFileInputStream::read(byteArray b)
58{
59 DWORD numberOfBytesRead;
60
61 BOOL result = m_saveFile->readFile(
62 m_file,
63 &b.data, // data buffer
64 b.length, // number of bytes to read
65 &numberOfBytesRead // number of bytes read
66 );
67
68 if( result == 0 )
69 {
70 // TODO 4J Stu - Some kind of error handling
71 return -1;
72 }
73 else if( numberOfBytesRead == 0 )
74 {
75 // File pointer is past the end of the file
76 return -1;
77 }
78
79 return numberOfBytesRead;
80}
81
82//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
83//is available; otherwise, no bytes are read and 0 is returned.
84//Parameters:
85//b - the buffer into which the data is read.
86//off - the start offset in the destination array b
87//len - the maximum number of bytes read.
88//Returns:
89//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.
90int ConsoleSaveFileInputStream::read(byteArray b, unsigned int offset, unsigned int length)
91{
92 // 4J Stu - We don't want to read any more than the array buffer can hold
93 assert( length <= ( b.length - offset ) );
94
95 DWORD numberOfBytesRead;
96
97 BOOL result = m_saveFile->readFile(
98 m_file,
99 &b[offset], // data buffer
100 length, // number of bytes to read
101 &numberOfBytesRead // number of bytes read
102 );
103
104 if( result == 0 )
105 {
106 // TODO 4J Stu - Some kind of error handling
107 return -1;
108 }
109 else if( numberOfBytesRead == 0 )
110 {
111 // File pointer is past the end of the file
112 return -1;
113 }
114
115 return numberOfBytesRead;
116}
117
118//Closes this file input stream and releases any system resources associated with the stream.
119//If this stream has an associated channel then the channel is closed as well.
120void ConsoleSaveFileInputStream::close()
121{
122 if( m_saveFile != NULL )
123 {
124 BOOL result = m_saveFile->closeHandle( m_file );
125
126 if( result == 0 )
127 {
128 // TODO 4J Stu - Some kind of error handling
129 }
130
131 // Stop the dtor from trying to close it again
132 m_saveFile = NULL;
133 }
134}