7 #include "assert_helpers.h"
8 #include "endian_swap.h"
11 * Write a 32-bit unsigned to an output stream being careful to
12 * re-endianize if caller-requested endianness differs from current
15 static inline void writeU32(std::ostream& out, uint32_t x, bool toBigEndian) {
16 uint32_t y = endianizeU32(x, toBigEndian);
17 out.write((const char*)&y, 4);
21 * Write a 32-bit unsigned to an output stream using the native
24 static inline void writeU32(std::ostream& out, uint32_t x) {
25 out.write((const char*)&x, 4);
29 * Write a 32-bit signed int to an output stream being careful to
30 * re-endianize if caller-requested endianness differs from current
33 static inline void writeI32(std::ostream& out, int32_t x, bool toBigEndian) {
34 int32_t y = endianizeI32(x, toBigEndian);
35 out.write((const char*)&y, 4);
39 * Write a 32-bit unsigned to an output stream using the native
42 static inline void writeI32(std::ostream& out, int32_t x) {
43 out.write((const char*)&x, 4);
47 * Read a 32-bit unsigned from an input stream, inverting endianness
50 static inline uint32_t readU32(std::istream& in, bool swap) {
52 in.read((char *)&x, 4);
53 assert_eq(4, in.gcount());
55 return endianSwapU32(x);
62 * Read a 32-bit unsigned from a file descriptor, optionally inverting
65 static inline uint32_t readU32(int in, bool swap) {
67 if(read(in, (void *)&x, 4) != 4) {
71 return endianSwapU32(x);
78 * Read a 32-bit unsigned from a FILE*, optionally inverting
81 static inline uint32_t readU32(FILE* in, bool swap) {
83 if(fread((void *)&x, 1, 4, in) != 4) {
87 return endianSwapU32(x);
95 * Read a 32-bit signed from an input stream, inverting endianness
98 static inline int32_t readI32(std::istream& in, bool swap) {
100 in.read((char *)&x, 4);
101 assert_eq(4, in.gcount());
103 return endianSwapI32(x);
110 * Read a 32-bit unsigned from a file descriptor, optionally inverting
113 static inline uint32_t readI32(int in, bool swap) {
115 if(read(in, (void *)&x, 4) != 4) {
119 return endianSwapI32(x);
126 * Read a 32-bit unsigned from a FILE*, optionally inverting
129 static inline uint32_t readI32(FILE* in, bool swap) {
131 if(fread((void *)&x, 1, 4, in) != 4) {
135 return endianSwapI32(x);
142 #endif /*WORD_IO_H_*/