From b8d3b08f104a2c234c50e7caac2a2f414d7bec06 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 18 Dec 2014 17:24:19 +0300 Subject: [PATCH] Implement fixed integer r/w methods --- SDL2pp/RWops.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/RWops.hh | 13 ++++++++++++ tests/test_rwops.cc | 30 ++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/SDL2pp/RWops.cc b/SDL2pp/RWops.cc index 0763fc1..49ebaea 100644 --- a/SDL2pp/RWops.cc +++ b/SDL2pp/RWops.cc @@ -197,4 +197,52 @@ Sint64 RWops::Size() { return size; } +Uint16 RWops::ReadBE16() { + return SDL_ReadBE16(rwops_); +} + +Uint32 RWops::ReadBE32() { + return SDL_ReadBE32(rwops_); +} + +Uint64 RWops::ReadBE64() { + return SDL_ReadBE64(rwops_); +} + +Uint16 RWops::ReadLE16() { + return SDL_ReadLE16(rwops_); +} + +Uint32 RWops::ReadLE32() { + return SDL_ReadLE32(rwops_); +} + +Uint64 RWops::ReadLE64() { + return SDL_ReadLE64(rwops_); +} + +size_t RWops::WriteBE16(Uint16 value) { + return SDL_WriteBE16(rwops_, value); +} + +size_t RWops::WriteBE32(Uint32 value) { + return SDL_WriteBE32(rwops_, value); +} + +size_t RWops::WriteBE64(Uint64 value) { + return SDL_WriteBE64(rwops_, value); +} + +size_t RWops::WriteLE16(Uint16 value) { + return SDL_WriteLE16(rwops_, value); +} + +size_t RWops::WriteLE32(Uint32 value) { + return SDL_WriteLE32(rwops_, value); +} + +size_t RWops::WriteLE64(Uint64 value) { + return SDL_WriteLE64(rwops_, value); +} + } diff --git a/SDL2pp/RWops.hh b/SDL2pp/RWops.hh index e46b8f8..504af67 100644 --- a/SDL2pp/RWops.hh +++ b/SDL2pp/RWops.hh @@ -113,6 +113,19 @@ public: size_t Write(const void* ptr, size_t size, size_t num); Sint64 Tell(); Sint64 Size(); + + Uint16 ReadBE16(); + Uint32 ReadBE32(); + Uint64 ReadBE64(); + Uint16 ReadLE16(); + Uint32 ReadLE32(); + Uint64 ReadLE64(); + size_t WriteBE16(Uint16 value); + size_t WriteBE32(Uint32 value); + size_t WriteBE64(Uint64 value); + size_t WriteLE16(Uint16 value); + size_t WriteLE32(Uint32 value); + size_t WriteLE64(Uint64 value); }; } diff --git a/tests/test_rwops.cc b/tests/test_rwops.cc index 9cc2a2a..b9b7275 100644 --- a/tests/test_rwops.cc +++ b/tests/test_rwops.cc @@ -233,6 +233,36 @@ BEGIN_TEST() rw.Close(); } + + // Fixed width reads/writes + { + std::vector data, outdata; + for (int i = 0; i < 28; i++) + data.push_back(i); + + RWops rw((ContainerRWops>(data))); + + EXPECT_EQUAL(rw.ReadBE16(), 0x0001U); + EXPECT_EQUAL(rw.ReadLE16(), 0x0302U); + EXPECT_EQUAL(rw.ReadBE32(), 0x04050607U); + EXPECT_EQUAL(rw.ReadLE32(), 0x0B0A0908U); + EXPECT_EQUAL(rw.ReadBE64(), 0x0C0D0E0F10111213ULL); + EXPECT_EQUAL(rw.ReadLE64(), 0x1B1A191817161514ULL); + + RWops rw1((ContainerRWops>(outdata))); + + EXPECT_EQUAL(rw1.WriteBE16(0x0001U), 1U); + EXPECT_EQUAL(rw1.WriteLE16(0x0302U), 1U); + EXPECT_EQUAL(rw1.WriteBE32(0x04050607U), 1U); + EXPECT_EQUAL(rw1.WriteLE32(0x0B0A0908U), 1U); + EXPECT_EQUAL(rw1.WriteBE64(0x0C0D0E0F10111213ULL), 1U); + EXPECT_EQUAL(rw1.WriteLE64(0x1B1A191817161514ULL), 1U); + + EXPECT_EQUAL(data.size(), outdata.size()); + + EXPECT_TRUE(data == outdata); + } + HANDLE_EXCEPTION(Exception& e) std::cerr << "unexpected SDL exception was thrown during the test: " << e.what() << ": " << e.GetSDLError() << std::endl; END_TEST()