endian_check.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00021 #include <stdio.h>
00022 #include <string.h>
00023 
00025 enum Endian {
00026   ENDIAN_LITTLE, 
00027   ENDIAN_BIG     
00028 };
00029 
00034 static inline void printf_endian(Endian endian)
00035 {
00036   printf("#define TTD_ENDIAN %s\n", endian == ENDIAN_LITTLE ? "TTD_LITTLE_ENDIAN" : "TTD_BIG_ENDIAN");
00037 }
00038 
00045 int main (int argc, char *argv[])
00046 {
00047   unsigned char endian_test[2] = { 1, 0 };
00048   int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
00049 
00050   if (argc > 1 && strcmp(argv[1], "BE") == 0) force_BE = 1;
00051   if (argc > 1 && strcmp(argv[1], "LE") == 0) force_LE = 1;
00052   if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
00053 
00054   printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
00055 
00056   if (force_LE == 1) {
00057     printf_endian(ENDIAN_LITTLE);
00058   } else if (force_BE == 1) {
00059     printf_endian(ENDIAN_BIG);
00060   } else if (force_PREPROCESSOR == 1) {
00061     /* Support for universal binaries on OSX
00062      * Universal binaries supports both PPC and x86
00063      * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
00064      */
00065     printf("#ifdef __BIG_ENDIAN__\n");
00066     printf_endian(ENDIAN_BIG);
00067     printf("#else\n");
00068     printf_endian(ENDIAN_LITTLE);
00069     printf("#endif\n");
00070   } else if (*(short*)endian_test == 1 ) {
00071     printf_endian(ENDIAN_LITTLE);
00072   } else {
00073     printf_endian(ENDIAN_BIG);
00074   }
00075   printf("#endif\n");
00076 
00077   return 0;
00078 }

Generated on Sun May 8 07:30:11 2011 for OpenTTD by  doxygen 1.6.1