From de66a66e8b38be6866b082ca539b2ce60b7d2f9e Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sat, 16 Feb 2013 00:00:29 +0100 Subject: [PATCH] fourcc_c: new functions shift_read(); equiv() for std::vector; human_readable() --- src/common/fourcc.cpp | 37 ++++++++++++++++++++++++++++++++++++ src/common/fourcc.h | 6 ++++++ tests/unit/common/fourcc.cpp | 28 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/common/fourcc.cpp b/src/common/fourcc.cpp index a4914fae9..fc3a40773 100644 --- a/src/common/fourcc.cpp +++ b/src/common/fourcc.cpp @@ -157,6 +157,24 @@ fourcc_c::equiv(char const *cmp) return balg::to_lower_copy(str()) == balg::to_lower_copy(std::string{cmp}); } +bool +fourcc_c::equiv(std::vector const &cmp) + const { + auto lower = balg::to_lower_copy(str()); + for (auto &s : cmp) + if (lower == balg::to_lower_copy(s)) + return true; + return false; +} + +bool +fourcc_c::human_readable(size_t min_num) + const { + static auto char_ok = [](char c) { return (('a' <= c) && ('z' >= c)) || (('A' <= c) && ('Z' >= c)) || (('0' <= c) && ('9' >= c)) || (0xa9 == c); }; + auto lower = balg::to_lower_copy(str()); + return min_num <= boost::accumulate(lower, 0u, [](unsigned num, char c) { return num + (char_ok(c) ? 1 : 0); }); +} + uint32_t fourcc_c::read(void const *mem, fourcc_c::byte_order_t byte_order) { @@ -168,3 +186,22 @@ fourcc_c::read(mm_io_c &io, fourcc_c::byte_order_t byte_order) { return val(io.read_uint32_be(), byte_order); } + +fourcc_c & +fourcc_c::shift_read(mm_io_cptr const &io, + byte_order_t byte_order) { + return shift_read(*io, byte_order); +} + +fourcc_c & +fourcc_c::shift_read(mm_io_c &io, + byte_order_t byte_order) { + m_value = big_endian == byte_order ? (m_value << 8) | io.read_uint8() : (m_value >> 8) | (io.read_uint8() << 24); + return *this; +} + +fourcc_c & +fourcc_c::shift_read(mm_io_c *io, + byte_order_t byte_order) { + return shift_read(*io, byte_order); +} diff --git a/src/common/fourcc.h b/src/common/fourcc.h index 07f38c35f..adb9c8327 100644 --- a/src/common/fourcc.h +++ b/src/common/fourcc.h @@ -47,6 +47,10 @@ public: fourcc_c(mm_io_c &io, byte_order_t byte_order = big_endian); fourcc_c(mm_io_c *io, byte_order_t byte_order = big_endian); + fourcc_c &shift_read(mm_io_cptr const &io, byte_order_t byte_order = big_endian); + fourcc_c &shift_read(mm_io_c &io, byte_order_t byte_order = big_endian); + fourcc_c &shift_read(mm_io_c *io, byte_order_t byte_order = big_endian); + size_t write(memory_cptr const &mem, byte_order_t byte_order = big_endian); size_t write(unsigned char *mem, byte_order_t byte_order = big_endian); size_t write(mm_io_cptr const &io, byte_order_t byte_order = big_endian); @@ -59,6 +63,8 @@ public: std::string str() const; bool equiv(char const *cmp) const; + bool equiv(std::vector const &cmp) const; + bool human_readable(size_t min_num = 3) const; explicit operator bool() const; bool operator ==(fourcc_c const &cmp) const; diff --git a/tests/unit/common/fourcc.cpp b/tests/unit/common/fourcc.cpp index b7840f987..35782ef62 100644 --- a/tests/unit/common/fourcc.cpp +++ b/tests/unit/common/fourcc.cpp @@ -300,6 +300,34 @@ TEST(FourCC, Equivalence) { EXPECT_FALSE(fourcc_c{"ABCD"}.equiv("qwer")); EXPECT_FALSE(fourcc_c{"abcd"}.equiv("qwer")); + + std::vector vec{ "abcd", "WeRt", "uiOP", "VBNM" }; + EXPECT_TRUE(fourcc_c{"ABCD"}.equiv(vec)); + EXPECT_TRUE(fourcc_c{"WeRt"}.equiv(vec)); + EXPECT_TRUE(fourcc_c{"vbnm"}.equiv(vec)); + + EXPECT_FALSE(fourcc_c{"dcba"}.equiv(vec)); +} + +TEST(FourCC, HumanReadable) { + EXPECT_TRUE(fourcc_c{"ABCD"}.human_readable()); + EXPECT_TRUE(fourcc_c{"ABC?"}.human_readable()); + EXPECT_TRUE(fourcc_c{"AB??"}.human_readable(2)); + + EXPECT_TRUE(fourcc_c{"abcd"}.human_readable()); + EXPECT_TRUE(fourcc_c{"abc?"}.human_readable()); + EXPECT_TRUE(fourcc_c{"ab??"}.human_readable(2)); + + EXPECT_TRUE(fourcc_c{"a007"}.human_readable()); + EXPECT_TRUE(fourcc_c{"?007"}.human_readable()); + + EXPECT_FALSE(fourcc_c{"abc?"}.human_readable(4)); + EXPECT_FALSE(fourcc_c{"ab??"}.human_readable()); + + EXPECT_FALSE(fourcc_c{"abc?"}.human_readable(4)); + EXPECT_FALSE(fourcc_c{"ab??"}.human_readable()); + + EXPECT_FALSE(fourcc_c{"??07"}.human_readable()); } }