2024-03-26 10:32:56 -03:00
# ifndef XNA_CSHARP_STREAM_HPP
# define XNA_CSHARP_STREAM_HPP
# include "../types.hpp"
# include "../enums.hpp"
# include "../xnaerror.hpp"
2024-03-26 15:56:17 -03:00
# include <fstream>
# include <filesystem>
2024-03-26 10:32:56 -03:00
namespace xna {
2024-05-29 09:42:18 -03:00
//A simplified port of the System.IO.Stream.
//Provides a generic view of a sequence of bytes. This is an abstract class.
2024-03-26 10:32:56 -03:00
class Stream {
public :
2024-05-01 19:09:43 -03:00
virtual ~ Stream ( ) { }
2024-05-29 09:42:18 -03:00
//Gets the length in bytes of the stream.
2024-03-26 10:32:56 -03:00
virtual Int Length ( ) = 0 ;
2024-05-29 09:42:18 -03:00
//Gets the position within the current stream.
virtual Long Position ( ) = 0 ;
//Closes the current stream and releases any resources
2024-03-26 10:32:56 -03:00
virtual void Close ( ) = 0 ;
2024-05-09 09:13:26 -03:00
virtual bool IsClosed ( ) = 0 ;
2024-05-29 09:42:18 -03:00
//Sets the position within the current stream.
virtual Long Seek ( Long offset , SeekOrigin const & origin ) = 0 ;
//
//Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
//
virtual Int Read ( Byte * buffer , Int bufferLength , Int offset , Int count ) = 0 ;
virtual Int Read ( std : : vector < Byte > & buffer , Int offset , Int count ) = 0 ;
//Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
virtual Int ReadByte ( ) = 0 ;
//
//When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
//
virtual void Write ( Byte const * buffer , Int bufferLength , Int offset , Int count ) = 0 ;
virtual void Write ( std : : vector < Byte > const & buffer , Int offset , Int count ) = 0 ;
//Writes a byte to the current position in the stream and advances the position within the stream by one byte.
virtual void WriteByte ( Byte value ) = 0 ;
2024-06-04 14:37:28 -03:00
virtual void * Data ( ) = 0 ;
2024-03-26 10:32:56 -03:00
} ;
2024-05-29 09:42:18 -03:00
//A simplified port of the System.IO.MemoryStream.
2024-03-26 10:32:56 -03:00
class MemoryStream : public Stream {
public :
2024-05-29 15:58:37 -03:00
constexpr MemoryStream ( std : : vector < Byte > const & bytes ) :
_buffer ( bytes ) , _length ( bytes . size ( ) ) { }
2024-03-26 10:32:56 -03:00
constexpr MemoryStream ( Int capacity ) :
_buffer ( static_cast < size_t > ( capacity ) ) ,
_length ( capacity > 0 ? capacity : 0 ) { }
virtual constexpr Int Length ( ) override {
if ( _closed )
return 0 ;
return _length ;
}
virtual constexpr Long Position ( ) override {
if ( _closed )
return 0 ;
return _position ;
}
virtual constexpr void Close ( ) override {
_closed = true ;
_buffer = std : : vector < Byte > ( ) ;
}
2024-05-09 09:13:26 -03:00
virtual constexpr bool IsClosed ( ) override {
return _closed ;
}
2024-05-29 09:42:18 -03:00
virtual Long Seek ( Long offset , SeekOrigin const & origin ) override ;
virtual Int Read ( Byte * buffer , Int bufferLength , Int offset , Int count ) override ;
virtual Int Read ( std : : vector < Byte > & buffer , Int offset , Int count ) override ;
virtual Int ReadByte ( ) override ;
virtual void Write ( Byte const * buffer , Int bufferLength , Int offset , Int count ) override ;
virtual void Write ( std : : vector < Byte > const & buffer , Int offset , Int count ) override ;
virtual void WriteByte ( Byte value ) override ;
2024-05-29 15:58:37 -03:00
2024-06-04 14:37:28 -03:00
virtual void * Data ( ) override {
return _buffer . data ( ) ;
}
2024-03-26 10:32:56 -03:00
public :
2024-05-29 15:58:37 -03:00
std : : vector < Byte > _buffer ;
private :
2024-03-26 10:32:56 -03:00
Int _position { 0 } ;
Int _origin { 0 } ;
Int _length { 0 } ;
bool _closed { false } ;
} ;
2024-03-26 15:56:17 -03:00
2024-05-29 09:42:18 -03:00
//A simplified port of the System.IO.FileStream.
2024-03-26 15:56:17 -03:00
class FileStream : public Stream {
public :
2024-05-29 09:42:18 -03:00
FileStream ( String const & path , FileMode fileMode ) ;
FileStream ( String const & path ) ;
2024-03-26 15:56:17 -03:00
~ FileStream ( ) {
Close ( ) ;
2024-03-26 17:22:00 -03:00
}
2024-03-26 15:56:17 -03:00
2024-05-29 09:42:18 -03:00
virtual Int Length ( ) override ;
virtual Long Position ( ) override ;
2024-03-26 15:56:17 -03:00
2024-05-29 09:42:18 -03:00
inline virtual void Close ( ) override {
2024-03-26 15:56:17 -03:00
_closed = true ;
if ( _fstream . is_open ( ) )
_fstream . close ( ) ;
}
2024-05-29 09:42:18 -03:00
inline virtual constexpr bool IsClosed ( ) override {
2024-05-09 09:13:26 -03:00
return _closed ;
}
2024-05-29 09:42:18 -03:00
virtual Long Seek ( Long offset , SeekOrigin const & origin ) override ;
virtual Int Read ( Byte * buffer , Int bufferLength , Int offset , Int count ) override ;
virtual Int Read ( std : : vector < Byte > & buffer , Int offset , Int count ) override ;
virtual Int ReadByte ( ) override ;
virtual void Write ( Byte const * buffer , Int bufferLength , Int offset , Int count ) override ;
virtual void Write ( std : : vector < Byte > const & buffer , Int offset , Int count ) override ;
virtual void WriteByte ( Byte value ) override ;
2024-03-26 15:56:17 -03:00
2024-06-04 14:37:28 -03:00
virtual void * Data ( ) override {
return _fstream . rdbuf ( ) ;
}
2024-03-26 15:56:17 -03:00
public :
std : : fstream _fstream ;
2024-05-08 20:42:15 -03:00
private :
std : : streampos _filesize { 0 } ;
2024-03-26 15:56:17 -03:00
bool _closed { false } ;
2024-05-09 09:13:26 -03:00
bool _truncated { false } ;
2024-03-26 17:22:00 -03:00
Int endOfFile ( ) {
if ( _closed )
return 0 ;
const auto pos = _fstream . tellg ( ) ;
_fstream . seekg ( 0 , std : : ios_base : : end ) ;
const auto end = _fstream . tellg ( ) ;
_fstream . seekg ( pos ) ;
return static_cast < Int > ( end ) ;
}
2024-03-26 15:56:17 -03:00
} ;
2024-03-26 10:32:56 -03:00
}
# endif