EMAN2
Static Public Member Functions | Static Private Attributes
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]
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 data, and is_host_big_endian().

                {
                        if (is_host_big_endian()) {
                                swap_bytes < T > (data, n);
                        }
                }
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::DM4IO::is_valid(), and EMAN::Df3IO::is_valid().

                {
                        if (!small_num_addr) {
                                return false;
                        }

                        bool data_big_endian = false;
                        size_t typesize = sizeof(T);
                        char *d = (char *) small_num_addr;

                        if (is_host_big_endian()) {
                                data_big_endian = false;
                                for (size_t i = typesize / 2; i < typesize; i++)
                                        {
                                                if (d[i] != 0) {
                                                        data_big_endian = true;
                                                        break;
                                                }
                                        }
                        }
                        else {
                                data_big_endian = true;
                                for (size_t j = 0; j < typesize / 2; j++) {
                                        if (d[j] != 0) {
                                                data_big_endian = false;
                                                break;
                                        }
                                }
                        }

                        return data_big_endian;
                }
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().

{
        bool is_big = false;
        
        if (Util::goodf(&f) && f > 0 && f < 65535.0 && f == floor(f)) {
                is_big = is_host_big_endian();
        }
        else {
                is_big = !is_host_big_endian();
        }

        return is_big;
}
bool ByteOrder::is_host_big_endian ( ) [static]
template<class T >
static void EMAN::ByteOrder::swap_bytes ( T *  data,
size_t  n = 1 
) [inline, static]

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: