libcruft-util/cruft/util/fourcc.cpp

71 lines
1.8 KiB
C++
Raw Permalink Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
*/
#include "fourcc.hpp"
2018-03-22 16:10:06 +11:00
#include <ostream>
using cruft::fourcc;
2012-11-09 15:11:02 +11:00
static_assert (sizeof(fourcc) == 4, "fourcc must be a 4 byte POD");
///////////////////////////////////////////////////////////////////////////////
fourcc
fourcc::from_chars (uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
fourcc lhs;
lhs.data[0] = a;
lhs.data[1] = b;
lhs.data[2] = c;
lhs.data[3] = d;
return lhs;
}
//-----------------------------------------------------------------------------
fourcc
2012-11-09 15:11:02 +11:00
fourcc::from_string (const char data[4]) {
fourcc lhs;
lhs.data[0] = static_cast<uint8_t> (data[0]);
lhs.data[1] = static_cast<uint8_t> (data[1]);
lhs.data[2] = static_cast<uint8_t> (data[2]);
lhs.data[3] = static_cast<uint8_t> (data[3]);
return lhs;
}
///////////////////////////////////////////////////////////////////////////////
bool
fourcc::operator== (const char rhs[4]) const {
return data[0] == rhs[0] &&
data[1] == rhs[1] &&
data[2] == rhs[2] &&
data[3] == rhs[3];
}
///////////////////////////////////////////////////////////////////////////////
fourcc::operator uint32_t (void) const {
return static_cast<uint32_t> (data[0] << 24U |
data[1] << 16U |
data[2] << 8U |
data[3]);
}
///////////////////////////////////////////////////////////////////////////////
std::ostream&
cruft::operator<< (std::ostream &os, fourcc f) {
os << f.data[0] << f.data[1] << f.data[2] << f.data[3];
return os;
}