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 modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru 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. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
25 struct BinIOPackContext {
30 static void BinIOPack(void *context, int type, int byte_order, int count)
32 struct BinIOPackContext *ctx = (struct BinIOPackContext*)context;
35 case BinIO_TYPE_IGNORE_BYTE: {
39 case BinIO_TYPE_BYTE: {
40 uint8_t value = va_arg(ctx->args, int);
41 BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
45 case BinIO_TYPE_INT16: {
46 uint16_t value = va_arg(ctx->args, int);
47 BinIOConvert2(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
51 case BinIO_TYPE_INT32: {
52 int value = va_arg(ctx->args, int);
53 BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
57 case BinIO_TYPE_INT64: {
58 uint64_t value = va_arg(ctx->args, uint64_t);
59 BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
63 case BinIO_TYPE_FLOAT32: {
64 float32_t value = (float32_t)va_arg(ctx->args, double);
65 BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
69 case BinIO_TYPE_FLOAT64: {
70 float64_t value = va_arg(ctx->args, float64_t);
71 BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
78 case BinIO_TYPE_IGNORE_BYTE:
79 ctx->buffer += 1 * count;
82 BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
83 ctx->buffer += 1 * count;
85 case BinIO_TYPE_INT16:
86 BinIOConvert2(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
87 ctx->buffer += 2 * count;
89 case BinIO_TYPE_INT32:
90 BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
91 ctx->buffer += 4 * count;
93 case BinIO_TYPE_INT64:
94 BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
95 ctx->buffer += 8 * count;
97 case BinIO_TYPE_FLOAT32:
98 BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
99 ctx->buffer += 4 * count;
101 case BinIO_TYPE_FLOAT64:
102 BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count);
103 ctx->buffer += 8 * count;
109 extern void packf(const char *format, ...)
112 va_start(args, format);
113 vfpackf(stdout, format, args);
117 extern void spackf(void *buffer, const char *format, ...)
120 va_start(args, format);
121 vspackf(buffer, format, args);
125 extern void fpackf(FILE *file, const char *format, ...)
128 va_start(args, format);
129 vfpackf(file, format, args);
133 extern void vspackf(void *buffer, const char *format, va_list args)
135 struct BinIOFormatCursor cursor;
136 struct BinIOPackContext context;
138 BinIOInitFormatCursor(&cursor, format);
140 context.buffer = (unsigned char *)buffer;
141 va_copy(context.args, args);
143 while (BinIONextChar(&context, &cursor, BinIOPack)) {}
145 va_end(context.args);
148 extern void vfpackf(FILE *file, const char *format, va_list args)
150 size_t n_bytes = BinIOFormatByteCount(format);
151 void* buffer = malloc(n_bytes);
153 vspackf(buffer, format, args);
155 fwrite(buffer, n_bytes, 1, file);