]> git.jsancho.org Git - lugaru.git/blob - Source/private.c
69eec69b3f2f1ed27fb386b477b32fc84699f67a
[lugaru.git] / Source / private.c
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 #include <string.h>
23
24 #include "private.h"
25
26 void BinIOConvert1(int from_byte_order, int to_byte_order,
27                    const uint8_t *src, uint8_t *dst,
28                    unsigned int count)
29 {
30     if (BinIONormalizeByteOrder(from_byte_order) !=
31             BinIONormalizeByteOrder(to_byte_order)) {
32         unsigned int i;
33         for (i = 0; i < count; ++i) {
34             BinIOSwap1(src, dst);
35             src += 1;
36             dst += 1;
37         }
38     } else {
39         memcpy(dst, src, 1 * count);
40     }
41 }
42
43 void BinIOConvert2(int from_byte_order, int to_byte_order,
44                    const uint8_t *src, uint8_t *dst,
45                    unsigned int count)
46 {
47     if (BinIONormalizeByteOrder(from_byte_order) !=
48             BinIONormalizeByteOrder(to_byte_order)) {
49         unsigned int i;
50         for (i = 0; i < count; ++i) {
51             BinIOSwap2(src, dst);
52             src += 2;
53             dst += 2;
54         }
55     } else {
56         memcpy(dst, src, 2 * count);
57     }
58 }
59
60 void BinIOConvert4(int from_byte_order, int to_byte_order,
61                    const uint8_t *src, uint8_t *dst,
62                    unsigned int count)
63 {
64     if (BinIONormalizeByteOrder(from_byte_order) !=
65             BinIONormalizeByteOrder(to_byte_order)) {
66         unsigned int i;
67         for (i = 0; i < count; ++i) {
68             BinIOSwap4(src, dst);
69             src += 4;
70             dst += 4;
71         }
72     } else {
73         memcpy(dst, src, 4 * count);
74     }
75 }
76
77 void BinIOConvert8(int from_byte_order, int to_byte_order,
78                    const uint8_t *src, uint8_t *dst,
79                    unsigned int count)
80 {
81     if (BinIONormalizeByteOrder(from_byte_order) !=
82             BinIONormalizeByteOrder(to_byte_order)) {
83         unsigned int i;
84         for (i = 0; i < count; ++i) {
85             BinIOSwap8(src, dst);
86             src += 8;
87             dst += 8;
88         }
89     } else {
90         memcpy(dst, src, 8 * count);
91     }
92 }
93
94 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
95                            const char               *format)
96 {
97     cursor->cursor = format;
98     cursor->byte_order = BinIO_HOST_BYTE_ORDER;
99     cursor->count = -1;
100 }
101
102 int BinIONextChar(void                     *context,
103                   struct BinIOFormatCursor *cursor,
104                   BinIOProcessFunction      func)
105 {
106     int count, value;
107     int c;
108     switch (c = *(cursor->cursor)++) {
109     case BinIO_LITTLE_ENDIAN_BYTE_ORDER:
110     case BinIO_BIG_ENDIAN_BYTE_ORDER:
111     case BinIO_HOST_BYTE_ORDER:
112     case BinIO_NETWORK_BYTE_ORDER:
113         cursor->byte_order = c;
114         break;
115
116     case '0':
117     case '1':
118     case '2':
119     case '3':
120     case '4':
121     case '5':
122     case '6':
123     case '7':
124     case '8':
125     case '9':
126         count = cursor->count;
127         value = c - '0';
128         if (count == -1) {
129             cursor->count = value;
130         } else {
131             cursor->count = (count * 10) + value;
132         }
133         break;
134
135     case BinIO_TYPE_IGNORE_BYTE:
136     case BinIO_TYPE_BYTE:
137     case BinIO_TYPE_INT16:
138     case BinIO_TYPE_INT32:
139     case BinIO_TYPE_INT64:
140     case BinIO_TYPE_FLOAT32:
141     case BinIO_TYPE_FLOAT64:
142         func(context, c, cursor->byte_order, cursor->count);
143         cursor->byte_order = BinIO_HOST_BYTE_ORDER;
144         cursor->count = -1;
145         break;
146
147     case ' ':
148     case '\t':
149     case '\r':
150     case '\n':
151         break;
152
153     default:
154         return 0;
155     }
156
157     return 1;
158 }
159
160 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
161 {
162     size_t type_size = 0;
163
164     if (count == -1) {
165         count = 1;
166     }
167
168     switch (type) {
169     case BinIO_TYPE_IGNORE_BYTE:
170         type_size = 1;
171         break;
172     case BinIO_TYPE_BYTE:
173         type_size = 1;
174         break;
175     case BinIO_TYPE_INT16:
176         type_size = 2;
177         break;
178     case BinIO_TYPE_INT32:
179         type_size = 4;
180         break;
181     case BinIO_TYPE_INT64:
182         type_size = 8;
183         break;
184     case BinIO_TYPE_FLOAT32:
185         type_size = 4;
186         break;
187     case BinIO_TYPE_FLOAT64:
188         type_size = 8;
189         break;
190     }
191
192     *(size_t*)context += type_size * count;
193 }
194
195 extern size_t BinIOFormatByteCount(const char *format)
196 {
197     struct BinIOFormatCursor cursor;
198     size_t n_bytes = 0;
199
200     BinIOInitFormatCursor(&cursor, format);
201
202     while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}
203
204     return n_bytes;
205 }