-// lowlevel.hxx -- routines to handle lowlevel compressed binary IO of
+// lowlevel.cxx -- routines to handle lowlevel compressed binary IO of
// various datatypes
//
// Shamelessly adapted from plib (plib.sourceforge.net) January 2001
//
// Original version Copyright (C) 2000 the plib team
-// Local changes Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
+// Local changes Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// $Id$
-//
+
+
+#include <string.h> // for memcpy()
#include "lowlevel.hxx"
+
static int read_error = false ;
static int write_error = false ;
int sgReadError() { return read_error ; }
int sgWriteError() { return write_error ; }
-static const int sgEndianTest = 1;
-#define sgIsLittleEndian (*((char *) &sgEndianTest ) != 0)
-#define sgIsBigEndian (*((char *) &sgEndianTest ) == 0)
-
-static inline void sgEndianSwap(unsigned short *x) {
- *x =
- (( *x >> 8 ) & 0x00FF ) |
- (( *x << 8 ) & 0xFF00 ) ;
-}
-
-static inline void sgEndianSwap(unsigned int *x) {
- *x =
- (( *x >> 24 ) & 0x000000FF ) |
- (( *x >> 8 ) & 0x0000FF00 ) |
- (( *x << 8 ) & 0x00FF0000 ) |
- (( *x << 24 ) & 0xFF000000 ) ;
-}
-
-static inline void sgEndianSwap(unsigned long long *x) {
- *x =
- (( *x >> 56 ) & 0x00000000000000FFULL ) |
- (( *x >> 40 ) & 0x000000000000FF00ULL ) |
- (( *x >> 24 ) & 0x0000000000FF0000ULL ) |
- (( *x >> 8 ) & 0x00000000FF000000ULL ) |
- (( *x << 8 ) & 0x000000FF00000000ULL ) |
- (( *x << 24 ) & 0x0000FF0000000000ULL ) |
- (( *x << 40 ) & 0x00FF000000000000ULL ) |
- (( *x << 56 ) & 0xFF00000000000000ULL ) ;
-}
-
+
void sgReadChar ( gzFile fd, char *var )
{
if ( gzread ( fd, var, sizeof(char) ) != sizeof(char) ) {
if ( gzread ( fd, var, sizeof(float) ) != sizeof(float) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)var);
}
}
void sgWriteFloat ( gzFile fd, const float var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)&var);
}
if ( gzwrite ( fd, (void *)(&var), sizeof(float) ) != sizeof(float) ) {
if ( gzread ( fd, var, sizeof(double) ) != sizeof(double) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
- sgEndianSwap( (unsigned long long*)var);
+ if ( sgIsBigEndian() ) {
+ sgEndianSwap( (uint64_t*)var);
}
}
void sgWriteDouble ( gzFile fd, const double var )
{
- if ( sgIsBigEndian ) {
- sgEndianSwap( (unsigned long long*)&var);
+ if ( sgIsBigEndian() ) {
+ sgEndianSwap( (uint64_t*)&var);
}
if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) != sizeof(double) ) {
write_error = true ;
if ( gzread ( fd, var, sizeof(unsigned int) ) != sizeof(unsigned int) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)var);
}
}
void sgWriteUInt ( gzFile fd, const unsigned int var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)&var);
}
if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned int) )
if ( gzread ( fd, var, sizeof(int) ) != sizeof(int) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)var);
}
}
void sgWriteInt ( gzFile fd, const int var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)&var);
}
if ( gzwrite ( fd, (void *)(&var), sizeof(int) ) != sizeof(int) ) {
}
-void sgReadLong ( gzFile fd, long int *var )
+void sgReadLong ( gzFile fd, int32_t *var )
{
- if ( gzread ( fd, var, sizeof(long int) ) != sizeof(long int) ) {
+ if ( gzread ( fd, var, sizeof(int32_t) ) != sizeof(int32_t) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)var);
}
}
-void sgWriteLong ( gzFile fd, const long int var )
+void sgWriteLong ( gzFile fd, const int32_t var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned int*)&var);
}
- if ( gzwrite ( fd, (void *)(&var), sizeof(long int) )
- != sizeof(long int) )
+ if ( gzwrite ( fd, (void *)(&var), sizeof(int32_t) )
+ != sizeof(int32_t) )
{
write_error = true ;
}
}
-void sgReadLongLong ( gzFile fd, long long int *var )
+void sgReadLongLong ( gzFile fd, int64_t *var )
{
- if ( gzread ( fd, var, sizeof(long long int) ) != sizeof(long long int) ) {
+ if ( gzread ( fd, var, sizeof(int64_t) ) != sizeof(int64_t) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
- sgEndianSwap( (unsigned long long int*)var);
+ if ( sgIsBigEndian() ) {
+ sgEndianSwap( (uint64_t*)var);
}
}
-void sgWriteLongLong ( gzFile fd, const long long int var )
+void sgWriteLongLong ( gzFile fd, const int64_t var )
{
- if ( sgIsBigEndian ) {
- sgEndianSwap( (unsigned long long*)&var);
+ if ( sgIsBigEndian() ) {
+ sgEndianSwap( (uint64_t*)&var);
}
- if ( gzwrite ( fd, (void *)(&var), sizeof(long long int) )
- != sizeof(long long int) )
+ if ( gzwrite ( fd, (void *)(&var), sizeof(int64_t) )
+ != sizeof(int64_t) )
{
write_error = true ;
}
if ( gzread ( fd, var, sizeof(unsigned short) ) != sizeof(unsigned short) ){
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned short int*)var);
}
}
void sgWriteUShort ( gzFile fd, const unsigned short var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned short*)&var);
}
if ( gzwrite ( fd, (void *)(&var), sizeof(unsigned short) )
if ( gzread ( fd, var, sizeof(short) ) != sizeof(short) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned short int*)var);
}
}
void sgWriteShort ( gzFile fd, const short var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
sgEndianSwap( (unsigned short*)&var);
}
if ( gzwrite ( fd, (void *)(&var), sizeof(short) ) != sizeof(short) ) {
if ( gzread ( fd, var, sizeof(float) * n ) != (int)(sizeof(float) * n) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
for ( unsigned int i = 0; i < n; ++i ) {
sgEndianSwap( (unsigned int*)var++);
}
void sgWriteFloat ( gzFile fd, const unsigned int n, const float *var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
float *swab = new float[n];
float *ptr = swab;
memcpy( swab, var, sizeof(float) * n );
if ( gzread ( fd, var, sizeof(double) * n ) != (int)(sizeof(double) * n) ) {
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
for ( unsigned int i = 0; i < n; ++i ) {
- sgEndianSwap( (unsigned long long*)var++);
+ sgEndianSwap( (uint64_t*)var++);
}
}
}
void sgWriteDouble ( gzFile fd, const unsigned int n, const double *var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
double *swab = new double[n];
double *ptr = swab;
memcpy( swab, var, sizeof(double) * n );
for ( unsigned int i = 0; i < n; ++i ) {
- sgEndianSwap( (unsigned long long*)ptr++);
+ sgEndianSwap( (uint64_t*)ptr++);
}
var = swab;
}
{
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
for ( unsigned int i = 0; i < n; ++i ) {
sgEndianSwap( (unsigned short int*)var++);
}
void sgWriteUShort ( gzFile fd, const unsigned int n, const unsigned short *var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
unsigned short *swab = new unsigned short[n];
unsigned short *ptr = swab;
memcpy( swab, var, sizeof(unsigned short) * n );
{
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
for ( unsigned int i = 0; i < n; ++i ) {
sgEndianSwap( (unsigned short int*)var++);
}
void sgWriteShort ( gzFile fd, const unsigned int n, const short *var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
short *swab = new short[n];
short *ptr = swab;
memcpy( swab, var, sizeof(short) * n );
{
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
for ( unsigned int i = 0; i < n; ++i ) {
sgEndianSwap( (unsigned int*)var++);
}
void sgWriteUInt ( gzFile fd, const unsigned int n, const unsigned int *var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
unsigned int *swab = new unsigned int[n];
unsigned int *ptr = swab;
memcpy( swab, var, sizeof(unsigned int) * n );
{
read_error = true ;
}
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
for ( unsigned int i = 0; i < n; ++i ) {
sgEndianSwap( (unsigned int*)var++);
}
void sgWriteInt ( gzFile fd, const unsigned int n, const int *var )
{
- if ( sgIsBigEndian ) {
+ if ( sgIsBigEndian() ) {
int *swab = new int[n];
int *ptr = swab;
memcpy( swab, var, sizeof(int) * n );