src/endian_check.cpp
changeset 7321 f91bdca345e8
parent 6432 226650eb2ef3
child 9543 a60fb4bacc66
equal deleted inserted replaced
7320:96d5862ec06a 7321:f91bdca345e8
    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 
    14 
    15 /** Main call of the endian_check program
    15 /**
       
    16  * Main call of the endian_check program
    16  * @param argc argument count
    17  * @param argc argument count
    17  * @param argv arguments themselves
    18  * @param argv arguments themselves
    18  * @return exit code */
    19  * @return exit code
    19 int main (int argc, char *argv[]) {
    20  */
    20 	unsigned char EndianTest[2] = { 1, 0 };
    21 int main (int argc, char *argv[])
       
    22 {
       
    23 	unsigned char endian_test[2] = { 1, 0 };
    21 	int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
    24 	int force_BE = 0, force_LE = 0, force_PREPROCESSOR = 0;
    22 
    25 
    23 	if (argc > 1 && strcmp(argv[1], "BE") == 0)
    26 	if (argc > 1 && strcmp(argv[1], "BE") == 0) force_BE = 1;
    24 		force_BE = 1;
    27 	if (argc > 1 && strcmp(argv[1], "LE") == 0) force_LE = 1;
    25 	if (argc > 1 && strcmp(argv[1], "LE") == 0)
    28 	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0) force_PREPROCESSOR = 1;
    26 		force_LE = 1;
       
    27 	if (argc > 1 && strcmp(argv[1], "PREPROCESSOR") == 0)
       
    28 		force_PREPROCESSOR = 1;
       
    29 
    29 
    30 	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
    30 	printf("#ifndef ENDIAN_H\n#define ENDIAN_H\n");
    31 
    31 
    32 	if (force_LE == 1) {
    32 	if (force_LE == 1) {
    33 		printf("#define TTD_LITTLE_ENDIAN\n");
    33 		printf("#define TTD_LITTLE_ENDIAN\n");
       
    34 	} else if (force_BE == 1) {
       
    35 		printf("#define TTD_BIG_ENDIAN\n");
       
    36 	} else if (force_PREPROCESSOR == 1) {
       
    37 		/* Support for universal binaries on OSX
       
    38 		 * 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
       
    40 		 */
       
    41 		printf("#ifdef __BIG_ENDIAN__\n");
       
    42 		printf("#define TTD_BIG_ENDIAN\n");
       
    43 		printf("#else\n");
       
    44 		printf("#define TTD_LITTLE_ENDIAN\n");
       
    45 		printf("#endif\n");
       
    46 	} else if (*(short*)endian_test == 1 ) {
       
    47 		printf("#define TTD_LITTLE_ENDIAN\n");
    34 	} else {
    48 	} else {
    35 		if (force_BE == 1) {
    49 		printf("#define TTD_BIG_ENDIAN\n");
    36 			printf("#define TTD_BIG_ENDIAN\n");
       
    37 		} else {
       
    38 			if (force_PREPROCESSOR == 1) {
       
    39 				/** adding support for universal binaries on OSX
       
    40 				 * Universal binaries supports both PPC and x86
       
    41 				 * If a compiler for OSX gets this setting, it will always pick the correct endian and no test is needed */
       
    42 				printf("#ifdef __BIG_ENDIAN__\n");
       
    43 				printf("#define TTD_BIG_ENDIAN\n");
       
    44 				printf("#else\n");
       
    45 				printf("#define TTD_LITTLE_ENDIAN\n");
       
    46 				printf("#endif\n");
       
    47 			} else {
       
    48 				if ( *(short *) EndianTest == 1 )
       
    49 					printf("#define TTD_LITTLE_ENDIAN\n");
       
    50 				else
       
    51 					printf("#define TTD_BIG_ENDIAN\n");
       
    52 			}
       
    53 		}
       
    54 	}
    50 	}
    55 	printf("#endif\n");
    51 	printf("#endif\n");
    56 
    52 
    57 	return 0;
    53 	return 0;
    58 }
    54 }