00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Teddy/SysSupport/EndianOut.h"
00026 #include "Teddy/SysSupport/Exception.h"
00027
00028
00029 namespace Teddy {
00030 namespace SysSupport {
00031
00032
00034 EndianOut::EndianOut( const char *file_name ){
00035 open( file_name );
00036 }
00037
00038
00040 EndianOut::~EndianOut(){
00041 close();
00042 }
00043
00044
00046 void EndianOut::open( const char *name ){
00047 ofs = new ofstream( name,ios::out|ios::trunc|ios::binary );
00048 }
00049
00050
00052 void EndianOut::close(){
00053 ofs->close(), ofs = NULL;
00054 }
00055
00056
00058 void EndianOut::write_byte( const int item ){
00059 if( !ofs->put((unsigned char)item) ){
00060 throw( Exception("EOF write error") );
00061 }
00062 }
00063
00064
00066 void EndianOut::write_short( const unsigned short item ){
00067 if( q_MSBfirst() ){
00068 write_byte((item>>8) & 0xff ),
00069 write_byte(item & 0xff );
00070 }else{
00071 write_byte(item & 0xff ),
00072 write_byte((item>>8) & 0xff );
00073 }
00074 }
00075
00076
00078 void EndianOut::write_long( const unsigned long item ){
00079 unsigned int t = item;
00080 int i;
00081
00082 if( q_MSBfirst() ){
00083 for(i=24; i>=0; i-=8){
00084 write_byte((t >> i) & 0xff );
00085 }
00086 }else{
00087 for(i=0; i<4; i++){
00088 write_byte(t & 0xff ), t >>= 8;
00089 }
00090 }
00091 }
00092
00093
00095 void EndianOut::write_float( const float item ){
00096 unsigned int t = *(unsigned int *)(&item);
00097 int i;
00098
00099 if( q_MSBfirst() ){
00100 for(i=24; i>=0; i-=8){
00101 write_byte((t >> i) & 0xff );
00102 }
00103 }else{
00104 for(i=0; i<4; i++){
00105 write_byte(t & 0xff ), t >>= 8;
00106 }
00107 }
00108 }
00109
00110
00111 };
00112 };
00113