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/EndianIn.h"
00026 #include "Teddy/SysSupport/Exception.h"
00027
00028
00029 namespace Teddy {
00030 namespace SysSupport {
00031
00032
00034 EndianIn::EndianIn( const std::string &file_name ){
00035 open( file_name );
00036 }
00037
00038
00040 EndianIn::~EndianIn(){
00041 close();
00042 }
00043
00044
00046 void EndianIn::open( const std::string &file_name ){
00047 ifs = new ifstream( file_name.c_str(), ios::in|ios::binary );
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 }
00062
00063
00065 void EndianIn::close(){
00066
00067 if( ifs != NULL ){
00068 ifs->close(), ifs = NULL;
00069 delete ifs;
00070 ifs = NULL;
00071 }
00072 }
00073
00074
00076 unsigned long EndianIn::len(){
00077 streampos original = ifs->tellg();
00078 ifs->seekg( 0, ios::end );
00079 unsigned long l = ifs->tellg();
00080 ifs->seekg( original, ios::beg );
00081 return l;
00082 }
00083
00084
00085 void EndianIn::read_all( char *buf ){
00086 ifs->read( buf, len() );
00087 }
00088
00089
00091 unsigned char EndianIn::read_byte(){
00092 int C;
00093 unsigned char c = 0;
00094
00095 C = ifs->get();
00096 if( C == EOF ){
00097 throw( Exception("EOF read error") );
00098 }else{
00099 c = (unsigned char)C;
00100 }
00101 return c;
00102 }
00103
00104
00106 unsigned short int EndianIn::read_short(){
00107 unsigned char c1, c2;
00108
00109 c1 = read_byte();
00110 c2 = read_byte();
00111
00112 if( q_MSBfirst() ){
00113 return (c1 << 8) | c2;
00114 }else{
00115 return (c2 << 8) | c1;
00116 }
00117 }
00118
00119
00121 unsigned long int EndianIn::read_long(){
00122 unsigned char c1, c2, c3, c4;
00123
00124 c1 = read_byte();
00125 c2 = read_byte();
00126 c3 = read_byte();
00127 c4 = read_byte();
00128
00129 if ( q_MSBfirst() ){
00130 return (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
00131 }else{
00132 return (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
00133 }
00134 }
00135
00136
00138 float EndianIn::read_float(){
00139 float ret_val;
00140 long *shadow = (long*)(&ret_val);
00141 unsigned char c1, c2, c3, c4;
00142
00143 c1 = read_byte();
00144 c2 = read_byte();
00145 c3 = read_byte();
00146 c4 = read_byte();
00147
00148 if ( q_MSBfirst() ){
00149 *shadow = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
00150 }else{
00151 *shadow = (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
00152 }
00153
00154 return ret_val;
00155 }
00156
00157
00158 };
00159 };
00160
00161
00162