summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorleo2001-01-04 15:24:22 +0000
committerleo2001-01-04 15:24:22 +0000
commit426468beb39bc18138bfe08494716fe60532d869 (patch)
treef9d274f6a8b1594b74daf7e32155bc462f14aa10
parent4006bb8cb30bf4b92a69f6f7d645197d9429bb5a (diff)
Read doubles correctly on big endian machines
git-svn-id: http://svn.leocad.org/trunk@204 c7d43263-9d01-0410-8a33-9dba5d9f93d6
-rw-r--r--common/camera.cpp20
-rw-r--r--common/file.cpp67
-rw-r--r--common/file.h2
3 files changed, 78 insertions, 11 deletions
diff --git a/common/camera.cpp b/common/camera.cpp
index 0ffd218..52c6350 100644
--- a/common/camera.cpp
+++ b/common/camera.cpp
@@ -251,21 +251,21 @@ bool Camera::FileLoad (File& file)
double d[3];
float f[3];
- file.Read(d, sizeof(d));
+ file.ReadDouble (d, 3);
f[0] = (float)d[0];
f[1] = (float)d[1];
f[2] = (float)d[2];
ChangeKey (1, false, true, f, LC_CK_EYE);
ChangeKey (1, true, true, f, LC_CK_EYE);
- file.Read(d, sizeof(d));
+ file.ReadDouble (d, 3);
f[0] = (float)d[0];
f[1] = (float)d[1];
f[2] = (float)d[2];
ChangeKey (1, false, true, f, LC_CK_TARGET);
ChangeKey (1, true, true, f, LC_CK_TARGET);
- file.Read(d, sizeof(d));
+ file.ReadDouble (d, 3);
f[0] = (float)d[0];
f[1] = (float)d[1];
f[2] = (float)d[2];
@@ -283,10 +283,10 @@ bool Camera::FileLoad (File& file)
double eye[3], target[3], up[3];
float f[3];
- file.Read(eye, sizeof(double[3]));
- file.Read(target, sizeof(double[3]));
- file.Read(up, sizeof(double[3]));
- file.Read(&step, 1);
+ file.ReadDouble (eye, 3);
+ file.ReadDouble (target, 3);
+ file.ReadDouble (up, 3);
+ file.ReadByte (&step, 1);
if (up[0] == 0 && up[1] == 0 && up[2] == 0)
up[2] = 1;
@@ -323,9 +323,9 @@ bool Camera::FileLoad (File& file)
if (version < 4)
{
double d;
- file.Read(&d, sizeof(d)); m_fovy = (float)d;
- file.Read(&d, sizeof(d)); m_zFar = (float)d;
- file.Read(&d, sizeof(d)); m_zNear= (float)d;
+ file.ReadDouble (&d, 1); m_fovy = (float)d;
+ file.ReadDouble (&d, 1); m_zFar = (float)d;
+ file.ReadDouble (&d, 1); m_zNear= (float)d;
}
else
{
diff --git a/common/file.cpp b/common/file.cpp
index f858342..70521a3 100644
--- a/common/file.cpp
+++ b/common/file.cpp
@@ -74,7 +74,7 @@ unsigned long File::ReadLong (void* pBuf, unsigned long nCount)
return read;
}
-// reads 4-byte integers
+// reads 4-byte floats
unsigned long File::ReadFloat (void* pBuf, unsigned long nCount)
{
unsigned long read;
@@ -103,6 +103,39 @@ unsigned long File::ReadFloat (void* pBuf, unsigned long nCount)
return read;
}
+// reads 8-byte floats
+unsigned long File::ReadDouble (void* pBuf, unsigned long nCount)
+{
+ unsigned long read;
+
+ read = Read (pBuf, nCount*8)/8;
+
+#ifdef LC_BIG_ENDIAN
+ unsigned long i;
+ double* val = (double*)pBuf;
+ union { unsigned char b[8]; double d; } in, out;
+
+ for (i = 0; i < read; i++)
+ {
+ in.d = *val;
+
+ out.b[0] = in.b[7];
+ out.b[1] = in.b[6];
+ out.b[2] = in.b[5];
+ out.b[3] = in.b[4];
+ out.b[4] = in.b[3];
+ out.b[5] = in.b[2];
+ out.b[6] = in.b[1];
+ out.b[7] = in.b[0];
+
+ *val = out.d;
+ val++;
+ }
+#endif
+
+ return read;
+}
+
// writes 1-byte integers
unsigned long File::WriteByte (const void* pBuf, unsigned long nCount)
{
@@ -177,6 +210,38 @@ unsigned long File::WriteFloat (const void* pBuf, unsigned long nCount)
#endif
}
+// writes 8-byte floats
+unsigned long File::WriteDouble (const void* pBuf, unsigned long nCount)
+{
+#ifdef LC_BIG_ENDIAN
+ unsigned long wrote = 0, i;
+ double* val = (double*)pBuf, x;
+ union { unsigned char b[8]; double d; } in, out;
+
+ for (i = 0; i < nCount; i++)
+ {
+ in.d = *val;
+ val++;
+
+ out.b[0] = in.b[7];
+ out.b[1] = in.b[6];
+ out.b[2] = in.b[5];
+ out.b[3] = in.b[4];
+ out.b[4] = in.b[3];
+ out.b[5] = in.b[2];
+ out.b[6] = in.b[1];
+ out.b[7] = in.b[0];
+ x = out.d;
+
+ wrote += Write (&x, 8)/8;
+ }
+
+ return wrote;
+#else
+ return Write (pBuf, nCount*8)/8;
+#endif
+}
+
// =============================================================================
FileMem::FileMem()
diff --git a/common/file.h b/common/file.h
index 7e9ba9c..5e2678f 100644
--- a/common/file.h
+++ b/common/file.h
@@ -31,10 +31,12 @@ class File
unsigned long ReadShort (void* pBuf, unsigned long nCount);
unsigned long ReadLong (void* pBuf, unsigned long nCount);
unsigned long ReadFloat (void* pBuf, unsigned long nCount);
+ unsigned long ReadDouble (void* pBuf, unsigned long nCount);
unsigned long WriteByte (const void* pBuf, unsigned long nCount);
unsigned long WriteShort (const void* pBuf, unsigned long nCount);
unsigned long WriteLong (const void* pBuf, unsigned long nCount);
unsigned long WriteFloat (const void* pBuf, unsigned long nCount);
+ unsigned long WriteDouble (const void* pBuf, unsigned long nCount);
virtual void Abort()=0;
virtual void Flush()=0;