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))
34 for (i = 0; i < count; ++i)
43 memcpy(dst, src, 1 * count);
47 void BinIOConvert2(int from_byte_order, int to_byte_order,
48 const uint8_t *src, uint8_t *dst,
51 if (BinIONormalizeByteOrder(from_byte_order) !=
52 BinIONormalizeByteOrder(to_byte_order))
55 for (i = 0; i < count; ++i)
64 memcpy(dst, src, 2 * count);
68 void BinIOConvert4(int from_byte_order, int to_byte_order,
69 const uint8_t *src, uint8_t *dst,
72 if (BinIONormalizeByteOrder(from_byte_order) !=
73 BinIONormalizeByteOrder(to_byte_order))
76 for (i = 0; i < count; ++i)
85 memcpy(dst, src, 4 * count);
89 void BinIOConvert8(int from_byte_order, int to_byte_order,
90 const uint8_t *src, uint8_t *dst,
93 if (BinIONormalizeByteOrder(from_byte_order) !=
94 BinIONormalizeByteOrder(to_byte_order))
97 for (i = 0; i < count; ++i)
106 memcpy(dst, src, 8 * count);
110 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
113 cursor->cursor = format;
114 cursor->byte_order = BinIO_HOST_BYTE_ORDER;
118 int BinIONextChar(void *context,
119 struct BinIOFormatCursor *cursor,
120 BinIOProcessFunction func)
124 switch (c = *(cursor->cursor)++)
126 case BinIO_LITTLE_ENDIAN_BYTE_ORDER:
127 case BinIO_BIG_ENDIAN_BYTE_ORDER:
128 case BinIO_HOST_BYTE_ORDER:
129 case BinIO_NETWORK_BYTE_ORDER:
130 cursor->byte_order = c;
133 case '0': case '1': case '2': case '3': case '4':
134 case '5': case '6': case '7': case '8': case '9':
135 count = cursor->count;
139 cursor->count = value;
143 cursor->count = (count * 10) + value;
147 case BinIO_TYPE_IGNORE_BYTE:
148 case BinIO_TYPE_BYTE:
149 case BinIO_TYPE_INT16:
150 case BinIO_TYPE_INT32:
151 case BinIO_TYPE_INT64:
152 case BinIO_TYPE_FLOAT32:
153 case BinIO_TYPE_FLOAT64:
154 func(context, c, cursor->byte_order, cursor->count);
155 cursor->byte_order = BinIO_HOST_BYTE_ORDER;
159 case ' ': case '\t': case '\r': case '\n':
169 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
171 size_t type_size = 0;
180 case BinIO_TYPE_IGNORE_BYTE: type_size = 1; break;
181 case BinIO_TYPE_BYTE: type_size = 1; break;
182 case BinIO_TYPE_INT16: type_size = 2; break;
183 case BinIO_TYPE_INT32: type_size = 4; break;
184 case BinIO_TYPE_INT64: type_size = 8; break;
185 case BinIO_TYPE_FLOAT32: type_size = 4; break;
186 case BinIO_TYPE_FLOAT64: type_size = 8; break;
189 *(size_t*)context += type_size * count;
192 extern size_t BinIOFormatByteCount(const char *format)
194 struct BinIOFormatCursor cursor;
197 BinIOInitFormatCursor(&cursor, format);
199 while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}