the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 153 lines 4.3 kB view raw
1#include "stdafx.h" 2#include "File.h" 3#include "FileOutputStream.h" 4 5//Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is 6//created to represent this file connection. 7//First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument. 8// 9//If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened 10//for any other reason then a FileNotFoundException is thrown. 11// 12//Parameters: 13//file - the file to be opened for writing. 14FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HANDLE_VALUE ) 15{ 16 if( file.exists() && file.isDirectory()) 17 { 18 // TODO 4J Stu - FileNotFoundException 19 return; 20 } 21 22#ifdef _DURANGO 23 m_fileHandle = CreateFile( 24 file.getPath().c_str() , // file name 25 GENERIC_WRITE, // access mode 26 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... 27 NULL, // Unused 28 OPEN_ALWAYS , // how to create 29 FILE_ATTRIBUTE_NORMAL , // file attributes 30 NULL // Unsupported 31 ); 32#else 33 m_fileHandle = CreateFile( 34 wstringtofilename(file.getPath()) , // file name 35 GENERIC_WRITE, // access mode 36 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... 37 NULL, // Unused 38 OPEN_ALWAYS , // how to create 39 FILE_ATTRIBUTE_NORMAL , // file attributes 40 NULL // Unsupported 41 ); 42#endif 43 44 if( m_fileHandle == INVALID_HANDLE_VALUE ) 45 { 46 DWORD error = GetLastError(); 47 // TODO 4J Stu - Any form of error/exception handling 48 } 49} 50 51FileOutputStream::~FileOutputStream() 52{ 53 if( m_fileHandle != INVALID_HANDLE_VALUE ) 54 CloseHandle( m_fileHandle ); 55} 56 57//Writes the specified byte to this file output stream. Implements the write method of OutputStream. 58//Parameters: 59//b - the byte to be written. 60void FileOutputStream::write(unsigned int b) 61{ 62 DWORD numberOfBytesWritten; 63 64 byte value = (byte) b; 65 66 BOOL result = WriteFile( 67 m_fileHandle, // handle to file 68 &value, // data buffer 69 1, // number of bytes to write 70 &numberOfBytesWritten, // number of bytes written 71 NULL // overlapped buffer 72 ); 73 74 if( result == 0 ) 75 { 76 // TODO 4J Stu - Some kind of error handling 77 } 78 else if( numberOfBytesWritten == 0 ) 79 { 80 // File pointer is past the end of the file 81 } 82} 83 84//Writes b.length bytes from the specified byte array to this file output stream. 85//Parameters: 86//b - the data. 87void FileOutputStream::write(byteArray b) 88{ 89 DWORD numberOfBytesWritten; 90 91 BOOL result = WriteFile( 92 m_fileHandle, // handle to file 93 &b.data, // data buffer 94 b.length, // number of bytes to write 95 &numberOfBytesWritten, // number of bytes written 96 NULL // overlapped buffer 97 ); 98 99 if( result == 0 ) 100 { 101 // TODO 4J Stu - Some kind of error handling 102 } 103 else if( numberOfBytesWritten == 0 || numberOfBytesWritten != b.length ) 104 { 105 // File pointer is past the end of the file 106 } 107} 108 109//Writes len bytes from the specified byte array starting at offset off to this file output stream. 110//Parameters: 111//b - the data. 112//off - the start offset in the data. 113//len - the number of bytes to write. 114void FileOutputStream::write(byteArray b, unsigned int offset, unsigned int length) 115{ 116 // 4J Stu - We don't want to write any more than the array buffer holds 117 assert( length <= ( b.length - offset ) ); 118 119 DWORD numberOfBytesWritten; 120 121 BOOL result = WriteFile( 122 m_fileHandle, // handle to file 123 &b[offset], // data buffer 124 length, // number of bytes to write 125 &numberOfBytesWritten, // number of bytes written 126 NULL // overlapped buffer 127 ); 128 129 if( result == 0 ) 130 { 131 // TODO 4J Stu - Some kind of error handling 132 } 133 else if( numberOfBytesWritten == 0 || numberOfBytesWritten != length ) 134 { 135 // File pointer is past the end of the file 136 } 137} 138// 139//Closes this file output stream and releases any system resources associated with this stream. 140//This file output stream may no longer be used for writing bytes. 141//If this stream has an associated channel then the channel is closed as well. 142void FileOutputStream::close() 143{ 144 BOOL result = CloseHandle( m_fileHandle ); 145 146 if( result == 0 ) 147 { 148 // TODO 4J Stu - Some kind of error handling 149 } 150 151 // Stop the dtor from trying to close it again 152 m_fileHandle = INVALID_HANDLE_VALUE; 153}