mixer: fade: Handle empty mood strings gracefully.
[paraslash.git] / portable_io.h
1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file portable_io.h Inline functions for binary IO. */
4
5 static inline uint64_t read_portable(unsigned bits, const void *buf)
6 {
7 uint64_t ret = 0;
8 int i, num_bytes = bits / 8;
9 const uint8_t *p = (typeof(p))buf;
10
11 for (i = 0; i < num_bytes; i++) {
12 unsigned char c = p[i];
13 ret += ((uint64_t)c << (8 * i));
14 }
15 return ret;
16 }
17
18 static inline uint64_t read_portable_be(unsigned bits, const void *buf)
19 {
20 uint64_t ret = 0;
21 int i, num_bytes = bits / 8;
22 const uint8_t *p = (typeof(p))buf;
23
24 for (i = 0; i < num_bytes; i++) {
25 unsigned char c = p[i];
26 ret += ((uint64_t)c << (8 * (num_bytes - i - 1)));
27 }
28 return ret;
29 }
30
31 static inline uint64_t read_u64(const void *buf)
32 {
33 return read_portable(64, buf);
34 }
35
36 static inline uint32_t read_u32(const void *buf)
37 {
38 return read_portable(32, buf);
39 }
40
41 static inline uint16_t read_u16(const void *buf)
42 {
43 return read_portable(16, buf);
44 }
45
46 static inline uint8_t read_u8(const void *buf)
47 {
48 return read_portable(8, buf);
49 }
50
51 static inline uint64_t read_u64_be(const void *buf)
52 {
53 return read_portable_be(64, buf);
54 }
55
56 static inline uint32_t read_u32_be(const void *buf)
57 {
58 return read_portable_be(32, buf);
59 }
60
61 static inline uint16_t read_u16_be(const void *buf)
62 {
63 return read_portable_be(16, buf);
64 }
65
66 static inline void write_portable(unsigned bits, char *buf, uint64_t val)
67 {
68 int i, num_bytes = bits / 8;
69 uint8_t *p = (typeof(p))buf;
70
71 for (i = 0; i < num_bytes; i++) {
72 p[i] = val & 0xff;
73 val = val >> 8;
74 }
75 }
76
77 static inline void write_portable_be(unsigned bits, char *buf, uint64_t val)
78 {
79 int i, num_bytes = bits / 8;
80 uint8_t *p = (typeof(p))buf;
81
82 for (i = 0; i < num_bytes; i++) {
83 p[num_bytes - i - 1] = val & 0xff;
84 val = val >> 8;
85 }
86 }
87
88 static inline void write_u64(void *buf, uint64_t val)
89 {
90 write_portable(64, buf, val);
91 }
92
93 static inline void write_u32(void *buf, uint32_t val)
94 {
95 write_portable(32, buf, (uint64_t) val);
96 }
97
98 static inline void write_u16(void *buf, uint16_t val)
99 {
100 write_portable(16, buf, (uint64_t) val);
101 }
102
103 static inline void write_u8(void *buf, uint8_t val)
104 {
105 write_portable(8, buf, (uint64_t) val);
106 }
107
108 static inline void write_u64_be(void *buf, uint64_t val)
109 {
110 write_portable_be(64, buf, val);
111 }
112
113 static inline void write_u32_be(void *buf, uint32_t val)
114 {
115 write_portable_be(32, buf, (uint64_t) val);
116 }
117
118 static inline void write_u16_be(void *buf, uint16_t val)
119 {
120 write_portable_be(16, buf, (uint64_t) val);
121 }