]> git.jsancho.org Git - lugaru.git/blob - Source/private.c
Added GPL license and headers.
[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     {
33         unsigned int i;
34         for (i = 0; i < count; ++i)
35         {
36             BinIOSwap1(src, dst);
37             src += 1;
38             dst += 1;
39         }
40     }
41     else
42     {
43         memcpy(dst, src, 1 * count);
44     }
45 }
46
47 void BinIOConvert2(int from_byte_order, int to_byte_order,
48                    const uint8_t *src, uint8_t *dst,
49                    unsigned int count)
50 {
51     if (BinIONormalizeByteOrder(from_byte_order) !=
52         BinIONormalizeByteOrder(to_byte_order))
53     {
54         unsigned int i;
55         for (i = 0; i < count; ++i)
56         {
57             BinIOSwap2(src, dst);
58             src += 2;
59             dst += 2;
60         }
61     }
62     else
63     {
64         memcpy(dst, src, 2 * count);
65     }
66 }
67
68 void BinIOConvert4(int from_byte_order, int to_byte_order,
69                    const uint8_t *src, uint8_t *dst,
70                    unsigned int count)
71 {
72     if (BinIONormalizeByteOrder(from_byte_order) !=
73         BinIONormalizeByteOrder(to_byte_order))
74     {
75         unsigned int i;
76         for (i = 0; i < count; ++i)
77         {
78             BinIOSwap4(src, dst);
79             src += 4;
80             dst += 4;
81         }
82     }
83     else
84     {
85         memcpy(dst, src, 4 * count);
86     }
87 }
88
89 void BinIOConvert8(int from_byte_order, int to_byte_order,
90                    const uint8_t *src, uint8_t *dst,
91                    unsigned int count)
92 {
93     if (BinIONormalizeByteOrder(from_byte_order) !=
94         BinIONormalizeByteOrder(to_byte_order))
95     {
96         unsigned int i;
97         for (i = 0; i < count; ++i)
98         {
99             BinIOSwap8(src, dst);
100             src += 8;
101             dst += 8;
102         }
103     }
104     else
105     {
106         memcpy(dst, src, 8 * count);
107     }
108 }
109
110 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
111                            const char               *format)
112 {
113     cursor->cursor = format;
114     cursor->byte_order = BinIO_HOST_BYTE_ORDER;
115     cursor->count = -1;
116 }
117
118 int BinIONextChar(void                     *context,
119                   struct BinIOFormatCursor *cursor,
120                   BinIOProcessFunction      func)
121 {
122     int count, value;
123     int c;
124     switch (c = *(cursor->cursor)++)
125     {
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;
131             break;
132         
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;
136             value = c - '0';
137             if (count == -1)
138             {
139                 cursor->count = value;
140             }
141             else
142             {
143                 cursor->count = (count * 10) + value;
144             }
145             break;
146             
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;
156             cursor->count = -1;
157             break;
158             
159         case ' ': case '\t': case '\r': case '\n':
160             break;
161             
162         default:
163             return 0;
164     }
165     
166     return 1;
167 }
168
169 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
170 {
171     size_t type_size = 0;
172     
173     if (count == -1)
174     {
175         count = 1;
176     }
177     
178     switch (type)
179     {
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;
187     }
188     
189     *(size_t*)context += type_size * count;
190 }
191
192 extern size_t BinIOFormatByteCount(const char *format)
193 {
194     struct BinIOFormatCursor cursor;
195     size_t n_bytes = 0;
196
197     BinIOInitFormatCursor(&cursor, format);
198     
199     while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}
200     
201     return n_bytes;
202 }