From 12008dbdb854af9fa8f65a6a2e4b5b919acdab06 Mon Sep 17 00:00:00 2001 From: Alvaro Date: Fri, 31 Jan 2025 23:58:44 +0100 Subject: [PATCH] Fixed (?) StreamBuffer and others in big endian machines --- CMakeLists.txt | 11 +++++++++++ include/asl/Matrix.h | 3 +++ src/CMakeLists.txt | 4 ++++ src/Log.cpp | 2 +- tests/unittests.cpp | 10 ++++++++-- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd95b10..3c54fe2 100644 --- CMakeLists.txt +++ CMakeLists.txt @@ -40,6 +40,17 @@ option(ASL_SAMPLES "Build samples") option(ASL_TESTS "Build tests") option(ASL_SOCKET_LOCAL "Support Unix sockets on Windows") +set(ASL_BIGENDIAN OFF) + +if(DEFINED CMAKE_CXX_BYTE_ORDER) + if(CMAKE_CXX_BYTE_ORDER STREQUAL BIG_ENDIAN) + set(ASL_BIGENDIAN ON) + endif() +else() + include(TestBigEndian) + test_big_endian(ASL_BIGENDIAN) +endif() + if(ASL_TLS) if(TARGET MbedTLS::mbedtls) set(mbedTLS_LIB MbedTLS::mbedtls) diff --git a/include/asl/Matrix.h b/include/asl/Matrix.h index f2088ab..c0af97c 100644 --- include/asl/Matrix.h +++ include/asl/Matrix.h @@ -83,6 +83,9 @@ class Matrix_ : public Array2 */ T trace() const { if (this->_rows != this->_cols) return 0; T t = 0; for (int i = 0; i < this->rows(); i++) t += (*this)(i, i); return t; } + /** + * copies the contents of matrix b into this + */ void copy(const Matrix_& b) { this->resize(b.rows(), b.cols()); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bf71bd7..bccb9d0 100644 --- src/CMakeLists.txt +++ src/CMakeLists.txt @@ -97,6 +97,10 @@ if(ASL_SOCKET_LOCAL) list(APPEND ASL_DEFSP ASL_SOCKET_LOCAL) endif() +if(ASL_BIGENDIAN) + list(APPEND ASL_DEFS ASL_BIGENDIAN) +endif() + if(POLICY CMP0022) cmake_policy(SET CMP0022 NEW) endif() diff --git a/src/Log.cpp b/src/Log.cpp index 3394b07..eaa6379 100644 --- src/Log.cpp +++ src/Log.cpp @@ -21,7 +21,7 @@ Log::Log() _logfile = "log.log"; _useconsole = true; _usefile = true; - _maxLevel = 3; + _maxLevel = 2; _mutex = new Mutex; } diff --git a/tests/unittests.cpp b/tests/unittests.cpp index 71822d1..0affef8 100644 --- tests/unittests.cpp +++ tests/unittests.cpp @@ -35,15 +35,21 @@ ASL_TEST(File) bfile << int(-3) << 3.5f; bfile.setEndian(ENDIAN_BIG); bfile << 0x10203040; + bfile << 0x10203040; bfile.close(); bfile.open(File::READ); - bfile.setEndian(ENDIAN_LITTLE); + bfile.setEndian(ENDIAN_NATIVE); int n; float x; bfile >> n >> x; ASL_ASSERT(n == -3 && x == 3.5f); - ASL_ASSERT(bfile.read() == 0x40302010); + bfile.setEndian(ENDIAN_LITTLE); + byte b[4]; + bfile >> b[0] >> b[1] >> b[2] >> b[3]; + ASL_ASSERT(b[0] == 0x10 && b[1] == 0x20 && b[2] == 0x30 && b[3] == 0x40); + bfile.setEndian(ENDIAN_BIG); + ASL_ASSERT(bfile.read() == 0x10203040); TextFile tfile("lines.txt", File::WRITE); String line1 = "123";