From 1bf933432bbe155737945620dd2ec176a6fdd33d Mon Sep 17 00:00:00 2001 From: Halofreak1990 Date: Thu, 10 Mar 2011 22:55:43 +0000 Subject: [PATCH] List class now works. TextureCollection is broken because it can somehow not resolve Texture as template argument for the List it uses. Added missing BinaryWriter and TextWriter classes to System::IO namespace Modified BinaryReader Added PacketReader and PacketWriter classes to XFX::Net namespace --- include/GamerServices/Guide.h | 10 +- include/Graphics/Enums.h | 2 +- include/Graphics/Texture.h | 21 ++- include/Graphics/Texture2D.h | 10 +- include/Net/PacketReader.h | 40 +++++ include/Net/PacketWriter.h | 40 +++++ include/System/Collections/Generic/List.h | 2 +- include/System/Delegates.h | 2 +- include/System/IO/BinaryReader.h | 10 +- include/System/IO/BinaryWriter.h | 67 ++++++++ include/System/IO/Enums.h | 18 ++ include/System/IO/MemoryStream.h | 2 +- include/System/IO/Stream.h | 18 +- include/System/IO/StreamWriter.h | 34 ++-- include/System/IO/TextWriter.h | 11 +- include/System/Text/Encoder.h | 5 +- include/System/Text/Encoding.h | 4 +- include/System/Text/UTF8Encoding.h | 50 ++++++ src/libXFX/DepthStencilBuffer.cpp | 8 +- src/libXFX/Guide.cpp | 16 +- src/libXFX/PacketReader.cpp | 123 ++++++++++++++ src/libXFX/PacketWriter.cpp | 115 +++++++++++++ src/libXFX/Texture.cpp | 8 +- src/libXFX/TextureCollection.cpp | 5 + src/libXFX/libXFX.vcproj | 16 ++ src/libXFX/makefile | 4 +- src/libmscorlib/BinaryReader.cpp | 98 ++++++++--- src/libmscorlib/BinaryWriter.cpp | 50 ++++++ src/libmscorlib/FileStream.cpp | 2 +- src/libmscorlib/MemoryStream.cpp | 40 ++--- src/libmscorlib/Stream.cpp | 14 ++ src/libmscorlib/StreamWriter.cpp | 190 +++++++++++++++++++++- src/libmscorlib/TextWriter.cpp | 67 ++++++++ src/libmscorlib/Version.cpp | 22 ++- src/libmscorlib/libmscorlib.vcproj | 16 ++ src/libmscorlib/makefile | 2 +- 36 files changed, 1014 insertions(+), 128 deletions(-) create mode 100644 include/Net/PacketReader.h create mode 100644 include/Net/PacketWriter.h create mode 100644 include/System/IO/BinaryWriter.h create mode 100644 include/System/Text/UTF8Encoding.h create mode 100644 src/libXFX/PacketReader.cpp create mode 100644 src/libXFX/PacketWriter.cpp create mode 100644 src/libmscorlib/BinaryWriter.cpp create mode 100644 src/libmscorlib/TextWriter.cpp diff --git a/include/GamerServices/Guide.h b/include/GamerServices/Guide.h index 9d3b28e..10a6198 100644 --- a/include/GamerServices/Guide.h +++ b/include/GamerServices/Guide.h @@ -36,11 +36,11 @@ namespace XFX static bool IsVisible(); static NotificationPosition_t notificationPosition; - static IAsyncResult* BeginShowKeyboardInput(PlayerIndex_t player, char* title, char* description, char* defaultText, ASyncCallback callback, Object* state); - static IAsyncResult* BeginShowStorageDeviceSelector(int sizeInBytes, int directoryCount, ASyncCallback callback, Object* state); - static IAsyncResult* BeginShowStorageDeviceSelector(ASyncCallback callback, Object* state); - static IAsyncResult* BeginShowStorageDeviceSelector(PlayerIndex_t player, int sizeInBytes, int directoryCount, ASyncCallback callback, object state); - static IAsyncResult* BeginShowStorageDeviceSelector(PlayerIndex_t player, ASyncCallback callback, Object* state); + static IAsyncResult* BeginShowKeyboardInput(PlayerIndex_t player, char* title, char* description, char* defaultText, AsyncCallback callback, Object* state); + static IAsyncResult* BeginShowStorageDeviceSelector(int sizeInBytes, int directoryCount, AsyncCallback callback, Object* state); + static IAsyncResult* BeginShowStorageDeviceSelector(AsyncCallback callback, Object* state); + static IAsyncResult* BeginShowStorageDeviceSelector(PlayerIndex_t player, int sizeInBytes, int directoryCount, AsyncCallback callback, object state); + static IAsyncResult* BeginShowStorageDeviceSelector(PlayerIndex_t player, AsyncCallback callback, Object* state); static char* EndShowKeyboardInput(IAsyncResult* result); static StorageDevice EndShowStorageDeviceSelector(IAsyncResult* result); }; diff --git a/include/Graphics/Enums.h b/include/Graphics/Enums.h index 7457c86..562b647 100644 --- a/include/Graphics/Enums.h +++ b/include/Graphics/Enums.h @@ -498,7 +498,7 @@ namespace XFX Deferred = 1, FrontToBack = 4, Immediate = 0, - Texture = 2 + Texture_ = 2 }; }; /// diff --git a/include/Graphics/Texture.h b/include/Graphics/Texture.h index 44996a8..7500448 100644 --- a/include/Graphics/Texture.h +++ b/include/Graphics/Texture.h @@ -23,7 +23,7 @@ namespace XFX /// /// Represents a texture resource. /// - class Texture : public GraphicsResource + class Texture : public GraphicsResource, virtual Object { private: static ImageFileFormat_t ILimageformat2XNAimageformat(int ILFormat); @@ -34,22 +34,25 @@ namespace XFX public: int LevelCount(); int LevelOfDetail; + + Texture(); + ~Texture(); - static Texture FromFile(GraphicsDevice graphicsDevice, Stream textureStream); + static Texture FromFile(GraphicsDevice graphicsDevice, Stream* textureStream); static Texture FromFile(GraphicsDevice graphicsDevice, char* filename); - static Texture FromFile(GraphicsDevice graphicsDevice, Stream textureStream, int numberBytes); - static Texture FromFile(GraphicsDevice graphicsDevice, Stream textureStream, TextureCreationParameters creationParameters); + static Texture FromFile(GraphicsDevice graphicsDevice, Stream* textureStream, int numberBytes); + static Texture FromFile(GraphicsDevice graphicsDevice, Stream* textureStream, TextureCreationParameters creationParameters); static Texture FromFile(GraphicsDevice graphicsDevice, char* filename, TextureCreationParameters creationParameters); - static Texture FromFile(GraphicsDevice graphicsDevice, Stream textureStream, int numberBytes, TextureCreationParameters creationParameters); + static Texture FromFile(GraphicsDevice graphicsDevice, Stream* textureStream, int numberBytes, TextureCreationParameters creationParameters); static Texture FromFile(GraphicsDevice graphicsDevice, char* filename, int width, int height, int depth); void GenerateMipMaps(TextureFilter_t filterType); - static TextureCreationParameters GetCreationParameters(GraphicsDevice graphicsDevice, Stream textureStream); + static TextureCreationParameters GetCreationParameters(GraphicsDevice graphicsDevice, Stream* textureStream); static TextureCreationParameters GetCreationParameters(GraphicsDevice graphicsDevice, char* filename); - static TextureCreationParameters GetCreationParameters(GraphicsDevice graphicsDevice, Stream textureStream, int numberBytes); - static TextureInformation GetTextureInformation(Stream textureStream); + static TextureCreationParameters GetCreationParameters(GraphicsDevice graphicsDevice, Stream* textureStream, int numberBytes); + static TextureInformation GetTextureInformation(Stream* textureStream); static TextureInformation GetTextureInformation(char* filename); - static TextureInformation GetTextureInformation(Stream textureStream, int numberBytes); + static TextureInformation GetTextureInformation(Stream* textureStream, int numberBytes); void Save(char* filename, ImageFileFormat_t format); }; } diff --git a/include/Graphics/Texture2D.h b/include/Graphics/Texture2D.h index b8bcfea..d3f6ef4 100644 --- a/include/Graphics/Texture2D.h +++ b/include/Graphics/Texture2D.h @@ -32,8 +32,8 @@ namespace XFX int textureId; // The reference ID of the texture in OpenGL memory int imageId; - Texture2D(GraphicsDevice graphicsDevice); void Load(byte buffer[]); + Texture2D(GraphicsDevice graphicsDevice); protected: void Dispose(bool disposing); @@ -49,10 +49,10 @@ namespace XFX Texture2D(GraphicsDevice graphicsDevice, int width, int height, int numberLevels, TextureUsage_t usage, SurfaceFormat_t format); Texture2D(const Texture2D &obj); - static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream textureStream); - static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream textureStream, TextureCreationParameters creationParameters); - static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream textureStream, int numberBytes); - static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream textureStream, int numberBytes, TextureCreationParameters creationParameters); + static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream* textureStream); + static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream* textureStream, TextureCreationParameters creationParameters); + static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream* textureStream, int numberBytes); + static Texture2D FromFile(GraphicsDevice graphicsDevice, Stream* textureStream, int numberBytes, TextureCreationParameters creationParameters); static Texture2D FromFile(GraphicsDevice graphicsDevice, char* filename); static Texture2D FromFile(GraphicsDevice graphicsDevice, char* filename, TextureCreationParameters creationParameters); static Texture2D FromFile(GraphicsDevice graphicsDevice, char* filename, int width, int height); diff --git a/include/Net/PacketReader.h b/include/Net/PacketReader.h new file mode 100644 index 0000000..e03e698 --- /dev/null +++ b/include/Net/PacketReader.h @@ -0,0 +1,40 @@ +#ifndef _XFX_NET_PACKETREADER_ +#define _XFX_NET_PACKETREADER_ + +#include + +using namespace System::IO; + +namespace XFX +{ + struct Matrix; + struct Quaternion; + struct Vector2; + struct Vector3; + struct Vector4; + + namespace Net + { + /// + /// Provides common functionality for efficiently reading incoming network packets. + /// + class PacketReader : public BinaryReader + { + public: + int Length(); + int Position(); + void Position(int newValue); + + PacketReader(); + PacketReader(int capacity); + + Matrix ReadMatrix(); + Quaternion ReadQuaternion(); + Vector2 ReadVector2(); + Vector3 ReadVector3(); + Vector4 ReadVEctor4(); + }; + } +} + +#endif //_XFX_NET_PACKETREADER_ diff --git a/include/Net/PacketWriter.h b/include/Net/PacketWriter.h new file mode 100644 index 0000000..d7df532 --- /dev/null +++ b/include/Net/PacketWriter.h @@ -0,0 +1,40 @@ +#ifndef _XFX_NET_PACKETWRITER_ +#define _XFX_NET_PACKETWRITER_ + +#include + +using namespace System::IO; + +namespace XFX +{ + struct Matrix; + struct Quaternion; + struct Vector2; + struct Vector3; + struct Vector4; + + namespace Net + { + /// + /// Provides common functionality for efficiently formatting outgoing network packets. + /// + class PacketWriter : public BinaryWriter + { + public: + int Length(); + int Position(); + void Position(int newValue); + + PacketWriter(); + PacketWriter(int capacity); + + void Write(Matrix value); + void Write(Quaternion value); + void Write(Vector2 value); + void Write(Vector3 value); + void Write(Vector4 value); + }; + } +} + +#endif //_XFX_NET_PACKETWRITER_ diff --git a/include/System/Collections/Generic/List.h b/include/System/Collections/Generic/List.h index 8aea697..3f420cb 100644 --- a/include/System/Collections/Generic/List.h +++ b/include/System/Collections/Generic/List.h @@ -211,7 +211,7 @@ namespace System { Array::Copy(_items, index +1, _items, index, _size - index); } - _items[_size] = new T(); + _items[_size] = T(); _version++; } diff --git a/include/System/Delegates.h b/include/System/Delegates.h index 8c3ae4e..e96f2cf 100644 --- a/include/System/Delegates.h +++ b/include/System/Delegates.h @@ -16,7 +16,7 @@ namespace System /// /// References a method to be called when a corresponding asynchronous operation completes. /// - typedef void (*ASyncCallback)(IAsyncResult* ar); + typedef void (*AsyncCallback)(IAsyncResult* ar); /// /// Represents the method that will handle an event that has no event data. diff --git a/include/System/IO/BinaryReader.h b/include/System/IO/BinaryReader.h index f230918..d351686 100644 --- a/include/System/IO/BinaryReader.h +++ b/include/System/IO/BinaryReader.h @@ -15,7 +15,7 @@ namespace System /// /// Reads primitive data types as binary values in a specific encoding. /// - class BinaryReader : public IDisposable, public Object + class BinaryReader : public IDisposable, virtual Object { private: bool m_2BytesPerChar; @@ -23,7 +23,7 @@ namespace System char* m_charBuffer; byte* m_charBytes; char* m_singleChar; - Stream m_stream; + Stream* m_stream; Encoding m_encoding; bool m_isMemoryStream; Decoder m_decoder; @@ -41,10 +41,10 @@ namespace System virtual void FillBuffer(int numBytes); public: - virtual Stream BaseStream(); + virtual Stream* BaseStream(); - BinaryReader(Stream input); - BinaryReader(Stream input, Encoding encoding); + BinaryReader(Stream* input); + BinaryReader(Stream* input, Encoding encoding); virtual ~BinaryReader(); virtual void Close(); diff --git a/include/System/IO/BinaryWriter.h b/include/System/IO/BinaryWriter.h new file mode 100644 index 0000000..57affde --- /dev/null +++ b/include/System/IO/BinaryWriter.h @@ -0,0 +1,67 @@ +#ifndef _SYSTEM_IO_BINARYWRITER_ +#define _SYSTEM_IO_BINARYWRITER_ + +#include +#include +#include +#include "Stream.h" + +using namespace System::Text; + +namespace System +{ + namespace IO + { + /// + /// Writes primitive types in binary to a stream and supports writing strings in a specific encoding. + /// + class BinaryWriter : public IDisposable, virtual Object + { + private: + byte* _buffer; + Encoder _encoder; + Encoding _encoding; + byte* _largeByteBuffer; + int _maxChars; + char* _tmpOneCharBuffer; + static const int LargeByteBufferSize; + + protected: + Stream* OutStream; + BinaryWriter(); + + virtual void Dispose(bool disposing); + void Write7BiteEncodedInt(int value); + + public: + static const BinaryWriter Null; + virtual Stream* BaseStream(); + + BinaryWriter(Stream* output); + BinaryWriter(Stream* output, Encoding encoding); + + virtual void Close(); + void Dispose(); + virtual void Flush(); + virtual Int64 Seek(int offset, SeekOrigin_t origin); + virtual void Write(bool value); + virtual void Write(byte value); + virtual void Write(byte* value); + virtual void Write(sbyte value); + virtual void Write(char ch); + virtual void Write(double value); + virtual void Write(short value); + virtual void Write(int value); + virtual void Write(Int64 value); + virtual void Write(float value); + virtual void Write(char* value); + virtual void Write(ushort value); + virtual void Write(uint value); + virtual void Write(ulong value); + virtual void Write(byte* buffer, int index, int count); + virtual void Write(char chars[], int index, int count); + }; + } +} + +#endif //_SYSTEM_IO_BINARYWRITER_ diff --git a/include/System/IO/Enums.h b/include/System/IO/Enums.h index 66f8e6f..6c11c9a 100644 --- a/include/System/IO/Enums.h +++ b/include/System/IO/Enums.h @@ -58,6 +58,23 @@ namespace System }; }; + /// + /// Represents additional options for creating a FileStream object. + /// + struct FileOptions + { + enum type + { + Asynchronous = 0x40000000, + DeleteOnClose = 0x4000000, + Encrypted = 0x4000, + None = 0, + RandomAccess = 0x10000000, + SequentialScan = 0x8000000, + WriteThrough = -2147483648 + }; + }; + /// /// Contains constants for controlling the kind of access other System.IO.FileStream objects can have to the same /// file. @@ -89,6 +106,7 @@ namespace System typedef FileAccess::type FileAccess_t; typedef FileAttributes::type FileAttributes_t; typedef FileMode::type FileMode_t; + typedef FileOptions::type FileOptions_t; typedef FileShare::type FileShare_t; typedef SeekOrigin::type SeekOrigin_t; } diff --git a/include/System/IO/MemoryStream.h b/include/System/IO/MemoryStream.h index 5b2e3c2..2d004c4 100644 --- a/include/System/IO/MemoryStream.h +++ b/include/System/IO/MemoryStream.h @@ -55,7 +55,7 @@ namespace System virtual byte* ToArray(); void Write(byte buffer[], int offset, int count); void WriteByte(byte value); - virtual void WriteTo(Stream stream); + virtual void WriteTo(Stream* stream); }; } } diff --git a/include/System/IO/Stream.h b/include/System/IO/Stream.h index fe52d7c..6dcb6d5 100644 --- a/include/System/IO/Stream.h +++ b/include/System/IO/Stream.h @@ -7,10 +7,10 @@ #ifndef _SYSTEM_IO_STREAM_ #define _SYSTEM_IO_STREAM_ -#include "../Types.h" +#include #include "Enums.h" -#include "../Delegates.h" -#include "../Interfaces.h" +#include +#include namespace System { @@ -27,12 +27,9 @@ namespace System int _asyncActiveCount; protected: - virtual WaitHandle CreateWaitHandle(); virtual void Dispose(bool disposing); public: - Stream(); - virtual bool CanRead(); virtual bool CanSeek(); virtual bool CanTimeOut(); @@ -41,10 +38,17 @@ namespace System Int64 Position; int ReadTimeOut; int WriteTimeOut; - static const Stream Null; + static const Stream* Null; + + Stream(); + virtual ~Stream(); + virtual IAsyncResult* BeginRead(byte buffer[], int offset, int count, AsyncCallback callback, Object* state); + virtual IAsyncResult* BeginWrite(byte buffer[], int offset, int count, AsyncCallback callback, Object* state); virtual void Close(); void Dispose(); + virtual int EndRead(IAsyncResult* asyncResult); + virtual void EndWrite(IAsyncResult* asyncResult); virtual void Flush(); virtual int Read(byte buffer[], int offset, int count); virtual int ReadByte(); diff --git a/include/System/IO/StreamWriter.h b/include/System/IO/StreamWriter.h index b70c999..42d5c85 100644 --- a/include/System/IO/StreamWriter.h +++ b/include/System/IO/StreamWriter.h @@ -7,18 +7,16 @@ #ifndef _SYSTEM_IO_STREAMWRITER_ #define _SYSTEM_IO_STREAMWRITER_ -#include "../Text/Encoding.h" -#include "../Text/Encoder.h" +#include +#include #include "Stream.h" #include "TextWriter.h" -#include "../Types.h" +#include using namespace System::Text; namespace System { - class String; - namespace IO { /// @@ -27,16 +25,21 @@ namespace System class StreamWriter : public TextWriter { private: - byte byteBuffer; - char charBuffer[]; + static Encoding _UTF8NoBOM; + bool autoFlush; + byte* byteBuffer; + char* charBuffer; int charLen; int charPos; bool closable; + static const int DefaultBufferSize; Encoder encoder; Encoding encoding; bool haveWrittenPreamble; - Stream stream; + Stream* stream; + static Stream* CreateFile(char* path, bool append); + void Init(Stream* stream, Encoding encoding, int bufferSize); void Flush(bool flushStream, bool flushEncoder); protected: @@ -48,13 +51,14 @@ namespace System Encoding Encoding_(); static const StreamWriter Null; - StreamWriter(const char* path); - StreamWriter(const char* path, bool append); - StreamWriter(const char* path, bool append, Encoding encoding); - StreamWriter(const char* path, bool append, Encoding encoding, int bufferSize); - StreamWriter(Stream stream); - StreamWriter(Stream stream, Encoding encoding); - StreamWriter(Stream stream, Encoding encoding, int bufferSize); + StreamWriter(Stream* stream); + StreamWriter(char* path); + StreamWriter(Stream* stream, Encoding encoding); + StreamWriter(char* path, bool append); + StreamWriter(Stream* stream, Encoding encoding, int bufferSize); + StreamWriter(char* path, bool append, Encoding encoding); + StreamWriter(char* path, bool append, Encoding encoding, int bufferSize); + virtual ~StreamWriter(); void Close(); void Flush(); diff --git a/include/System/IO/TextWriter.h b/include/System/IO/TextWriter.h index 57e0a0f..7665fe4 100644 --- a/include/System/IO/TextWriter.h +++ b/include/System/IO/TextWriter.h @@ -7,9 +7,8 @@ #ifndef _SYSTEM_IO_TEXTWRITER_ #define _SYSTEM_IO_TEXTWRITER_ -#include "../Interfaces.h" -#include "../String.h" -#include "../Text/Encoding.h" +#include +#include namespace System { @@ -20,10 +19,16 @@ namespace System /// class TextWriter : public IDisposable { + private: + IFormatProvider* InternalFormatProvider; + static const char* InitialNewLine; + protected: char CoreNewLine[]; TextWriter(); + TextWriter(IFormatProvider* provider); + TextWriter(const TextWriter &obj); virtual void Dispose(bool disposing); diff --git a/include/System/Text/Encoder.h b/include/System/Text/Encoder.h index d05b2b6..4f1aa67 100644 --- a/include/System/Text/Encoder.h +++ b/include/System/Text/Encoder.h @@ -1,7 +1,7 @@ #ifndef _TEXT_ENCODER_ #define _TEXT_ENCODER_ -#include "../Types.h" +#include namespace System { @@ -12,10 +12,9 @@ namespace System /// class Encoder { - protected: + public: Encoder(); - public: virtual void Convert(char* chars, int charCount, byte* bytes, int byteCount, bool flush, int charsUsed, int bytesUsed, bool completed); virtual void Convert(char chars[], int charIndex, int charCount, byte bytes[], int byteIndex, int byteCount, bool flush, int charsUsed, int bytesUsed, bool completed); virtual int GetByteCount(char* chars, int count, bool flush); diff --git a/include/System/Text/Encoding.h b/include/System/Text/Encoding.h index 2f31382..8b72a21 100644 --- a/include/System/Text/Encoding.h +++ b/include/System/Text/Encoding.h @@ -59,8 +59,8 @@ namespace System int GetHashCode(); virtual int GetMaxByteCount(int charCount); virtual int GetMaxCharCount(int byteCount); - virtual byte *GetPreamble(); - virtual String GetString(byte bytes[], int index, int count); + virtual byte* GetPreamble(); + virtual char* GetString(byte bytes[], int index, int count); }; } } diff --git a/include/System/Text/UTF8Encoding.h b/include/System/Text/UTF8Encoding.h new file mode 100644 index 0000000..aa17403 --- /dev/null +++ b/include/System/Text/UTF8Encoding.h @@ -0,0 +1,50 @@ +#ifndef _SYSTEM_TEXT_UTF8ENCODING_ +#define _SYSTEM_TEXT_UTF8ENCODING_ + +#include "Encoding.h" + +namespace System +{ + namespace Text + { + /// + /// Represents a UTF-8 encoding of Unicode characters. + /// + class UTF8Encoding : public Encoding + { + private: + bool emitUTF8Identifier; + static const int FinalByte = 0x20000000; + bool isThrowException; + static const int SupplimentarySeq = 0x10000000; + static const int ThreeByteSeq = 0x8000000; + static const int UTF8_CODEPAGE = 0xfde9; + + byte* GetBytesUnknown(byte* pSrc, int ch); + + public: + UTF8Encoding(); + UTF8Encoding(bool encoderShouldEmitUTF8Identifier); + UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes); + bool Equals(Object* value); + int GetByteCount(char* chars); + int GetByteCount(char* chars, int count); + int GetByteCount(char chars[], int index, int count); + int GetBytes(char* chars, int charCount, byte* bytes, int byteCount); + int GetBytes(char chars[], int charIndex, int charCount, byte bytes[], int byteIndex); + int GetCharCount(byte* bytes, int count); + int GetCharCount(byte bytes[], int index, int count); + int GetChars(byte* bytes, int byteCount, char* chars, int charCount); + int GetChars(byte bytes[], int byteIndex, int byteCount, char chars[], int charIndex); + Decoder GetDecoder(); + Encoder GetEncoder(); + int GetHashCode(); + int GetMaxByteCount(int charCount); + int GetMaxCharCount(int byteCount); + byte* GetPreamble(); + char* GetString(byte bytes[], int index, int count); + }; + } +} + +#endif //_SYSTEM_TEXT_UTF8ENCODING_ diff --git a/src/libXFX/DepthStencilBuffer.cpp b/src/libXFX/DepthStencilBuffer.cpp index 83add7a..7cfe041 100644 --- a/src/libXFX/DepthStencilBuffer.cpp +++ b/src/libXFX/DepthStencilBuffer.cpp @@ -4,7 +4,7 @@ namespace XFX { namespace Graphics { - DepthStencilBuffer() + DepthStencilBuffer::DepthStencilBuffer() { _graphicsDevice = null; _width = 0; @@ -16,7 +16,7 @@ namespace XFX DepthStencilBuffer::DepthStencilBuffer(GraphicsDevice* graphicsDevice, int width, int height, DepthFormat_t format) { - _graphicsDevice = graphiceDevice; + _graphicsDevice = graphicsDevice; _width = width; _height = height; _format = format; @@ -26,7 +26,7 @@ namespace XFX DepthStencilBuffer::DepthStencilBuffer(GraphicsDevice* graphicsDevice, int width, int height, DepthFormat_t format, MultiSampleType_t multiSampleType, int multiSampleQuality) { - _graphicsDevice = graphiceDevice; + _graphicsDevice = graphicsDevice; _width = width; _height = height; _format = format; @@ -61,7 +61,7 @@ namespace XFX bool DepthStencilBuffer::IsDisposed() { - return isDisposed(); + return isDisposed; } int DepthStencilBuffer::Height() diff --git a/src/libXFX/Guide.cpp b/src/libXFX/Guide.cpp index 1f48500..9b92caa 100644 --- a/src/libXFX/Guide.cpp +++ b/src/libXFX/Guide.cpp @@ -37,40 +37,40 @@ namespace XFX return _isVisible; } - IAsyncResult* Guide::BeginShowKeyboardInput(PlayerIndex_t player, char *title, char *description, char *defaultText, ASyncCallback callback, Object* state) + IAsyncResult* Guide::BeginShowKeyboardInput(PlayerIndex_t player, char *title, char *description, char *defaultText, AsyncCallback callback, Object* state) { - if (title == null) + if (!title) title = ""; - if (description == null) + if (!description) description = ""; - if (defaultText == null) + if (!defaultText) defaultText = ""; // just return null to stop warning until this thing's coded return null; } - IAsyncResult* Guide::BeginShowStorageDeviceSelector(int sizeInBytes, int directoryCount, ASyncCallback callback, Object* state) + IAsyncResult* Guide::BeginShowStorageDeviceSelector(int sizeInBytes, int directoryCount, AsyncCallback callback, Object* state) { // just return null to stop warning until this thing's coded return null; } - IAsyncResult* Guide::BeginShowStorageDeviceSelector(ASyncCallback callback, Object* state) + IAsyncResult* Guide::BeginShowStorageDeviceSelector(AsyncCallback callback, Object* state) { // just return null to stop warning until this thing's coded return null; } - IAsyncResult* Guide::BeginShowStorageDeviceSelector(PlayerIndex_t player, int sizeInBytes, int directoryCount, ASyncCallback callback, object state) + IAsyncResult* Guide::BeginShowStorageDeviceSelector(PlayerIndex_t player, int sizeInBytes, int directoryCount, AsyncCallback callback, object state) { // just return null to stop warning until this thing's coded return null; } - IAsyncResult* Guide::BeginShowStorageDeviceSelector(PlayerIndex_t player, ASyncCallback callback, Object* state) + IAsyncResult* Guide::BeginShowStorageDeviceSelector(PlayerIndex_t player, AsyncCallback callback, Object* state) { // just return null to stop warning until this thing's coded return null; diff --git a/src/libXFX/PacketReader.cpp b/src/libXFX/PacketReader.cpp new file mode 100644 index 0000000..0f7dd65 --- /dev/null +++ b/src/libXFX/PacketReader.cpp @@ -0,0 +1,123 @@ +// Copyright (C) 2010-2012, XFX Team +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of the copyright holder nor the names of any +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include +#include +#include +#include + +namespace XFX +{ + namespace Net + { + int PacketReader::Length() + { + return (int)BaseStream().Length(); + } + + int PacketReader::Position() + { + return (int)BaseStream().Position; + } + + int PacketReader::Position(int newValue) + { + BaseStream().Position = newValue; + } + + PacketReader::PacketReader() + : BinaryReader(MemoryStream(0)) + { + } + + PacketReader::PacketReader(int capacity) + : BinaryReader(MemoryStream(capacity)) + { + } + + Matrix PacketReader::ReadMatrix() + { + Matrix matrix; + matrix.M11 = ReadSingle(); + matrix.M12 = ReadSingle(); + matrix.M13 = ReadSingle(); + matrix.M14 = ReadSingle(); + matrix.M21 = ReadSingle(); + matrix.M22 = ReadSingle(); + matrix.M23 = ReadSingle(); + matrix.M24 = ReadSingle(); + matrix.M31 = ReadSingle(); + matrix.M32 = ReadSingle(); + matrix.M33 = ReadSingle(); + matrix.M34 = ReadSingle(); + matrix.M41 = ReadSingle(); + matrix.M42 = ReadSingle(); + matrix.M43 = ReadSingle(); + matrix.M44 = ReadSingle(); + return matrix; + } + + Quaternion PacketReader::ReadQuaternion() + { + Quaternion quaternion; + quaternion.X = ReadSingle(); + quaternion.Y = ReadSingle(); + quaternion.Z = ReadSingle(); + quaternion.W = ReadSingle(); + return quaternion; + } + + Vector2 PacketReader::ReadVector2() + { + Vector2 vector2; + vector2.X = ReadSingle(); + vector2.Y = ReadSingle(); + return vector2; + } + + Vector3 PacketReader::ReadVector3() + { + Vector3 vector3; + vector3.X = ReadSingle(); + vector3.Y = ReadSingle(); + vector3.Z = ReadSingle(); + return vector3; + } + + Vector4 PacketReader::ReadVector4() + { + Vector4 vector4; + vector4.X = ReadSingle(); + vector4.Y = ReadSingle(); + vector4.Z = ReadSingle(); + vector4.W = ReadSingle(); + return vector4; + } + } +} diff --git a/src/libXFX/PacketWriter.cpp b/src/libXFX/PacketWriter.cpp new file mode 100644 index 0000000..7ef824d --- /dev/null +++ b/src/libXFX/PacketWriter.cpp @@ -0,0 +1,115 @@ +// Copyright (C) 2010-2012, XFX Team +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of the copyright holder nor the names of any +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include + +#include +#include +#include +#include +#include + +namespace XFX +{ + namespace Net + { + int PacketWriter::Length() + { + return (int)BaseStream.Length; + } + + int PacketWriter::Position() + { + return (int)BaseStream.Position; + } + + void PacketWriter::Position(int newValue) + { + BaseStream.Position = newValue); + } + + PacketWriter::PacketWriter() + : BinaryWriter(MemoryStream(0)) + { + } + + PacketWriter::PacketWriter(int capacity) + : BinaryWriter(MemoryStream(capacity)) + { + } + + void PacketWriter::Write(Matrix value) + { + Write(value.M11); + Write(value.M12); + Write(value.M13); + Write(value.M14); + Write(value.M21); + Write(value.M22); + Write(value.M23); + Write(value.M24); + Write(value.M31); + Write(value.M32); + Write(value.M33); + Write(value.M34); + Write(value.M41); + Write(value.M42); + Write(value.M43); + Write(value.M44); + } + + void PacketWriter::Write(Quaternion value) + { + Write(value.X); + Write(value.Y); + Write(value.Z); + Write(value.W); + } + + void PacketWriter::Write(Vector2 value) + { + Write(value.X); + Write(value.Y); + } + + void PacketWriter::Write(Vector3 value) + { + Write(value.X); + Write(value.Y); + Write(value.Z); + } + + void PacketWriter::Write(Vector4 value) + { + Write(value.X); + Write(value.Y); + Write(value.Z); + Write(value.W); + } + } +} diff --git a/src/libXFX/Texture.cpp b/src/libXFX/Texture.cpp index 795c469..c912125 100644 --- a/src/libXFX/Texture.cpp +++ b/src/libXFX/Texture.cpp @@ -46,10 +46,10 @@ namespace XFX if (creationParameters.Width == 0) creationParameters.Width = texinfo.Width; if (creationParameters.Height == 0) creationParameters.Height = texinfo.Height; if (creationParameters.Depth == 0) creationParameters.Depth = texinfo.Depth; - /*if (texinfo.ResourceType == ResourceType::Texture2D) + if (texinfo.ResourceType == ResourceType::Texture2D) { int ImgID; - Il::ilGenImages(1, out ImgID); + /*Il::ilGenImages(1, out ImgID); Il::ilBindImage(ImgID); Il::ilLoadImage(filename); int width = Il::ilGetInteger(Il::IL_IMAGE_WIDTH); @@ -64,10 +64,10 @@ namespace XFX glTexImage2D(GL_TEXTURE_2D, 0, Il::ilGetInteger(Il::IL_IMAGE_BYTES_PER_PIXEL), creationParameters.Width, creationParameters.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Il::ilGetData()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - Il::ilBindImage(0); + Il::ilBindImage(0); */ //Il::ilDeleteImage(0); return tex; - } */ + } else if (texinfo.ResourceType_ == ResourceType::Texture3D) { } else if (texinfo.ResourceType_ == ResourceType::Texture3DVolume) { } //FIXME: Should we handle this here too? else if (texinfo.ResourceType_ == ResourceType::TextureCube) { } diff --git a/src/libXFX/TextureCollection.cpp b/src/libXFX/TextureCollection.cpp index 99403b3..8232998 100644 --- a/src/libXFX/TextureCollection.cpp +++ b/src/libXFX/TextureCollection.cpp @@ -37,6 +37,11 @@ namespace XFX Dispose(true); } + void TextureCollection::Dispose(bool disposing) + { + textures.Clear(); + } + TextureCollection::TextureCollection() { // Nothing diff --git a/src/libXFX/libXFX.vcproj b/src/libXFX/libXFX.vcproj index 01b402f..a3c9c82 100644 --- a/src/libXFX/libXFX.vcproj +++ b/src/libXFX/libXFX.vcproj @@ -331,6 +331,14 @@ + + + + + + + + CanRead()) + throw ArgumentException("The stream doesn't support reading."); + + m_disposed = false; + m_stream = input; + m_encoding = Encoding::UTF8(); + m_decoder = m_encoding.GetDecoder(); + m_buffer = new byte[32]; } - BinaryReader::BinaryReader(Stream input, Encoding encoding) + BinaryReader::BinaryReader(Stream* input, Encoding encoding) { - if (!input.CanRead()) + if (!input->CanRead()) throw ArgumentException("The stream doesn't support reading."); m_disposed = false; @@ -74,14 +81,16 @@ namespace System void BinaryReader::Dispose(bool disposing) { - m_stream.Close(); + m_stream->Close(); m_disposed = true; delete m_buffer; - m_encoding = NULL; - m_stream.Close(); - m_stream = Stream::Null; + m_buffer = null; + m_encoding = null; + m_stream->Close(); + m_stream = null; delete[] m_charBuffer; + m_charBuffer = null; } void BinaryReader::FillBuffer(int numBytes) @@ -93,7 +102,7 @@ namespace System int num2 = 0; if (numBytes == 1) { - num2 = m_stream.ReadByte(); + num2 = m_stream->ReadByte(); if (num2 == -1) { throw EndOfStreamException("Attempted to read beyond End OF File."); @@ -104,7 +113,7 @@ namespace System { do { - num2 = m_stream.Read(m_buffer, offset, numBytes - offset); + num2 = m_stream->Read(m_buffer, offset, numBytes - offset); if (num2 == 0) { throw EndOfStreamException("Attempted to read beyond End OF File."); @@ -117,7 +126,48 @@ namespace System int BinaryReader::InternalReadChars(char buffer[], int index, int count) { - + int num = 0; + int num2 = 0; + int num3 = count; + if (!m_charBytes) + { + m_charBytes = new byte[0x80]; + } + while (num3 > 0) + { + num2 = num3; + if (m_2BytesPerChar) + { + num2 = num2 << 1; + } + if (num2 > 0x80) + { + num2 = 0x80; + } + if (m_isMemoryStream) + { + /*MemoryStream stream = (MemoryStream)*m_stream; + int position = stream.InternalGetPosition(); + num2 = stream.InternalEmulateRead(num2); + if (num2 == 0) + { + return (count - num3); + } + num = m_decoder.GetChars(stream.InternalGetBuffer(), position, num2, buffer, index);*/ + } + else + { + num2 = m_stream->Read(m_charBytes, 0, num2); + if (num2 == 0) + { + return (count - num3); + } + num = m_decoder.GetChars(m_charBytes, 0, num2, buffer, index); + } + num3 -= num; + index += num; + } + return count; } int BinaryReader::InternalReadOneChar() @@ -125,10 +175,10 @@ namespace System Int64 position; int num = 0; int byteCount = 0; - position = position = 0LL; - if (m_stream.CanSeek()) + position = 0LL; + if (m_stream->CanSeek()) { - position = m_stream.Position; + position = m_stream->Position; } if (m_charBytes == null) { @@ -141,7 +191,7 @@ namespace System while (num == 0) { byteCount = m_2BytesPerChar ? 2 : 1; - int num4 = m_stream.ReadByte(); + int num4 = m_stream->ReadByte(); m_charBytes[0] = (byte) num4; if (num4 == -1) { @@ -149,7 +199,7 @@ namespace System } if (byteCount == 2) { - num4 = m_stream.ReadByte(); + num4 = m_stream->ReadByte(); m_charBytes[1] = (byte) num4; if (num4 == -1) { @@ -167,9 +217,9 @@ namespace System } catch (Exception) { - if (m_stream.CanSeek()) + if (m_stream->CanSeek()) { - m_stream.Seek(position - m_stream.Position, SeekOrigin::Current); + m_stream->Seek(position - m_stream->Position, SeekOrigin::Current); } throw; } @@ -183,13 +233,13 @@ namespace System int BinaryReader::PeekChar() { - if (!m_stream.CanSeek()) + if (!m_stream->CanSeek()) { return -1; } - long position = m_stream.Position; + Int64 position = m_stream->Position; int num2 = this->Read(); - m_stream.Position = position; + m_stream->Position = position; return num2; } @@ -200,7 +250,7 @@ namespace System int BinaryReader::Read(byte buffer[], int index, int count) { - if(&m_stream == null) + if(!m_stream) { if (m_disposed) throw ObjectDisposedException("BinaryReader", "Cannot read from a closed BinaryReader."); @@ -220,7 +270,7 @@ namespace System if (Array::Length(buffer) < index + count) throw ArgumentException("buffer is too small"); - int bytes_read = m_stream.Read(buffer, index, count); + int bytes_read = m_stream->Read(buffer, index, count); return(bytes_read); } diff --git a/src/libmscorlib/BinaryWriter.cpp b/src/libmscorlib/BinaryWriter.cpp new file mode 100644 index 0000000..24cdc13 --- /dev/null +++ b/src/libmscorlib/BinaryWriter.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +namespace System +{ + namespace IO + { + const int BinaryWriter::LargeByteBufferSize = 0x100; + + BinaryWriter::BinaryWriter() + { + _tmpOneCharBuffer = new char[1]; + OutStream = new Stream(); + _buffer = new byte[0x10]; + _encoding = UTF8Encoding(false, true); + _encoder = _encoding.GetEncoder(); + } + + BinaryWriter::BinaryWriter(Stream* output) + { + _tmpOneCharBuffer = new char[1]; + + if (!output) + { + throw ArgumentNullException("output"); + } + + OutStream = output; + _buffer = new byte[0x10]; + _encoding = UTF8Encoding(false, true); + _encoder = _encoding.GetEncoder(); + } + + BinaryWriter::BinaryWriter(Stream* output, Encoding encoding) + { + _tmpOneCharBuffer = new char[1]; + + if (!output) + { + throw ArgumentNullException("output"); + } + + OutStream = output; + _buffer = new byte[0x10]; + _encoding = encoding; + _encoder = _encoding.GetEncoder(); + } + } +} diff --git a/src/libmscorlib/FileStream.cpp b/src/libmscorlib/FileStream.cpp index 6466822..a792b79 100644 --- a/src/libmscorlib/FileStream.cpp +++ b/src/libmscorlib/FileStream.cpp @@ -289,7 +289,7 @@ namespace System void FileStream::Write(byte array[], int offset, int count) { - XWriteFile(handle, &array[offset], count, null); + XWriteFile(handle, &array[offset], count, null); } void FileStream::WriteByte(byte value) diff --git a/src/libmscorlib/MemoryStream.cpp b/src/libmscorlib/MemoryStream.cpp index 1324e56..e7529c9 100644 --- a/src/libmscorlib/MemoryStream.cpp +++ b/src/libmscorlib/MemoryStream.cpp @@ -35,8 +35,7 @@ namespace System { namespace IO { - // The XBOX Limits us to 64MB; we do not take XDK/Debug units into account - const int MemoryStream::MemStreamMaxLength = 0x40000000; + const int MemoryStream::MemStreamMaxLength = 0x7fffffff; bool MemoryStream::CanRead() { @@ -123,7 +122,7 @@ namespace System MemoryStream::MemoryStream(byte buffer[]) { - if (buffer == null) + if (!buffer) { throw ArgumentNullException("buffer", "Buffer was NULL."); } @@ -138,7 +137,7 @@ namespace System MemoryStream::MemoryStream(byte buffer[], bool writable) { - if (buffer == null) + if (!buffer) { throw ArgumentNullException("buffer", "Buffer was NULL."); } @@ -152,7 +151,7 @@ namespace System MemoryStream::MemoryStream(byte buffer[], int index, int count) { - if (buffer == null) + if (!buffer) { throw ArgumentNullException("buffer", "Buffer was NULL."); } @@ -168,12 +167,13 @@ namespace System { throw ArgumentException("Invalid offset or length."); } - _buffer = buffer; - _origin = _position = index; - _length = _capacity = index + count; + _buffer = new byte[count]; + Array::Copy(buffer, index, _buffer, 0, count); + _origin = 0; + _length = _capacity = count; _writable = true; _exposable = false; - _expandable = true; + _expandable = false; _isOpen = true; } @@ -195,9 +195,10 @@ namespace System { throw ArgumentException("Invalid offset or length."); } - _buffer = buffer; - _origin = _position = index; - _length = _capacity = index + count; + _buffer = new byte[count]; + Array::Copy(buffer, index, _buffer, 0, count); + _origin = 0; + _length = _capacity = count; _writable = writable; _exposable = false; _expandable = true; @@ -222,12 +223,13 @@ namespace System { throw ArgumentException("Invalid offset or length."); } - _buffer = buffer; - _origin = _position = index; - _length = _capacity = index + count; + _buffer = new byte[count]; + Array::Copy(buffer, index, _buffer, 0, count); + _origin = 0; + _length = _capacity = count; _writable = writable; _exposable = publiclyVisible; - _expandable = true; + _expandable = false; _isOpen = true; } @@ -423,7 +425,7 @@ namespace System { throw NotSupportedException("This Stream does not support writing."); } - if (_buffer == null) + if (!buffer) { throw ArgumentNullException("buffer", "Buffer was null."); } @@ -499,13 +501,13 @@ namespace System _buffer[_position++] = value; } - void MemoryStream::WriteTo(Stream stream) + void MemoryStream::WriteTo(Stream* stream) { if (!_isOpen) { throw ObjectDisposedException(null, "Stream is closed."); } - stream.Write(_buffer, _origin, _length - _origin); + stream->Write(_buffer, _origin, _length - _origin); } } } diff --git a/src/libmscorlib/Stream.cpp b/src/libmscorlib/Stream.cpp index 35e0143..c01b48d 100644 --- a/src/libmscorlib/Stream.cpp +++ b/src/libmscorlib/Stream.cpp @@ -33,11 +33,17 @@ namespace System { namespace IO { + const Stream* Stream::Null = new Stream(); + Stream::Stream() { _asyncActiveCount = 1; } + Stream::~Stream() + { + } + bool Stream::CanTimeOut() { return false; @@ -53,6 +59,14 @@ namespace System Close(); } + void Stream::EndWrite(IAsyncResult* asyncResult) + { + if (!asyncResult) + { + throw ArgumentNullException("asyncResult"); + } + } + int Stream::ReadByte() { byte* buffer = new byte[1]; diff --git a/src/libmscorlib/StreamWriter.cpp b/src/libmscorlib/StreamWriter.cpp index 2b87cd9..9dd5694 100644 --- a/src/libmscorlib/StreamWriter.cpp +++ b/src/libmscorlib/StreamWriter.cpp @@ -1,15 +1,144 @@ +// Copyright (C) 2010-2012, XFX Team +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of the copyright holder nor the names of any +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + #include +#include +#include +#include #include +#include namespace System { namespace IO { + const int StreamWriter::DefaultBufferSize = 0x400; + Encoding StreamWriter::_UTF8NoBOM = UTF8Encoding(false, true); + + StreamWriter::StreamWriter(Stream* stream) + : TextWriter(null) + { + if (!stream) + { + throw ArgumentNullException("stream"); + } + if (!stream->CanWrite()) + { + throw ArgumentException(""); + } + Init(stream, _UTF8NoBOM, DefaultBufferSize); + } + + StreamWriter::StreamWriter(char* path) + : TextWriter(null) + { + if (!path) + { + throw ArgumentNullException("path"); + } + Stream* stream = CreateFile(path, true); + Init(stream, _UTF8NoBOM, 0x400); + } + + StreamWriter::StreamWriter(Stream* stream, Encoding encoding) + : TextWriter(null) + { + if (!stream) + { + throw ArgumentNullException("stream"); + } + if (!stream->CanWrite()) + { + throw ArgumentException(""); + } + Init(stream, encoding, DefaultBufferSize); + } + + StreamWriter::StreamWriter(char* path, bool append) + : TextWriter(null) + { + if (!path) + { + throw ArgumentNullException("path"); + } + Stream* stream = CreateFile(path, append); + Init(stream, _UTF8NoBOM, 0x400); + } + + StreamWriter::StreamWriter(Stream* stream, Encoding encoding, int bufferSize) + : TextWriter(null) + { + if (!stream) + { + throw ArgumentNullException("stream"); + } + if (!stream->CanWrite()) + { + throw ArgumentException(""); + } + if (bufferSize < 0) + { + throw ArgumentOutOfRangeException("bufferSize", "Non-negative number required."); + } + Init(stream, encoding, bufferSize); + } + + StreamWriter::StreamWriter(char* path, bool append, Encoding encoding) + : TextWriter(null) + { + if (!path) + { + throw ArgumentNullException("path"); + } + Stream* stream = CreateFile(path, append); + Init(stream, encoding, 0x400); + } + + StreamWriter::StreamWriter(char* path, bool append, Encoding encoding, int bufferSize) + : TextWriter(null) + { + if (!path) + { + throw ArgumentNullException("path"); + } + Stream* stream = CreateFile(path, append); + Init(stream, encoding, bufferSize); + } + void StreamWriter::Close() { Dispose(true); } + Stream* StreamWriter::CreateFile(char* path, bool append) + { + return new FileStream(path, append ? FileMode::Append : FileMode::Create, FileAccess::Write, FileShare::Read, 0x1000, FileOptions::SequentialScan); + } + void StreamWriter::Dispose(bool disposing) { if(!closable) @@ -33,28 +162,81 @@ namespace System byte* preamble = encoding.GetPreamble(); if (Array::Length(preamble) > 0) { - stream.Write(preamble, 0, Array::Length(preamble)); + stream->Write(preamble, 0, Array::Length(preamble)); } } - int count = encoder.GetBytes(charBuffer, 0, charPos, &byteBuffer, 0, flushEncoder); + int count = encoder.GetBytes(charBuffer, 0, charPos, byteBuffer, 0, flushEncoder); charPos = 0; if (count > 0) { - stream.Write(&byteBuffer, 0, count); + stream->Write(byteBuffer, 0, count); } if(flushStream) { - stream.Flush(); + stream->Flush(); } } } + void StreamWriter::Init(Stream* stream, Encoding encoding, int bufferSize) + { + this->stream = stream; + this->encoding = encoding; + encoder = this->encoding.GetEncoder(); + if (bufferSize < 0x80) + { + bufferSize = 0x80; + } + charBuffer = new char[bufferSize]; + byteBuffer = new byte[encoding.GetMaxByteCount(bufferSize)]; + charLen = bufferSize; + if (stream->CanSeek() && (stream->Position > 0L)) + { + haveWrittenPreamble = true; + } + closable = true; + } + void StreamWriter::Write(char value) { if(charPos == charLen) { Flush(false, false); } + charBuffer[charPos] = value; + charPos++; + if (autoFlush) + { + Flush(true, false); + } + } + + void StreamWriter::Write(char buffer[]) + { + if (buffer) + { + int num3; + int num = 0; + for (int i = Array::Length(buffer); i > 0; i -= num3) + { + if (charPos == charLen) + { + Flush(false, false); + } + num3 = charLen - charPos; + if (num3 > i) + { + num3 = i; + } + Buffer::BlockCopy(buffer, num * 2, charBuffer, charPos * 2, num3 * 2); + charPos += num3; + num += num3; + } + if (autoFlush) + { + Flush(true, false); + } + } } } } diff --git a/src/libmscorlib/TextWriter.cpp b/src/libmscorlib/TextWriter.cpp new file mode 100644 index 0000000..b7a7e6e --- /dev/null +++ b/src/libmscorlib/TextWriter.cpp @@ -0,0 +1,67 @@ +// Copyright (C) 2010-2012, XFX Team +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of the copyright holder nor the names of any +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include + +namespace System +{ + namespace IO + { + const char* TextWriter::InitialNewLine = "\r\n"; + TextWriter::TextWriter() + { + CoreNewLine = new char[] { '\r', '\n' }; + InternalFormatProvider = null; + } + + TextWriter::TextWriter(IFormatProvider* provider) + { + CoreNewLine = new char[] { '\r', '\n' }; + InternalFormatProvider = provider; + } + + TextWriter::TextWriter(const TextWriter &obj) + { + CoreNewLine = obj.CoreNewLine; + InternalFormatProvider = obj.InternalFormatProvider; + } + + void TextWriter::Write(bool value) + { + Write((value ? "True" : "False")); + } + + void TextWriter::Write(char buffer[]) + { + if (buffer) + { + Write(buffer, 0, Array::Length(buffer)); + } + + } + } +} diff --git a/src/libmscorlib/Version.cpp b/src/libmscorlib/Version.cpp index 6f34c65..35f59fe 100644 --- a/src/libmscorlib/Version.cpp +++ b/src/libmscorlib/Version.cpp @@ -85,10 +85,26 @@ namespace System char* Version::ToString(int fieldCount) { - if(fieldCount <= 0 || fieldCount > 4) + switch(fieldCount) + { + case 0: + return ""; + break; + case 1: + return String::Format("%d", _major); + break; + case 2: + return String::Format("%d.%d", _major, _minor); + break; + case 3: + return String::Format("%d.%d.%d", _major, _minor, _build); + break; + case 4: + return String::Format("%d.%d.%d.%d", _major, _minor, _build, _revision); + break; + default: throw ArgumentOutOfRangeException("fieldCount"); - - + } } bool Version::operator !=(Version other) diff --git a/src/libmscorlib/libmscorlib.vcproj b/src/libmscorlib/libmscorlib.vcproj index 37f65c8..a4e238c 100644 --- a/src/libmscorlib/libmscorlib.vcproj +++ b/src/libmscorlib/libmscorlib.vcproj @@ -183,6 +183,10 @@ RelativePath=".\BinaryReader.cpp" > + + @@ -227,6 +231,10 @@ RelativePath=".\StreamWriter.cpp" > + + + + @@ -541,6 +553,10 @@ RelativePath="..\..\include\System\Text\Encoding.h" > + +