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
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36// <summary>
37// A class with static functions to convert littleEndian format
38// </summary>
39
40// <use visibility=export>
41
42// <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tVAXConversion" demos="">
43// </reviewed>
44
45// <synopsis>
46// This class is intended to be used as a common class for all
47// classes converting data to/from little-endian format.
48// </synopsis>
49
50// <motivation>
51// Sometimes data are stored in little-endian format (e.g. old VAX-data).
52// Instead of putting all these conversion functions in all such classes,
53// it is better to keep them separate to be able to use them elsewhere.
54// However, note that this version handles a long as 4 bytes.
55// On several little-endian machines (e.g. DEC-alpha) a long is 8 bytes,
56// so a special function is needed for them.
57// </motivation>
58
59// <todo asof="$DATE$">
60// <li> Support data type long double.
61// <li> Support a long as 4 or as 8 bytes.
62// </todo>
63
64
66{
67public:
68 // Convert one value from littleEndian format to local format.
69 // The from and to buffer should not overlap.
70 // <group>
71 static void toLocal (char& to, const void* from);
72 static void toLocal (unsigned char& to, const void* from);
73 static void toLocal (short& to, const void* from);
74 static void toLocal (unsigned short& to, const void* from);
75 static void toLocal (int& to, const void* from);
76 static void toLocal (unsigned int& to, const void* from);
77 static void toLocal (Int64& to, const void* from);
78 static void toLocal (uInt64& to, const void* from);
79 static void toLocal (float& to, const void* from);
80 static void toLocal (double& to, const void* from);
81 // </group>
82
83 // Convert nr values from littleEndian format to local format.
84 // The from and to buffer should not overlap.
85 // <group>
86 static void toLocal (char* to, const void* from,
87 size_t nr);
88 static void toLocal (unsigned char* to, const void* from,
89 size_t nr);
90 static void toLocal (short* to, const void* from,
91 size_t nr);
92 static void toLocal (unsigned short* to, const void* from,
93 size_t nr);
94 static void toLocal (int* to, const void* from,
95 size_t nr);
96 static void toLocal (unsigned int* to, const void* from,
97 size_t nr);
98 static void toLocal (Int64* to, const void* from,
99 size_t nr);
100 static void toLocal (uInt64* to, const void* from,
101 size_t nr);
102 static void toLocal (float* to, const void* from,
103 size_t nr);
104 static void toLocal (double* to, const void* from,
105 size_t nr);
106 // </group>
107
108 // Convert one value from local format to littleEndian format.
109 // The from and to buffer should not overlap.
110 // <group>
111 static void fromLocal (void* to, char from);
112 static void fromLocal (void* to, unsigned char from);
113 static void fromLocal (void* to, short from);
114 static void fromLocal (void* to, unsigned short from);
115 static void fromLocal (void* to, int from);
116 static void fromLocal (void* to, unsigned int from);
117 static void fromLocal (void* to, Int64 from);
118 static void fromLocal (void* to, uInt64 from);
119 static void fromLocal (void* to, float from);
120 static void fromLocal (void* to, double from);
121 // </group>
122
123 // Convert nr values from local format to littleEndian format.
124 // The from and to buffer should not overlap.
125 // <group>
126 static void fromLocal (void* to, const char* from,
127 size_t nr);
128 static void fromLocal (void* to, const unsigned char* from,
129 size_t nr);
130 static void fromLocal (void* to, const short* from,
131 size_t nr);
132 static void fromLocal (void* to, const unsigned short* from,
133 size_t nr);
134 static void fromLocal (void* to, const int* from,
135 size_t nr);
136 static void fromLocal (void* to, const unsigned int* from,
137 size_t nr);
138 static void fromLocal (void* to, const Int64* from,
139 size_t nr);
140 static void fromLocal (void* to, const uInt64* from,
141 size_t nr);
142 static void fromLocal (void* to, const float* from,
143 size_t nr);
144 static void fromLocal (void* to, const double* from,
145 size_t nr);
146 // </group>
147
148private:
149 // This class should not be constructed
150 // (so declare the constructor private).
152};
153
154
155
156inline void LittleEndianConversion::toLocal (char& to, const void* from)
157{
158 to = *(char*)from;
159}
160
161inline void LittleEndianConversion::toLocal (unsigned char& to,
162 const void* from)
163{
164 to = *(unsigned char*)from;
165}
166
167inline void LittleEndianConversion::toLocal (short& to, const void* from)
168{
169 if (sizeof(short) != 2) {
170 if (((signed char*)from)[2] < 0) {
171 to = -1;
172 }else{
173 to = 0;
174 }
175 }
176#if !defined(AIPS_LITTLE_ENDIAN)
177 CanonicalConversion::reverse2 (((char*)&to)+sizeof(short)-2, from);
178#else
179 CanonicalConversion::move2 (&to, from);
180#endif
181}
182
183inline void LittleEndianConversion::toLocal (unsigned short& to,
184 const void* from)
185{
186 if (sizeof(unsigned short) != 2) {
187 to = 0;
188 }
189#if !defined(AIPS_LITTLE_ENDIAN)
190 CanonicalConversion::reverse2 (((char*)&to)+sizeof(unsigned short)-2,from);
191#else
192 CanonicalConversion::move2 (&to, from);
193#endif
194}
195
196inline void LittleEndianConversion::toLocal (int& to, const void* from)
197{
198 if (sizeof(int) != 4) {
199 if (((signed char*)from)[3] < 0) {
200 to = -1;
201 }else{
202 to = 0;
203 }
204 }
205#if !defined(AIPS_LITTLE_ENDIAN)
206 CanonicalConversion::reverse4 (((char*)&to)+sizeof(int)-4, from);
207#else
208 CanonicalConversion::move4 (&to, from);
209#endif
210}
211
212inline void LittleEndianConversion::toLocal (unsigned int& to,
213 const void* from)
214{
215 if (sizeof(unsigned int) != 4) {
216 to = 0;
217 }
218#if !defined(AIPS_LITTLE_ENDIAN)
219 CanonicalConversion::reverse4 (((char*)&to)+sizeof(unsigned int)-4, from);
220#else
221 CanonicalConversion::move4 (&to, from);
222#endif
223}
224
225inline void LittleEndianConversion::toLocal (Int64& to, const void* from)
226{
227 int tmp;
229 to = tmp;
230}
231
233 const void* from)
234{
235 unsigned int tmp;
237 to = tmp;
238}
239
240inline void LittleEndianConversion::toLocal (float& to, const void* from)
241{
242#if !defined(AIPS_LITTLE_ENDIAN)
244#else
245 CanonicalConversion::move4 (&to, from);
246#endif
247}
248
249inline void LittleEndianConversion::toLocal (double& to, const void* from)
250{
251#if !defined(AIPS_LITTLE_ENDIAN)
253#else
254 CanonicalConversion::move8 (&to, from);
255#endif
256}
257
258
259inline void LittleEndianConversion::fromLocal (void* to, char from)
260{
261 *(char*)to = from;
262}
263inline void LittleEndianConversion::fromLocal (void* to, unsigned char from)
264{
265 *(unsigned char*)to = from;
266}
267
268inline void LittleEndianConversion::fromLocal (void* to, short from)
269{
270#if !defined(AIPS_LITTLE_ENDIAN)
271 CanonicalConversion::reverse2 (to, ((char*)&from)+sizeof(short)-2);
272#else
273 CanonicalConversion::move2 (to, &from);
274#endif
275}
276
277inline void LittleEndianConversion::fromLocal (void* to, unsigned short from)
278{
279#if !defined(AIPS_LITTLE_ENDIAN)
280 CanonicalConversion::reverse2 (to,((char*)&from)+sizeof(unsigned short)-2);
281#else
282 CanonicalConversion::move2 (to, &from);
283#endif
284}
285
286inline void LittleEndianConversion::fromLocal (void* to, int from)
287{
288#if !defined(AIPS_LITTLE_ENDIAN)
289 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(int)-4);
290#else
291 CanonicalConversion::move4 (to, &from);
292#endif
293}
294
295inline void LittleEndianConversion::fromLocal (void* to, unsigned int from)
296{
297#if !defined(AIPS_LITTLE_ENDIAN)
298 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(unsigned int)-4);
299#else
300 CanonicalConversion::move4 (to, &from);
301#endif
302}
303
304inline void LittleEndianConversion::fromLocal (void* to, Int64 from)
305{
306#if !defined(AIPS_LITTLE_ENDIAN)
307 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(Int64)-4);
308#else
309 CanonicalConversion::move4 (to, &from);
310#endif
311}
312
313inline void LittleEndianConversion::fromLocal (void* to, uInt64 from)
314{
315#if !defined(AIPS_LITTLE_ENDIAN)
316 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(uInt64)-4);
317#else
318 CanonicalConversion::move4 (to, &from);
319#endif
320}
321
322inline void LittleEndianConversion::fromLocal (void* to, float from)
323{
324#if !defined(AIPS_LITTLE_ENDIAN)
325 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(float)-4);
326#else
327 CanonicalConversion::move4 (to, &from);
328#endif
329}
330
331inline void LittleEndianConversion::fromLocal (void* to, double from)
332{
333#if !defined(AIPS_LITTLE_ENDIAN)
334 CanonicalConversion::reverse8 (to, ((char*)&from)+sizeof(double)-8);
335#else
336 CanonicalConversion::move8 (to, &from);
337#endif
338}
339
340
341
342
343} //# NAMESPACE CASACORE - END
344
345#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.
this file contains all the compiler specific defines
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