[R-gregmisc-users] SF.net SVN: r-gregmisc:[1847] trunk/SASxport/src
Brought to you by:
warnes
From: <wa...@us...> - 2014-07-20 02:01:43
|
Revision: 1847 http://sourceforge.net/p/r-gregmisc/code/1847 Author: warnes Date: 2014-07-20 02:01:34 +0000 (Sun, 20 Jul 2014) Log Message: ----------- Rename 'host_to_be' to 'to_bigend' to be more transparent about purpose Added Paths: ----------- trunk/SASxport/src/to_bigend.c trunk/SASxport/src/to_bigend.h Removed Paths: ------------- trunk/SASxport/src/host_to_be.c Deleted: trunk/SASxport/src/host_to_be.c =================================================================== --- trunk/SASxport/src/host_to_be.c 2014-07-19 03:37:50 UTC (rev 1846) +++ trunk/SASxport/src/host_to_be.c 2014-07-20 02:01:34 UTC (rev 1847) @@ -1,80 +0,0 @@ -#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) -{ - size_t i; - unsigned char tmp; - - short twobytes = 0x0001; - char onebyte = *(char*) &twobytes; - - /* Test if we are on a big endian or little endian platform */ - if (onebyte == 1) - { - /* Native byte order is little endian, so swap bytes */ - /* printf("Little Endian Machine!\n"); */ - - for(i=0; i < size/2; i++) - { - tmp = (unsigned char) intp[i]; - intp[i] = intp[size-i-1]; - intp[size-i-1] = tmp; - } - } - else - { - /* The native byte order is big endian, so do nothing */ - /* printf("Big Endian Machine!\n"); */ - } - - 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 Copied: trunk/SASxport/src/to_bigend.c (from rev 1846, trunk/SASxport/src/host_to_be.c) =================================================================== --- trunk/SASxport/src/to_bigend.c (rev 0) +++ trunk/SASxport/src/to_bigend.c 2014-07-20 02:01:34 UTC (rev 1847) @@ -0,0 +1,80 @@ +#include "writeSAS.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <assert.h> +#include <sys/types.h> + +/* to_bigend: convert current host byte order to big endian */ +void to_bigend( unsigned char *intp, size_t size) +{ + size_t i; + unsigned char tmp; + + short twobytes = 0x0001; + char onebyte = *(char*) &twobytes; + + /* Test if we are on a big endian or little endian platform */ + if (onebyte == 1) + { + /* Native byte order is little endian, so swap bytes */ + /* printf("Little Endian Machine!\n"); */ + + for(i=0; i < size/2; i++) + { + tmp = (unsigned char) intp[i]; + intp[i] = intp[size-i-1]; + intp[size-i-1] = tmp; + } + } + else + { + /* The native byte order is big endian, so do nothing */ + /* printf("Big Endian Machine!\n"); */ + } + + return; +} + +/* test code */ +void test_to_bigend() +{ + 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 to_bigend, then test */ + + /* byte */ + to_bigend( &byte_value, sizeof(unsigned char) ); + ASSERT( (unsigned char) *byte_pattern == byte_value ); + + /* short */ + to_bigend( (unsigned char*) &short_value, sizeof(short) ); + ASSERT( *((short *) short_pattern) == short_value ); + + /* int */ + to_bigend( (unsigned char*) &int_value, sizeof(int) ); + ASSERT( *((int *) int_pattern) == int_value ); + + /* long */ + to_bigend( (unsigned char*) &long_value, sizeof(long) ); + ASSERT( *((long*) long_pattern) == long_value ); + +} + +#ifdef DO_TEST +int main(int argc, char **argv) +{ + test_to_bigend(); +} +#endif Added: trunk/SASxport/src/to_bigend.h =================================================================== --- trunk/SASxport/src/to_bigend.h (rev 0) +++ trunk/SASxport/src/to_bigend.h 2014-07-20 02:01:34 UTC (rev 1847) @@ -0,0 +1,26 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, a copy is available at + * http://www.r-project.org/Licenses/ + */ +#ifndef TO_BIGEND_H +#define TO_BIGEND_H + +void to_bigend( unsigned char *intp, size_t size); + +/* Convert (if necessary) to Big-Endian */ +#define TO_BIGEND_SHORT(a) to_bigend( (unsigned char*) &a, sizeof(short) ) +#define TO_BIGEND_INT(a) to_bigend( (unsigned char*) &a, sizeof(int) ) +#define TO_BIGEND_DOUBLE(a) to_bigend( (unsigned char*) &a, sizeof(double) ) + +#endif /* TO_BIGEND_H */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |