src/endian_check.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 9551 6f60dca6c566
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1692
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1692
diff changeset
     2
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     3
/** @file endian_check.cpp
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     4
 * This pretty simple file checks if the system is LITTLE_ENDIAN or BIG_ENDIAN
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     5
 *  it does that by putting a 1 and a 0 in an array, and read it out as one
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     6
 *  number. If it is 1, it is LITTLE_ENDIAN, if it is 256, it is BIG_ENDIAN
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     7
 *
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     8
 * After that it outputs the contents of an include files (endian.h)
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
     9
 *  that says or TTD_LITTLE_ENDIAN, or TTD_BIG_ENDIAN. Makefile takes
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
    10
 *  care of the real writing to the file. */
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5587
diff changeset
    11
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    12
#include <stdio.h>
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    13
#include <string.h>
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    14
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    15
/** Supported endian types */
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    16
enum Endian {
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    17
	ENDIAN_LITTLE, ///< little endian
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    18
	ENDIAN_BIG     ///< big endian
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    19
};
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    20
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    21
/**
9551
6f60dca6c566 (svn r13571) -Codechange: define channels in struct Colour in different order on LE and BE machines
smatz
parents: 9543
diff changeset
    22
 * Shortcut to printf("#define TTD_ENDIAN TTD_*_ENDIAN")
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    23
 * @param endian endian type to define
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    24
 */
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    25
static inline void printf_endian(Endian endian)
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    26
{
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    27
	printf("#define TTD_ENDIAN %s\n", endian == ENDIAN_LITTLE ? "TTD_LITTLE_ENDIAN" : "TTD_BIG_ENDIAN");
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    28
}
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    29
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    30
/**
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    31
 * Main call of the endian_check program
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6125
diff changeset
    32
 * @param argc argument count
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6125
diff changeset
    33
 * @param argv arguments themselves
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    34
 * @return exit code
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    35
 */
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    36
int main (int argc, char *argv[])
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    37
{
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    38
	unsigned char endian_test[2] = { 1, 0 };
2719
75b0750edccf (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    39
	int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
1692
0b52df38cabc (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    40
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    41
	if (argc > 1 && strcmp(argv[1], "BE") == 0) force_BE = 1;
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    42
	if (argc > 1 && strcmp(argv[1], "LE") == 0) force_LE = 1;
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    43
	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
1692
0b52df38cabc (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    44
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    45
	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    46
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    47
	if (force_LE == 1) {
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    48
		printf_endian(ENDIAN_LITTLE);
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    49
	} else if (force_BE == 1) {
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    50
		printf_endian(ENDIAN_BIG);
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    51
	} else if (force_PREPROCESSOR == 1) {
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    52
		/* Support for universal binaries on OSX
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    53
		 * Universal binaries supports both PPC and x86
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    54
		 * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    55
		 */
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    56
		printf("#ifdef __BIG_ENDIAN__\n");
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    57
		printf_endian(ENDIAN_BIG);
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    58
		printf("#else\n");
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    59
		printf_endian(ENDIAN_LITTLE);
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    60
		printf("#endif\n");
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 6432
diff changeset
    61
	} else if (*(short*)endian_test == 1 ) {
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    62
		printf_endian(ENDIAN_LITTLE);
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    63
	} else {
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 7321
diff changeset
    64
		printf_endian(ENDIAN_BIG);
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    65
	}
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    66
	printf("#endif\n");
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    67
2713
9c42385e4f41 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    68
	return 0;
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    69
}