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 fwrite_be_C[] = "$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fwrite_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
00063 int fwrite_be(const int* aa, int size, int nb, FILE* fich) {
00064
00065 assert(size == 4) ;
00066
00067
00068
00069
00070 int itest = 1 ;
00071 bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00072
00073 if (little_endian) {
00074
00075 int size_tot = 4 * nb ;
00076
00077 char* bytes_big = new char[size_tot] ;
00078 char* pbig = bytes_big ;
00079 const char* plit = reinterpret_cast<const char*>(aa) ;
00080
00081 for (int j=0; j< nb; j++) {
00082
00083 for (int i=0; i<4; i++) {
00084 pbig[i] = plit[3-i] ;
00085 }
00086
00087 plit += 4 ;
00088 pbig += 4 ;
00089
00090 }
00091
00092
00093 int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 4) ;
00094
00095 delete [] bytes_big ;
00096
00097 return nx ;
00098
00099 }
00100 else {
00101
00102 return int(fwrite(aa, size, nb, fich)) ;
00103 }
00104
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 int fwrite_be(const double* aa, int size, int nb, FILE* fich) {
00114
00115 assert(size == 8) ;
00116
00117
00118
00119
00120 int itest = 1 ;
00121 bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00122
00123 if (little_endian) {
00124
00125 int size_tot = 8 * nb ;
00126
00127 char* bytes_big = new char[size_tot] ;
00128 char* pbig = bytes_big ;
00129 const char* plit = reinterpret_cast<const char*>(aa) ;
00130
00131 for (int j=0; j< nb; j++) {
00132
00133 for (int i=0; i<8; i++) {
00134 pbig[i] = plit[7-i] ;
00135 }
00136
00137 plit += 8 ;
00138 pbig += 8 ;
00139
00140 }
00141
00142 int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 8) ;
00143 delete [] bytes_big ;
00144
00145 return nx ;
00146
00147 }
00148 else {
00149
00150 return int(fwrite(aa, size, nb, fich)) ;
00151 }
00152
00153 }