4
* 2001-February-3 Jason Rohrer
6
* Fixed parameter names to match convention.
8
* 2003-January-11 Jason Rohrer
11
* 2004-May-9 Jason Rohrer
12
* Added support for shorts.
15
#include "minorGems/common.h"
18
#ifndef TYPE_IO_INCLUDED
19
#define TYPE_IO_INCLUDED
22
* Interfaces for platform-independent type input and output.
24
* The specification for the input/output format for types is as follows:
26
* Types should be output in the order and format that a big-endian Linux
27
* outputs them by default.
29
* Note that minorGems/numtest.cpp can be used to test how doubles are
30
* stored on a specific platform.
32
* @author Jason Rohrer
39
* Converts an 32-bit integer to a byte array in a
40
* platform-independent fashion.
42
* @param inInt the integer to convert to a byte array.
43
* @param outBytes preallocated array where bytes will be returned.
45
static void longToBytes( long inInt, unsigned char *outBytes );
49
* Converts a 4-byte array to a 32-bit integer
50
* platform-independent fashion.
52
* @param inBytes array of bytes to convert.
54
* @return the integer represented by the bytes.
56
static long bytesToLong( unsigned char *inBytes );
60
* Converts an 16-bit integer to a byte array in a
61
* platform-independent fashion.
63
* @param inInt the integer to convert to a byte array.
64
* @param outBytes preallocated array where bytes will be returned.
66
static void shortToBytes( short inInt, unsigned char *outBytes );
70
* Converts a 2-byte array to a 16-bit integer
71
* platform-independent fashion.
73
* @param inBytes array of bytes to convert.
75
* @return the integer represented by the bytes.
77
static short bytesToShort( unsigned char *inBytes );
81
* Converts an 64-bit float to a byte array in a
82
* platform-independent fashion.
84
* @param inDouble the double to convert to a byte array.
85
* @param outBytes preallocated array where bytes will be returned.
87
static void doubleToBytes(
88
double inDouble, unsigned char *outBytes );
92
* Converts a 8-byte array to a 64-bit float
93
* platform-independent fashion.
95
* @param inBytes array of bytes to convert.
97
* @return the double represented by the bytes.
99
static double bytesToDouble( unsigned char *inBytes );
105
// for now, these long IO functions can be implemented in the same way
106
// on every platform.
108
inline void TypeIO::longToBytes( long inInt,
109
unsigned char *outBytes ) {
110
// use a big-endian conversion
111
outBytes[0] = (unsigned char)( inInt >> 24 & 0xFF );
112
outBytes[1] = (unsigned char)( inInt >> 16 & 0xFF );
113
outBytes[2] = (unsigned char)( inInt >> 8 & 0xFF );
114
outBytes[3] = (unsigned char)( inInt & 0xFF );
119
inline long TypeIO::bytesToLong( unsigned char *inBytes ) {
120
return (long)( inBytes[0] << 24 |
121
inBytes[1] << 16 | inBytes[2] << 8 | inBytes[3] );
126
inline void TypeIO::shortToBytes( short inInt,
127
unsigned char *outBytes ) {
128
// use a big-endian conversion
129
outBytes[0] = (unsigned char)( inInt >> 8 & 0xFF );
130
outBytes[1] = (unsigned char)( inInt & 0xFF );
135
inline short TypeIO::bytesToShort( unsigned char *inBytes ) {
136
return (short)( inBytes[0] << 8 | inBytes[1] );