00001 #include "num_read_write.h" 00002 namespace tpt { 00003 typedef unsigned char uchar; 00004 00005 void 00006 numwrite(std::ostream& out, uint16_t const& x) 00007 { 00008 char buf[2]; 00009 buf[0] = x%256; 00010 buf[1] = (x>>8)%256; 00011 out.write(buf,2); 00012 } 00013 00014 void 00015 numwrite(std::ostream& out, uint32_t const& x) 00016 { 00017 char buf[4]; 00018 buf[0] = x%256; 00019 buf[1] = (x>>8)%256; 00020 buf[2] = (x>>16)%256; 00021 buf[3] = (x>>24)%256; 00022 out.write(buf,4); 00023 } 00024 00025 void 00026 numwrite(std::ostream& out, uint64_t const& x) 00027 { 00028 char buf[8]; 00029 buf[0] = x%256; 00030 buf[1] = (x>>8)%256; 00031 buf[2] = (x>>16)%256; 00032 buf[3] = (x>>24)%256; 00033 buf[4] = (x>>32)%256; 00034 buf[5] = (x>>40)%256; 00035 buf[6] = (x>>48)%256; 00036 buf[7] = (x>>56)%256; 00037 out.write(buf,8); 00038 } 00039 00040 char const* 00041 numread(char const* src, uint16_t & x) 00042 { 00043 uchar const* d = reinterpret_cast<uchar const*>(src); 00044 x = (uint16_t(d[0])<<0) | (uint16_t(d[1])<<8); 00045 return src+2; 00046 } 00047 00048 char const* 00049 numread(char const* src, uint32_t & x) 00050 { 00051 uchar const* d = reinterpret_cast<uchar const*>(src); 00052 x = ((uint32_t(d[0])<<0) | 00053 (uint32_t(d[1])<<8) | 00054 (uint32_t(d[2])<<16)| 00055 (uint32_t(d[3])<<24)); 00056 return src+4; 00057 } 00058 00059 char const* 00060 numread(char const* src, uint64_t & x) 00061 { 00062 uchar const* d = reinterpret_cast<uchar const*>(src); 00063 x = ((uint64_t(d[0])<<0) | 00064 (uint64_t(d[1])<<8) | 00065 (uint64_t(d[2])<<16) | 00066 (uint64_t(d[3])<<24) | 00067 (uint64_t(d[4])<<32) | 00068 (uint64_t(d[5])<<40) | 00069 (uint64_t(d[6])<<48) | 00070 (uint64_t(d[7])<<56)); 00071 return src+8; 00072 } 00073 00074 }