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