BWAPI
|
00001 // Copyright (c) 1997 Utrecht University (The Netherlands), 00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany), 00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg 00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria), 00005 // and Tel-Aviv University (Israel). All rights reserved. 00006 // 00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public License as 00009 // published by the Free Software Foundation; version 2.1 of the License. 00010 // See the file LICENSE.LGPL distributed with CGAL. 00011 // 00012 // Licensees holding a valid commercial license may use this file in 00013 // accordance with the commercial license agreement provided with the software. 00014 // 00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 // 00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Stream_support/include/CGAL/IO/binary_file_io.h $ 00019 // $Id: binary_file_io.h 28567 2006-02-16 14:30:13Z lsaboret $ 00020 // 00021 // 00022 // Author(s) : Lutz Kettner <kettner@mpi-sb.mpg.de> 00023 00024 00025 #ifndef CGAL_IO_BINARY_FILE_IO_H 00026 #define CGAL_IO_BINARY_FILE_IO_H 00027 00028 #include <CGAL/basic.h> 00029 #include <CGAL/known_bit_size_integers.h> 00030 #include <iostream> 00031 00032 CGAL_BEGIN_NAMESPACE 00033 00034 inline void 00035 I_Binary_write_integer32(std::ostream& out, Integer32 i) { 00036 out.write( (char*)(&i), 4); 00037 } 00038 inline void 00039 I_Binary_write_float32(std::ostream& out, float f) { 00040 out.write( (char*)(&f), 4); 00041 } 00042 00043 inline void 00044 I_Binary_read_integer32(std::istream& in, Integer32& i) { 00045 in.read( (char*)(&i), 4); 00046 } 00047 inline void 00048 I_Binary_read_float32(std::istream& in, float& f) { 00049 in.read( (char*)(&f), 4); 00050 } 00051 00052 inline void 00053 I_swap_to_big_endian( UInteger32& u) { 00054 (void) u; 00055 #ifdef CGAL_LITTLE_ENDIAN 00056 u = ((u >> 24) | (u << 24) | ((u >> 8) & 0xff00) | ((u << 8) & 0xff0000)); 00057 #endif 00058 } 00059 00060 inline void 00061 I_swap_to_big_endian( Integer32& i) { 00062 // We need to use a union instead of the 2 lines below, 00063 // otherwise we get aliasing issues. 00064 // UInteger32& u = (UInteger32&)i; 00065 // I_swap_to_big_endian( u); 00066 union { 00067 Integer32 in; 00068 UInteger32 ui; 00069 } u; 00070 u.in = i; 00071 I_swap_to_big_endian(u.ui); 00072 i = u.in; 00073 } 00074 00075 inline void 00076 I_swap_to_big_endian( float& f) { 00077 // We need to use a union instead of the 2 lines below, 00078 // otherwise we get aliasing issues. 00079 // UInteger32& u = (UInteger32&)f; 00080 // I_swap_to_big_endian( u); 00081 union { 00082 UInteger32 ui; 00083 float fl; 00084 } u; 00085 u.fl = f; 00086 I_swap_to_big_endian(u.ui); 00087 f = u.fl; 00088 } 00089 00090 inline void 00091 I_Binary_write_big_endian_integer32(std::ostream& out, Integer32 i) { 00092 I_swap_to_big_endian( i); 00093 out.write( (char*)(&i), 4); 00094 } 00095 inline void 00096 I_Binary_write_big_endian_float32(std::ostream& out, float f) { 00097 I_swap_to_big_endian( f); 00098 out.write( (char*)(&f), 4); 00099 } 00100 00101 inline void 00102 I_Binary_read_big_endian_integer32(std::istream& in, Integer32& i) { 00103 in.read( (char*)(&i), 4); 00104 I_swap_to_big_endian( i); 00105 } 00106 inline void 00107 I_Binary_read_big_endian_float32(std::istream& in, float& f) { 00108 in.read( (char*)(&f), 4); 00109 I_swap_to_big_endian( f); 00110 } 00111 00112 CGAL_END_NAMESPACE 00113 00114 #endif // CGAL_IO_BINARY_FILE_IO_H