mirror of
https://github.com/glest/glest-source.git
synced 2025-08-11 02:43:57 +02:00
- added new byte order code
This commit is contained in:
43
source/shared_lib/include/platform/common/byte_order.h
Normal file
43
source/shared_lib/include/platform/common/byte_order.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef BYTE_ORDER_H
|
||||
#define BYTE_ORDER_H
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Shared{ namespace PlatformByteOrder {
|
||||
|
||||
template<class T> 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<class T> T toCommonEndian(T t) {
|
||||
static bool bigEndianSystem = isBigEndian();
|
||||
if(bigEndianSystem == true) {
|
||||
t = EndianReverse(t);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
template<class T> T fromCommonEndian(T t) {
|
||||
static bool bigEndianSystem = isBigEndian();
|
||||
if(bigEndianSystem == true) {
|
||||
t = EndianReverse(t);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
#endif
|
17
source/shared_lib/sources/platform/common/byte_order.cpp
Normal file
17
source/shared_lib/sources/platform/common/byte_order.cpp
Normal file
@@ -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{
|
||||
|
||||
|
||||
}};
|
Reference in New Issue
Block a user