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 
28 
29 /*
30  * $Id: fread_be.C,v 1.7 2016/12/05 16:18:11 j_novak Exp $
31  * $Log: fread_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/fread_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 namespace Lorene {
72 int fread_be(int* aa, int size, int nb, FILE* fich) {
73 
74  assert(size == 4) ;
75 
76  // Determines whether the default storage is big endian
77  // or large endians
78 
79  int itest = 1 ;
80  bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
81 
82  if (little_endian) {
83 
84  int size_tot = 4 * nb ;
85 
86  char* bytes_big = new char[size_tot] ;
87 
88  int nr = int(fread(bytes_big, 1, size_tot, fich)) ;
89 
90  char* pbig = bytes_big ;
91  char* plit = reinterpret_cast<char*>( aa );
92 
93  for (int j=0; j< nb; j++) {
94 
95  for (int i=0; i<4; i++) {
96  plit[i] = pbig[3-i] ;
97  }
98 
99  plit += 4 ; // next item
100  pbig += 4 ;
101 
102  }
103 
104  delete [] bytes_big ;
105 
106  return nr / 4 ;
107 
108  }
109  else { // Big endian case: nothing to do:
110 
111  return int(fread(aa, size, nb, fich)) ;
112  }
113 
114 }
115 
116  //-------------------------//
117  // double version //
118  //-------------------------//
119 
120 int fread_be(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 
136  int nr = int(fread(bytes_big, 1, size_tot, fich)) ;
137 
138  char* pbig = bytes_big ;
139  char* plit = reinterpret_cast<char*>( aa );
140 
141  for (int j=0; j< nb; j++) {
142 
143  for (int i=0; i<8; i++) {
144  plit[i] = pbig[7-i] ;
145  }
146 
147  plit += 8 ; // next item
148  pbig += 8 ;
149 
150  }
151 
152  delete [] bytes_big ;
153 
154  return nr / 8 ;
155 
156  }
157  else { // Big endian case: nothing to do:
158 
159  return int(fread(aa, size, nb, fich)) ;
160  }
161 
162 }
163 
164 }
Lorene prototypes.
Definition: app_hor.h:67
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:72