/**************************************************************** * * Set of utility routines fir the level 2p processing * * byteSwapInt : Routine to do the byte swapping on an int variable * byteSwapShort : Routine to do the byte swapping on a short variable * byteSwapFloat : Routine to do the byte swapping on a float variable * printError : Program wide error reporting including time of error * * * Version History: * * Origjnal version * 20 March 2006 - Author : Jonathan Mittaz * * 17 April 2006 - Tidy up of code : Jonathan Mittaz * */ /* Include files */ #include #include #include #include #include #include /* Union structures to enable the correct byte swapping of idfferent * variable types */ union char_to_int { unsigned char charval[4]; int intval; }; union char_to_long { unsigned char charval[4]; long intval; }; union char_to_short { unsigned char charval[2]; short intval; }; union char_to_float { unsigned char charval[4]; float floatval; }; /* Routines to do byte swapping */ /* Byte swap an int variable */ int byteSwapInt( int inValue ) { union char_to_int intUnion; union char_to_int intUnion2; #ifdef BIG_ENDIAN_MACHINE return inValue; #else intUnion.intval = inValue; intUnion2.charval[0] = intUnion.charval[3]; intUnion2.charval[1] = intUnion.charval[2]; intUnion2.charval[2] = intUnion.charval[1]; intUnion2.charval[3] = intUnion.charval[0]; return intUnion2.intval; #endif } /* Byte swap an long variable */ long byteSwapLong( long inValue ) { union char_to_long intUnion; union char_to_long intUnion2; #ifdef BIG_ENDIAN_MACHINE return inValue; #else intUnion.intval = inValue; intUnion2.charval[0] = intUnion.charval[3]; intUnion2.charval[1] = intUnion.charval[2]; intUnion2.charval[2] = intUnion.charval[1]; intUnion2.charval[3] = intUnion.charval[0]; return intUnion2.intval; #endif } /* Byte swap a short variable */ short byteSwapShort( short inValue ) { union char_to_short intUnion; union char_to_short intUnion2; #ifdef BIG_ENDIAN_MACHINE return inValue; #else intUnion.intval = inValue; intUnion2.charval[0] = intUnion.charval[1]; intUnion2.charval[1] = intUnion.charval[0]; return intUnion2.intval; #endif } /* Byte swap a float variable */ float byteSwapFlt( float inValue ) { union char_to_float fltUnion; union char_to_float fltUnion2; #ifdef BIG_ENDIAN_MACHINE return inValue; #else fltUnion.floatval = inValue; fltUnion2.charval[0] = fltUnion.charval[3]; fltUnion2.charval[1] = fltUnion.charval[2]; fltUnion2.charval[2] = fltUnion.charval[1]; fltUnion2.charval[3] = fltUnion.charval[0]; return fltUnion2.floatval; #endif } /* Generic routine for error reporting - outputs an error string of the * form : * * L2P : ERROR : YYYY-MM-DD:hh:ss : Some error string * * where YYYY-MM-DD:hh:ss is the time at which the error is trapped * */ /* * Arguments: * * pStr1 : Program identifuer (in this case this will be L2P * pStr2 : Description of error trapped * pFloat : Input floating point variable to be included in the * description string * pInString : Input string variable to be included in the description * string */ #define MAX_ERROR_STRING 1000 void printError( char *pStr1, char *pStr2, float_fp_kind *pFloat, char *pInString ) { /* Temporary storage strings for output */ char tempString[MAX_ERROR_STRING]; char errorString[MAX_ERROR_STRING]; char timeString[MAX_ERROR_STRING]; /* Structures needed to get the current time */ struct timeval tv; struct timezone tz; struct tm result; /* Get time */ gettimeofday(&tv,&tz); /* Reformat time to output format */ gmtime_r(&tv.tv_sec,&result); sprintf(timeString,"%4.4d-%2.2d-%2.2d:%2.2d:%2.2d", 1900+result.tm_year,result.tm_mon,result.tm_mday,result.tm_hour, result.tm_min); /* Make output string */ /* Add program stem (in our case L2P) */ strcpy(errorString,pStr1); strcat(errorString," : ERROR : "); /* Add in time string */ strcat(errorString,timeString); strcat(errorString," : "); if( NULL != pFloat ){ /* If a floating point number is output - format will be included in the * pStr2 string */ sprintf(tempString,pStr2,*pFloat); } else if( NULL != pInString ) { /* If a filename or string is output - format will be included in the * pStr2 string */ sprintf(tempString,pStr2,pInString); } else { /* Just copy the input error string */ strcpy(tempString,pStr2); } /* concatenate error description string to error message */ strcat(errorString,tempString); /* Output error string to stderr and exit with code -1 */ fprintf(stderr,"%s\n",errorString); exit(-1); }