From be0403079cd37132bc2a4a201b7e5f0a60984237 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 29 Dec 1999 00:01:48 +0000 Subject: Added some functions to read integers with endianess check git-svn-id: http://svn.leocad.org/trunk@41 c7d43263-9d01-0410-8a33-9dba5d9f93d6 --- common/file.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- common/file.h | 7 ++++ 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/common/file.cpp b/common/file.cpp index d64b4fa..ad7372a 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -5,6 +5,7 @@ #include #include #include "file.h" +#include "defines.h" ///////////////////////////////////////////////////////////////////////////// // File construction/destruction @@ -17,6 +18,102 @@ File::~File() { } +// ======================================================== + +unsigned long File::ReadByte(void* pBuf, unsigned long nCount) +{ + return Read(pBuf, nCount); +} + +// Reads a 2-byte value +unsigned long File::ReadShort(void* pBuf, unsigned long nCount) +{ + unsigned long read; + + read = Read(pBuf, nCount*2)/2; + +#ifdef _BIG_ENDIAN + unsigned long i; + unsigned short* val = (unsigned short*)pBuf, x; + + for (i = 0; i < read; i++) + { + x = *val; + *val = ((x>>8) | (x<<8)); + val++; + } +#endif + + return read; +} + +// Read a 4-byte value +unsigned long File::ReadLong(void* pBuf, unsigned long nCount) +{ + unsigned long read; + + read = Read(pBuf, nCount*4)/4; + +#ifdef _BIG_ENDIAN + unsigned long i; + unsigned long* val = (unsigned long*)pBuf, x; + + for (i = 0; i < read; i++) + { + x = *val; + *val = ((x>>24) | ((x>>8) & 0xff00) | ((x<<8) & 0xff0000) | (x<<24)); + val++; + } +#endif + + return read; +} + +unsigned long File::WriteByte(const void* pBuf, unsigned long nCount) +{ + return Write(pBuf, nCount); +} + +unsigned long File::WriteShort(const void* pBuf, unsigned long nCount) +{ +#ifdef _BIG_ENDIAN + unsigned long wrote = 0, i; + unsigned short* val = (unsigned short*)pBuf, x; + + for (i = 0; i < nCount; i++) + { + x = (((*val)>>8) | ((*val)<<8)); + val++; + wrote += Write(&x, 2)/2; + } + + return wrote; +#else + return Write(pBuf, nCount*2); +#endif +} + +unsigned long File::WriteLong(const void* pBuf, unsigned long nCount) +{ +#ifdef _BIG_ENDIAN + unsigned long wrote = 0, i; + unsigned long* val = (unsigned long*)pBuf, x; + + for (i = 0; i < nCount; i++) + { + x = (((*val)>>24) | (((*val)>>8) & 0xff00) | (((*val)<<8) & 0xff0000) | ((*val)<<24)); + val++; + wrote += Write(&x, 4)/4; + } + + return wrote; +#else + return Write(pBuf, nCount*4); +#endif +} + +// ======================================================== + FileMem::FileMem() { m_nGrowBytes = 1024; @@ -319,5 +416,3 @@ unsigned long FileDisk::GetLength() const return nLen; } - - diff --git a/common/file.h b/common/file.h index 90b3395..6445a61 100644 --- a/common/file.h +++ b/common/file.h @@ -27,6 +27,13 @@ public: virtual int GetChar()=0; virtual int PutChar(int c)=0; + unsigned long ReadByte(void* pBuf, unsigned long nCount); + unsigned long ReadShort(void* pBuf, unsigned long nCount); + unsigned long ReadLong(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); + virtual void Abort()=0; virtual void Flush()=0; virtual void Close()=0; -- cgit v1.2.3