From ae57e56a98aa4c62d0d0c882dd81b2883e52036f Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Thu, 26 Nov 2009 08:26:01 +0000 Subject: [PATCH] FS#70 - Add support for unicode strings in sf::Packet git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1287 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/Network/Packet.hpp | 4 +++ samples/opengl/OpenGL.cpp | 1 - src/SFML/Network/Packet.cpp | 44 ++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/SFML/Network/Packet.hpp b/include/SFML/Network/Packet.hpp index 05cd9159f..0fde1d1f7 100644 --- a/include/SFML/Network/Packet.hpp +++ b/include/SFML/Network/Packet.hpp @@ -35,6 +35,8 @@ namespace sf { +class String; + //////////////////////////////////////////////////////////// /// Packet wraps data to send / to receive through the network //////////////////////////////////////////////////////////// @@ -120,6 +122,7 @@ public : Packet& operator >>(std::string& data); Packet& operator >>(wchar_t* data); Packet& operator >>(std::wstring& data); + Packet& operator >>(String& data); //////////////////////////////////////////////////////////// /// Operator << overloads to put data into the packet @@ -138,6 +141,7 @@ public : Packet& operator <<(const std::string& data); Packet& operator <<(const wchar_t* data); Packet& operator <<(const std::wstring& data); + Packet& operator <<(const String& data); private : diff --git a/samples/opengl/OpenGL.cpp b/samples/opengl/OpenGL.cpp index 4fdf74e8a..a5232ee13 100644 --- a/samples/opengl/OpenGL.cpp +++ b/samples/opengl/OpenGL.cpp @@ -145,7 +145,6 @@ int main() text.SetPosition(250.f, 450.f); text.SetColor(sf::Color(255, 255, 255, 170)); window.Draw(text); - std::wstring s = text.GetString(); // Finally, display the rendered frame on screen window.Display(); diff --git a/src/SFML/Network/Packet.cpp b/src/SFML/Network/Packet.cpp index df370f223..e54b0fce9 100644 --- a/src/SFML/Network/Packet.cpp +++ b/src/SFML/Network/Packet.cpp @@ -27,6 +27,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include @@ -248,7 +249,7 @@ Packet& Packet::operator >>(wchar_t* data) Uint32 length; *this >> length; - if ((length > 0) && CheckSize(length * sizeof(Int32))) + if ((length > 0) && CheckSize(length * sizeof(Uint32))) { // Then extract characters for (Uint32 i = 0; i < length; ++i) @@ -269,7 +270,7 @@ Packet& Packet::operator >>(std::wstring& data) *this >> length; data.clear(); - if ((length > 0) && CheckSize(length * sizeof(Int32))) + if ((length > 0) && CheckSize(length * sizeof(Uint32))) { // Then extract characters for (Uint32 i = 0; i < length; ++i) @@ -282,6 +283,26 @@ Packet& Packet::operator >>(std::wstring& data) return *this; } +Packet& Packet::operator >>(String& data) +{ + // First extract the string length + Uint32 length; + *this >> length; + + data.Clear(); + if ((length > 0) && CheckSize(length * sizeof(Uint32))) + { + // Then extract characters + for (Uint32 i = 0; i < length; ++i) + { + Uint32 character; + *this >> character; + data += character; + } + } + + return *this; +} //////////////////////////////////////////////////////////// @@ -373,7 +394,7 @@ Packet& Packet::operator <<(const wchar_t* data) // Then insert characters for (const wchar_t* c = data; *c != L'\0'; ++c) - *this << static_cast(*c); + *this << static_cast(*c); return *this; } @@ -387,7 +408,22 @@ Packet& Packet::operator <<(const std::wstring& data) if (length > 0) { for (std::wstring::const_iterator c = data.begin(); c != data.end(); ++c) - *this << static_cast(*c); + *this << static_cast(*c); + } + + return *this; +} +Packet& Packet::operator <<(const String& data) +{ + // First insert the string length + Uint32 length = static_cast(data.GetSize()); + *this << length; + + // Then insert characters + if (length > 0) + { + for (String::ConstIterator c = data.Begin(); c != data.End(); ++c) + *this << *c; } return *this;