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
This commit is contained in:
LaurentGom 2009-11-26 08:26:01 +00:00
parent 78247bd386
commit ae57e56a98
3 changed files with 44 additions and 5 deletions

View File

@ -35,6 +35,8 @@
namespace sf namespace sf
{ {
class String;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Packet wraps data to send / to receive through the network /// Packet wraps data to send / to receive through the network
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -120,6 +122,7 @@ public :
Packet& operator >>(std::string& data); Packet& operator >>(std::string& data);
Packet& operator >>(wchar_t* data); Packet& operator >>(wchar_t* data);
Packet& operator >>(std::wstring& data); Packet& operator >>(std::wstring& data);
Packet& operator >>(String& data);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Operator << overloads to put data into the packet /// Operator << overloads to put data into the packet
@ -138,6 +141,7 @@ public :
Packet& operator <<(const std::string& data); Packet& operator <<(const std::string& data);
Packet& operator <<(const wchar_t* data); Packet& operator <<(const wchar_t* data);
Packet& operator <<(const std::wstring& data); Packet& operator <<(const std::wstring& data);
Packet& operator <<(const String& data);
private : private :

View File

@ -145,7 +145,6 @@ int main()
text.SetPosition(250.f, 450.f); text.SetPosition(250.f, 450.f);
text.SetColor(sf::Color(255, 255, 255, 170)); text.SetColor(sf::Color(255, 255, 255, 170));
window.Draw(text); window.Draw(text);
std::wstring s = text.GetString();
// Finally, display the rendered frame on screen // Finally, display the rendered frame on screen
window.Display(); window.Display();

View File

@ -27,6 +27,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Network/Packet.hpp> #include <SFML/Network/Packet.hpp>
#include <SFML/Network/SocketHelper.hpp> #include <SFML/Network/SocketHelper.hpp>
#include <SFML/System/String.hpp>
#include <string.h> #include <string.h>
@ -248,7 +249,7 @@ Packet& Packet::operator >>(wchar_t* data)
Uint32 length; Uint32 length;
*this >> length; *this >> length;
if ((length > 0) && CheckSize(length * sizeof(Int32))) if ((length > 0) && CheckSize(length * sizeof(Uint32)))
{ {
// Then extract characters // Then extract characters
for (Uint32 i = 0; i < length; ++i) for (Uint32 i = 0; i < length; ++i)
@ -269,7 +270,7 @@ Packet& Packet::operator >>(std::wstring& data)
*this >> length; *this >> length;
data.clear(); data.clear();
if ((length > 0) && CheckSize(length * sizeof(Int32))) if ((length > 0) && CheckSize(length * sizeof(Uint32)))
{ {
// Then extract characters // Then extract characters
for (Uint32 i = 0; i < length; ++i) for (Uint32 i = 0; i < length; ++i)
@ -282,6 +283,26 @@ Packet& Packet::operator >>(std::wstring& data)
return *this; 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 // Then insert characters
for (const wchar_t* c = data; *c != L'\0'; ++c) for (const wchar_t* c = data; *c != L'\0'; ++c)
*this << static_cast<Int32>(*c); *this << static_cast<Uint32>(*c);
return *this; return *this;
} }
@ -387,7 +408,22 @@ Packet& Packet::operator <<(const std::wstring& data)
if (length > 0) if (length > 0)
{ {
for (std::wstring::const_iterator c = data.begin(); c != data.end(); ++c) for (std::wstring::const_iterator c = data.begin(); c != data.end(); ++c)
*this << static_cast<Int32>(*c); *this << static_cast<Uint32>(*c);
}
return *this;
}
Packet& Packet::operator <<(const String& data)
{
// First insert the string length
Uint32 length = static_cast<Uint32>(data.GetSize());
*this << length;
// Then insert characters
if (length > 0)
{
for (String::ConstIterator c = data.Begin(); c != data.End(); ++c)
*this << *c;
} }
return *this; return *this;