casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
IBMConversion.h
Go to the documentation of this file.
00001 //# IBMConversion.h: A class with static functions to convert IBM format
00002 //# Copyright (C) 1996,1997,1999,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: IBMConversion.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_IBMCONVERSION_H
00029 #define CASA_IBMCONVERSION_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/OS/CanonicalConversion.h>
00034 
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 // Define the IBM sizes of the built-in data types.
00039 
00040 #define SIZE_IBM_CHAR     1
00041 #define SIZE_IBM_UCHAR    1
00042 #define SIZE_IBM_SHORT    2
00043 #define SIZE_IBM_USHORT   2
00044 #define SIZE_IBM_INT      4
00045 #define SIZE_IBM_UINT     4
00046 #define SIZE_IBM_INT64    4
00047 #define SIZE_IBM_UINT64   4
00048 #define SIZE_IBM_FLOAT    4
00049 #define SIZE_IBM_DOUBLE   8
00050 
00051 
00052 // <summary>
00053 // A class with static functions to convert IBM format
00054 // </summary>
00055 
00056 // <use visibility=export>
00057 
00058 // <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tIBMConversion" demos="">
00059 // </reviewed>
00060 
00061 // <synopsis>
00062 // This class contains static toLocal functions to convert data from IBM-360
00063 // format to local format and vice-versa. It also handles the conversion
00064 // of the IBM EBCDIC characters to ASCII characters (for data type char).
00065 // <p>
00066 // The functions work well on big-endian as well as little-endian machines.
00067 // </synopsis>
00068 
00069 // <motivation>
00070 // Archived WSRT data can be stored in the old IBM format
00071 // (EBCDIC characters and floats with base 16).
00072 // Conversion functions are needed to read these data.
00073 // </motivation>
00074 
00075 // <todo asof="$DATE$">
00076 //  <li> Support data type long double.
00077 // </todo>
00078 
00079 
00080 class IBMConversion
00081 {
00082 public:
00083     // Convert one value from IBM format to local format.
00084     // The from and to buffer should not overlap.
00085     // <note>
00086     // The char version converts from EBCDIC to ASCII, while the
00087     // unsigned char version is a simple copy.
00088     // </note>
00089     // <group>
00090     static void toLocal (char&           to, const void* from);
00091     static void toLocal (unsigned char&  to, const void* from);
00092     static void toLocal (short&          to, const void* from);
00093     static void toLocal (unsigned short& to, const void* from);
00094     static void toLocal (int&            to, const void* from);
00095     static void toLocal (unsigned int&   to, const void* from);
00096     static void toLocal (Int64&          to, const void* from);
00097     static void toLocal (uInt64&         to, const void* from);
00098     static void toLocal (float&          to, const void* from);
00099     static void toLocal (double&         to, const void* from);
00100     // </group>
00101     
00102     // Convert nr values from IBM format to local format.
00103     // The from and to buffer should not overlap.
00104     // <note>
00105     // The char version converts from EBCDIC to ASCII, while the
00106     // unsigned char version is a simple copy.
00107     // </note>
00108     // <group>
00109     static void toLocal (char*           to, const void* from,
00110                          unsigned int nr);
00111     static void toLocal (unsigned char*  to, const void* from,
00112                          unsigned int nr);
00113     static void toLocal (short*          to, const void* from,
00114                          unsigned int nr);
00115     static void toLocal (unsigned short* to, const void* from,
00116                          unsigned int nr);
00117     static void toLocal (int*            to, const void* from,
00118                          unsigned int nr);
00119     static void toLocal (unsigned int*   to, const void* from,
00120                          unsigned int nr);
00121     static void toLocal (Int64*          to, const void* from,
00122                          unsigned int nr);
00123     static void toLocal (uInt64*         to, const void* from,
00124                          unsigned int nr);
00125     static void toLocal (float*          to, const void* from,
00126                          unsigned int nr);
00127     static void toLocal (double*         to, const void* from,
00128                          unsigned int nr);
00129     // </group>
00130 
00131     // Convert one value from local format to IBM format.
00132     // The from and to buffer should not overlap.
00133     // <note>
00134     // The char version converts from ASCII to EBCDIC, while the
00135     // unsigned char version is a simple copy.
00136     // </note>
00137     // <group>
00138     static void fromLocal (void* to, char           from);
00139     static void fromLocal (void* to, unsigned char  from);
00140     static void fromLocal (void* to, short          from);
00141     static void fromLocal (void* to, unsigned short from);
00142     static void fromLocal (void* to, int            from);
00143     static void fromLocal (void* to, unsigned int   from);
00144     static void fromLocal (void* to, Int64          from);
00145     static void fromLocal (void* to, uInt64         from);
00146     static void fromLocal (void* to, float          from);
00147     static void fromLocal (void* to, double         from);
00148     // </group>
00149     
00150     // Convert nr values from local format to IBM format.
00151     // The from and to buffer should not overlap.
00152     // <note>
00153     // The char version converts from ASCII to EBCDIC, while the
00154     // unsigned char version is a simple copy.
00155     // </note>
00156     // <group>
00157     static void fromLocal (void* to, const char*           from,
00158                            unsigned int nr);
00159     static void fromLocal (void* to, const unsigned char*  from,
00160                            unsigned int nr);
00161     static void fromLocal (void* to, const short*          from,
00162                            unsigned int nr);
00163     static void fromLocal (void* to, const unsigned short* from,
00164                            unsigned int nr);
00165     static void fromLocal (void* to, const int*            from,
00166                            unsigned int nr);
00167     static void fromLocal (void* to, const unsigned int*   from,
00168                            unsigned int nr);
00169     static void fromLocal (void* to, const Int64*          from,
00170                            unsigned int nr);
00171     static void fromLocal (void* to, const uInt64*         from,
00172                            unsigned int nr);
00173     static void fromLocal (void* to, const float*          from,
00174                            unsigned int nr);
00175     static void fromLocal (void* to, const double*         from,
00176                            unsigned int nr);
00177     // </group>
00178     
00179 private:
00180     // This class should not be constructed
00181     // (so declare the constructor private).
00182     IBMConversion();
00183 };
00184 
00185 
00186 
00187 inline void IBMConversion::toLocal (unsigned char& to, const void* from)
00188 {
00189     CanonicalConversion::toLocal (to, from);
00190 }
00191 
00192 inline void IBMConversion::toLocal (short& to, const void* from)
00193 {
00194     CanonicalConversion::toLocal (to, from);
00195 }
00196 
00197 inline void IBMConversion::toLocal (unsigned short& to, const void* from)
00198 {
00199     CanonicalConversion::toLocal (to, from);
00200 }
00201 
00202 inline void IBMConversion::toLocal (int& to, const void* from)
00203 {
00204     CanonicalConversion::toLocal (to, from);
00205 }
00206 
00207 inline void IBMConversion::toLocal (unsigned int& to, const void* from)
00208 {
00209     CanonicalConversion::toLocal (to, from);
00210 }
00211 
00212 inline void IBMConversion::toLocal (Int64& to, const void* from)
00213 {
00214     if (sizeof(Int64) != 4) {
00215         if (((signed char*)from)[0] < 0) {
00216             to = -1;
00217         }else{
00218             to = 0;
00219         }
00220     }
00221 #if defined(AIPS_LITTLE_ENDIAN)
00222     CanonicalConversion::reverse4 (&to, from);
00223 #else
00224     CanonicalConversion::move4 (((char*)&to)+sizeof(Int64)-4, from);
00225 #endif
00226 }
00227 
00228 inline void IBMConversion::toLocal (uInt64& to, const void* from)
00229 {
00230     if (sizeof(uInt64) != 4) {
00231         to = 0;
00232     }
00233 #if defined(AIPS_LITTLE_ENDIAN)
00234     CanonicalConversion::reverse4 (&to, from);
00235 #else
00236     CanonicalConversion::move4 (((char*)&to)+sizeof(uInt64)-4, from);
00237 #endif
00238 }
00239 
00240 inline void IBMConversion::toLocal (float& to, const void* from)
00241 {
00242     toLocal (&to, from, 1);
00243 }
00244 
00245 inline void IBMConversion::toLocal (double& to, const void* from)
00246 {
00247     toLocal (&to, from, 1);
00248 }
00249 
00250 inline void IBMConversion::toLocal (unsigned char* to, const void* from,
00251                                     unsigned int nr)
00252 {
00253     CanonicalConversion::toLocal (to, from, nr);
00254 }
00255 
00256 inline void IBMConversion::toLocal (short* to, const void* from,
00257                                     unsigned int nr)
00258 {
00259     CanonicalConversion::toLocal (to, from, nr);
00260 }
00261 
00262 inline void IBMConversion::toLocal (unsigned short* to, const void* from,
00263                                     unsigned int nr)
00264 {
00265     CanonicalConversion::toLocal (to, from, nr);
00266 }
00267 
00268 inline void IBMConversion::toLocal (int* to, const void* from,
00269                                     unsigned int nr)
00270 {
00271     CanonicalConversion::toLocal (to, from, nr);
00272 }
00273 
00274 inline void IBMConversion::toLocal (unsigned int* to, const void* from,
00275                                     unsigned int nr)
00276 {
00277     CanonicalConversion::toLocal (to, from, nr);
00278 }
00279 
00280 
00281 inline void IBMConversion::fromLocal (void* to, unsigned char from)
00282 {
00283     CanonicalConversion::fromLocal (to, from);
00284 }
00285 
00286 inline void IBMConversion::fromLocal (void* to, short from)
00287 {
00288     CanonicalConversion::fromLocal (to, from);
00289 }
00290 
00291 inline void IBMConversion::fromLocal (void* to, unsigned short from)
00292 {
00293     CanonicalConversion::fromLocal (to, from);
00294 }
00295 
00296 inline void IBMConversion::fromLocal (void* to, int from)
00297 {
00298     CanonicalConversion::fromLocal (to, from);
00299 }
00300 
00301 inline void IBMConversion::fromLocal (void* to, unsigned int from)
00302 {
00303     CanonicalConversion::fromLocal (to, from);
00304 }
00305 
00306 inline void IBMConversion::fromLocal (void* to, Int64 from)
00307 {
00308 #if defined(AIPS_LITTLE_ENDIAN)
00309     CanonicalConversion::reverse4 (to, &from);
00310 #else
00311     CanonicalConversion::move4 (to, ((char*)&from)+sizeof(Int64)-4);
00312 #endif
00313 }
00314 
00315 inline void IBMConversion::fromLocal (void* to, uInt64 from)
00316 {
00317 #if defined(AIPS_LITTLE_ENDIAN)
00318     CanonicalConversion::reverse4 (to, &from);
00319 #else
00320     CanonicalConversion::move4 (to,((char*)&from)+sizeof(uInt64)-4);
00321 #endif
00322 }
00323 
00324 inline void IBMConversion::fromLocal (void* to, float from)
00325 {
00326     fromLocal (to, &from, 1);
00327 }
00328 
00329 inline void IBMConversion::fromLocal (void* to, double from)
00330 {
00331     fromLocal (to, &from, 1);
00332 }
00333 
00334 
00335 inline void IBMConversion::fromLocal (void* to, const unsigned char* from,
00336                                       unsigned int nr)
00337 { 
00338     CanonicalConversion::fromLocal (to, from, nr);
00339 }
00340 
00341 inline void IBMConversion::fromLocal (void* to, const short* from,
00342                                       unsigned int nr)
00343 { 
00344     CanonicalConversion::fromLocal (to, from, nr);
00345 }
00346 
00347 inline void IBMConversion::fromLocal (void* to, const unsigned short* from,
00348                                       unsigned int nr)
00349 { 
00350     CanonicalConversion::fromLocal (to, from, nr);
00351 }
00352 
00353 inline void IBMConversion::fromLocal (void* to, const int* from,
00354                                       unsigned int nr)
00355 { 
00356     CanonicalConversion::fromLocal (to, from, nr);
00357 }
00358 
00359 inline void IBMConversion::fromLocal (void* to, const unsigned int* from,
00360                                       unsigned int nr)
00361 { 
00362     CanonicalConversion::fromLocal (to, from, nr);
00363 }
00364 
00365 
00366 
00367 
00368 } //# NAMESPACE CASA - END
00369 
00370 #endif