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