]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/lowlevel.hxx
Mac OS X fixes from Markus Morawitz
[simgear.git] / simgear / io / lowlevel.hxx
index 979f243bf12d4b17dbf4a3620744656c71ca7621..fc8398f54d2b74d38b689cbad85dee8b8e49055f 100644 (file)
@@ -4,7 +4,7 @@
 // Shamelessly adapted from plib  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
 
 
 #include <stdio.h>
-
-#ifdef HAVE_ZLIB
-#  include <zlib.h>
-#else
-#  include <simgear/zlib/zlib.h>
-#endif
+#include <zlib.h>
 
 #include <plib/sg.h>
 
+#ifdef _MSC_VER
+typedef __int64 int64;
+typedef __int64 uint64;
+#else
+typedef long long int64;
+typedef unsigned long long uint64;
+#endif
 
 // Note that output is written in little endian form (and converted as
-// necessary)
+// necessary for big endian machines)
 
 void sgReadChar ( gzFile fd, char *var ) ;
 void sgWriteChar ( gzFile fd, const char var ) ;
@@ -54,8 +56,8 @@ void sgReadInt ( gzFile fd, int *var ) ;
 void sgWriteInt ( gzFile fd, const int var ) ;
 void sgReadLong ( gzFile fd, long int *var ) ;
 void sgWriteLong ( gzFile fd, const long int var ) ;
-void sgReadLongLong ( gzFile fd, long long int *var ) ;
-void sgWriteLongLong ( gzFile fd, const long long int var ) ;
+void sgReadLongLong ( gzFile fd, int64 *var ) ;
+void sgWriteLongLong ( gzFile fd, const int64 var ) ;
 void sgReadUShort ( gzFile fd, unsigned short *var ) ;
 void sgWriteUShort ( gzFile fd, const unsigned short var ) ;
 void sgReadShort ( gzFile fd, short *var ) ;
@@ -79,22 +81,92 @@ void sgWriteBytes ( gzFile fd, const unsigned int n, const void *var ) ;
 void sgReadString ( gzFile fd, char **var ) ;
 void sgWriteString ( gzFile fd, const char *var ) ;
 
-void sgReadVec2 ( gzFile fd, sgVec2 var ) ;
-void sgWriteVec2 ( gzFile fd, const sgVec2 var ) ;
-void sgReadVec3 ( gzFile fd, sgVec3 var ) ;
-void sgWriteVec3 ( gzFile fd, const sgVec3 var ) ;
-void sgReaddVec3 ( gzFile fd, sgdVec3 var ) ;
-void sgWritedVec3 ( gzFile fd, const sgdVec3 var ) ;
-void sgReadVec4 ( gzFile fd, sgVec4 var ) ;
-void sgWriteVec4 ( gzFile fd, const sgVec4 var ) ;
-
-void sgReadMat4 ( gzFile fd, sgMat4 var ) ;
-void sgWriteMat4 ( gzFile fd, const sgMat4 var ) ;
+inline void sgReadVec2  ( gzFile fd, sgVec2 var ) {
+    sgReadFloat  ( fd, 2, var ) ;
+}
+inline void sgWriteVec2 ( gzFile fd, const sgVec2 var ) {
+    sgWriteFloat ( fd, 2, var ) ;
+}
+
+inline void sgReadVec3  ( gzFile fd, sgVec3 var ) {
+    sgReadFloat  ( fd, 3, var ) ;
+}
+inline void sgWriteVec3 ( gzFile fd, const sgVec3 var ) {
+    sgWriteFloat ( fd, 3, var ) ;
+}
+
+inline void sgReaddVec3  ( gzFile fd, sgdVec3 var ) {
+    sgReadDouble  ( fd, 3, var ) ;
+}
+inline void sgWritedVec3 ( gzFile fd, const sgdVec3 var ) {
+    sgWriteDouble ( fd, 3, var ) ;
+}
+
+inline void sgReadVec4  ( gzFile fd, sgVec4 var ) {
+    sgReadFloat  ( fd, 4, var ) ;
+}
+inline void sgWriteVec4 ( gzFile fd, const sgVec4 var ) {
+    sgWriteFloat ( fd, 4, var ) ;
+}
+
+inline void sgReadMat4  ( gzFile fd, sgMat4 var ) {
+    sgReadFloat  ( fd, 16, (float *)var ) ;
+}
+inline void sgWriteMat4 ( gzFile fd, const sgMat4 var ) {
+    sgWriteFloat ( fd, 16, (float *)var ) ;
+}
 
 void sgClearReadError();
 void sgClearWriteError();
 int sgReadError();
 int sgWriteError();
 
+inline bool sgIsLittleEndian() {
+    static const int sgEndianTest = 1;
+    return (*((char *) &sgEndianTest ) != 0);
+}
+
+inline bool sgIsBigEndian() {
+    static const int sgEndianTest = 1;
+    return (*((char *) &sgEndianTest ) == 0);
+}
+
+inline void sgEndianSwap(unsigned short *x) {
+    *x =
+        (( *x >>  8 ) & 0x00FF ) | 
+        (( *x <<  8 ) & 0xFF00 ) ;
+}
+  
+inline void sgEndianSwap(unsigned int *x) {
+    *x =
+        (( *x >> 24 ) & 0x000000FF ) | 
+        (( *x >>  8 ) & 0x0000FF00 ) | 
+        (( *x <<  8 ) & 0x00FF0000 ) | 
+        (( *x << 24 ) & 0xFF000000 ) ;
+}
+  
+inline void sgEndianSwap(uint64 *x) {
+#ifndef _MSC_VER
+    *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 ) ;
+#else
+    *x =
+        (( *x >> 56 ) & 0x00000000000000FF ) | 
+        (( *x >> 40 ) & 0x000000000000FF00 ) | 
+        (( *x >> 24 ) & 0x0000000000FF0000 ) | 
+        (( *x >>  8 ) & 0x00000000FF000000 ) | 
+        (( *x <<  8 ) & 0x000000FF00000000 ) | 
+        (( *x << 24 ) & 0x0000FF0000000000 ) |
+        (( *x << 40 ) & 0x00FF000000000000 ) |
+        (( *x << 56 ) & 0xFF00000000000000 ) ;
+#endif
+}
 
 #endif // _SG_LOWLEVEL_HXX