]> git.mxchange.org Git - simgear.git/blob - simgear/io/lowlevel.hxx
Mac OS X fixes from Markus Morawitz
[simgear.git] / simgear / io / lowlevel.hxx
1 // lowlevel.hxx -- routines to handle lowlevel compressed binary IO of
2 //                 various datatypes
3 //
4 // Shamelessly adapted from plib  January 2001
5 //
6 // Original version Copyright (C) 2000  the plib team
7 // Local changes Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24 //
25
26
27 #ifndef _SG_LOWLEVEL_HXX
28 #define _SG_LOWLEVEL_HXX
29
30
31 #include <stdio.h>
32 #include <zlib.h>
33
34 #include <plib/sg.h>
35
36 #ifdef _MSC_VER
37 typedef __int64 int64;
38 typedef __int64 uint64;
39 #else
40 typedef long long int64;
41 typedef unsigned long long uint64;
42 #endif
43
44 // Note that output is written in little endian form (and converted as
45 // necessary for big endian machines)
46
47 void sgReadChar ( gzFile fd, char *var ) ;
48 void sgWriteChar ( gzFile fd, const char var ) ;
49 void sgReadFloat ( gzFile fd, float *var ) ;
50 void sgWriteFloat ( gzFile fd, const float var ) ;
51 void sgReadDouble ( gzFile fd, double *var ) ;
52 void sgWriteDouble ( gzFile fd, const double var ) ;
53 void sgReadUInt ( gzFile fd, unsigned int *var ) ;
54 void sgWriteUInt ( gzFile fd, const unsigned int var ) ;
55 void sgReadInt ( gzFile fd, int *var ) ;
56 void sgWriteInt ( gzFile fd, const int var ) ;
57 void sgReadLong ( gzFile fd, long int *var ) ;
58 void sgWriteLong ( gzFile fd, const long int var ) ;
59 void sgReadLongLong ( gzFile fd, int64 *var ) ;
60 void sgWriteLongLong ( gzFile fd, const int64 var ) ;
61 void sgReadUShort ( gzFile fd, unsigned short *var ) ;
62 void sgWriteUShort ( gzFile fd, const unsigned short var ) ;
63 void sgReadShort ( gzFile fd, short *var ) ;
64 void sgWriteShort ( gzFile fd, const short var ) ;
65
66 void sgReadFloat ( gzFile fd, const unsigned int n, float *var ) ;
67 void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var ) ;
68 void sgReadDouble ( gzFile fd, const unsigned int n, double *var ) ;
69 void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var ) ;
70 void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var ) ;
71 void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var ) ;
72 void sgReadInt ( gzFile fd, const unsigned int n, int *var ) ;
73 void sgWriteInt ( gzFile fd, const unsigned int n, const int *var ) ;
74 void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var ) ;
75 void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var ) ;
76 void sgReadShort ( gzFile fd, const unsigned int n, short *var ) ;
77 void sgWriteShort ( gzFile fd, const unsigned int n, const short *var ) ;
78 void sgReadBytes ( gzFile fd, const unsigned int n, void *var ) ;
79 void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var ) ;
80
81 void sgReadString ( gzFile fd, char **var ) ;
82 void sgWriteString ( gzFile fd, const char *var ) ;
83
84 inline void sgReadVec2  ( gzFile fd, sgVec2 var ) {
85     sgReadFloat  ( fd, 2, var ) ;
86 }
87 inline void sgWriteVec2 ( gzFile fd, const sgVec2 var ) {
88     sgWriteFloat ( fd, 2, var ) ;
89 }
90
91 inline void sgReadVec3  ( gzFile fd, sgVec3 var ) {
92     sgReadFloat  ( fd, 3, var ) ;
93 }
94 inline void sgWriteVec3 ( gzFile fd, const sgVec3 var ) {
95     sgWriteFloat ( fd, 3, var ) ;
96 }
97
98 inline void sgReaddVec3  ( gzFile fd, sgdVec3 var ) {
99     sgReadDouble  ( fd, 3, var ) ;
100 }
101 inline void sgWritedVec3 ( gzFile fd, const sgdVec3 var ) {
102     sgWriteDouble ( fd, 3, var ) ;
103 }
104
105 inline void sgReadVec4  ( gzFile fd, sgVec4 var ) {
106     sgReadFloat  ( fd, 4, var ) ;
107 }
108 inline void sgWriteVec4 ( gzFile fd, const sgVec4 var ) {
109     sgWriteFloat ( fd, 4, var ) ;
110 }
111
112 inline void sgReadMat4  ( gzFile fd, sgMat4 var ) {
113     sgReadFloat  ( fd, 16, (float *)var ) ;
114 }
115 inline void sgWriteMat4 ( gzFile fd, const sgMat4 var ) {
116     sgWriteFloat ( fd, 16, (float *)var ) ;
117 }
118
119 void sgClearReadError();
120 void sgClearWriteError();
121 int sgReadError();
122 int sgWriteError();
123
124 inline bool sgIsLittleEndian() {
125     static const int sgEndianTest = 1;
126     return (*((char *) &sgEndianTest ) != 0);
127 }
128
129 inline bool sgIsBigEndian() {
130     static const int sgEndianTest = 1;
131     return (*((char *) &sgEndianTest ) == 0);
132 }
133
134 inline void sgEndianSwap(unsigned short *x) {
135     *x =
136         (( *x >>  8 ) & 0x00FF ) | 
137         (( *x <<  8 ) & 0xFF00 ) ;
138 }
139   
140 inline void sgEndianSwap(unsigned int *x) {
141     *x =
142         (( *x >> 24 ) & 0x000000FF ) | 
143         (( *x >>  8 ) & 0x0000FF00 ) | 
144         (( *x <<  8 ) & 0x00FF0000 ) | 
145         (( *x << 24 ) & 0xFF000000 ) ;
146 }
147   
148 inline void sgEndianSwap(uint64 *x) {
149 #ifndef _MSC_VER
150     *x =
151         (( *x >> 56 ) & 0x00000000000000FFULL ) | 
152         (( *x >> 40 ) & 0x000000000000FF00ULL ) | 
153         (( *x >> 24 ) & 0x0000000000FF0000ULL ) | 
154         (( *x >>  8 ) & 0x00000000FF000000ULL ) | 
155         (( *x <<  8 ) & 0x000000FF00000000ULL ) | 
156         (( *x << 24 ) & 0x0000FF0000000000ULL ) |
157         (( *x << 40 ) & 0x00FF000000000000ULL ) |
158         (( *x << 56 ) & 0xFF00000000000000ULL ) ;
159 #else
160     *x =
161         (( *x >> 56 ) & 0x00000000000000FF ) | 
162         (( *x >> 40 ) & 0x000000000000FF00 ) | 
163         (( *x >> 24 ) & 0x0000000000FF0000 ) | 
164         (( *x >>  8 ) & 0x00000000FF000000 ) | 
165         (( *x <<  8 ) & 0x000000FF00000000 ) | 
166         (( *x << 24 ) & 0x0000FF0000000000 ) |
167         (( *x << 40 ) & 0x00FF000000000000 ) |
168         (( *x << 56 ) & 0xFF00000000000000 ) ;
169 #endif
170 }
171
172 #endif // _SG_LOWLEVEL_HXX