| 1 |
/* |
| 2 |
* Write binary data into a file according to the Big Endian convention |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (c) 2001 Eric Gourgoulhon |
| 8 |
* |
| 9 |
* This file is part of LORENE. |
| 10 |
* |
| 11 |
* LORENE is free software; you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation; either version 2 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* LORENE is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with LORENE; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
char fwrite_be_C[] = "$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fwrite_be.C,v 1.6 2014/10/13 08:53:32 j_novak Exp $" ; |
| 28 |
|
| 29 |
/* |
| 30 |
* $Id: fwrite_be.C,v 1.6 2014/10/13 08:53:32 j_novak Exp $ |
| 31 |
* $Log: fwrite_be.C,v $ |
| 32 |
* Revision 1.6 2014/10/13 08:53:32 j_novak |
| 33 |
* Lorene classes and functions now belong to the namespace Lorene. |
| 34 |
* |
| 35 |
* Revision 1.5 2014/10/06 15:16:11 j_novak |
| 36 |
* Modified #include directives to use c++ syntax. |
| 37 |
* |
| 38 |
* Revision 1.4 2009/01/19 15:23:17 j_novak |
| 39 |
* Change of some casts to avoid warnings |
| 40 |
* |
| 41 |
* Revision 1.3 2008/08/19 06:42:01 j_novak |
| 42 |
* Minor modifications to avoid warnings with gcc 4.3. Most of them concern |
| 43 |
* cast-type operations, and constant strings that must be defined as const char* |
| 44 |
* |
| 45 |
* Revision 1.2 2001/12/13 15:01:19 e_gourgoulhon |
| 46 |
* Array bytes_big now created with a new char[] |
| 47 |
* |
| 48 |
* Revision 1.1 2001/12/04 21:32:39 e_gourgoulhon |
| 49 |
* Functions similar to the stdio fread/fwrite except that they ensure |
| 50 |
* the big endian convention, whatever the system convention is. |
| 51 |
* |
| 52 |
* Revision 1.1 2001/11/23 15:09:09 e_gourgoulhon |
| 53 |
* Templates for new source files |
| 54 |
* |
| 55 |
* |
| 56 |
* $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fwrite_be.C,v 1.6 2014/10/13 08:53:32 j_novak Exp $ |
| 57 |
* |
| 58 |
*/ |
| 59 |
|
| 60 |
// C headers |
| 61 |
#include <cstdio> |
| 62 |
#include <cassert> |
| 63 |
|
| 64 |
//-------------------------// |
| 65 |
// int version // |
| 66 |
//-------------------------// |
| 67 |
|
| 68 |
|
| 69 |
namespace Lorene { |
| 70 |
int fwrite_be(const int* aa, int size, int nb, FILE* fich) { |
| 71 |
|
| 72 |
assert(size == 4) ; |
| 73 |
|
| 74 |
// Determines whether the default storage is big endian |
| 75 |
// or large endians |
| 76 |
|
| 77 |
int itest = 1 ; |
| 78 |
bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ; |
| 79 |
|
| 80 |
if (little_endian) { |
| 81 |
|
| 82 |
int size_tot = 4 * nb ; |
| 83 |
|
| 84 |
char* bytes_big = new char[size_tot] ; |
| 85 |
char* pbig = bytes_big ; |
| 86 |
const char* plit = reinterpret_cast<const char*>(aa) ; |
| 87 |
|
| 88 |
for (int j=0; j< nb; j++) { |
| 89 |
|
| 90 |
for (int i=0; i<4; i++) { |
| 91 |
pbig[i] = plit[3-i] ; |
| 92 |
} |
| 93 |
|
| 94 |
plit += 4 ; // next item |
| 95 |
pbig += 4 ; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 4) ; |
| 101 |
|
| 102 |
delete [] bytes_big ; |
| 103 |
|
| 104 |
return nx ; |
| 105 |
|
| 106 |
} |
| 107 |
else { // Big endian case: nothing to do: |
| 108 |
|
| 109 |
return int(fwrite(aa, size, nb, fich)) ; |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
//-------------------------// |
| 116 |
// double version // |
| 117 |
//-------------------------// |
| 118 |
|
| 119 |
|
| 120 |
int fwrite_be(const double* aa, int size, int nb, FILE* fich) { |
| 121 |
|
| 122 |
assert(size == 8) ; |
| 123 |
|
| 124 |
// Determines whether the default storage is big endian |
| 125 |
// or large endians |
| 126 |
|
| 127 |
int itest = 1 ; |
| 128 |
bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ; |
| 129 |
|
| 130 |
if (little_endian) { |
| 131 |
|
| 132 |
int size_tot = 8 * nb ; |
| 133 |
|
| 134 |
char* bytes_big = new char[size_tot] ; |
| 135 |
char* pbig = bytes_big ; |
| 136 |
const char* plit = reinterpret_cast<const char*>(aa) ; |
| 137 |
|
| 138 |
for (int j=0; j< nb; j++) { |
| 139 |
|
| 140 |
for (int i=0; i<8; i++) { |
| 141 |
pbig[i] = plit[7-i] ; |
| 142 |
} |
| 143 |
|
| 144 |
plit += 8 ; // next item |
| 145 |
pbig += 8 ; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 8) ; |
| 150 |
delete [] bytes_big ; |
| 151 |
|
| 152 |
return nx ; |
| 153 |
|
| 154 |
} |
| 155 |
else { // Big endian case: nothing to do: |
| 156 |
|
| 157 |
return int(fwrite(aa, size, nb, fich)) ; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
} |