Thread: [R-gregmisc-users] SF.net SVN: r-gregmisc: [1144] trunk/SASxport/src/reverse.c
Brought to you by:
warnes
From: <wa...@us...> - 2007-08-15 06:04:58
|
Revision: 1144 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1144&view=rev Author: warnes Date: 2007-08-14 23:04:57 -0700 (Tue, 14 Aug 2007) Log Message: ----------- Restore reverse.c Added Paths: ----------- trunk/SASxport/src/reverse.c Copied: trunk/SASxport/src/reverse.c (from rev 1136, trunk/SASxport/src/reverse.c) =================================================================== --- trunk/SASxport/src/reverse.c (rev 0) +++ trunk/SASxport/src/reverse.c 2007-08-15 06:04:57 UTC (rev 1144) @@ -0,0 +1,83 @@ +#include "writeSAS.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#import <assert.h> +#import <sys/types.h> + +/* reverse: convert current byte order to little endian */ +void reverse( u_char *intp, size_t size) +{ + static u_char endianTest[2] = {0x01,0x00}; + size_t i; + u_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 ) + { + /* Little Endian */ + /* Do nothing */ + return; + } +#endif + + /* Big Endian */ + /* Swap bytes */ + for(i=0; i < size/2; i++) + { + tmp = (u_char) intp[i]; + intp[i] = intp[size-i-1]; + intp[size-i] = tmp; + } + + return; +} + + +/* test code */ +void test_reverse() +{ + u_char byte_pattern[1] = { 0x00 }; + u_char byte_value = 0x00; + + u_char short_pattern[2] = { 0x00, 0x01 }; /* NB: little endian byte pattern */ + short short_value = 0x0100; /* NB: hex is written big endian */ + + u_char int_pattern[4] = { 0x0, 0x01, 0x02, 0x03 }; + int int_value = 0x03020100; + + u_char long_pattern[4] = { 0x0, 0x01, 0x02, 0x03 }; + long long_value = 0x03020100; + + /* Do the reverse, then test */ + + /* byte */ + REVERSE( &byte_value, sizeof(u_char) ); + assert( (u_char) *byte_pattern == byte_value ); + + /* short */ + REVERSE( &short_value, sizeof(short) ); + assert( *((short *) short_pattern) == short_value ); + + /* int */ + REVERSE( &int_value, sizeof(int) ); + assert( *((int *) int_pattern) == int_value ); + + /* long */ + REVERSE( &long_value, sizeof(long) ); + assert( *((long*) long_pattern) == long_value ); + +} + + +#ifdef DO_TEST +int main(int argc, u_char *argv) +{ + test_reverse(); +} + + + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-08-15 06:06:11
|
Revision: 1145 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1145&view=rev Author: warnes Date: 2007-08-14 23:06:09 -0700 (Tue, 14 Aug 2007) Log Message: ----------- Fix reverse.c because we need to swap everything to match Big-Endian, rather than Little-Endian. Also, dont' call the macro. Modified Paths: -------------- trunk/SASxport/src/reverse.c Modified: trunk/SASxport/src/reverse.c =================================================================== --- trunk/SASxport/src/reverse.c 2007-08-15 06:04:57 UTC (rev 1144) +++ trunk/SASxport/src/reverse.c 2007-08-15 06:06:09 UTC (rev 1145) @@ -1,4 +1,4 @@ -#include "writeSAS.h" +//#include "writeSAS.h" #include <stdio.h> #include <string.h> @@ -6,30 +6,29 @@ #import <assert.h> #import <sys/types.h> -/* reverse: convert current byte order to little endian */ -void reverse( u_char *intp, size_t size) +/* reverse: convert current byte order to big endian */ +void reverse( unsigned char *intp, size_t size) { - static u_char endianTest[2] = {0x01,0x00}; + static unsigned char endianTest[2] = {0x01,0x00}; size_t i; - u_char tmp; + 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 ) + if( (short) *endianTest != 1 ) { - /* Little Endian */ - /* Do nothing */ + /* The native byte order is big endian, so do nothing */ + sprintf("Big Endian Machine!\n"); return; } #endif - /* Big Endian */ - /* Swap bytes */ + /* If native byte order is little endian, we need to swap bytes */ for(i=0; i < size/2; i++) { - tmp = (u_char) intp[i]; + tmp = (unsigned char) intp[i]; intp[i] = intp[size-i-1]; - intp[size-i] = tmp; + intp[size-i-1] = tmp; } return; @@ -39,41 +38,41 @@ /* test code */ void test_reverse() { - u_char byte_pattern[1] = { 0x00 }; - u_char byte_value = 0x00; + unsigned char byte_pattern[1] = { 0x00 }; + unsigned char byte_value = 0x00; - u_char short_pattern[2] = { 0x00, 0x01 }; /* NB: little endian byte pattern */ - short short_value = 0x0100; /* NB: hex is written big endian */ + unsigned char short_pattern[2] = { 0x01, 0x00 }; /* NB: big endian byte pattern */ + short short_value = 0x0100; /* NB: hex is also written big endian */ - u_char int_pattern[4] = { 0x0, 0x01, 0x02, 0x03 }; + unsigned char int_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; int int_value = 0x03020100; - u_char long_pattern[4] = { 0x0, 0x01, 0x02, 0x03 }; + unsigned char long_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; long long_value = 0x03020100; /* Do the reverse, then test */ /* byte */ - REVERSE( &byte_value, sizeof(u_char) ); - assert( (u_char) *byte_pattern == byte_value ); + reverse( &byte_value, sizeof(unsigned char) ); + assert( (unsigned char) *byte_pattern == byte_value ); /* short */ - REVERSE( &short_value, sizeof(short) ); + reverse( (unsigned char*) &short_value, sizeof(short) ); assert( *((short *) short_pattern) == short_value ); /* int */ - REVERSE( &int_value, sizeof(int) ); + reverse( (unsigned char*) &int_value, sizeof(int) ); assert( *((int *) int_pattern) == int_value ); /* long */ - REVERSE( &long_value, sizeof(long) ); + reverse( (unsigned char*) &long_value, sizeof(long) ); assert( *((long*) long_pattern) == long_value ); } #ifdef DO_TEST -int main(int argc, u_char *argv) +int main(int argc, char *argv) { test_reverse(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-08-22 01:19:50
|
Revision: 1158 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1158&view=rev Author: warnes Date: 2007-08-21 18:19:46 -0700 (Tue, 21 Aug 2007) Log Message: ----------- sprintf() was being used where printf() was intended. Modified Paths: -------------- trunk/SASxport/src/reverse.c Modified: trunk/SASxport/src/reverse.c =================================================================== --- trunk/SASxport/src/reverse.c 2007-08-21 18:16:47 UTC (rev 1157) +++ trunk/SASxport/src/reverse.c 2007-08-22 01:19:46 UTC (rev 1158) @@ -18,7 +18,7 @@ if( (short) *endianTest != 1 ) { /* The native byte order is big endian, so do nothing */ - sprintf("Big Endian Machine!\n"); + printf("Big Endian Machine!\n"); return; } #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-10-29 15:34:23
|
Revision: 1198 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1198&view=rev Author: warnes Date: 2007-10-29 08:34:21 -0700 (Mon, 29 Oct 2007) Log Message: ----------- Comment out debugging message in reverse() Modified Paths: -------------- trunk/SASxport/src/reverse.c Modified: trunk/SASxport/src/reverse.c =================================================================== --- trunk/SASxport/src/reverse.c 2007-10-29 15:32:50 UTC (rev 1197) +++ trunk/SASxport/src/reverse.c 2007-10-29 15:34:21 UTC (rev 1198) @@ -18,7 +18,7 @@ if( (short) *endianTest != 1 ) { /* The native byte order is big endian, so do nothing */ - printf("Big Endian Machine!\n"); + //printf("Big Endian Machine!\n"); return; } #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |