casacore
IBMConversion.h
Go to the documentation of this file.
1 //# IBMConversion.h: A class with static functions to convert IBM 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: aips2-request@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 //# $Id$
27 
28 #ifndef CASA_IBMCONVERSION_H
29 #define CASA_IBMCONVERSION_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/OS/CanonicalConversion.h>
34 
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 // Define the IBM sizes of the built-in data types.
39 
40 #define SIZE_IBM_CHAR 1
41 #define SIZE_IBM_UCHAR 1
42 #define SIZE_IBM_SHORT 2
43 #define SIZE_IBM_USHORT 2
44 #define SIZE_IBM_INT 4
45 #define SIZE_IBM_UINT 4
46 #define SIZE_IBM_INT64 4
47 #define SIZE_IBM_UINT64 4
48 #define SIZE_IBM_FLOAT 4
49 #define SIZE_IBM_DOUBLE 8
50 
51 
52 // <summary>
53 // A class with static functions to convert IBM format
54 // </summary>
55 
56 // <use visibility=export>
57 
58 // <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tIBMConversion" demos="">
59 // </reviewed>
60 
61 // <synopsis>
62 // This class contains static toLocal functions to convert data from IBM-360
63 // format to local format and vice-versa. It also handles the conversion
64 // of the IBM EBCDIC characters to ASCII characters (for data type char).
65 // <p>
66 // The functions work well on big-endian as well as little-endian machines.
67 // </synopsis>
68 
69 // <motivation>
70 // Archived WSRT data can be stored in the old IBM format
71 // (EBCDIC characters and floats with base 16).
72 // Conversion functions are needed to read these data.
73 // </motivation>
74 
75 // <todo asof="$DATE$">
76 // <li> Support data type long double.
77 // </todo>
78 
79 
81 {
82 public:
83  // Convert one value from IBM format to local format.
84  // The from and to buffer should not overlap.
85  // <note>
86  // The char version converts from EBCDIC to ASCII, while the
87  // unsigned char version is a simple copy.
88  // </note>
89  // <group>
90  static void toLocal (char& to, const void* from);
91  static void toLocal (unsigned char& to, const void* from);
92  static void toLocal (short& to, const void* from);
93  static void toLocal (unsigned short& to, const void* from);
94  static void toLocal (int& to, const void* from);
95  static void toLocal (unsigned int& to, const void* from);
96  static void toLocal (Int64& to, const void* from);
97  static void toLocal (uInt64& to, const void* from);
98  static void toLocal (float& to, const void* from);
99  static void toLocal (double& to, const void* from);
100  // </group>
101 
102  // Convert nr values from IBM format to local format.
103  // The from and to buffer should not overlap.
104  // <note>
105  // The char version converts from EBCDIC to ASCII, while the
106  // unsigned char version is a simple copy.
107  // </note>
108  // <group>
109  static void toLocal (char* to, const void* from,
110  size_t nr);
111  static void toLocal (unsigned char* to, const void* from,
112  size_t nr);
113  static void toLocal (short* to, const void* from,
114  size_t nr);
115  static void toLocal (unsigned short* to, const void* from,
116  size_t nr);
117  static void toLocal (int* to, const void* from,
118  size_t nr);
119  static void toLocal (unsigned int* to, const void* from,
120  size_t nr);
121  static void toLocal (Int64* to, const void* from,
122  size_t nr);
123  static void toLocal (uInt64* to, const void* from,
124  size_t nr);
125  static void toLocal (float* to, const void* from,
126  size_t nr);
127  static void toLocal (double* to, const void* from,
128  size_t nr);
129  // </group>
130 
131  // Convert one value from local format to IBM format.
132  // The from and to buffer should not overlap.
133  // <note>
134  // The char version converts from ASCII to EBCDIC, while the
135  // unsigned char version is a simple copy.
136  // </note>
137  // <group>
138  static void fromLocal (void* to, char from);
139  static void fromLocal (void* to, unsigned char from);
140  static void fromLocal (void* to, short from);
141  static void fromLocal (void* to, unsigned short from);
142  static void fromLocal (void* to, int from);
143  static void fromLocal (void* to, unsigned int from);
144  static void fromLocal (void* to, Int64 from);
145  static void fromLocal (void* to, uInt64 from);
146  static void fromLocal (void* to, float from);
147  static void fromLocal (void* to, double from);
148  // </group>
149 
150  // Convert nr values from local format to IBM format.
151  // The from and to buffer should not overlap.
152  // <note>
153  // The char version converts from ASCII to EBCDIC, while the
154  // unsigned char version is a simple copy.
155  // </note>
156  // <group>
157  static void fromLocal (void* to, const char* from,
158  size_t nr);
159  static void fromLocal (void* to, const unsigned char* from,
160  size_t nr);
161  static void fromLocal (void* to, const short* from,
162  size_t nr);
163  static void fromLocal (void* to, const unsigned short* from,
164  size_t nr);
165  static void fromLocal (void* to, const int* from,
166  size_t nr);
167  static void fromLocal (void* to, const unsigned int* from,
168  size_t nr);
169  static void fromLocal (void* to, const Int64* from,
170  size_t nr);
171  static void fromLocal (void* to, const uInt64* from,
172  size_t nr);
173  static void fromLocal (void* to, const float* from,
174  size_t nr);
175  static void fromLocal (void* to, const double* from,
176  size_t nr);
177  // </group>
178 
179 private:
180  // This class should not be constructed
181  // (so declare the constructor private).
183 };
184 
185 
186 
187 inline void IBMConversion::toLocal (unsigned char& to, const void* from)
188 {
189  CanonicalConversion::toLocal (to, from);
190 }
191 
192 inline void IBMConversion::toLocal (short& to, const void* from)
193 {
194  CanonicalConversion::toLocal (to, from);
195 }
196 
197 inline void IBMConversion::toLocal (unsigned short& to, const void* from)
198 {
199  CanonicalConversion::toLocal (to, from);
200 }
201 
202 inline void IBMConversion::toLocal (int& to, const void* from)
203 {
204  CanonicalConversion::toLocal (to, from);
205 }
206 
207 inline void IBMConversion::toLocal (unsigned int& to, const void* from)
208 {
209  CanonicalConversion::toLocal (to, from);
210 }
211 
212 inline void IBMConversion::toLocal (Int64& to, const void* from)
213 {
214  if (sizeof(Int64) != 4) {
215  if (((signed char*)from)[0] < 0) {
216  to = -1;
217  }else{
218  to = 0;
219  }
220  }
221 #if defined(AIPS_LITTLE_ENDIAN)
222  CanonicalConversion::reverse4 (&to, from);
223 #else
224  CanonicalConversion::move4 (((char*)&to)+sizeof(Int64)-4, from);
225 #endif
226 }
227 
228 inline void IBMConversion::toLocal (uInt64& to, const void* from)
229 {
230  if (sizeof(uInt64) != 4) {
231  to = 0;
232  }
233 #if defined(AIPS_LITTLE_ENDIAN)
234  CanonicalConversion::reverse4 (&to, from);
235 #else
236  CanonicalConversion::move4 (((char*)&to)+sizeof(uInt64)-4, from);
237 #endif
238 }
239 
240 inline void IBMConversion::toLocal (float& to, const void* from)
241 {
242  toLocal (&to, from, 1);
243 }
244 
245 inline void IBMConversion::toLocal (double& to, const void* from)
246 {
247  toLocal (&to, from, 1);
248 }
249 
250 inline void IBMConversion::toLocal (unsigned char* to, const void* from,
251  size_t nr)
252 {
253  CanonicalConversion::toLocal (to, from, nr);
254 }
255 
256 inline void IBMConversion::toLocal (short* to, const void* from,
257  size_t nr)
258 {
259  CanonicalConversion::toLocal (to, from, nr);
260 }
261 
262 inline void IBMConversion::toLocal (unsigned short* to, const void* from,
263  size_t nr)
264 {
265  CanonicalConversion::toLocal (to, from, nr);
266 }
267 
268 inline void IBMConversion::toLocal (int* to, const void* from,
269  size_t nr)
270 {
271  CanonicalConversion::toLocal (to, from, nr);
272 }
273 
274 inline void IBMConversion::toLocal (unsigned int* to, const void* from,
275  size_t nr)
276 {
277  CanonicalConversion::toLocal (to, from, nr);
278 }
279 
280 
281 inline void IBMConversion::fromLocal (void* to, unsigned char from)
282 {
284 }
285 
286 inline void IBMConversion::fromLocal (void* to, short from)
287 {
289 }
290 
291 inline void IBMConversion::fromLocal (void* to, unsigned short from)
292 {
294 }
295 
296 inline void IBMConversion::fromLocal (void* to, int from)
297 {
299 }
300 
301 inline void IBMConversion::fromLocal (void* to, unsigned int from)
302 {
304 }
305 
306 inline void IBMConversion::fromLocal (void* to, Int64 from)
307 {
308 #if defined(AIPS_LITTLE_ENDIAN)
309  CanonicalConversion::reverse4 (to, &from);
310 #else
311  CanonicalConversion::move4 (to, ((char*)&from)+sizeof(Int64)-4);
312 #endif
313 }
314 
315 inline void IBMConversion::fromLocal (void* to, uInt64 from)
316 {
317 #if defined(AIPS_LITTLE_ENDIAN)
318  CanonicalConversion::reverse4 (to, &from);
319 #else
320  CanonicalConversion::move4 (to,((char*)&from)+sizeof(uInt64)-4);
321 #endif
322 }
323 
324 inline void IBMConversion::fromLocal (void* to, float from)
325 {
326  fromLocal (to, &from, 1);
327 }
328 
329 inline void IBMConversion::fromLocal (void* to, double from)
330 {
331  fromLocal (to, &from, 1);
332 }
333 
334 
335 inline void IBMConversion::fromLocal (void* to, const unsigned char* from,
336  size_t nr)
337 {
338  CanonicalConversion::fromLocal (to, from, nr);
339 }
340 
341 inline void IBMConversion::fromLocal (void* to, const short* from,
342  size_t nr)
343 {
344  CanonicalConversion::fromLocal (to, from, nr);
345 }
346 
347 inline void IBMConversion::fromLocal (void* to, const unsigned short* from,
348  size_t nr)
349 {
350  CanonicalConversion::fromLocal (to, from, nr);
351 }
352 
353 inline void IBMConversion::fromLocal (void* to, const int* from,
354  size_t nr)
355 {
356  CanonicalConversion::fromLocal (to, from, nr);
357 }
358 
359 inline void IBMConversion::fromLocal (void* to, const unsigned int* from,
360  size_t nr)
361 {
362  CanonicalConversion::fromLocal (to, from, nr);
363 }
364 
365 
366 
367 
368 } //# NAMESPACE CASACORE - END
369 
370 #endif
static size_t fromLocal(void *to, const char &from)
Convert one value from local format to canonical format.
static void reverse4(void *to, const void *from)
Reverse 4 bytes.
static size_t toLocal(char &to, const void *from)
Convert one value from canonical format to local format.
static void move4(void *to, const void *from)
Move 4 bytes.
IBMConversion()
This class should not be constructed (so declare the constructor private).
static void fromLocal(void *to, char from)
Convert one value from local format to IBM format.
static void fromLocal(void *to, const float *from, size_t nr)
static void toLocal(double *to, const void *from, size_t nr)
static void fromLocal(void *to, const double *from, size_t nr)
static void fromLocal(void *to, const char *from, size_t nr)
Convert nr values from local format to IBM format.
static void fromLocal(void *to, const Int64 *from, size_t nr)
static void toLocal(Int64 *to, const void *from, size_t nr)
static void toLocal(char &to, const void *from)
Convert one value from IBM format to local format.
static void fromLocal(void *to, const uInt64 *from, size_t nr)
static void toLocal(char *to, const void *from, size_t nr)
Convert nr values from IBM format to local format.
static void toLocal(float *to, const void *from, size_t nr)
static void toLocal(uInt64 *to, const void *from, size_t nr)
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:38
unsigned long long uInt64
Definition: aipsxtype.h:39