src/endian_check.cpp
changeset 10998 04f58fa3dbb0
parent 7817 f24498d934ac
child 11015 cf4b53f23fde
equal deleted inserted replaced
10997:968df7476121 10998:04f58fa3dbb0
     9  *  that says or TTD_LITTLE_ENDIAN, or TTD_BIG_ENDIAN. Makefile takes
     9  *  that says or TTD_LITTLE_ENDIAN, or TTD_BIG_ENDIAN. Makefile takes
    10  *  care of the real writing to the file. */
    10  *  care of the real writing to the file. */
    11 
    11 
    12 #include <stdio.h>
    12 #include <stdio.h>
    13 #include <string.h>
    13 #include <string.h>
       
    14 
       
    15 /** Supported endian types */
       
    16 enum Endian {
       
    17 	ENDIAN_LITTLE, ///< little endian
       
    18 	ENDIAN_BIG     ///< big endian
       
    19 };
       
    20 
       
    21 /**
       
    22  * Shortcut to printf("#define TTD_*_ENDIAN 0/1")
       
    23  * @param endian endian type to define
       
    24  */
       
    25 static inline void printf_endian(Endian endian)
       
    26 {
       
    27 	printf("#define TTD_ENDIAN %s\n", endian == ENDIAN_LITTLE ? "TTD_LITTLE_ENDIAN" : "TTD_BIG_ENDIAN");
       
    28 }
    14 
    29 
    15 /**
    30 /**
    16  * Main call of the endian_check program
    31  * Main call of the endian_check program
    17  * @param argc argument count
    32  * @param argc argument count
    18  * @param argv arguments themselves
    33  * @param argv arguments themselves
    28 	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
    43 	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
    29 
    44 
    30 	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
    45 	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
    31 
    46 
    32 	if (force_LE == 1) {
    47 	if (force_LE == 1) {
    33 		printf("#define TTD_LITTLE_ENDIAN\n");
    48 		printf_endian(ENDIAN_LITTLE);
    34 	} else if (force_BE == 1) {
    49 	} else if (force_BE == 1) {
    35 		printf("#define TTD_BIG_ENDIAN\n");
    50 		printf_endian(ENDIAN_BIG);
    36 	} else if (force_PREPROCESSOR == 1) {
    51 	} else if (force_PREPROCESSOR == 1) {
    37 		/* Support for universal binaries on OSX
    52 		/* Support for universal binaries on OSX
    38 		 * Universal binaries supports both PPC and x86
    53 		 * Universal binaries supports both PPC and x86
    39 		 * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
    54 		 * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed
    40 		 */
    55 		 */
    41 		printf("#ifdef __BIG_ENDIAN__\n");
    56 		printf("#ifdef __BIG_ENDIAN__\n");
    42 		printf("#define TTD_BIG_ENDIAN\n");
    57 		printf_endian(ENDIAN_BIG);
    43 		printf("#else\n");
    58 		printf("#else\n");
    44 		printf("#define TTD_LITTLE_ENDIAN\n");
    59 		printf_endian(ENDIAN_LITTLE);
    45 		printf("#endif\n");
    60 		printf("#endif\n");
    46 	} else if (*(short*)endian_test == 1 ) {
    61 	} else if (*(short*)endian_test == 1 ) {
    47 		printf("#define TTD_LITTLE_ENDIAN\n");
    62 		printf_endian(ENDIAN_LITTLE);
    48 	} else {
    63 	} else {
    49 		printf("#define TTD_BIG_ENDIAN\n");
    64 		printf_endian(ENDIAN_BIG);
    50 	}
    65 	}
    51 	printf("#endif\n");
    66 	printf("#endif\n");
    52 
    67 
    53 	return 0;
    68 	return 0;
    54 }
    69 }