~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/io/TypeIO.h

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Modification History
 
3
 *
 
4
 * 2001-February-3              Jason Rohrer
 
5
 * Created.
 
6
 * Fixed parameter names to match convention.
 
7
 *
 
8
 * 2003-January-11              Jason Rohrer
 
9
 * Added missing casts.
 
10
 *
 
11
 * 2004-May-9   Jason Rohrer
 
12
 * Added support for shorts.
 
13
 */
 
14
 
 
15
#include "minorGems/common.h"
 
16
 
 
17
 
 
18
#ifndef TYPE_IO_INCLUDED
 
19
#define TYPE_IO_INCLUDED
 
20
 
 
21
/**
 
22
 * Interfaces for platform-independent type input and output.
 
23
 *
 
24
 * The specification for the input/output format for types is as follows:
 
25
 *
 
26
 * Types should be output in the order and format that a big-endian Linux
 
27
 * outputs them by default.
 
28
 *
 
29
 * Note that minorGems/numtest.cpp can be used to test how doubles are
 
30
 * stored on a specific platform. 
 
31
 *
 
32
 * @author Jason Rohrer
 
33
 */ 
 
34
class TypeIO {
 
35
 
 
36
        public:
 
37
                
 
38
                /**
 
39
                 * Converts an 32-bit integer to a byte array in a 
 
40
                 * platform-independent fashion.
 
41
                 *
 
42
                 * @param inInt the integer to convert to a byte array.
 
43
                 * @param outBytes preallocated array where bytes will be returned.
 
44
                 */
 
45
                static void longToBytes( long inInt, unsigned char *outBytes ); 
 
46
                
 
47
                
 
48
                /**
 
49
                 * Converts a 4-byte array to a 32-bit integer  
 
50
                 * platform-independent fashion.
 
51
                 *
 
52
                 * @param inBytes array of bytes to convert.
 
53
                 *
 
54
                 * @return the integer represented by the bytes.
 
55
                 */
 
56
                static long bytesToLong( unsigned char *inBytes );
 
57
 
 
58
                
 
59
                /**
 
60
                 * Converts an 16-bit integer to a byte array in a 
 
61
                 * platform-independent fashion.
 
62
                 *
 
63
                 * @param inInt the integer to convert to a byte array.
 
64
                 * @param outBytes preallocated array where bytes will be returned.
 
65
                 */
 
66
                static void shortToBytes( short inInt, unsigned char *outBytes ); 
 
67
                
 
68
                
 
69
                /**
 
70
                 * Converts a 2-byte array to a 16-bit integer  
 
71
                 * platform-independent fashion.
 
72
                 *
 
73
                 * @param inBytes array of bytes to convert.
 
74
                 *
 
75
                 * @return the integer represented by the bytes.
 
76
                 */
 
77
                static short bytesToShort( unsigned char *inBytes );
 
78
                
 
79
                
 
80
                /**
 
81
                 * Converts an 64-bit float to a byte array in a 
 
82
                 * platform-independent fashion.
 
83
                 *
 
84
                 * @param inDouble the double to convert to a byte array.
 
85
                 * @param outBytes preallocated array where bytes will be returned.
 
86
                 */
 
87
                static void doubleToBytes( 
 
88
                        double inDouble, unsigned char *outBytes ); 
 
89
                
 
90
                
 
91
                /**
 
92
                 * Converts a 8-byte array to a 64-bit float  
 
93
                 * platform-independent fashion.
 
94
                 *
 
95
                 * @param inBytes array of bytes to convert.
 
96
                 *
 
97
                 * @return the double represented by the bytes.
 
98
                 */
 
99
                static double bytesToDouble( unsigned char *inBytes );
 
100
                
 
101
        };              
 
102
 
 
103
 
 
104
 
 
105
// for now, these long IO functions can be implemented in the same way 
 
106
// on every platform.
 
107
 
 
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 );
 
115
        } 
 
116
 
 
117
 
 
118
 
 
119
inline long TypeIO::bytesToLong( unsigned char *inBytes ) {
 
120
        return (long)( inBytes[0] << 24 | 
 
121
                inBytes[1] << 16 | inBytes[2] << 8 | inBytes[3] );
 
122
        }
 
123
 
 
124
 
 
125
 
 
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 );
 
131
        } 
 
132
 
 
133
 
 
134
 
 
135
inline short TypeIO::bytesToShort( unsigned char *inBytes ) {
 
136
        return (short)( inBytes[0] << 8 | inBytes[1] );
 
137
        }
 
138
 
 
139
 
 
140
        
 
141
#endif