fread_be.C

00001 /*
00002  *  Read binary data from a file according to the Big Endian convention
00003  *
00004  */
00005 
00006 /*
00007  *   Copyright (c) 2001 Eric Gourgoulhon
00008  *
00009  *   This file is part of LORENE.
00010  *
00011  *   LORENE is free software; you can redistribute it and/or modify
00012  *   it under the terms of the GNU General Public License as published by
00013  *   the Free Software Foundation; either version 2 of the License, or
00014  *   (at your option) any later version.
00015  *
00016  *   LORENE is distributed in the hope that it will be useful,
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *   GNU General Public License for more details.
00020  *
00021  *   You should have received a copy of the GNU General Public License
00022  *   along with LORENE; if not, write to the Free Software
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  */
00026 
00027 char fread_be_C[] = "$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fread_be.C,v 1.4 2009/01/19 15:23:17 j_novak Exp $" ;
00028 
00029 /*
00030  * $Id: fread_be.C,v 1.4 2009/01/19 15:23:17 j_novak Exp $
00031  * $Log: fread_be.C,v $
00032  * Revision 1.4  2009/01/19 15:23:17  j_novak
00033  * Change of some casts to avoid warnings
00034  *
00035  * Revision 1.3  2008/08/19 06:42:01  j_novak
00036  * Minor modifications to avoid warnings with gcc 4.3. Most of them concern
00037  * cast-type operations, and constant strings that must be defined as const char*
00038  *
00039  * Revision 1.2  2001/12/13 15:01:19  e_gourgoulhon
00040  * Array bytes_big now created with a new char[]
00041  *
00042  * Revision 1.1  2001/12/04 21:32:39  e_gourgoulhon
00043  * Functions similar to the stdio fread/fwrite except that they ensure
00044  * the big endian convention, whatever the system convention is.
00045  *
00046  * Revision 1.1  2001/11/23 15:09:09  e_gourgoulhon
00047  * Templates for new source files
00048  *
00049  *
00050  * $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fread_be.C,v 1.4 2009/01/19 15:23:17 j_novak Exp $
00051  *
00052  */
00053 
00054 // C headers
00055 #include <stdio.h>
00056 #include <assert.h>
00057 
00058             //-------------------------//
00059             //  int version        //
00060             //-------------------------//
00061             
00062 int fread_be(int* aa, int size, int nb, FILE* fich) {
00063 
00064     assert(size == 4) ;
00065     
00066     // Determines whether the default storage is big endian
00067     //  or large endians
00068     
00069     int itest = 1 ;
00070     bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00071     
00072     if (little_endian) {
00073 
00074         int size_tot = 4 * nb ;
00075 
00076         char* bytes_big = new char[size_tot] ;
00077         
00078         int nr = int(fread(bytes_big, 1, size_tot, fich)) ;
00079         
00080         char* pbig =  bytes_big ;
00081         char* plit = reinterpret_cast<char*>( aa );
00082         
00083         for (int j=0; j< nb; j++) {
00084         
00085             for (int i=0; i<4; i++) {
00086                 plit[i] = pbig[3-i] ;
00087             }
00088         
00089             plit += 4 ;     // next item
00090             pbig += 4 ;
00091             
00092         }
00093         
00094         delete [] bytes_big ; 
00095 
00096         return nr / 4 ;     
00097 
00098     }
00099     else {  // Big endian case: nothing to do:
00100     
00101         return int(fread(aa, size, nb, fich)) ;
00102     }
00103         
00104 }
00105 
00106             //-------------------------//
00107             //  double version     //
00108             //-------------------------//
00109             
00110 int fread_be(double* aa, int size, int nb, FILE* fich) {
00111 
00112     assert(size == 8) ;
00113     
00114     // Determines whether the default storage is big endian
00115     //  or large endians
00116     
00117     int itest = 1 ;
00118     bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00119     
00120     if (little_endian) {
00121 
00122         int size_tot = 8 * nb ;
00123 
00124         char* bytes_big = new char[size_tot] ;
00125 
00126         int nr = int(fread(bytes_big, 1, size_tot, fich)) ;
00127         
00128         char* pbig =  bytes_big ;
00129         char* plit = reinterpret_cast<char*>( aa );
00130         
00131         for (int j=0; j< nb; j++) {
00132         
00133             for (int i=0; i<8; i++) {
00134                 plit[i] = pbig[7-i] ;
00135             }
00136         
00137             plit += 8 ;     // next item
00138             pbig += 8 ;
00139             
00140         }
00141 
00142         delete [] bytes_big ;
00143 
00144         return nr / 8 ;     
00145         
00146     }
00147     else {  // Big endian case: nothing to do:
00148     
00149         return int(fread(aa, size, nb, fich)) ;
00150     }
00151         
00152 }
00153 

Generated on Tue Feb 7 01:35:17 2012 for LORENE by  doxygen 1.4.6