]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/binio.h
Update copyright year to 2017
[lugaru.git] / Source / Utils / binio.h
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 #ifndef binio_h
22 #define binio_h
23
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #if defined(__cplusplus)
28 extern "C" {
29 #endif
30
31     /*
32     Notes on format of format strings:
33     * whitespace is ignored
34     * each "group" consists of an optional count (defaults to 1),
35     an optional byte-order marker (defaults to H, "host-native"),
36     and a  data-type specifier.
37     * when unpacking, each variable argument is a pointer to the
38     appropriate number of Object::objects of the appropriate type.
39     * when packing, each variable argument is an object of the
40     appropriate type if the count is omitted, or a pointer to the
41     appropriate number of Object::objects of the appropriate type if the
42     count is specified.
43     * the buffer supplied to pack/unpack must be of sufficient length
44     to hold all the data, or the behavior is unspecified.
45     * the file provided to the "f" versions of the functions must be
46     open in the appropriate mode, or the behavior is unspecified.
47     * the file supplied to funpackf must be of sufficient length to
48     hold all the data, or the behavior is unspecified.
49     * the behavior of all functions is unspecified if the format string
50     is incorrectly-formed.
51
52     Data-type specifiers:
53     x skipped byte; no corresponding argument
54     b byte
55     s two-byte two's-complement integer
56     i four-byte two's-complement integer
57     l eight-byte two's-complement integer
58     f four-byte IEEE754 float
59     d eight-byte IEEE754 double
60
61     Byte-order specifiers:
62     L little-endian
63     B big-endian
64     H host's native byte order
65     N network byte order
66     */
67
68 #ifndef ALREADY_DID_BINIO_STDINT
69 #define ALREADY_DID_BINIO_STDINT
70 #if defined(BinIO_STDINT_HEADER)
71 #include BinIO_STDINT_HEADER
72     typedef float              float32_t;
73     typedef double             float64_t;
74 #else
75     typedef unsigned char      uint8_t;
76     typedef unsigned short     uint16_t;
77     typedef unsigned long      uint32_t;
78 #ifdef WIN32
79     typedef unsigned __int64   uint64_t;
80 #else
81     typedef unsigned long long uint64_t;
82 #endif
83     typedef float              float32_t;
84     typedef double             float64_t;
85 #endif
86 #endif
87
88     typedef struct {
89         float64_t d;
90         uint64_t  l;
91         int  i;
92         float32_t f;
93         uint16_t  s;
94         uint8_t   b;
95     }
96     test_data;
97
98     extern void packf    (                    const char *format, ...);
99     extern void spackf   (void *buffer,       const char *format, ...);
100     extern void fpackf   (FILE *file,         const char *format, ...);
101     extern void vspackf  (void *buffer,       const char *format, va_list args);
102     extern void vfpackf  (FILE *file,         const char *format, va_list args);
103
104     extern void unpackf  (                    const char *format, ...);
105     extern void sunpackf (const void *buffer, const char *format, ...);
106     extern void funpackf (FILE       *file,   const char *format, ...);
107     extern void vsunpackf(const void *buffer, const char *format, va_list args);
108     extern void vfunpackf(FILE       *file,   const char *format, va_list args);
109
110 #ifdef _MSC_VER
111 #ifndef va_copy
112 #define va_copy(dest,src) do { dest = src; } while (0)
113 #endif
114 #endif
115
116 #if defined(__cplusplus)
117 }
118 #endif
119
120 #endif
121