00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CASA_LITTLEENDIANCONVERSION_H
00029 #define CASA_LITTLEENDIANCONVERSION_H
00030
00031
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/casa/OS/CanonicalConversion.h>
00034
00035
00036 namespace casacore {
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 class LittleEndianConversion
00068 {
00069 public:
00070
00071
00072
00073 static void toLocal (char& to, const void* from);
00074 static void toLocal (unsigned char& to, const void* from);
00075 static void toLocal (short& to, const void* from);
00076 static void toLocal (unsigned short& to, const void* from);
00077 static void toLocal (int& to, const void* from);
00078 static void toLocal (unsigned int& to, const void* from);
00079 static void toLocal (Int64& to, const void* from);
00080 static void toLocal (uInt64& to, const void* from);
00081 static void toLocal (float& to, const void* from);
00082 static void toLocal (double& to, const void* from);
00083
00084
00085
00086
00087
00088 static void toLocal (char* to, const void* from,
00089 size_t nr);
00090 static void toLocal (unsigned char* to, const void* from,
00091 size_t nr);
00092 static void toLocal (short* to, const void* from,
00093 size_t nr);
00094 static void toLocal (unsigned short* to, const void* from,
00095 size_t nr);
00096 static void toLocal (int* to, const void* from,
00097 size_t nr);
00098 static void toLocal (unsigned int* to, const void* from,
00099 size_t nr);
00100 static void toLocal (Int64* to, const void* from,
00101 size_t nr);
00102 static void toLocal (uInt64* to, const void* from,
00103 size_t nr);
00104 static void toLocal (float* to, const void* from,
00105 size_t nr);
00106 static void toLocal (double* to, const void* from,
00107 size_t nr);
00108
00109
00110
00111
00112
00113 static void fromLocal (void* to, char from);
00114 static void fromLocal (void* to, unsigned char from);
00115 static void fromLocal (void* to, short from);
00116 static void fromLocal (void* to, unsigned short from);
00117 static void fromLocal (void* to, int from);
00118 static void fromLocal (void* to, unsigned int from);
00119 static void fromLocal (void* to, Int64 from);
00120 static void fromLocal (void* to, uInt64 from);
00121 static void fromLocal (void* to, float from);
00122 static void fromLocal (void* to, double from);
00123
00124
00125
00126
00127
00128 static void fromLocal (void* to, const char* from,
00129 size_t nr);
00130 static void fromLocal (void* to, const unsigned char* from,
00131 size_t nr);
00132 static void fromLocal (void* to, const short* from,
00133 size_t nr);
00134 static void fromLocal (void* to, const unsigned short* from,
00135 size_t nr);
00136 static void fromLocal (void* to, const int* from,
00137 size_t nr);
00138 static void fromLocal (void* to, const unsigned int* from,
00139 size_t nr);
00140 static void fromLocal (void* to, const Int64* from,
00141 size_t nr);
00142 static void fromLocal (void* to, const uInt64* from,
00143 size_t nr);
00144 static void fromLocal (void* to, const float* from,
00145 size_t nr);
00146 static void fromLocal (void* to, const double* from,
00147 size_t nr);
00148
00149
00150 private:
00151
00152
00153 LittleEndianConversion();
00154 };
00155
00156
00157
00158 inline void LittleEndianConversion::toLocal (char& to, const void* from)
00159 {
00160 to = *(char*)from;
00161 }
00162
00163 inline void LittleEndianConversion::toLocal (unsigned char& to,
00164 const void* from)
00165 {
00166 to = *(unsigned char*)from;
00167 }
00168
00169 inline void LittleEndianConversion::toLocal (short& to, const void* from)
00170 {
00171 if (sizeof(short) != 2) {
00172 if (((signed char*)from)[2] < 0) {
00173 to = -1;
00174 }else{
00175 to = 0;
00176 }
00177 }
00178 #if !defined(AIPS_LITTLE_ENDIAN)
00179 CanonicalConversion::reverse2 (((char*)&to)+sizeof(short)-2, from);
00180 #else
00181 CanonicalConversion::move2 (&to, from);
00182 #endif
00183 }
00184
00185 inline void LittleEndianConversion::toLocal (unsigned short& to,
00186 const void* from)
00187 {
00188 if (sizeof(unsigned short) != 2) {
00189 to = 0;
00190 }
00191 #if !defined(AIPS_LITTLE_ENDIAN)
00192 CanonicalConversion::reverse2 (((char*)&to)+sizeof(unsigned short)-2,from);
00193 #else
00194 CanonicalConversion::move2 (&to, from);
00195 #endif
00196 }
00197
00198 inline void LittleEndianConversion::toLocal (int& to, const void* from)
00199 {
00200 if (sizeof(int) != 4) {
00201 if (((signed char*)from)[3] < 0) {
00202 to = -1;
00203 }else{
00204 to = 0;
00205 }
00206 }
00207 #if !defined(AIPS_LITTLE_ENDIAN)
00208 CanonicalConversion::reverse4 (((char*)&to)+sizeof(int)-4, from);
00209 #else
00210 CanonicalConversion::move4 (&to, from);
00211 #endif
00212 }
00213
00214 inline void LittleEndianConversion::toLocal (unsigned int& to,
00215 const void* from)
00216 {
00217 if (sizeof(unsigned int) != 4) {
00218 to = 0;
00219 }
00220 #if !defined(AIPS_LITTLE_ENDIAN)
00221 CanonicalConversion::reverse4 (((char*)&to)+sizeof(unsigned int)-4, from);
00222 #else
00223 CanonicalConversion::move4 (&to, from);
00224 #endif
00225 }
00226
00227 inline void LittleEndianConversion::toLocal (Int64& to, const void* from)
00228 {
00229 int tmp;
00230 LittleEndianConversion::toLocal (tmp, from);
00231 to = tmp;
00232 }
00233
00234 inline void LittleEndianConversion::toLocal (uInt64& to,
00235 const void* from)
00236 {
00237 unsigned int tmp;
00238 LittleEndianConversion::toLocal (tmp, from);
00239 to = tmp;
00240 }
00241
00242 inline void LittleEndianConversion::toLocal (float& to, const void* from)
00243 {
00244 #if !defined(AIPS_LITTLE_ENDIAN)
00245 CanonicalConversion::reverse4 (&to, from);
00246 #else
00247 CanonicalConversion::move4 (&to, from);
00248 #endif
00249 }
00250
00251 inline void LittleEndianConversion::toLocal (double& to, const void* from)
00252 {
00253 #if !defined(AIPS_LITTLE_ENDIAN)
00254 CanonicalConversion::reverse8 (&to, from);
00255 #else
00256 CanonicalConversion::move8 (&to, from);
00257 #endif
00258 }
00259
00260
00261 inline void LittleEndianConversion::fromLocal (void* to, char from)
00262 {
00263 *(char*)to = from;
00264 }
00265 inline void LittleEndianConversion::fromLocal (void* to, unsigned char from)
00266 {
00267 *(unsigned char*)to = from;
00268 }
00269
00270 inline void LittleEndianConversion::fromLocal (void* to, short from)
00271 {
00272 #if !defined(AIPS_LITTLE_ENDIAN)
00273 CanonicalConversion::reverse2 (to, ((char*)&from)+sizeof(short)-2);
00274 #else
00275 CanonicalConversion::move2 (to, &from);
00276 #endif
00277 }
00278
00279 inline void LittleEndianConversion::fromLocal (void* to, unsigned short from)
00280 {
00281 #if !defined(AIPS_LITTLE_ENDIAN)
00282 CanonicalConversion::reverse2 (to,((char*)&from)+sizeof(unsigned short)-2);
00283 #else
00284 CanonicalConversion::move2 (to, &from);
00285 #endif
00286 }
00287
00288 inline void LittleEndianConversion::fromLocal (void* to, int from)
00289 {
00290 #if !defined(AIPS_LITTLE_ENDIAN)
00291 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(int)-4);
00292 #else
00293 CanonicalConversion::move4 (to, &from);
00294 #endif
00295 }
00296
00297 inline void LittleEndianConversion::fromLocal (void* to, unsigned int from)
00298 {
00299 #if !defined(AIPS_LITTLE_ENDIAN)
00300 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(unsigned int)-4);
00301 #else
00302 CanonicalConversion::move4 (to, &from);
00303 #endif
00304 }
00305
00306 inline void LittleEndianConversion::fromLocal (void* to, Int64 from)
00307 {
00308 #if !defined(AIPS_LITTLE_ENDIAN)
00309 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(Int64)-4);
00310 #else
00311 CanonicalConversion::move4 (to, &from);
00312 #endif
00313 }
00314
00315 inline void LittleEndianConversion::fromLocal (void* to, uInt64 from)
00316 {
00317 #if !defined(AIPS_LITTLE_ENDIAN)
00318 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(uInt64)-4);
00319 #else
00320 CanonicalConversion::move4 (to, &from);
00321 #endif
00322 }
00323
00324 inline void LittleEndianConversion::fromLocal (void* to, float from)
00325 {
00326 #if !defined(AIPS_LITTLE_ENDIAN)
00327 CanonicalConversion::reverse4 (to, ((char*)&from)+sizeof(float)-4);
00328 #else
00329 CanonicalConversion::move4 (to, &from);
00330 #endif
00331 }
00332
00333 inline void LittleEndianConversion::fromLocal (void* to, double from)
00334 {
00335 #if !defined(AIPS_LITTLE_ENDIAN)
00336 CanonicalConversion::reverse8 (to, ((char*)&from)+sizeof(double)-8);
00337 #else
00338 CanonicalConversion::move8 (to, &from);
00339 #endif
00340 }
00341
00342
00343
00344
00345 }
00346
00347 #endif