12
UNKNOWN_ENDIAN = 0x0000
15
#define UNKNOWN_ENDIAN (0x0000)
17
int get_host_endian();
18
int parse_endian(const char* arg);
19
const char* endian2str(int endian);
21
// change the byte order of given variable
22
template <typename Type>
23
Type change_byte_order(const Type& v)
26
const size_t size = sizeof(v);
27
uint8_t* first = (uint8_t*)(&t);
28
uint8_t* last = first+size-1;
29
while (first < last) {
30
std::swap(*first++, *last--);
40
static TargetType create(const T& from)
47
#define DEFINE_OTHER_TYPE(__T__) typedef __T__##_BE TargetType
49
#define DEFINE_OTHER_TYPE(__T__) typedef __T__##_LE TargetType
53
// we always defined a reversed layout of big-endian and little-endian
54
// bit-field struct, so such kind of struct need to be reverted if host
55
// arch is different from build arch.
58
bool revert_write(const T& t, FILE *fp)
60
T reverted = change_byte_order(t);
61
typename OtherEndian<T>::TargetType o =
62
OtherEndian<T>::create(reverted);
63
return fwrite(&o, sizeof(o), 1, fp) == 1;
67
// if the struct has non-bit-field member(s), TTransUnit, among others,
68
// the order of members is the same as how they are defined.
74
Writer(FILE *fp, bool doRevert)
75
: m_fp (fp), m_doRevert(doRevert)
79
bool write(const T& t)
82
return revert_write(t, m_fp);
84
return fwrite(&t, sizeof(t), 1, m_fp) == 1;
89
bool write(const T* t, size_t len)
91
for (unsigned i = 0; i < len; i++) {
99
const bool m_doRevert;