EMAN::ByteOrder Class Reference

ByteOrder defines functions to work on big/little endian byte orders. More...

#include <byteorder.h>

List of all members.

Static Public Member Functions

static bool is_host_big_endian ()
static bool is_float_big_endian (float small_number)
 given a small floating number, return whether the number is in big endian or not.
template<class T>
static bool is_data_big_endian (const T *small_num_addr)
 given a pointer to a reasonable small integer number, return whether the number is big endian or not.
template<class T>
static void become_big_endian (T *data, size_t n=1)
 convert data from host byte order to big endian order.
template<class T>
static void become_little_endian (T *data, size_t n=1)
 convert data from host byte order to little endian order.
template<class T>
static void swap_bytes (T *data, size_t n=1)
 swap the byte order of data with 'n' T-type elements.

Static Private Attributes

static bool is_host_endian_checked = false
static bool host_big_endian = false


Detailed Description

ByteOrder defines functions to work on big/little endian byte orders.

The byte order is the order in which bytes are stored to create larger data types such as the int and long values. Different kinds of computers use different byte order conventions.

There are 2 major byte orders: big-endian and little-endian.

big-endian (like SGI) store the most significant bytes (i.e. the bytes that hold the largest part of the value) first. little-endian (like x86) store the most significant byte last.

The host byte order is the byte order used on the current host.

ByteOrder class defines functions for checking running-host byte-order, checking data byte-order, convert data from one byte-order to another byte-order.

Definition at line 66 of file byteorder.h.


Member Function Documentation

template<class T>
static void EMAN::ByteOrder::become_big_endian ( T *  data,
size_t  n = 1 
) [inline, static]

convert data from host byte order to big endian order.

'n' is the number of elements of type T.

Definition at line 120 of file byteorder.h.

References is_host_big_endian().

Referenced by EMAN::DM3IO::is_valid(), EMAN::GatanDM4::TagGroup::read(), EMAN::GatanDM4::TagEntry::read(), EMAN::GatanDM4::TagData::read(), EMAN::Gatan::TagEntry::read(), EMAN::Gatan::TagGroup::read(), EMAN::Gatan::TagData::read(), EMAN::GatanDM4::TagData::read_any(), EMAN::Gatan::TagData::read_any(), EMAN::GatanDM4::TagData::read_array_data(), EMAN::Gatan::TagData::read_array_data(), EMAN::GatanDM4::TagData::read_array_types(), EMAN::Gatan::TagData::read_array_types(), EMAN::GatanDM4::TagData::read_struct_types(), and EMAN::Gatan::TagData::read_struct_types().

00121                 {
00122                         if (!is_host_big_endian()) {
00123                                 swap_bytes < T > (data, n);
00124                         }
00125                 }

template<class T>
static void EMAN::ByteOrder::become_little_endian ( T *  data,
size_t  n = 1 
) [inline, static]

convert data from host byte order to little endian order.

'n' is the number of elements of type T.

Definition at line 130 of file byteorder.h.

References is_host_big_endian().

00131                 {
00132                         if (is_host_big_endian()) {
00133                                 swap_bytes < T > (data, n);
00134                         }
00135                 }

template<class T>
static bool EMAN::ByteOrder::is_data_big_endian ( const T *  small_num_addr  )  [inline, static]

given a pointer to a reasonable small integer number, return whether the number is big endian or not.

For a n-bit integer, the number should < (2 ^ (n/2)). e.g., for 'int', number should < 65535; for 'short', number should < 255.

Definition at line 84 of file byteorder.h.

References is_host_big_endian().

Referenced by EMAN::MrcIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::EmIO::is_valid(), EMAN::EmimIO::is_valid(), EMAN::DM4IO::is_valid(), and EMAN::Df3IO::is_valid().

00085                 {
00086                         if (!small_num_addr) {
00087                                 return false;
00088                         }
00089 
00090                         bool data_big_endian = false;
00091                         size_t typesize = sizeof(T);
00092                         char *d = (char *) small_num_addr;
00093 
00094                         if (is_host_big_endian()) {
00095                                 data_big_endian = false;
00096                                 for (size_t i = typesize / 2; i < typesize; i++)
00097                                         {
00098                                                 if (d[i] != 0) {
00099                                                         data_big_endian = true;
00100                                                         break;
00101                                                 }
00102                                         }
00103                         }
00104                         else {
00105                                 data_big_endian = true;
00106                                 for (size_t j = 0; j < typesize / 2; j++) {
00107                                         if (d[j] != 0) {
00108                                                 data_big_endian = false;
00109                                                 break;
00110                                         }
00111                                 }
00112                         }
00113 
00114                         return data_big_endian;
00115                 }

bool ByteOrder::is_float_big_endian ( float  small_number  )  [static]

given a small floating number, return whether the number is in big endian or not.

If a number is smaller than 65535, it is defined as a "small" number here.

Definition at line 63 of file byteorder.cpp.

