]> git.jsancho.org Git - lugaru.git/blob - Source/private.h
License: Update GPLv2+ header to match current FSF recommendation
[lugaru.git] / Source / private.h
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef private_h
21 #define private_h
22
23 #include <stdarg.h>
24 #include <stddef.h>
25
26 #define BinIO_TYPE_IGNORE_BYTE         'x'
27 #define BinIO_TYPE_BYTE                'b'
28 #define BinIO_TYPE_INT16               's'
29 #define BinIO_TYPE_INT32               'i'
30 #define BinIO_TYPE_INT64               'l'
31 #define BinIO_TYPE_FLOAT32             'f'
32 #define BinIO_TYPE_FLOAT64             'd'
33
34 #define BinIO_LITTLE_ENDIAN_BYTE_ORDER 'L'
35 #define BinIO_BIG_ENDIAN_BYTE_ORDER    'B'
36 #define BinIO_HOST_BYTE_ORDER          'H'
37 #define BinIO_NETWORK_BYTE_ORDER       'N'
38
39 #ifndef ALREADY_DID_BINIO_STDINT
40 #define ALREADY_DID_BINIO_STDINT
41 #if defined(BinIO_STDINT_HEADER)
42 #include BinIO_STDINT_HEADER
43 typedef float              float32_t;
44 typedef double             float64_t;
45 #else
46 typedef unsigned char      uint8_t;
47 typedef unsigned short     uint16_t;
48 typedef unsigned long      uint32_t;
49 #ifdef WIN32
50 typedef unsigned __int64   uint64_t;
51 #else
52 typedef unsigned long long uint64_t;
53 #endif
54 typedef float              float32_t;
55 typedef double             float64_t;
56 #endif
57 #endif
58
59 #ifndef BinIO_INLINE
60 #if defined(__GNUC__)
61 #define BinIO_INLINE static inline
62 #else
63 #define BinIO_INLINE static
64 #endif
65 #endif
66
67 #ifndef BinIO_BYTE_ORDER
68 #if defined(__ppc__) || defined(__POWERPC__)
69 #define BinIO_BYTE_ORDER BinIO_BIG_ENDIAN_BYTE_ORDER
70 #else
71 #define BinIO_BYTE_ORDER BinIO_LITTLE_ENDIAN_BYTE_ORDER
72 #endif
73 #endif
74
75 BinIO_INLINE void BinIOSwap1(const uint8_t *src, uint8_t *dst)
76 {
77     *dst = *src;
78 }
79
80 BinIO_INLINE void BinIOSwap2(const uint8_t *src, uint8_t *dst)
81 {
82     *(dst + 1) = *(src + 0);
83     *(dst + 0) = *(src + 1);
84 }
85
86 BinIO_INLINE void BinIOSwap4(const uint8_t *src, uint8_t *dst)
87 {
88     *(dst + 3) = *(src + 0);
89     *(dst + 2) = *(src + 1);
90     *(dst + 1) = *(src + 2);
91     *(dst + 0) = *(src + 3);
92 }
93
94 BinIO_INLINE void BinIOSwap8(const uint8_t *src, uint8_t *dst)
95 {
96     *(dst + 7) = *(src + 0);
97     *(dst + 6) = *(src + 1);
98     *(dst + 5) = *(src + 2);
99     *(dst + 4) = *(src + 3);
100     *(dst + 3) = *(src + 4);
101     *(dst + 2) = *(src + 5);
102     *(dst + 1) = *(src + 6);
103     *(dst + 0) = *(src + 7);
104 }
105
106 BinIO_INLINE int BinIONormalizeByteOrder(int byte_order)
107 {
108     if (byte_order == BinIO_HOST_BYTE_ORDER) {
109         byte_order = BinIO_BYTE_ORDER;
110     } else if (byte_order == BinIO_NETWORK_BYTE_ORDER) {
111         byte_order = BinIO_BIG_ENDIAN_BYTE_ORDER;
112     }
113
114     return byte_order;
115 }
116
117 extern void BinIOConvert1(int from_byte_order, int to_byte_order,
118                           const uint8_t *src, uint8_t *dst,
119                           unsigned int count);
120 extern void BinIOConvert2(int from_byte_order, int to_byte_order,
121                           const uint8_t *src, uint8_t *dst,
122                           unsigned int count);
123 extern void BinIOConvert4(int from_byte_order, int to_byte_order,
124                           const uint8_t *src, uint8_t *dst,
125                           unsigned int count);
126 extern void BinIOConvert8(int from_byte_order, int to_byte_order,
127                           const uint8_t *src, uint8_t *dst,
128                           unsigned int count);
129
130 struct BinIOFormatCursor {
131     const char *cursor;
132     int         byte_order;
133     int         count;
134 };
135
136 typedef void (*BinIOProcessFunction)(void *context,
137                                      int   type,
138                                      int   byte_order,
139                                      int   count);
140
141 extern void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
142                                   const char               *format);
143
144 extern int BinIONextChar(void                     *context,
145                          struct BinIOFormatCursor *cursor,
146                          BinIOProcessFunction      func);
147
148 extern void BinIOCountBytes(void *context, int type, int byte_order, int count);
149
150 extern size_t BinIOFormatByteCount(const char *format);
151
152 #endif
153