]> git.jsancho.org Git - lugaru.git/blob - Source/unpack.c
8568b89e484b0f89e170694af4c470553907ab4a
[lugaru.git] / Source / unpack.c
1 #include <stdlib.h>
2
3 #include "binio.h"
4 #include "private.h"
5 #include "unpack_private.h"
6
7 void unpackf(const char *format, ...)
8 {
9     va_list args;
10     va_start(args, format);
11     vfunpackf(stdin, format, args);
12     va_end(args);
13 }
14
15 void sunpackf(const void *buffer, const char *format, ...)
16 {
17     va_list args;
18     va_start(args, format);
19     vsunpackf(buffer, format, args);
20     va_end(args);
21 }
22
23 void funpackf(FILE *file, const char *format, ...)
24 {
25     va_list args;
26     va_start(args, format);
27     vfunpackf(file, format, args);
28     va_end(args);
29 }
30
31 void vsunpackf(const void *buffer, const char *format, va_list args)
32 {
33     struct BinIOFormatCursor cursor;
34     struct BinIOUnpackContext context;
35
36     BinIOInitFormatCursor(&cursor, format);
37     
38     context.data = (const unsigned char*)buffer;
39     va_copy(context.args, args);
40     
41     while (BinIONextChar(&context, &cursor, BinIOUnpack)) {}
42
43     va_end(context.args);
44 }
45
46 void vfunpackf(FILE *file, const char *format, va_list args)
47 {
48     size_t n_bytes = BinIOFormatByteCount(format);
49     void* buffer = malloc(n_bytes);
50     fread(buffer, n_bytes, 1, file);
51     
52     vsunpackf(buffer, format, args);
53     
54     free(buffer);
55 }