LORENE
fwrite_be.C
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
28
29/*
30 * $Id: fwrite_be.C,v 1.7 2016/12/05 16:18:11 j_novak Exp $
31 * $Log: fwrite_be.C,v $
32 * Revision 1.7 2016/12/05 16:18:11 j_novak
33 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
34 *
35 * Revision 1.6 2014/10/13 08:53:32 j_novak
36 * Lorene classes and functions now belong to the namespace Lorene.
37 *
38 * Revision 1.5 2014/10/06 15:16:11 j_novak
39 * Modified #include directives to use c++ syntax.
40 *
41 * Revision 1.4 2009/01/19 15:23:17 j_novak
42 * Change of some casts to avoid warnings
43 *
44 * Revision 1.3 2008/08/19 06:42:01 j_novak
45 * Minor modifications to avoid warnings with gcc 4.3. Most of them concern
46 * cast-type operations, and constant strings that must be defined as const char*
47 *
48 * Revision 1.2 2001/12/13 15:01:19 e_gourgoulhon
49 * Array bytes_big now created with a new char[]
50 *
51 * Revision 1.1 2001/12/04 21:32:39 e_gourgoulhon
52 * Functions similar to the stdio fread/fwrite except that they ensure
53 * the big endian convention, whatever the system convention is.
54 *
55 * Revision 1.1 2001/11/23 15:09:09 e_gourgoulhon
56 * Templates for new source files
57 *
58 *
59 * $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fwrite_be.C,v 1.7 2016/12/05 16:18:11 j_novak Exp $
60 *
61 */
62
63// C headers
64#include <cstdio>
65#include <cassert>
66
67 //-------------------------//
68 // int version //
69 //-------------------------//
70
71
72namespace Lorene {
73int fwrite_be(const int* aa, int size, int nb, FILE* fich) {
74
75 assert(size == 4) ;
76
77 // Determines whether the default storage is big endian
78 // or large endians
79
80 int itest = 1 ;
81 bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
82
83 if (little_endian) {
84
85 int size_tot = 4 * nb ;
86
87 char* bytes_big = new char[size_tot] ;
88 char* pbig = bytes_big ;
89 const char* plit = reinterpret_cast<const char*>(aa) ;
90
91 for (int j=0; j< nb; j++) {
92
93 for (int i=0; i<4; i++) {
94 pbig[i] = plit[3-i] ;
95 }
96
97 plit += 4 ; // next item
98 pbig += 4 ;
99
100 }
101
102
103 int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 4) ;
104
105 delete [] bytes_big ;
106
107 return nx ;
108
109 }
110 else { // Big endian case: nothing to do:
111
112 return int(fwrite(aa, size, nb, fich)) ;
113 }
114
115}
116
117
118 //-------------------------//
119 // double version //
120 //-------------------------//
121
122
123int fwrite_be(const double* aa, int size, int nb, FILE* fich) {
124
125 assert(size == 8) ;
126
127 // Determines whether the default storage is big endian
128 // or large endians
129
130 int itest = 1 ;
131 bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
132
133 if (little_endian) {
134
135 int size_tot = 8 * nb ;
136
137 char* bytes_big = new char[size_tot] ;
138 char* pbig = bytes_big ;
139 const char* plit = reinterpret_cast<const char*>(aa) ;
140
141 for (int j=0; j< nb; j++) {
142
143 for (int i=0; i<8; i++) {
144 pbig[i] = plit[7-i] ;
145 }
146
147 plit += 8 ; // next item
148 pbig += 8 ;
149
150 }
151
152 int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 8) ;
153 delete [] bytes_big ;
154
155 return nx ;
156
157 }
158 else { // Big endian case: nothing to do:
159
160 return int(fwrite(aa, size, nb, fich)) ;
161 }
162
163}
164}
int fwrite_be(const int *aa, int size, int nb, FILE *fich)
Writes integer(s) into a binary file according to the big endian convention.
Definition fwrite_be.C:73
Lorene prototypes.
Definition app_hor.h:67