diff --git a/source/shared_lib/include/platform/common/byte_order.h b/source/shared_lib/include/platform/common/byte_order.h new file mode 100644 index 000000000..5726dec60 --- /dev/null +++ b/source/shared_lib/include/platform/common/byte_order.h @@ -0,0 +1,43 @@ +#ifndef BYTE_ORDER_H +#define BYTE_ORDER_H + +#include + +namespace Shared{ namespace PlatformByteOrder { + +template T EndianReverse(T t) { + unsigned char uc[sizeof t]; + memcpy(uc, &t, sizeof t); + + for (unsigned char *b = uc, *e = uc + sizeof(T) - 1; b < e; ++b, --e) { + std::swap(*b, *e); + } + memcpy(&t, uc, sizeof t); + return t; +} + +static bool isBigEndian() { + short n = 0x1; + return (*(char*)(&n) == 0x0); +} + + +template T toCommonEndian(T t) { + static bool bigEndianSystem = isBigEndian(); + if(bigEndianSystem == true) { + t = EndianReverse(t); + } + return t; +} + +template T fromCommonEndian(T t) { + static bool bigEndianSystem = isBigEndian(); + if(bigEndianSystem == true) { + t = EndianReverse(t); + } + return t; +} + +}} + +#endif diff --git a/source/shared_lib/sources/platform/common/byte_order.cpp b/source/shared_lib/sources/platform/common/byte_order.cpp new file mode 100644 index 000000000..cf393ed77 --- /dev/null +++ b/source/shared_lib/sources/platform/common/byte_order.cpp @@ -0,0 +1,17 @@ +// ============================================================== +// This file is part of MegaGlest (www.megaglest.org) +// +// Copyright (C) 2012 Mark Vejvoda +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "byte_order.h" + +namespace Shared{ namespace PlatformByteOrder{ + + +}};