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 char fread_be_C[] = "$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fread_be.C,v 1.4 2009/01/19 15:23:17 j_novak Exp $" ;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <stdio.h>
00056 #include <assert.h>
00057
00058
00059
00060
00061
00062 int fread_be(int* aa, int size, int nb, FILE* fich) {
00063
00064 assert(size == 4) ;
00065
00066
00067
00068
00069 int itest = 1 ;
00070 bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00071
00072 if (little_endian) {
00073
00074 int size_tot = 4 * nb ;
00075
00076 char* bytes_big = new char[size_tot] ;
00077
00078 int nr = int(fread(bytes_big, 1, size_tot, fich)) ;
00079
00080 char* pbig = bytes_big ;
00081 char* plit = reinterpret_cast<char*>( aa );
00082
00083 for (int j=0; j< nb; j++) {
00084
00085 for (int i=0; i<4; i++) {
00086 plit[i] = pbig[3-i] ;
00087 }
00088
00089 plit += 4 ;
00090 pbig += 4 ;
00091
00092 }
00093
00094 delete [] bytes_big ;
00095
00096 return nr / 4 ;
00097
00098 }
00099 else {
00100
00101 return int(fread(aa, size, nb, fich)) ;
00102 }
00103
00104 }
00105
00106
00107
00108
00109
00110 int fread_be(double* aa, int size, int nb, FILE* fich) {
00111
00112 assert(size == 8) ;
00113
00114
00115
00116
00117 int itest = 1 ;
00118 bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00119
00120 if (little_endian) {
00121
00122 int size_tot = 8 * nb ;
00123
00124 char* bytes_big = new char[size_tot] ;
00125
00126 int nr = int(fread(bytes_big, 1, size_tot, fich)) ;
00127
00128 char* pbig = bytes_big ;
00129 char* plit = reinterpret_cast<char*>( aa );
00130
00131 for (int j=0; j< nb; j++) {
00132
00133 for (int i=0; i<8; i++) {
00134 plit[i] = pbig[7-i] ;
00135 }
00136
00137 plit += 8 ;
00138 pbig += 8 ;
00139
00140 }
00141
00142 delete [] bytes_big ;
00143
00144 return nr / 8 ;
00145
00146 }
00147 else {
00148
00149 return int(fread(aa, size, nb, fich)) ;
00150 }
00151
00152 }
00153