Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

util_unformatted_io.C

Go to the documentation of this file.
00001 
00002        //=======================================================//    _\|/_
00003       //  __  _____           ___                    ___       //      /|\ ~
00004      //  /      |      ^     |   \  |         ^     |   \     //          _\|/_
00005     //   \__    |     / \    |___/  |        / \    |___/    //            /|\ ~
00006    //       \   |    /___\   |  \   |       /___\   |   \   // _\|/_
00007   //     ___/   |   /     \  |   \  |____  /     \  |___/  //   /|\ ~
00008  //                                                       //            _\|/_
00009 //=======================================================//              /|\ ~
00010 
00011 // util_unformatted_io.C:  functions for unformatted I/O.  May require lowering
00012 //                         the optimization level with some compilers...
00013 
00014 #include "starlab_vector.h"
00015 #include "util_io.h"
00016 #include "story.h"
00017 #include <ctype.h>
00018 
00019 // Until we get an official word from autoconf,
00020 // try to guess whether we're little-endian or not.
00021 
00022 #ifndef WORDS_BIGENDIAN
00023 # if MIPSEB || __MIPSEB || __MIPSEB__ || sparc || __sparc || __sparc__
00024 #  define WORDS_BIGENDIAN 1
00025 # elif i386 || __i386__ || alpha || __alpha__ || MIPSEL || __MIPSEL || __MIPSEL__
00026 #  define WORDS_BIGENDIAN 0
00027 # else
00028 #  error  Is this machine big- or little-endian?  Need to define WORDS_BIGENDIAN=0 or 1
00029 # endif
00030 #endif
00031 
00032 #undef isalnum          /* Hacks for Irix 6.5 <ctype.h> backward compatibility */
00033 #undef isspace
00034 
00035 void write_unformatted_real( ostream & s, real v )
00036 {
00037 #if WORDS_BIGENDIAN
00038     s.write( (char *)&v, 8 );
00039 #else
00040     unsigned long long lv = *(unsigned long long *)&v;
00041     lv = (lv>>32) | (lv<<32);
00042     lv = (lv&0x0000FFFF0000FFFFLL)<<16
00043        | (lv>>16)&0x0000FFFF0000FFFFLL;
00044     lv = (lv&0x00FF00FF00FF00FFLL)<<8
00045        | (lv>>8)&0x00FF00FF00FF00FFLL;
00046     s.write( (char *)&lv, 8 );
00047 #endif
00048 }
00049 
00050 void write_unformatted32_real( ostream & s, real v )
00051 {
00052     float f = v;
00053 #if WORDS_BIGENDIAN
00054     s.write( (char *)&f, 4 );
00055 #else
00056     unsigned int l = (*(unsigned int *)&f)>>16 | (*(unsigned int *)&f)<<16;
00057     l = (l&0x00FF00FF)<<8
00058       | (l>>8)&0x00FF00FF;
00059     s.write( (char *)&l, 4 );
00060 #endif
00061 }
00062 
00063 void write_unformatted_vector( ostream & s, vector & v )
00064 {
00065     write_unformatted_real( s, v[0] );
00066     write_unformatted_real( s, v[1] );
00067     write_unformatted_real( s, v[2] );
00068 }
00069 
00070 void write_unformatted32_vector( ostream & s, vector & v )
00071 {
00072     write_unformatted32_real( s, v[0] );
00073     write_unformatted32_real( s, v[1] );
00074     write_unformatted32_real( s, v[2] );
00075 }
00076 
00077 real read_unformatted_real( istream & s )
00078 {
00079 #if WORDS_BIGENDIAN
00080     real r;
00081     s.read( (char *)&r, 8 );
00082     return r;
00083 #else
00084     unsigned long long lv;
00085     s.read( (char *)&lv, 8 );
00086     lv = (lv>>32) | (lv<<32);
00087     lv = (lv&0x0000FFFF0000FFFFLL)<<16
00088        | (lv>>16)&0x0000FFFF0000FFFFLL;
00089     lv = (lv&0x00FF00FF00FF00FFLL)<<8
00090        | (lv>>8)&0x00FF00FF00FF00FFLL;
00091     return *(real *)&lv;
00092 #endif
00093 }
00094 
00095 real read_unformatted32_real( istream & s )
00096 {
00097 #if WORDS_BIGENDIAN
00098     float f;
00099     s.read( (char *)&f, 4 );
00100     return f;
00101 #else
00102     unsigned int iv;
00103     s.read( (char *)&iv, 4 );
00104     iv = (iv>>16) | (iv<<16);
00105     iv = (iv&0x00FF00FF)<<8
00106         | (iv>>8)&0x00FF00FF;
00107     return *(float *)&iv;
00108 #endif
00109 }
00110 
00111 void read_unformatted_vector( istream & s, vector & v )
00112 {
00113     v[0] = read_unformatted_real( s );
00114     v[1] = read_unformatted_real( s );
00115     v[2] = read_unformatted_real( s );
00116 }
00117 
00118 void read_unformatted32_vector( istream & s, vector & v )
00119 {
00120     v[0] = read_unformatted32_real( s );
00121     v[1] = read_unformatted32_real( s );
00122     v[2] = read_unformatted32_real( s );
00123 }

Generated at Sun Feb 24 09:57:20 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001