diff --git a/Minecraft.World/IO/Files/FileInputStream.cpp b/Minecraft.World/IO/Files/FileInputStream.cpp index c5586a781..14d91f8b2 100644 --- a/Minecraft.World/IO/Files/FileInputStream.cpp +++ b/Minecraft.World/IO/Files/FileInputStream.cpp @@ -2,11 +2,31 @@ #include "File.h" #include "FileInputStream.h" -#include -#include // for close() +#include extern CConsoleMinecraftApp app; +namespace +{ + __int64 FileTell(std::FILE *file) + { +#if defined(_WIN32) + return _ftelli64(file); +#else + return static_cast<__int64>(ftello(file)); +#endif + } + + bool FileSeek(std::FILE *file, __int64 offset, int origin) + { +#if defined(_WIN32) + return _fseeki64(file, offset, origin) == 0; +#else + return fseeko(file, static_cast(offset), origin) == 0; +#endif + } +} + //Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. //A new FileDescriptor object is created to represent this file connection. //First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument. @@ -20,34 +40,16 @@ extern CConsoleMinecraftApp app; //FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be //opened for reading. //SecurityException - if a security manager exists and its checkRead method denies read access to the file. -FileInputStream::FileInputStream(const File &file) +FileInputStream::FileInputStream(const File &file) : m_fileHandle(NULL) { - const char *pchFilename=wstringtofilename(file.getPath()); -#ifdef _UNICODE - m_fileHandle = CreateFile( - file.getPath().c_str(), // file name - GENERIC_READ, // access mode - 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... - NULL, // Unused - OPEN_EXISTING , // how to create // TODO 4J Stu - Assuming that the file already exists if we are opening to read from it - FILE_FLAG_SEQUENTIAL_SCAN, // file attributes - NULL // Unsupported - ); -#elif defined(__linux__) - m_fileHandle = (HANDLE)(intptr_t)open(pchFilename, O_RDONLY); +#if defined(_WIN32) + m_fileHandle = _wfopen(file.getPath().c_str(), L"rb"); #else - m_fileHandle = CreateFile( - pchFilename, // file name - GENERIC_READ, // access mode - 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... - NULL, // Unused - OPEN_EXISTING , // how to create // TODO 4J Stu - Assuming that the file already exists if we are opening to read from it - FILE_FLAG_SEQUENTIAL_SCAN, // file attributes - NULL // Unsupported - ); + const std::string nativePath = wstringtofilename(file.getPath()); + m_fileHandle = std::fopen(nativePath.c_str(), "rb"); #endif - if( m_fileHandle == INVALID_HANDLE_VALUE ) + if( m_fileHandle == NULL ) { // TODO 4J Stu - Any form of error/exception handling //__debugbreak(); @@ -57,45 +59,26 @@ FileInputStream::FileInputStream(const File &file) FileInputStream::~FileInputStream() { - if( m_fileHandle != INVALID_HANDLE_VALUE ) -#ifndef __linux__ - CloseHandle( m_fileHandle ); -#else - ::close( (int)(intptr_t)m_fileHandle ); -#endif -} - -#if defined(__linux__) -ssize_t ReadFile(int fd, void* buffer, size_t byteRead, DWORD* numberOfBytesRead, int JustAddANULL) { - ssize_t result = read(fd, buffer, byteRead); - - if (result == -1) { - perror("read failed"); - return -1; - } else { - *numberOfBytesRead = result; - return 0; + if( m_fileHandle != NULL ) + { + std::fclose( m_fileHandle ); } } -#endif // __linux__ //Reads a byte of data from this input stream. This method blocks if no input is yet available. //Returns: //the next byte of data, or -1 if the end of the file is reached. int FileInputStream::read() { + if( m_fileHandle == NULL ) + { + return -1; + } + uint8_t byteRead = static_cast(0); - DWORD numberOfBytesRead; + const size_t numberOfBytesRead = std::fread(&byteRead, 1, 1, m_fileHandle); - BOOL bSuccess = ReadFile( - m_fileHandle, // handle to file - &byteRead, // data buffer - 1, // number of bytes to read - &numberOfBytesRead, // number of bytes read - NULL // overlapped buffer - ); - - if( bSuccess==FALSE ) + if( std::ferror(m_fileHandle) != 0 ) { // TODO 4J Stu - Some kind of error handling app.FatalLoadError(); @@ -117,17 +100,14 @@ int FileInputStream::read() //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. int FileInputStream::read(byteArray b) { - DWORD numberOfBytesRead; + if( m_fileHandle == NULL ) + { + return -1; + } - BOOL bSuccess = ReadFile( - m_fileHandle, // handle to file - b.data, // data buffer - b.length, // number of bytes to read - &numberOfBytesRead, // number of bytes read - NULL // overlapped buffer - ); + const size_t numberOfBytesRead = std::fread(b.data, 1, b.length, m_fileHandle); - if( bSuccess==FALSE ) + if( std::ferror(m_fileHandle) != 0 ) { // TODO 4J Stu - Some kind of error handling app.FatalLoadError(); @@ -155,17 +135,14 @@ int FileInputStream::read(byteArray b, unsigned int offset, unsigned int length) // 4J Stu - We don't want to read any more than the array buffer can hold assert( length <= ( b.length - offset ) ); - DWORD numberOfBytesRead; + if( m_fileHandle == NULL ) + { + return -1; + } - BOOL bSuccess = ReadFile( - m_fileHandle, // handle to file - &b[offset], // data buffer - length, // number of bytes to read - &numberOfBytesRead, // number of bytes read - NULL // overlapped buffer - ); + const size_t numberOfBytesRead = std::fread(&b[offset], 1, length, m_fileHandle); - if( bSuccess==FALSE ) + if( std::ferror(m_fileHandle) != 0 ) { // TODO 4J Stu - Some kind of error handling app.FatalLoadError(); @@ -184,21 +161,21 @@ int FileInputStream::read(byteArray b, unsigned int offset, unsigned int length) //If this stream has an associated channel then the channel is closed as well. void FileInputStream::close() { - if(m_fileHandle==INVALID_HANDLE_VALUE) + if(m_fileHandle==NULL) { //printf("\n\nFileInputStream::close - TRYING TO CLOSE AN INVALID FILE HANDLE\n\n"); return; } - int result = ::close( (int)(intptr_t)m_fileHandle ); + int result = std::fclose( m_fileHandle ); - if( result == 0 ) + if( result != 0 ) { // TODO 4J Stu - Some kind of error handling } // Stop the dtor from trying to close it again - m_fileHandle = INVALID_HANDLE_VALUE; + m_fileHandle = NULL; } @@ -211,18 +188,34 @@ void FileInputStream::close() //the actual number of bytes skipped. __int64 FileInputStream::skip(__int64 n) { -#ifdef _XBOX - LARGE_INTEGER li; - li.QuadPart = n; - li.LowPart = SetFilePointer(m_fileHandle, li.LowPart, &li.HighPart, FILE_CURRENT); - - if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) + if( m_fileHandle == NULL || n <= 0 ) { - li.QuadPart = 0; + return 0; } - return li.QuadPart; -#else - return 0; -#endif + const __int64 start = FileTell(m_fileHandle); + if( start < 0 ) + { + return 0; + } + + if( !FileSeek(m_fileHandle, 0, SEEK_END) ) + { + return 0; + } + + const __int64 end = FileTell(m_fileHandle); + if( end < 0 ) + { + return 0; + } + + const __int64 offset = std::min(n, std::max<__int64>(0, end - start)); + const __int64 target = start + offset; + if( !FileSeek(m_fileHandle, target, SEEK_SET) ) + { + return 0; + } + + return offset; } diff --git a/Minecraft.World/IO/Files/FileInputStream.h b/Minecraft.World/IO/Files/FileInputStream.h index ecf3ff599..7afda60a2 100644 --- a/Minecraft.World/IO/Files/FileInputStream.h +++ b/Minecraft.World/IO/Files/FileInputStream.h @@ -1,8 +1,12 @@ #pragma once // 4J Stu - Represents Java standard library class +#include + #include "../Streams/InputStream.h" +class File; + class FileInputStream : public InputStream { public: @@ -15,6 +19,6 @@ public: virtual __int64 skip(__int64 n); private: - HANDLE m_fileHandle; + std::FILE *m_fileHandle; -}; \ No newline at end of file +}; diff --git a/Minecraft.World/IO/Files/FileOutputStream.cpp b/Minecraft.World/IO/Files/FileOutputStream.cpp index 8b0ff4213..904f6fca7 100644 --- a/Minecraft.World/IO/Files/FileOutputStream.cpp +++ b/Minecraft.World/IO/Files/FileOutputStream.cpp @@ -1,14 +1,6 @@ #include "../../Platform/stdafx.h" #include "File.h" #include "FileOutputStream.h" -#include -#include -#include -#include -#include -#include -#include -#include //Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is //created to represent this file connection. @@ -19,7 +11,7 @@ // //Parameters: //file - the file to be opened for writing. -FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HANDLE_VALUE ) +FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( NULL ) { if( file.exists() && file.isDirectory()) { @@ -27,46 +19,26 @@ FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HAN return; } -#ifdef _DURANGO - m_fileHandle = CreateFile( - file.getPath().c_str() , // file name - GENERIC_WRITE, // access mode - 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... - NULL, // Unused - OPEN_ALWAYS , // how to create - FILE_ATTRIBUTE_NORMAL , // file attributes - NULL // Unsupported - ); -#elif defined(__linux__) - std::wstring path = file.getPath(); - char* convertedPath = new char[path.size() + 1]; - std::wcstombs(convertedPath, path.c_str(), path.size() + 1); - - m_fileHandle = (HANDLE)(intptr_t)open(convertedPath, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR); - delete[] convertedPath; +#if defined(_WIN32) + m_fileHandle = _wfopen(file.getPath().c_str(), L"wb"); #else - m_fileHandle = CreateFile( - wstringtofilename(file.getPath()) , // file name - GENERIC_WRITE, // access mode - 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... - NULL, // Unused - OPEN_ALWAYS , // how to create - FILE_ATTRIBUTE_NORMAL , // file attributes - NULL // Unsupported - ); + const std::string nativePath = wstringtofilename(file.getPath()); + m_fileHandle = std::fopen(nativePath.c_str(), "wb"); #endif - if( m_fileHandle == INVALID_HANDLE_VALUE ) + if( m_fileHandle == NULL ) { // TODO 4J Stu - Any form of error/exception handling - DWORD error = GetLastError(); + perror("FileOutputStream::FileOutputStream"); } } FileOutputStream::~FileOutputStream() { - if( m_fileHandle != INVALID_HANDLE_VALUE ) - ::close( (int)(intptr_t)m_fileHandle ); + if( m_fileHandle != NULL ) + { + std::fclose( m_fileHandle ); + } } //Writes the specified byte to this file output stream. Implements the write method of OutputStream. @@ -74,23 +46,16 @@ FileOutputStream::~FileOutputStream() //b - the byte to be written. void FileOutputStream::write(unsigned int b) { + if( m_fileHandle == NULL ) + { + return; + } + uint8_t value = (uint8_t) b; + const size_t numberOfBytesWritten = std::fwrite(&value, 1, 1, m_fileHandle); + const int result = std::ferror(m_fileHandle); -#if defined(_WIN32) - BOOL result = WriteFile( - m_fileHandle, // handle to file - &value, // data buffer - 1, // number of bytes to write - &numberOfBytesWritten, // number of bytes written - NULL // overlapped buffer - ); -#else // LINUX - int fileDescriptor = (int)(intptr_t)(m_fileHandle); - ssize_t numberOfBytesWritten = ::write(fileDescriptor, &value, 1); - int result = static_cast(numberOfBytesWritten); -#endif // _WIN32 - - if( result == 0 ) + if( result != 0 ) { // TODO 4J Stu - Some kind of error handling } @@ -105,21 +70,15 @@ void FileOutputStream::write(unsigned int b) //b - the data. void FileOutputStream::write(byteArray b) { -#if defined(_WIN32) - BOOL result = WriteFile( - m_fileHandle, // handle to file - &b.data, // data buffer - b.length, // number of bytes to write - &numberOfBytesWritten, // number of bytes written - NULL // overlapped buffer - ); -#else // Linux - int fileDescriptor = (int)(intptr_t)(m_fileHandle); - ssize_t numberOfBytesWritten = ::write(fileDescriptor, static_cast(b.data), b.length); - int result = static_cast(numberOfBytesWritten); -#endif // _WIN32 + if( m_fileHandle == NULL ) + { + return; + } - if( result == 0 ) + const size_t numberOfBytesWritten = std::fwrite(b.data, 1, b.length, m_fileHandle); + const int result = std::ferror(m_fileHandle); + + if( result != 0 ) { // TODO 4J Stu - Some kind of error handling } @@ -139,23 +98,15 @@ void FileOutputStream::write(byteArray b, unsigned int offset, unsigned int leng // 4J Stu - We don't want to write any more than the array buffer holds assert( length <= ( b.length - offset ) ); -#if defined(_WIN32) - DWORD numberOfBytesWritten; + if( m_fileHandle == NULL ) + { + return; + } - BOOL result = WriteFile( - m_fileHandle, // handle to file - &b[offset], // data buffer - length, // number of bytes to write - &numberOfBytesWritten, // number of bytes written - NULL // overlapped buffer - ); -#else - int fileDescriptor = (int)(intptr_t)(m_fileHandle); - ssize_t numberOfBytesWritten = ::write(fileDescriptor, static_cast(&b[offset]), length); - int result = static_cast(numberOfBytesWritten); -#endif // _WIN32 + const size_t numberOfBytesWritten = std::fwrite(&b[offset], 1, length, m_fileHandle); + const int result = std::ferror(m_fileHandle); - if( result == 0 ) + if( result != 0 ) { // TODO 4J Stu - Some kind of error handling } @@ -170,16 +121,25 @@ void FileOutputStream::write(byteArray b, unsigned int offset, unsigned int leng //If this stream has an associated channel then the channel is closed as well. void FileOutputStream::close() { -#ifdef _WIN32 - BOOL result = CloseHandle( m_fileHandle ); -#else // __linux__ - int result = ::close( (int)(intptr_t)m_fileHandle ); -#endif // _WIN32 - if( result == 0 ) + if( m_fileHandle == NULL ) + { + return; + } + + int result = std::fclose( m_fileHandle ); + if( result != 0 ) { // TODO 4J Stu - Some kind of error handling } // Stop the dtor from trying to close it again - m_fileHandle = INVALID_HANDLE_VALUE; + m_fileHandle = NULL; +} + +void FileOutputStream::flush() +{ + if( m_fileHandle != NULL ) + { + std::fflush( m_fileHandle ); + } } diff --git a/Minecraft.World/IO/Files/FileOutputStream.h b/Minecraft.World/IO/Files/FileOutputStream.h index 577994303..03734802c 100644 --- a/Minecraft.World/IO/Files/FileOutputStream.h +++ b/Minecraft.World/IO/Files/FileOutputStream.h @@ -1,8 +1,12 @@ #pragma once // 4J Stu - Represents Java standard lib abstract +#include + #include "../Streams/OutputStream.h" +class File; + class FileOutputStream : public OutputStream { public: @@ -12,8 +16,8 @@ public: virtual void write(byteArray b); virtual void write(byteArray b, unsigned int offset, unsigned int length); virtual void close(); - virtual void flush() {} + virtual void flush(); private: - HANDLE m_fileHandle; -}; \ No newline at end of file + std::FILE *m_fileHandle; +};