src/endian_check.cpp
author truebrain
Fri, 13 Jun 2008 19:57:25 +0000
branchnoai
changeset 10957 7a140b4cd91d
parent 9694 e72987579514
child 10998 04f58fa3dbb0
child 11044 097ea3e7ec56
permissions -rw-r--r--
(svn r13511) [NoAI] -Fix: add a reference to objects given in Valuate(), so they remain valid during their usage. This allows nameless functions (lambda functions) in Valuate() on long lists.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1692
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1692
diff changeset
     2
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     3
/** @file endian_check.cpp
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     4
 * This pretty simple file checks if the system is LITTLE_ENDIAN or BIG_ENDIAN
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     5
 *  it does that by putting a 1 and a 0 in an array, and read it out as one
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     6
 *  number. If it is 1, it is LITTLE_ENDIAN, if it is 256, it is BIG_ENDIAN
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     7
 *
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     8
 * After that it outputs the contents of an include files (endian.h)
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
     9
 *  that says or TTD_LITTLE_ENDIAN, or TTD_BIG_ENDIAN. Makefile takes
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
    10
 *  care of the real writing to the file. */
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5838
diff changeset
    11
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    12
#include <stdio.h>
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    13
#include <string.h>
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    14
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    15
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    16
 * Main call of the endian_check program
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6451
diff changeset
    17
 * @param argc argument count
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6451
diff changeset
    18
 * @param argv arguments themselves
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    19
 * @return exit code
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    20
 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    21
int main (int argc, char *argv[])
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    22
{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    23
	unsigned char endian_test[2] = { 1, 0 };
2719
146eaa0ec83c (svn r3264) -Codechange: [OSX] OSX targets will now always use the preprocessor to determine endianess
bjarni
parents: 2713
diff changeset
    24
	int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
1692
2587eee1c632 (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    25
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    26
	if (argc > 1 && strcmp(argv[1], "BE") == 0) force_BE = 1;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    27
	if (argc > 1 && strcmp(argv[1], "LE") == 0) force_LE = 1;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    28
	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
1692
2587eee1c632 (svn r2196) -Change: updated the Makefile, now it works for crossplatform compiling
TrueLight
parents: 200
diff changeset
    29
2713
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    30
	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    31
2713
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    32
	if (force_LE == 1) {
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    33
		printf("#define TTD_LITTLE_ENDIAN\n");
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    34
	} else if (force_BE == 1) {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    35
		printf("#define TTD_BIG_ENDIAN\n");
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    36
	} else if (force_PREPROCESSOR == 1) {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    37
		/* Support for universal binaries on OSX
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    38
		 * Universal binaries supports both PPC and x86
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    39
		 * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    40
		 */
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    41
		printf("#ifdef __BIG_ENDIAN__\n");
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    42
		printf("#define TTD_BIG_ENDIAN\n");
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    43
		printf("#else\n");
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    44
		printf("#define TTD_LITTLE_ENDIAN\n");
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    45
		printf("#endif\n");
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    46
	} else if (*(short*)endian_test == 1 ) {
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    47
		printf("#define TTD_LITTLE_ENDIAN\n");
2713
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    48
	} else {
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9574
diff changeset
    49
		printf("#define TTD_BIG_ENDIAN\n");
2713
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    50
	}
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    51
	printf("#endif\n");
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    52
2713
9f06e1f94ce9 (svn r3258) -Feature: [OSX] added support for universal binaries
bjarni
parents: 2186
diff changeset
    53
	return 0;
157
dd017fa3bad8 (svn r158) -Fix: make endianess check 100% accurate (hopefully ;))
truelight
parents:
diff changeset
    54
}