1 // lowlevel.hxx -- routines to handle lowlevel compressed binary IO of
4 // Shamelessly adapted from plib January 2001
6 // Original version Copyright (C) 2000 the plib team
7 // Local changes Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #ifndef _SG_LOWLEVEL_HXX
28 #define _SG_LOWLEVEL_HXX
37 #include <simgear/compiler.h>
39 // Note that output is written in little endian form (and converted as
40 // necessary for big endian machines)
42 void sgReadChar ( gzFile fd, char *var ) ;
43 void sgWriteChar ( gzFile fd, const char var ) ;
44 void sgReadFloat ( gzFile fd, float *var ) ;
45 void sgWriteFloat ( gzFile fd, const float var ) ;
46 void sgReadDouble ( gzFile fd, double *var ) ;
47 void sgWriteDouble ( gzFile fd, const double var ) ;
48 void sgReadUInt ( gzFile fd, unsigned int *var ) ;
49 void sgWriteUInt ( gzFile fd, const unsigned int var ) ;
50 void sgReadInt ( gzFile fd, int *var ) ;
51 void sgWriteInt ( gzFile fd, const int var ) ;
52 void sgReadLong ( gzFile fd, int32_t *var ) ;
53 void sgWriteLong ( gzFile fd, const int32_t var ) ;
54 void sgReadLongLong ( gzFile fd, int64_t *var ) ;
55 void sgWriteLongLong ( gzFile fd, const int64_t var ) ;
56 void sgReadUShort ( gzFile fd, unsigned short *var ) ;
57 void sgWriteUShort ( gzFile fd, const unsigned short var ) ;
58 void sgReadShort ( gzFile fd, short *var ) ;
59 void sgWriteShort ( gzFile fd, const short var ) ;
61 void sgReadFloat ( gzFile fd, const unsigned int n, float *var ) ;
62 void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var ) ;
63 void sgReadDouble ( gzFile fd, const unsigned int n, double *var ) ;
64 void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var ) ;
65 void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var ) ;
66 void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var ) ;
67 void sgReadInt ( gzFile fd, const unsigned int n, int *var ) ;
68 void sgWriteInt ( gzFile fd, const unsigned int n, const int *var ) ;
69 void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var ) ;
70 void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var ) ;
71 void sgReadShort ( gzFile fd, const unsigned int n, short *var ) ;
72 void sgWriteShort ( gzFile fd, const unsigned int n, const short *var ) ;
73 void sgReadBytes ( gzFile fd, const unsigned int n, void *var ) ;
74 void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var ) ;
76 void sgReadString ( gzFile fd, char **var ) ;
77 void sgWriteString ( gzFile fd, const char *var ) ;
79 inline void sgReadVec2 ( gzFile fd, sgVec2 var ) {
80 sgReadFloat ( fd, 2, var ) ;
82 inline void sgWriteVec2 ( gzFile fd, const sgVec2 var ) {
83 sgWriteFloat ( fd, 2, var ) ;
86 inline void sgReadVec3 ( gzFile fd, sgVec3 var ) {
87 sgReadFloat ( fd, 3, var ) ;
89 inline void sgWriteVec3 ( gzFile fd, const sgVec3 var ) {
90 sgWriteFloat ( fd, 3, var ) ;
93 inline void sgReaddVec3 ( gzFile fd, sgdVec3 var ) {
94 sgReadDouble ( fd, 3, var ) ;
96 inline void sgWritedVec3 ( gzFile fd, const sgdVec3 var ) {
97 sgWriteDouble ( fd, 3, var ) ;
100 inline void sgReadVec4 ( gzFile fd, sgVec4 var ) {
101 sgReadFloat ( fd, 4, var ) ;
103 inline void sgWriteVec4 ( gzFile fd, const sgVec4 var ) {
104 sgWriteFloat ( fd, 4, var ) ;
107 inline void sgReadMat4 ( gzFile fd, sgMat4 var ) {
108 sgReadFloat ( fd, 16, (float *)var ) ;
110 inline void sgWriteMat4 ( gzFile fd, const sgMat4 var ) {
111 sgWriteFloat ( fd, 16, (float *)var ) ;
114 void sgClearReadError();
115 void sgClearWriteError();
119 inline bool sgIsLittleEndian() {
120 static const int sgEndianTest = 1;
121 return (*((char *) &sgEndianTest ) != 0);
124 inline bool sgIsBigEndian() {
125 static const int sgEndianTest = 1;
126 return (*((char *) &sgEndianTest ) == 0);
129 inline void sgEndianSwap(unsigned short *x) {
131 (( *x >> 8 ) & 0x00FF ) |
132 (( *x << 8 ) & 0xFF00 ) ;
135 inline void sgEndianSwap(unsigned int *x) {
137 (( *x >> 24 ) & 0x000000FF ) |
138 (( *x >> 8 ) & 0x0000FF00 ) |
139 (( *x << 8 ) & 0x00FF0000 ) |
140 (( *x << 24 ) & 0xFF000000 ) ;
143 inline void sgEndianSwap(uint64_t *x) {
146 (( *x >> 56 ) & 0x00000000000000FFULL ) |
147 (( *x >> 40 ) & 0x000000000000FF00ULL ) |
148 (( *x >> 24 ) & 0x0000000000FF0000ULL ) |
149 (( *x >> 8 ) & 0x00000000FF000000ULL ) |
150 (( *x << 8 ) & 0x000000FF00000000ULL ) |
151 (( *x << 24 ) & 0x0000FF0000000000ULL ) |
152 (( *x << 40 ) & 0x00FF000000000000ULL ) |
153 (( *x << 56 ) & 0xFF00000000000000ULL ) ;
156 (( *x >> 56 ) & 0x00000000000000FF ) |
157 (( *x >> 40 ) & 0x000000000000FF00 ) |
158 (( *x >> 24 ) & 0x0000000000FF0000 ) |
159 (( *x >> 8 ) & 0x00000000FF000000 ) |
160 (( *x << 8 ) & 0x000000FF00000000 ) |
161 (( *x << 24 ) & 0x0000FF0000000000 ) |
162 (( *x << 40 ) & 0x00FF000000000000 ) |
163 (( *x << 56 ) & 0xFF00000000000000 ) ;
167 #endif // _SG_LOWLEVEL_HXX