endian_check.cpp

Go to the documentation of this file.
00001 /* $Id: endian_check.cpp 13571 2008-06-18 21:19:04Z smatz $ */
00002 
00012 #include <stdio.h>
00013 #include <string.h>
00014 
00016 enum Endian {
00017   ENDIAN_LITTLE, 
00018   ENDIAN_BIG     
00019 };
00020 
00025 static inline void printf_endian(Endian endian)
00026 {
00027   printf("#define TTD_ENDIAN %s\n", endian == ENDIAN_LITTLE ? "TTD_LITTLE_ENDIAN" : "TTD_BIG_ENDIAN");
00028 }
00029 
00036 int main (int argc, char *argv[])
00037 {
00038   unsigned char endian_test[2] = { 1, 0 };
00039   int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
00040 
00041   if (argc > 1 && strcmp(argv[1], "BE") == 0) force_BE = 1;
00042   if (argc > 1 && strcmp(argv[1], "LE") == 0) force_LE = 1;
00043   if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
00044 
00045   printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
00046 
00047   if (force_LE == 1) {
00048     printf_endian(ENDIAN_LITTLE);
00049   } else if (force_BE == 1) {
00050     printf_endian(ENDIAN_BIG);
00051   } else if (force_PREPROCESSOR == 1) {
00052     /* Support for universal binaries on OSX
00053      * Universal binaries supports both PPC and x86
00054      * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
00055      */
00056     printf("#ifdef __BIG_ENDIAN__\n");
00057     printf_endian(ENDIAN_BIG);
00058     printf("#else\n");
00059     printf_endian(ENDIAN_LITTLE);
00060     printf("#endif\n");
00061   } else if (*(short*)endian_test == 1 ) {
00062     printf_endian(ENDIAN_LITTLE);
00063   } else {
00064     printf_endian(ENDIAN_BIG);
00065   }
00066   printf("#endif\n");
00067 
00068   return 0;
00069 }

Generated on Wed Apr 1 14:38:06 2009 for OpenTTD by  doxygen 1.5.6