LORENE
fread_be.C
1
/*
2
* Read binary data from 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
fread_be_C[] =
"$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fread_be.C,v 1.6 2014/10/13 08:53:32 j_novak Exp $"
;
28
29
/*
30
* $Id: fread_be.C,v 1.6 2014/10/13 08:53:32 j_novak Exp $
31
* $Log: fread_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/fread_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
namespace
Lorene
{
69
int
fread_be
(
int
* aa,
int
size,
int
nb,
FILE
*
fich
) {
70
71
assert
(size == 4) ;
72
73
// Determines whether the default storage is big endian
74
// or large endians
75
76
int
itest
= 1 ;
77
bool
little_endian
= ( *(
reinterpret_cast<
char
*
>
(&
itest
) ) == 1) ;
78
79
if
(
little_endian
) {
80
81
int
size_tot
= 4 * nb ;
82
83
char
*
bytes_big
=
new
char
[
size_tot
] ;
84
85
int
nr =
int
(
fread
(
bytes_big
, 1,
size_tot
,
fich
)) ;
86
87
char
*
pbig
=
bytes_big
;
88
char
*
plit
=
reinterpret_cast<
char
*
>
( aa );
89
90
for
(
int
j
=0;
j
< nb;
j
++) {
91
92
for
(
int
i
=0;
i
<4;
i
++) {
93
plit
[
i
] =
pbig
[3-
i
] ;
94
}
95
96
plit
+= 4 ;
// next item
97
pbig
+= 4 ;
98
99
}
100
101
delete
[]
bytes_big
;
102
103
return
nr / 4 ;
104
105
}
106
else
{
// Big endian case: nothing to do:
107
108
return
int
(
fread
(aa, size, nb,
fich
)) ;
109
}
110
111
}
112
113
//-------------------------//
114
// double version //
115
//-------------------------//
116
117
int
fread_be
(
double
* aa,
int
size,
int
nb,
FILE
*
fich
) {
118
119
assert
(size == 8) ;
120
121
// Determines whether the default storage is big endian
122
// or large endians
123
124
int
itest
= 1 ;
125
bool
little_endian
= ( *(
reinterpret_cast<
char
*
>
(&
itest
) ) == 1) ;
126
127
if
(
little_endian
) {
128
129
int
size_tot
= 8 * nb ;
130
131
char
*
bytes_big
=
new
char
[
size_tot
] ;
132
133
int
nr =
int
(
fread
(
bytes_big
, 1,
size_tot
,
fich
)) ;
134
135
char
*
pbig
=
bytes_big
;
136
char
*
plit
=
reinterpret_cast<
char
*
>
( aa );
137
138
for
(
int
j
=0;
j
< nb;
j
++) {
139
140
for
(
int
i
=0;
i
<8;
i
++) {
141
plit
[
i
] =
pbig
[7-
i
] ;
142
}
143
144
plit
+= 8 ;
// next item
145
pbig
+= 8 ;
146
147
}
148
149
delete
[]
bytes_big
;
150
151
return
nr / 8 ;
152
153
}
154
else
{
// Big endian case: nothing to do:
155
156
return
int
(
fread
(aa, size, nb,
fich
)) ;
157
}
158
159
}
160
161
}
Lorene::Evolution_std
Time evolution with partial storage (*** under development ***).
Definition
evolution.h:371
Lorene::fread_be
int fread_be(int *aa, int size, int nb, FILE *fich)
Reads integer(s) from a binary file according to the big endian convention.
Definition
fread_be.C:69
Lorene
Lorene prototypes.
Definition
app_hor.h:64
C++
Source
Non_class_members
Utilities
fread_be.C
Generated by
1.9.8