From b5dbd26dc5bfce122b24303fa2fa15ac387da1f8 Mon Sep 17 00:00:00 2001 From: Norihiro Watanabe <norihiro.watanabe@ufz.de> Date: Tue, 30 Oct 2012 09:37:46 +0100 Subject: [PATCH] follow the advice of Lars for checking endian --- BaseLib/SystemTools.h | 10 +++++----- Tests/BaseLib/TestSystemTools.cpp | 25 +++++++++++++++++++++++++ scripts/cmake/CheckTypeSizes.cmake | 14 +++++++++++++- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 Tests/BaseLib/TestSystemTools.cpp diff --git a/BaseLib/SystemTools.h b/BaseLib/SystemTools.h index 5f775ddf4f6..8939814f1fe 100644 --- a/BaseLib/SystemTools.h +++ b/BaseLib/SystemTools.h @@ -19,11 +19,11 @@ namespace BaseLib /// return if this system supports little endian or not inline bool IsLittleEndian() { - int x = 0x00000001; - if (*(char*)&x) - return true; //am little - else - return false; //am big +#ifdef ENDIAN_IS_BIG + return false; +#elif ENDIAN_IS_LITTLE + return true; +#endif } } diff --git a/Tests/BaseLib/TestSystemTools.cpp b/Tests/BaseLib/TestSystemTools.cpp new file mode 100644 index 00000000000..16666ee91db --- /dev/null +++ b/Tests/BaseLib/TestSystemTools.cpp @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + * \file TestSystemTools.cpp + * + * Created on 2012-10-30 by Norihiro Watanabe + */ + +// ** INCLUDES ** +#include "gtest.h" + +#include "SystemTools.h" + +TEST(BaseLib, EndianLittle) { + bool isLittle = false; + int x = 0x00000001; + if (*(char*)&x) + isLittle = true; //am little + + ASSERT_EQ (isLittle, BaseLib::IsLittleEndian()); +} + diff --git a/scripts/cmake/CheckTypeSizes.cmake b/scripts/cmake/CheckTypeSizes.cmake index 4d9f23b54b1..e88ff9e73d1 100644 --- a/scripts/cmake/CheckTypeSizes.cmake +++ b/scripts/cmake/CheckTypeSizes.cmake @@ -14,4 +14,16 @@ ELSE() SET( HAVE_64_BIT 1 ) ADD_DEFINITIONS(-DHAVE_64_BIT) SET( BITS 64) -ENDIF() \ No newline at end of file +ENDIF() + +# Check endian of the system +INCLUDE (TestBigEndian) +TEST_BIG_ENDIAN (IS_BIG_ENDIAN) +IF (IS_BIG_ENDIAN) + ADD_DEFINITIONS(-DENDIAN_IS_BIG) + # cannot use BIG_ENDIAN because it's reserved in Linux +ELSE () + ADD_DEFINITIONS(-DENDIAN_IS_LITTLE) + # cannot use LITTLE_ENDIAN because it's reserved in Linux +ENDIF () # IS_BIG_ENDIAN + -- GitLab