References EMAN::Util::goodf(), and is_host_big_endian().

Referenced by EMAN::SingleSpiderIO::is_valid(), and EMAN::SpiderIO::is_valid().

00064 {
00065         bool is_big = false;
00066         
00067         if (Util::goodf(&f) && f > 0 && f < 65535.0 && f == floor(f)) {
00068                 is_big = is_host_big_endian();
00069         }
00070         else {
00071                 is_big = !is_host_big_endian();
00072         }
00073 
00074         return is_big;
00075 }

bool ByteOrder::is_host_big_endian (  )  [static]

Definition at line 44 of file byteorder.cpp.

References host_big_endian, and is_host_endian_checked.

Referenced by become_big_endian(), EMAN::ImageIO::become_host_endian(), EMAN::GatanDM4::TagTable::become_host_endian(), EMAN::Gatan::TagTable::become_host_endian(), become_little_endian(), EMAN::DM3IO::DM3IO(), EMAN::DM4IO::DM4IO(), EMAN::EmimIO::EmimIO(), EMAN::EmIO::EmIO(), EMAN::FitsIO::FitsIO(), EMAN::Gatan2IO::Gatan2IO(), EMAN::MrcIO::generate_machine_stamp(), EMAN::IcosIO::IcosIO(), EMAN::ImagicIO::ImagicIO(), EMAN::ImagicIO2::ImagicIO2(), is_data_big_endian(), is_float_big_endian(), EMAN::SingleSpiderIO::is_valid(), EMAN::SpiderIO::is_valid(), EMAN::PifIO::is_valid(), EMAN::OmapIO::is_valid(), EMAN::MrcIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::EmIO::is_valid(), EMAN::EmimIO::is_valid(), EMAN::DM4IO::is_valid(), EMAN::LstFastIO::LstFastIO(), EMAN::LstIO::LstIO(), EMAN::ImagicIO2::make_header_host_endian(), EMAN::ImagicIO::make_header_host_endian(), EMAN::MrcIO::MrcIO(), EMAN::SpiderIO::need_swap(), EMAN::OmapIO::OmapIO(), EMAN::PifIO::PifIO(), EMAN::Util::recv_broadcast(), EMAN::EMData::save_byteorder_to_dict(), EMAN::VtkIO::VtkIO(), EMAN::XplorIO::XplorIO(), and EMAN::XYZIO::XYZIO().

00045 {
00046         if (!is_host_endian_checked) {
00047                 int one = 1;
00048                 char *p_one = (char *) (&one);
00049 
00050                 if (p_one[0] == 1 && p_one[1] == 0 && p_one[2] == 0 && p_one[3] == 0) {
00051                         host_big_endian = false;
00052                 }
00053                 else {
00054                         host_big_endian = true;
00055                 }
00056 
00057                 is_host_endian_checked = true;
00058         }
00059 
00060         return host_big_endian;
00061 }

template<class T>
static void EMAN::ByteOrder::swap_bytes ( T *  data,
size_t  n = 1 
) [inline, static]

swap the byte order of data with 'n' T-type elements.

Definition at line 139 of file byteorder.h.

Referenced by EMAN::ImageIO::become_host_endian(), EMAN::GatanDM4::TagTable::become_host_endian(), EMAN::Gatan::TagTable::become_host_endian(), EMAN::SingleSpiderIO::is_valid(), EMAN::SpiderIO::is_valid(), EMAN::PifIO::is_valid(), EMAN::OmapIO::is_valid(), EMAN::MrcIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::EmIO::is_valid(), EMAN::EmimIO::is_valid(), EMAN::DM4IO::is_valid(), EMAN::SpiderIO::swap_data(), EMAN::SpiderIO::swap_header(), EMAN::MrcIO::swap_header(), EMAN::ImagicIO2::swap_header(), EMAN::ImagicIO::swap_header(), EMAN::SpiderIO::write_single_data(), and EMAN::SpiderIO::write_single_header().

00140                 {
00141                         unsigned char s;
00142                         size_t p = sizeof(T);
00143                         char *d = (char *) data;
00144 
00145                         if (p > 1) {
00146                                 for (size_t i = 0; i < n; i++, d += p) {
00147                                         for (size_t j = 0; j < p / 2; j++) {
00148                                                 s = d[j];
00149                                                 d[j] = d[p - 1 - j];
00150                                                 d[p - 1 - j] = s;
00151                                         }
00152                                 }
00153                         }
00154                 }


Member Data Documentation

bool ByteOrder::host_big_endian = false [static, private]

Definition at line 158 of file byteorder.h.

Referenced by is_host_big_endian().

bool ByteOrder::is_host_endian_checked = false [static, private]

Definition at line 157 of file byteorder.h.

Referenced by is_host_big_endian().


The documentation for this class was generated from the following files:
Generated on Tue Jun 11 12:42:55 2013 for EMAN2 by  doxygen 1.4.7