]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/lowlevel.cxx
Update the SoundSample api so we can request that a copy of the sample be
[simgear.git] / simgear / io / lowlevel.cxx
index 54ba40c365cb2b2371844801c2c6113e29f1ed89..f8c72342393e591583d2892ca321b8cd30d8f1d8 100644 (file)
@@ -1,4 +1,4 @@
-// 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
 // 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 ;
 
@@ -33,36 +36,7 @@ void sgClearWriteError() { 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) ) {
@@ -84,7 +58,7 @@ void sgReadFloat ( gzFile fd, float *var )
     if ( gzread ( fd, var, sizeof(float) ) != sizeof(float) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned int*)var);
     }
 }
@@ -92,7 +66,7 @@ void sgReadFloat ( gzFile fd, float *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) ) {
@@ -106,16 +80,16 @@ void sgReadDouble ( gzFile fd, double *var )
     if ( gzread ( fd, var, sizeof(double) ) != sizeof(double) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
-        sgEndianSwap( (unsigned long long*)var);
+    if ( sgIsBigEndian() ) {
+        sgEndianSwap( (uint64*)var);
     }
 }
 
 
 void sgWriteDouble ( gzFile fd, const double var )
 {
-    if ( sgIsBigEndian ) {
-        sgEndianSwap( (unsigned long long*)&var);
+    if ( sgIsBigEndian() ) {
+        sgEndianSwap( (uint64*)&var);
     }
     if ( gzwrite ( fd, (void *)(&var), sizeof(double) ) != sizeof(double) ) {
         write_error = true ;
@@ -128,7 +102,7 @@ void sgReadUInt ( gzFile fd, unsigned int *var )
     if ( gzread ( fd, var, sizeof(unsigned int) ) != sizeof(unsigned int) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned int*)var);
     }
 }
@@ -136,7 +110,7 @@ void sgReadUInt ( gzFile fd, 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) )
@@ -152,7 +126,7 @@ void sgReadInt ( gzFile fd, int *var )
     if ( gzread ( fd, var, sizeof(int) ) != sizeof(int) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned int*)var);
     }
 }
@@ -160,7 +134,7 @@ void sgReadInt ( gzFile fd, 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) ) {
@@ -174,7 +148,7 @@ void sgReadLong ( gzFile fd, long int *var )
     if ( gzread ( fd, var, sizeof(long int) ) != sizeof(long int) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned int*)var);
     }
 }
@@ -182,7 +156,7 @@ void sgReadLong ( gzFile fd, long int *var )
 
 void sgWriteLong ( gzFile fd, const long int var )
 {
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned int*)&var);
     }
     if ( gzwrite ( fd, (void *)(&var), sizeof(long int) )
@@ -193,24 +167,24 @@ void sgWriteLong ( gzFile fd, const long int var )
 }
 
 
-void sgReadLongLong ( gzFile fd, long long int *var )
+void sgReadLongLong ( gzFile fd, int64 *var )
 {
-    if ( gzread ( fd, var, sizeof(long long int) ) != sizeof(long long int) ) {
+    if ( gzread ( fd, var, sizeof(int64) ) != sizeof(int64) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
-        sgEndianSwap( (unsigned long long int*)var);
+    if ( sgIsBigEndian() ) {
+        sgEndianSwap( (uint64*)var);
     }
 }
 
 
-void sgWriteLongLong ( gzFile fd, const long long int var )
+void sgWriteLongLong ( gzFile fd, const int64 var )
 {
-    if ( sgIsBigEndian ) {
-        sgEndianSwap( (unsigned long long*)&var);
+    if ( sgIsBigEndian() ) {
+        sgEndianSwap( (uint64*)&var);
     }
-    if ( gzwrite ( fd, (void *)(&var), sizeof(long long int) )
-         != sizeof(long long int) )
+    if ( gzwrite ( fd, (void *)(&var), sizeof(int64) )
+         != sizeof(int64) )
     {
         write_error = true ;
     }
@@ -222,7 +196,7 @@ void sgReadUShort ( gzFile fd, unsigned short *var )
     if ( gzread ( fd, var, sizeof(unsigned short) ) != sizeof(unsigned short) ){
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned short int*)var);
     }
 }
@@ -230,7 +204,7 @@ void sgReadUShort ( gzFile fd, unsigned short *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) )
@@ -246,7 +220,7 @@ void sgReadShort ( gzFile fd, short *var )
     if ( gzread ( fd, var, sizeof(short) ) != sizeof(short) ) {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         sgEndianSwap( (unsigned short int*)var);
     }
 }
@@ -254,7 +228,7 @@ void sgReadShort ( gzFile fd, short *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) ) {
@@ -268,7 +242,7 @@ void sgReadFloat ( gzFile fd, const unsigned int n, float *var )
     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++);
         }
@@ -278,7 +252,7 @@ void sgReadFloat ( gzFile fd, const unsigned int n, float *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 );
@@ -299,9 +273,9 @@ void sgReadDouble ( gzFile fd, const unsigned int n, double *var )
     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*)var++);
         }
     }
 }
@@ -309,12 +283,12 @@ void sgReadDouble ( gzFile fd, const unsigned int n, double *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*)ptr++);
         }
         var = swab;
     }
@@ -349,7 +323,7 @@ void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *var )
     {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         for ( unsigned int i = 0; i < n; ++i ) {
             sgEndianSwap( (unsigned short int*)var++);
         }
@@ -359,7 +333,7 @@ void sgReadUShort ( gzFile fd, const unsigned int n, unsigned short *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 );
@@ -384,7 +358,7 @@ void sgReadShort ( gzFile fd, const unsigned int n, short *var )
     {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         for ( unsigned int i = 0; i < n; ++i ) {
             sgEndianSwap( (unsigned short int*)var++);
         }
@@ -394,7 +368,7 @@ void sgReadShort ( gzFile fd, const unsigned int n, short *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 );
@@ -418,7 +392,7 @@ void sgReadUInt ( gzFile fd, const unsigned int n, unsigned int *var )
     {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         for ( unsigned int i = 0; i < n; ++i ) {
             sgEndianSwap( (unsigned int*)var++);
         }
@@ -428,7 +402,7 @@ void sgReadUInt ( gzFile fd, const unsigned int n, 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 );
@@ -453,7 +427,7 @@ void sgReadInt ( gzFile fd, const unsigned int n, int *var )
     {
         read_error = true ;
     }
-    if ( sgIsBigEndian ) {
+    if ( sgIsBigEndian() ) {
         for ( unsigned int i = 0; i < n; ++i ) {
             sgEndianSwap( (unsigned int*)var++);
         }
@@ -463,7 +437,7 @@ void sgReadInt ( gzFile fd, const unsigned int n, 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 );