2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
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.
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.
15 See the GNU General Public License for more details.
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.
26 void BinIOConvert1(int from_byte_order, int to_byte_order,
27 const uint8_t *src, uint8_t *dst,
30 if (BinIONormalizeByteOrder(from_byte_order) !=
31 BinIONormalizeByteOrder(to_byte_order)) {
33 for (i = 0; i < count; ++i) {
39 memcpy(dst, src, 1 * count);
43 void BinIOConvert2(int from_byte_order, int to_byte_order,
44 const uint8_t *src, uint8_t *dst,
47 if (BinIONormalizeByteOrder(from_byte_order) !=
48 BinIONormalizeByteOrder(to_byte_order)) {
50 for (i = 0; i < count; ++i) {
56 memcpy(dst, src, 2 * count);
60 void BinIOConvert4(int from_byte_order, int to_byte_order,
61 const uint8_t *src, uint8_t *dst,
64 if (BinIONormalizeByteOrder(from_byte_order) !=
65 BinIONormalizeByteOrder(to_byte_order)) {
67 for (i = 0; i < count; ++i) {
73 memcpy(dst, src, 4 * count);
77 void BinIOConvert8(int from_byte_order, int to_byte_order,
78 const uint8_t *src, uint8_t *dst,
81 if (BinIONormalizeByteOrder(from_byte_order) !=
82 BinIONormalizeByteOrder(to_byte_order)) {
84 for (i = 0; i < count; ++i) {
90 memcpy(dst, src, 8 * count);
94 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
97 cursor->cursor = format;
98 cursor->byte_order = BinIO_HOST_BYTE_ORDER;
102 int BinIONextChar(void *context,
103 struct BinIOFormatCursor *cursor,
104 BinIOProcessFunction func)
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;
126 count = cursor->count;
129 cursor->count = value;
131 cursor->count = (count * 10) + value;
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;
160 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
162 size_t type_size = 0;
169 case BinIO_TYPE_IGNORE_BYTE:
172 case BinIO_TYPE_BYTE:
175 case BinIO_TYPE_INT16:
178 case BinIO_TYPE_INT32:
181 case BinIO_TYPE_INT64:
184 case BinIO_TYPE_FLOAT32:
187 case BinIO_TYPE_FLOAT64:
192 *(size_t*)context += type_size * count;
195 extern size_t BinIOFormatByteCount(const char *format)
197 struct BinIOFormatCursor cursor;
200 BinIOInitFormatCursor(&cursor, format);
202 while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}