fourcc_c: new functions shift_read(); equiv() for std::vector<std::string>; human_readable()

This commit is contained in:
Moritz Bunkus 2013-02-16 00:00:29 +01:00
parent b65bafe694
commit de66a66e8b
3 changed files with 71 additions and 0 deletions

View File

@ -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<std::string> 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);
}

View File

@ -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<std::string> const &cmp) const;
bool human_readable(size_t min_num = 3) const;
explicit operator bool() const;
bool operator ==(fourcc_c const &cmp) const;

View File

@ -300,6 +300,34 @@ TEST(FourCC, Equivalence) {
EXPECT_FALSE(fourcc_c{"ABCD"}.equiv("qwer"));
EXPECT_FALSE(fourcc_c{"abcd"}.equiv("qwer"));
std::vector<std::string> 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());
}
}