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