[R-gregmisc-users] SF.net SVN: r-gregmisc:[1842] trunk/SASxport/src
Brought to you by:
warnes
From: <wa...@us...> - 2014-07-18 20:41:07
|
Revision: 1842 http://sourceforge.net/p/r-gregmisc/code/1842 Author: warnes Date: 2014-07-18 20:40:57 +0000 (Fri, 18 Jul 2014) Log Message: ----------- - Rename function and file 'reverse' to 'host_to_be' (short for host-endian to big-endian') to clarify purpose of the function. - Remove pre-processeor definition of REVERSE, and add definitions of HTOBE_SHORT, HTOBE_INT, and HTOBE_DOUBLE. Modified Paths: -------------- trunk/SASxport/src/writeSAS.c trunk/SASxport/src/writeSAS.h Added Paths: ----------- trunk/SASxport/src/host_to_be.c Removed Paths: ------------- trunk/SASxport/src/reverse.c Copied: trunk/SASxport/src/host_to_be.c (from rev 1825, trunk/SASxport/src/reverse.c) =================================================================== --- trunk/SASxport/src/host_to_be.c (rev 0) +++ trunk/SASxport/src/host_to_be.c 2014-07-18 20:40:57 UTC (rev 1842) @@ -0,0 +1,75 @@ +#include "writeSAS.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <assert.h> +#include <sys/types.h> + +/* host_to_be: convert current host byte order to big endian */ +void host_to_be( unsigned char *intp, size_t size) +{ + static unsigned char endianTest[2] = {0x01,0x00}; + size_t i; + unsigned char tmp; + + /* Test if we are on a big endian or little endian platform */ + if( (short) *endianTest != 1 ) + { + /* The native byte order is big endian, so do nothing */ + //printf("Big Endian Machine!\n"); + return; + } + + /* If native byte order is little endian, we need to swap bytes */ + for(i=0; i < size/2; i++) + { + tmp = (unsigned char) intp[i]; + intp[i] = intp[size-i-1]; + intp[size-i-1] = tmp; + } + + return; +} + +/* test code */ +void test_host_to_be() +{ + unsigned char byte_pattern[1] = { 0x00 }; + unsigned char byte_value = 0x00; + + unsigned char short_pattern[2] = { 0x01, 0x00 }; /* NB: big endian byte pattern */ + short short_value = 0x0100; /* NB: hex is also written big endian */ + + unsigned char int_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; + int int_value = 0x03020100; + + unsigned char long_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; + long long_value = 0x03020100; + + /* Do the host_to_be, then test */ + + /* byte */ + host_to_be( &byte_value, sizeof(unsigned char) ); + ASSERT( (unsigned char) *byte_pattern == byte_value ); + + /* short */ + host_to_be( (unsigned char*) &short_value, sizeof(short) ); + ASSERT( *((short *) short_pattern) == short_value ); + + /* int */ + host_to_be( (unsigned char*) &int_value, sizeof(int) ); + ASSERT( *((int *) int_pattern) == int_value ); + + /* long */ + host_to_be( (unsigned char*) &long_value, sizeof(long) ); + ASSERT( *((long*) long_pattern) == long_value ); + +} + +#ifdef DO_TEST +int main(int argc, char *argv) +{ + test_host_to_be(); +} +#endif Deleted: trunk/SASxport/src/reverse.c =================================================================== --- trunk/SASxport/src/reverse.c 2014-07-18 18:37:51 UTC (rev 1841) +++ trunk/SASxport/src/reverse.c 2014-07-18 20:40:57 UTC (rev 1842) @@ -1,77 +0,0 @@ -#include "writeSAS.h" - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <assert.h> -#include <sys/types.h> - -/* reverse: convert current byte order to big endian */ -void reverse( unsigned char *intp, size_t size) -{ - static unsigned char endianTest[2] = {0x01,0x00}; - size_t i; - unsigned char tmp; - -#if !defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) - /* Test if we are on a big endian or little endian platform */ - if( (short) *endianTest != 1 ) - { - /* The native byte order is big endian, so do nothing */ - //printf("Big Endian Machine!\n"); - return; - } -#endif - - /* If native byte order is little endian, we need to swap bytes */ - for(i=0; i < size/2; i++) - { - tmp = (unsigned char) intp[i]; - intp[i] = intp[size-i-1]; - intp[size-i-1] = tmp; - } - - return; -} - -/* test code */ -void test_reverse() -{ - unsigned char byte_pattern[1] = { 0x00 }; - unsigned char byte_value = 0x00; - - unsigned char short_pattern[2] = { 0x01, 0x00 }; /* NB: big endian byte pattern */ - short short_value = 0x0100; /* NB: hex is also written big endian */ - - unsigned char int_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; - int int_value = 0x03020100; - - unsigned char long_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; - long long_value = 0x03020100; - - /* Do the reverse, then test */ - - /* byte */ - reverse( &byte_value, sizeof(unsigned char) ); - ASSERT( (unsigned char) *byte_pattern == byte_value ); - - /* short */ - reverse( (unsigned char*) &short_value, sizeof(short) ); - ASSERT( *((short *) short_pattern) == short_value ); - - /* int */ - reverse( (unsigned char*) &int_value, sizeof(int) ); - ASSERT( *((int *) int_pattern) == int_value ); - - /* long */ - reverse( (unsigned char*) &long_value, sizeof(long) ); - ASSERT( *((long*) long_pattern) == long_value ); - -} - -#ifdef DO_TEST -int main(int argc, char *argv) -{ - test_reverse(); -} -#endif Modified: trunk/SASxport/src/writeSAS.c =================================================================== --- trunk/SASxport/src/writeSAS.c 2014-07-18 18:37:51 UTC (rev 1841) +++ trunk/SASxport/src/writeSAS.c 2014-07-18 20:40:57 UTC (rev 1842) @@ -260,22 +260,18 @@ zeroFill(namestr_record.rest, 52); /* remaining fields are irrelevant */ - /* Flip byte order if necessary */ -#define SHORTREV(a) REVERSE( &a, sizeof(short) ) -#define INTREV(a) REVERSE( &a, sizeof(int) ) + HTOBE_SHORT( namestr_record.ntype ); + HTOBE_SHORT( namestr_record.nhfun ); + HTOBE_SHORT( namestr_record.nlng ); + HTOBE_SHORT( namestr_record.nvar0 ); + HTOBE_SHORT( namestr_record.nfl ); + HTOBE_SHORT( namestr_record.nfd ); + HTOBE_SHORT( namestr_record.nfj ); + HTOBE_SHORT( namestr_record.nifl ); + HTOBE_SHORT( namestr_record.nifd ); - SHORTREV( namestr_record.ntype ); - SHORTREV( namestr_record.nhfun ); - SHORTREV( namestr_record.nlng ); - SHORTREV( namestr_record.nvar0 ); - SHORTREV( namestr_record.nfl ); - SHORTREV( namestr_record.nfd ); - SHORTREV( namestr_record.nfj ); - SHORTREV( namestr_record.nifl ); - SHORTREV( namestr_record.nifd ); + HTOBE_INT ( namestr_record.npos ); - INTREV ( namestr_record.npos ); - /* copy filled struct to return area */ memcpy( raw_buffer, &namestr_record, sizeof(namestr_record) ); @@ -309,7 +305,10 @@ { /* convert to IBM floating point */ - reverse( (unsigned char*) value, sizeof(double) ); + /* first convert to big-endian layout */ + HTOBE_DOUBLE( value ); + + /* now convert to ibm flaoting point format */ ieee2ibm( (unsigned char *) raw_buffer, (unsigned char *) value, 1); //cnxptiee( (char *) value, 0, raw_buffer , 1 ); Modified: trunk/SASxport/src/writeSAS.h =================================================================== --- trunk/SASxport/src/writeSAS.h 2014-07-18 18:37:51 UTC (rev 1841) +++ trunk/SASxport/src/writeSAS.h 2014-07-18 20:40:57 UTC (rev 1842) @@ -39,17 +39,6 @@ #define MISSING 0x2e000000 /* Standard SAS missing value: '.' */ /***** - REVERSE macro, used as a wrapper for the reverse() function to avoid - compiling/calling it on big-endian, where it is a NOOP. - *****/ - -#if ( defined(BIG_ENDIAN) && BIG_ENDIAN ) || ( defined(LITTLE_ENDIAN) && !LITTLE_ENDIAN ) -# define REVERSE(a,b) ( a ) -#else -# define REVERSE(a,b) reverse( (unsigned char*) a, (size_t) b ) -#endif - -/***** * Useful macro functions *****/ @@ -60,7 +49,15 @@ #define ASSERT(x) if(!(x)) error("Assertion failed: x") #endif +/* Convert (if necessary) to Big-Endian */ +# define HTOBE_SHORT(a) host_to_be( (unsigned char*) &a, sizeof(short) ) +# define HTOBE_INT(a) host_to_be( (unsigned char*) &a, sizeof(int) ) +# define HTOBE_DOUBLE(a) host_to_be( (unsigned char*) value, sizeof(double) ); +/* Alternative definition using system functions: */ +/* #define HTOBE_SHORT(a) (a) = htons( a ) */ +/* #define HTOBE_INT(a) (a) = htonl( a ) */ + /***** * File Record Structures *****/ @@ -175,7 +172,7 @@ void doTest(); -void reverse( unsigned char *intp, size_t size); +void host_to_be( unsigned char *intp, size_t size); void ieee2ibm(register unsigned char *out, register const unsigned char *in, int count); #endif /* FIELDS_H */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |