2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
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.
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.
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/>.
26 struct BinIOPackContext {
31 static void BinIOPack(void *context, int type, int byte_order, int count)
33 struct BinIOPackContext *ctx = (struct BinIOPackContext*)context;
36 case BinIO_TYPE_IGNORE_BYTE: {
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);
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);
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);
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);
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);
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);
79 case BinIO_TYPE_IGNORE_BYTE:
80 ctx->buffer += 1 * count;
83 BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
84 ctx->buffer += 1 * count;
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;
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;
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;
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;
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;
110 extern void packf(const char *format, ...)
113 va_start(args, format);
114 vfpackf(stdout, format, args);
118 extern void spackf(void *buffer, const char *format, ...)
121 va_start(args, format);
122 vspackf(buffer, format, args);
126 extern void fpackf(FILE *file, const char *format, ...)
129 va_start(args, format);
130 vfpackf(file, format, args);
134 extern void vspackf(void *buffer, const char *format, va_list args)
136 struct BinIOFormatCursor cursor;
137 struct BinIOPackContext context;
139 BinIOInitFormatCursor(&cursor, format);
141 context.buffer = (unsigned char *)buffer;
142 va_copy(context.args, args);
144 while (BinIONextChar(&context, &cursor, BinIOPack)) {}
146 va_end(context.args);
149 extern void vfpackf(FILE *file, const char *format, va_list args)
151 size_t n_bytes = BinIOFormatByteCount(format);
152 void* buffer = malloc(n_bytes);
154 vspackf(buffer, format, args);
156 fwrite(buffer, n_bytes, 1, file);