summaryrefslogtreecommitdiff
path: root/common/file.cpp
diff options
context:
space:
mode:
authorleo2001-01-04 15:24:22 +0000
committerleo2001-01-04 15:24:22 +0000
commit426468beb39bc18138bfe08494716fe60532d869 (patch)
treef9d274f6a8b1594b74daf7e32155bc462f14aa10 /common/file.cpp
parent4006bb8cb30bf4b92a69f6f7d645197d9429bb5a (diff)
Read doubles correctly on big endian machines
git-svn-id: http://svn.leocad.org/trunk@204 c7d43263-9d01-0410-8a33-9dba5d9f93d6
Diffstat (limited to 'common/file.cpp')
-rw-r--r--common/file.cpp67
1 files changed, 66 insertions, 1 deletions
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()