diff --git a/src/opatIO/private/opatIO.cpp b/src/opatIO/private/opatIO.cpp index 7a8adb2..b637d38 100644 --- a/src/opatIO/private/opatIO.cpp +++ b/src/opatIO/private/opatIO.cpp @@ -11,6 +11,26 @@ #include #include +// Function to check system endianness +bool is_big_endian() { + uint16_t test = 0x1; + return reinterpret_cast(&test)[0] == 0; +} + +// Generic function to swap bytes for any type +template +T swap_bytes(T value) { + static_assert(std::is_trivially_copyable::value, "swap_bytes only supports trivial types."); + T result; + uint8_t* src = reinterpret_cast(&value); + uint8_t* dest = reinterpret_cast(&result); + for (size_t i = 0; i < sizeof(T); i++) { + dest[i] = src[sizeof(T) - 1 - i]; + } + return result; +} + + // Constructor OpatIO::OpatIO() {} @@ -57,6 +77,12 @@ void OpatIO::readHeader(std::ifstream &file) { if (file.gcount() != sizeof(Header)) { throw std::runtime_error("Error reading header from file: " + filename); } + + if (is_big_endian()) { + header.version = swap_bytes(header.version); + header.numTables = swap_bytes(header.numTables); + header.indexOffset = swap_bytes(header.indexOffset); + } } // Read the table index from the file @@ -67,6 +93,7 @@ void OpatIO::readTableIndex(std::ifstream &file) { if (file.gcount() != static_cast(header.numTables * sizeof(TableIndex))) { throw std::runtime_error("Error reading table index from file: " + filename); } + buildTableIDToComposition(); } diff --git a/src/opatIO/public/opatIO.h b/src/opatIO/public/opatIO.h index 859615f..1da6ee9 100644 --- a/src/opatIO/public/opatIO.h +++ b/src/opatIO/public/opatIO.h @@ -9,6 +9,7 @@ #include #include +#pragma pack(1) struct Header { char magic[4]; uint16_t version; @@ -21,6 +22,7 @@ struct Header { char reserved[26]; }; +#pragma pack() struct TableIndex { double X; double Z;