casacore
Loading...
Searching...
No Matches
LittleEndianConversion.h
Go to the documentation of this file.
1// # LittleEndianConversion.h: A class with static functions to convert littleEndian format
2// # Copyright (C) 1996,1997,1999,2001
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_LITTLEENDIANCONVERSION_H
27#define CASA_LITTLEENDIANCONVERSION_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/OS/CanonicalConversion.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <summary>
36// A class with static functions to convert littleEndian format
37// </summary>
38
39// <use visibility=export>
40
41// <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tVAXConversion" demos="">
42// </reviewed>
43
44// <synopsis>
45// This class is intended to be used as a common class for all
46// classes converting data to/from little-endian format.
47// </synopsis>
48
49// <motivation>
50// Sometimes data are stored in little-endian format (e.g. old VAX-data).
51// Instead of putting all these conversion functions in all such classes,
52// it is better to keep them separate to be able to use them elsewhere.
53// However, note that this version handles a long as 4 bytes.
54// On several little-endian machines (e.g. DEC-alpha) a long is 8 bytes,
55// so a special function is needed for them.
56// </motivation>
57
58// <todo asof="$DATE$">
59// <li> Support data type long double.
60// <li> Support a long as 4 or as 8 bytes.
61// </todo>
62
64 public:
65 // Convert one value from littleEndian format to local format.
66 // The from and to buffer should not overlap.
67 // <group>
68 static void toLocal(char& to, const void* from);
69 static void toLocal(unsigned char& to, const void* from);
70 static void toLocal(short& to, const void* from);
71 static void toLocal(unsigned short& to, const void* from);
72 static void toLocal(int& to, const void* from);
73 static void toLocal(unsigned int& to, const void* from);
74 static void toLocal(Int64& to, const void* from);
75 static void toLocal(uInt64& to, const void* from);
76 static void toLocal(float& to, const void* from);
77 static void toLocal(double& to, const void* from);
78 // </group>
79
80 // Convert nr values from littleEndian format to local format.
81 // The from and to buffer should not overlap.
82 // <group>
83 static void toLocal(char* to, const void* from, size_t nr);
84 static void toLocal(unsigned char* to, const void* from, size_t nr);
85 static void toLocal(short* to, const void* from, size_t nr);
86 static void toLocal(unsigned short* to, const void* from, size_t nr);
87 static void toLocal(int* to, const void* from, size_t nr);
88 static void toLocal(unsigned int* to, const void* from, size_t nr);
89 static void toLocal(Int64* to, const void* from, size_t nr);
90 static void toLocal(uInt64* to, const void* from, size_t nr);
91 static void toLocal(float* to, const void* from, size_t nr);
92 static void toLocal(double* to, const void* from, size_t nr);
93 // </group>
94
95 // Convert one value from local format to littleEndian format.
96 // The from and to buffer should not overlap.
97 // <group>
98 static void fromLocal(void* to, char from);
99 static void fromLocal(void* to, unsigned char from);
100 static void fromLocal(void* to, short from);
101 static void fromLocal(void* to, unsigned short from);
102 static void fromLocal(void* to, int from);
103 static void fromLocal(void* to, unsigned int from);
104 static void fromLocal(void* to, Int64 from);
105 static void fromLocal(void* to, uInt64 from);
106 static void fromLocal(void* to, float from);
107 static void fromLocal(void* to, double from);
108 // </group>
109
110 // Convert nr values from local format to littleEndian format.
111 // The from and to buffer should not overlap.
112 // <group>
113 static void fromLocal(void* to, const char* from, size_t nr);
114 static void fromLocal(void* to, const unsigned char* from, size_t nr);
115 static void fromLocal(void* to, const short* from, size_t nr);
116 static void fromLocal(void* to, const unsigned short* from, size_t nr);
117 static void fromLocal(void* to, const int* from, size_t nr);
118 static void fromLocal(void* to, const unsigned int* from, size_t nr);
119 static void fromLocal(void* to, const Int64* from, size_t nr);
120 static void fromLocal(void* to, const uInt64* from, size_t nr);
121 static void fromLocal(void* to, const float* from, size_t nr);
122 static void fromLocal(void* to, const double* from, size_t nr);
123 // </group>
124
125 private:
126 // This class should not be constructed
127 // (so declare the constructor private).
129};
130
131inline void LittleEndianConversion::toLocal(char& to, const void* from) { to = *(char*)from; }
132
133inline void LittleEndianConversion::toLocal(unsigned char& to, const void* from) {
134 to = *(unsigned char*)from;
135}
136
137inline void LittleEndianConversion::toLocal(short& to, const void* from) {
138 if (sizeof(short) != 2) {
139 if (((signed char*)from)[2] < 0) {
140 to = -1;
141 } else {
142 to = 0;
143 }
144 }
145#if !defined(AIPS_LITTLE_ENDIAN)
146 CanonicalConversion::reverse2(((char*)&to) + sizeof(short) - 2, from);
147#else
149#endif
150}
151
152inline void LittleEndianConversion::toLocal(unsigned short& to, const void* from) {
153 if (sizeof(unsigned short) != 2) {
154 to = 0;
155 }
156#if !defined(AIPS_LITTLE_ENDIAN)
157 CanonicalConversion::reverse2(((char*)&to) + sizeof(unsigned short) - 2, from);
158#else
160#endif
161}
162
163inline void LittleEndianConversion::toLocal(int& to, const void* from) {
164 if (sizeof(int) != 4) {
165 if (((signed char*)from)[3] < 0) {
166 to = -1;
167 } else {
168 to = 0;
169 }
170 }
171#if !defined(AIPS_LITTLE_ENDIAN)
172 CanonicalConversion::reverse4(((char*)&to) + sizeof(int) - 4, from);
173#else
175#endif
176}
177
178inline void LittleEndianConversion::toLocal(unsigned int& to, const void* from) {
179 if (sizeof(unsigned int) != 4) {
180 to = 0;
181 }
182#if !defined(AIPS_LITTLE_ENDIAN)
183 CanonicalConversion::reverse4(((char*)&to) + sizeof(unsigned int) - 4, from);
184#else
186#endif
187}
188
189inline void LittleEndianConversion::toLocal(Int64& to, const void* from) {
190 int tmp;
192 to = tmp;
193}
194
195inline void LittleEndianConversion::toLocal(uInt64& to, const void* from) {
196 unsigned int tmp;
198 to = tmp;
199}
200
201inline void LittleEndianConversion::toLocal(float& to, const void* from) {
202#if !defined(AIPS_LITTLE_ENDIAN)
204#else
206#endif
207}
208
209inline void LittleEndianConversion::toLocal(double& to, const void* from) {
210#if !defined(AIPS_LITTLE_ENDIAN)
212#else
214#endif
215}
216
217inline void LittleEndianConversion::fromLocal(void* to, char from) { *(char*)to = from; }
218inline void LittleEndianConversion::fromLocal(void* to, unsigned char from) {
219 *(unsigned char*)to = from;
220}
221
222inline void LittleEndianConversion::fromLocal(void* to, short from) {
223#if !defined(AIPS_LITTLE_ENDIAN)
224 CanonicalConversion::reverse2(to, ((char*)&from) + sizeof(short) - 2);
225#else
227#endif
228}
229
230inline void LittleEndianConversion::fromLocal(void* to, unsigned short from) {
231#if !defined(AIPS_LITTLE_ENDIAN)
232 CanonicalConversion::reverse2(to, ((char*)&from) + sizeof(unsigned short) - 2);
233#else
235#endif
236}
237
238inline void LittleEndianConversion::fromLocal(void* to, int from) {
239#if !defined(AIPS_LITTLE_ENDIAN)
240 CanonicalConversion::reverse4(to, ((char*)&from) + sizeof(int) - 4);
241#else
243#endif
244}
245
246inline void LittleEndianConversion::fromLocal(void* to, unsigned int from) {
247#if !defined(AIPS_LITTLE_ENDIAN)
248 CanonicalConversion::reverse4(to, ((char*)&from) + sizeof(unsigned int) - 4);
249#else
251#endif
252}
253
254inline void LittleEndianConversion::fromLocal(void* to, Int64 from) {
255#if !defined(AIPS_LITTLE_ENDIAN)
256 CanonicalConversion::reverse4(to, ((char*)&from) + sizeof(Int64) - 4);
257#else
259#endif
260}
261
262inline void LittleEndianConversion::fromLocal(void* to, uInt64 from) {
263#if !defined(AIPS_LITTLE_ENDIAN)
264 CanonicalConversion::reverse4(to, ((char*)&from) + sizeof(uInt64) - 4);
265#else
267#endif
268}
269
270inline void LittleEndianConversion::fromLocal(void* to, float from) {
271#if !defined(AIPS_LITTLE_ENDIAN)
272 CanonicalConversion::reverse4(to, ((char*)&from) + sizeof(float) - 4);
273#else
275#endif
276}
277
278inline void LittleEndianConversion::fromLocal(void* to, double from) {
279#if !defined(AIPS_LITTLE_ENDIAN)
280 CanonicalConversion::reverse8(to, ((char*)&from) + sizeof(double) - 8);
281#else
283#endif
284}
285
286} // namespace casacore
287
288#endif
static void reverse8(void *to, const void *from)
Reverse 8 bytes.
static void move8(void *to, const void *from)
Move 8 bytes.
static void reverse4(void *to, const void *from)
Reverse 4 bytes.
static void reverse2(void *to, const void *from)
Reverse 2 bytes.
static void move2(void *to, const void *from)
Move 2 bytes.
static void move4(void *to, const void *from)
Move 4 bytes.
static void fromLocal(void *to, const short *from, size_t nr)
static void toLocal(unsigned int *to, const void *from, size_t nr)
static void fromLocal(void *to, const char *from, size_t nr)
Convert nr values from local format to littleEndian format.
static void fromLocal(void *to, const float *from, size_t nr)
static void toLocal(Int64 *to, const void *from, size_t nr)
static void fromLocal(void *to, const Int64 *from, size_t nr)
static void toLocal(unsigned char *to, const void *from, size_t nr)
static void toLocal(int *to, const void *from, size_t nr)
static void toLocal(unsigned short *to, const void *from, size_t nr)
static void toLocal(char *to, const void *from, size_t nr)
Convert nr values from littleEndian format to local format.
static void fromLocal(void *to, const double *from, size_t nr)
static void toLocal(uInt64 *to, const void *from, size_t nr)
static void fromLocal(void *to, const unsigned short *from, size_t nr)
static void fromLocal(void *to, const unsigned char *from, size_t nr)
static void toLocal(char &to, const void *from)
Convert one value from littleEndian format to local format.
static void toLocal(float *to, const void *from, size_t nr)
static void toLocal(double *to, const void *from, size_t nr)
static void fromLocal(void *to, const unsigned int *from, size_t nr)
LittleEndianConversion()
This class should not be constructed (so declare the constructor private).
static void fromLocal(void *to, const uInt64 *from, size_t nr)
static void toLocal(short *to, const void *from, size_t nr)
static void fromLocal(void *to, const int *from, size_t nr)
static void fromLocal(void *to, char from)
Convert one value from local format to littleEndian format.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
unsigned long long uInt64
Definition aipsxtype.h:37