fwrite_be.C

00001 /*
00002  *  Write binary data into 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 fwrite_be_C[] = "$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/fwrite_be.C,v 1.4 2009/01/19 15:23:17 j_novak Exp $" ;
00028 
00029 /*
00030  * $Id: fwrite_be.C,v 1.4 2009/01/19 15:23:17 j_novak Exp $
00031  * $Log: fwrite_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/fwrite_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 
00063 int fwrite_be(const int* aa, int size, int nb, FILE* fich) {
00064 
00065     assert(size == 4) ;
00066     
00067     // Determines whether the default storage is big endian
00068     //  or large endians
00069     
00070     int itest = 1 ;
00071     bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00072     
00073     if (little_endian) {
00074 
00075         int size_tot = 4 * nb ;
00076 
00077         char* bytes_big = new char[size_tot] ;
00078         char* pbig =  bytes_big ;
00079         const char* plit = reinterpret_cast<const char*>(aa) ;
00080         
00081         for (int j=0; j< nb; j++) {
00082         
00083             for (int i=0; i<4; i++) {
00084                 pbig[i] = plit[3-i] ;
00085             }
00086         
00087             plit += 4 ;     // next item
00088             pbig += 4 ;
00089             
00090         }
00091         
00092         
00093         int nx =  int(fwrite(bytes_big, 1, size_tot, fich) / 4) ;
00094 
00095         delete [] bytes_big ; 
00096         
00097         return nx ; 
00098 
00099     }
00100     else {  // Big endian case: nothing to do:
00101     
00102         return int(fwrite(aa, size, nb, fich)) ;
00103     }
00104         
00105 }
00106 
00107 
00108             //-------------------------//
00109             //  double version     //
00110             //-------------------------//
00111             
00112 
00113 int fwrite_be(const double* aa, int size, int nb, FILE* fich) {
00114 
00115     assert(size == 8) ;
00116     
00117     // Determines whether the default storage is big endian
00118     //  or large endians
00119     
00120     int itest = 1 ;
00121     bool little_endian = ( *( reinterpret_cast<char*>(&itest) ) == 1) ;
00122     
00123     if (little_endian) {
00124 
00125         int size_tot = 8 * nb ;
00126 
00127         char* bytes_big = new char[size_tot] ;
00128         char* pbig =  bytes_big ;
00129         const char* plit = reinterpret_cast<const char*>(aa) ;
00130         
00131         for (int j=0; j< nb; j++) {
00132         
00133             for (int i=0; i<8; i++) {
00134                 pbig[i] = plit[7-i] ;
00135             }
00136         
00137             plit += 8 ;     // next item
00138             pbig += 8 ;
00139             
00140         }
00141         
00142         int nx = int(fwrite(bytes_big, 1, size_tot, fich) / 8) ;        
00143         delete [] bytes_big ; 
00144         
00145         return nx ; 
00146 
00147     }
00148     else {  // Big endian case: nothing to do:
00149     
00150         return int(fwrite(aa, size, nb, fich)) ;
00151     }
00152         
00153 }

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