]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/pack.c
Update copyright year to 2017
[lugaru.git] / Source / Utils / pack.c
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "binio.h"
22 #include "private.h"
23
24 #include <stdlib.h>
25
26 struct BinIOPackContext {
27     uint8_t *buffer;
28     va_list  args;
29 };
30
31 static void BinIOPack(void *context, int type, int byte_order, int count)
32 {
33     struct BinIOPackContext *ctx = (struct BinIOPackContext*)context;
34     if (count == -1) {
35         switch (type) {
36         case BinIO_TYPE_IGNORE_BYTE: {
37             ctx->buffer += 1;
38         }
39         break;
40         case BinIO_TYPE_BYTE: {
41             uint8_t value = va_arg(ctx->args, int);
42             BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
43             ctx->buffer += 1;
44         }
45         break;
46         case BinIO_TYPE_INT16: {
47             uint16_t value = va_arg(ctx->args, int);
48             BinIOConvert2(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
49             ctx->buffer += 2;
50         }
51         break;
52         case BinIO_TYPE_INT32: {
53             int value = va_arg(ctx->args, int);
54             BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
55             ctx->buffer += 4;
56         }
57         break;
58         case BinIO_TYPE_INT64: {
59             uint64_t value = va_arg(ctx->args, uint64_t);
60             BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
61             ctx->buffer += 8;
62         }
63         break;
64         case BinIO_TYPE_FLOAT32: {
65             float32_t value = (float32_t)va_arg(ctx->args, double);
66             BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
67             ctx->buffer += 4;
68         }
69         break;
70         case BinIO_TYPE_FLOAT64: {
71             float64_t value = va_arg(ctx->args, float64_t);
72             BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
73             ctx->buffer += 8;
74         }
75         break;
76         }
77     } else {
78         switch (type) {
79         case BinIO_TYPE_IGNORE_BYTE:
80             ctx->buffer += 1 * count;
81             break;
82         case BinIO_TYPE_BYTE:
83             BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
84             ctx->buffer += 1 * count;
85             break;
86         case BinIO_TYPE_INT16:
87             BinIOConvert2(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
88             ctx->buffer += 2 * count;
89             break;
90         case BinIO_TYPE_INT32:
91             BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
92             ctx->buffer += 4 * count;
93             break;
94         case BinIO_TYPE_INT64:
95             BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
96             ctx->buffer += 8 * count;
97             break;
98         case BinIO_TYPE_FLOAT32:
99             BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
100             ctx->buffer += 4 * count;
101             break;
102         case BinIO_TYPE_FLOAT64:
103             BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
104             ctx->buffer += 8 * count;
105             break;
106         }
107     }
108 }
109
110 extern void packf(const char *format, ...)
111 {
112     va_list args;
113     va_start(args, format);
114     vfpackf(stdout, format, args);
115     va_end(args);
116 }
117
118 extern void spackf(void *buffer, const char *format, ...)
119 {
120     va_list args;
121     va_start(args, format);
122     vspackf(buffer, format, args);
123     va_end(args);
124 }
125
126 extern void fpackf(FILE *file, const char *format, ...)
127 {
128     va_list args;
129     va_start(args, format);
130     vfpackf(file, format, args);
131     va_end(args);
132 }
133
134 extern void vspackf(void *buffer, const char *format, va_list args)
135 {
136     struct BinIOFormatCursor cursor;
137     struct BinIOPackContext context;
138
139     BinIOInitFormatCursor(&cursor, format);
140
141     context.buffer = (unsigned char *)buffer;
142     va_copy(context.args, args);
143
144     while (BinIONextChar(&context, &cursor, BinIOPack)) {}
145
146     va_end(context.args);
147 }
148
149 extern void vfpackf(FILE *file, const char *format, va_list args)
150 {
151     size_t n_bytes = BinIOFormatByteCount(format);
152     void* buffer = malloc(n_bytes);
153
154     vspackf(buffer, format, args);
155
156     fwrite(buffer, n_bytes, 1, file);
157     free(buffer);
158 }