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