Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
moab::SysUtil Namespace Reference

Functions

void setmem (void *mem, const void *value, unsigned value_size, size_t num_elem)
 Similar to memset, but accepts values larger than 1 char.
long filesize (FILE *filp)
 Get size of file (if it is a regular file)
long filesize (std::ifstream &str)
 Get size of file (if it is a regular file)
void byteswap (void *data, unsigned value_size, size_t num_elem)
 Swap byte order (e.g. change from big-endian to little-endian)
static uint16_t swap_bytes (uint16_t value)
static uint32_t swap_bytes (uint32_t value)
static uint64_t swap_bytes (uint64_t value)
void byteswap2 (void *data, size_t num_elem)
 Alternate byteswap optimized for 2-byte values.
void byteswap4 (void *data, size_t num_elem)
 Alternate byteswap optimized for 4-byte values.
void byteswap8 (void *data, size_t num_elem)
 Alternate byteswap optimized for 8-byte values.
bool little_endian ()
 Check if platform is little-endian.
bool big_endian ()
 Check if platform is big-endian.
template<typename T >
void byteswap (T *data, size_t num_elem)
 Type-specific byte swap.

Variables

const uint64_t m64b1 = 0xFF
const uint64_t m64b2 = m64b1 << 8
const uint64_t m64b3 = m64b1 << 16
const uint64_t m64b4 = m64b1 << 24
const uint64_t m64b5 = m64b1 << 32
const uint64_t m64b6 = m64b1 << 40
const uint64_t m64b7 = m64b1 << 48

Function Documentation

bool moab::SysUtil::big_endian ( ) [inline]

Check if platform is big-endian.

Check if platform is big-endian (least significant byte at lowest memory address.)

Definition at line 61 of file SysUtil.hpp.

Referenced by moab::ReadSTL::load_file(), and moab::WriteSTL::write_file().

    {
        const unsigned one = 1;
        return !( ( (char*)&one )[sizeof( unsigned ) - 1] );
    }
void moab::SysUtil::byteswap ( void *  data,
unsigned  value_size,
size_t  num_elem 
)

Swap byte order (e.g. change from big-endian to little-endian)

Reverse byte order or array of values.

Parameters:
dataPointer to beginning of memory block to modify
values_sizeSize of one value
num_elemNumber of values of size 'value_size' in 'data'

Definition at line 57 of file SysUtil.cpp.

Referenced by moab::ReadSTL::binary_read_triangles(), moab::WriteSTL::binary_write_triangles(), and byteswap().

    {
        char* mem       = reinterpret_cast< char* >( data );
        char* const end = mem + value_size * num_elem;
        for( ; mem < end; mem += value_size )
        {
            unsigned i = 0, j = value_size - 1;
            while( i < j )
                std::swap( mem[i++], mem[j--] );
        }
    }
template<typename T >
void moab::SysUtil::byteswap ( T data,
size_t  num_elem 
) [inline]

Type-specific byte swap.

Definition at line 85 of file SysUtil.hpp.

References byteswap(), byteswap2(), byteswap4(), byteswap8(), and T.

    {
        switch( sizeof( T ) )
        {
            case 1:
                break;
            case 2:
                byteswap2( data, num_elem );
                break;
            case 4:
                byteswap4( data, num_elem );
                break;
            case 8:
                byteswap8( data, num_elem );
                break;
            default:
                byteswap( data, sizeof( T ), num_elem );
                break;
        }
    }
void moab::SysUtil::byteswap2 ( void *  data,
size_t  num_elem 
)

Alternate byteswap optimized for 2-byte values.

Definition at line 128 of file SysUtil.cpp.

References swap_bytes().

Referenced by byteswap().

    {
        uint16_t* mem = reinterpret_cast< uint16_t* >( data );
        uint16_t* end = mem + num_elem;
        for( ; mem < end; ++mem )
            *mem = swap_bytes( *mem );
    }
void moab::SysUtil::byteswap4 ( void *  data,
size_t  num_elem 
)

Alternate byteswap optimized for 4-byte values.

Definition at line 136 of file SysUtil.cpp.

References swap_bytes().

Referenced by byteswap().

    {
        uint32_t* mem = reinterpret_cast< uint32_t* >( data );
        uint32_t* end = mem + num_elem;
        for( ; mem < end; ++mem )
            *mem = swap_bytes( *mem );
    }
void moab::SysUtil::byteswap8 ( void *  data,
size_t  num_elem 
)

Alternate byteswap optimized for 8-byte values.

Definition at line 144 of file SysUtil.cpp.

References swap_bytes().

Referenced by byteswap().

    {
        if( sizeof( void* ) >= 8 )
        {
            uint64_t* mem = reinterpret_cast< uint64_t* >( data );
            uint64_t* end = mem + num_elem;
            for( ; mem < end; ++mem )
                *mem = swap_bytes( *mem );
        }
        else
        {
            uint32_t* mem = reinterpret_cast< uint32_t* >( data );
            uint32_t* end = mem + 2 * num_elem;
            for( ; mem < end; mem += 2 )
            {
                uint32_t tmp = swap_bytes( mem[0] );
                mem[0]       = swap_bytes( mem[1] );
                mem[1]       = tmp;
            }
        }
    }
long moab::SysUtil::filesize ( FILE *  filp)

Get size of file (if it is a regular file)

Get size of regular file.

Returns:
- file size if known
  • -1 if file size cannot be determined (e.g. a pipe)
  • -2 if an unexpected failure occured (may indicate change in file position.)

Definition at line 27 of file SysUtil.cpp.

References length().

Referenced by moab::ReadSTL::binary_read_triangles().

    {
        long curr_pos = ftell( filp );
        if( fseek( filp, 0, SEEK_END ) ) return -1;

        long length = ftell( filp );
        if( fseek( filp, curr_pos, SEEK_SET ) )
        {
            assert( 0 );
            return -2;
        }

        return length;
    }
long moab::SysUtil::filesize ( std::ifstream &  str)

Get size of file (if it is a regular file)

Get size of regular file.

Returns:
- file size if known
  • -1 if file size cannot be determined (e.g. a pipe)
  • -2 if an unexpected failure occured (may indicate change in file position.)

Definition at line 42 of file SysUtil.cpp.

References length().

    {
        std::istream::pos_type curr_pos = str.tellg();
        if( !str.seekg( 0, std::ios_base::end ) ) return -1;

        long length = static_cast< long >( str.tellg() );
        if( !str.seekg( curr_pos, std::ios_base::beg ) )
        {
            assert( 0 );
            return -2;
        }

        return length;
    }
bool moab::SysUtil::little_endian ( ) [inline]

Check if platform is little-endian.

Check if platform is little-endian (least significant byte at highest memory address.)

Definition at line 50 of file SysUtil.hpp.

Referenced by moab::ReadSTL::binary_read_triangles(), moab::WriteSTL::binary_write_triangles(), moab::ReadSTL::load_file(), and moab::WriteSTL::write_file().

    {
        const unsigned one = 1;
        return !*( (char*)&one );
    }
void moab::SysUtil::setmem ( void *  mem,
const void *  value,
unsigned  value_size,
size_t  num_elem 
)

Similar to memset, but accepts values larger than 1 char.

Set block of memory to repeating copies of a sequene of bytes.

Parameters:
memPointer to start of memory block to initialize
valueByte sequence to initialize mem with
value_sizeSize of 'value'
num_elemSize of 'mem' as a multiple of value_size (the number of copies of 'value' to write into 'mem'.)

Definition at line 15 of file SysUtil.cpp.

Referenced by moab::DenseTag::clear_data(), moab::SequenceData::create_data(), moab::MeshTag::get_data(), moab::DenseTag::get_data(), moab::VarLenDenseTag::get_data(), moab::SparseTag::get_data(), and moab::WriteVtk::write_tag().

    {
        if( !num_elem ) return;

        char* array = reinterpret_cast< char* >( mem );
        memcpy( array, value, value_size );
        size_t count;
        for( count = 1; count * 2 < num_elem; count *= 2 )
            memcpy( array + count * value_size, array, count * value_size );
        memcpy( array + count * value_size, array, ( num_elem - count ) * value_size );
    }
static uint16_t moab::SysUtil::swap_bytes ( uint16_t  value) [inline, static]

Definition at line 69 of file SysUtil.cpp.

Referenced by moab::ReadSTL::binary_read_triangles(), moab::WriteSTL::binary_write_triangles(), byteswap2(), byteswap4(), and byteswap8().

    {
        return ( value >> 8 ) | ( value << 8 );
    }
static uint32_t moab::SysUtil::swap_bytes ( uint32_t  value) [inline, static]

Definition at line 74 of file SysUtil.cpp.

    {
        return ( ( value /*& (uint32_t)0xFF000000*/ ) >> 24 ) | ( ( value & (uint32_t)0x00FF0000 ) >> 8 ) |
               ( ( value & (uint32_t)0x0000FF00 ) << 8 ) | ( ( value /*& (uint32_t)0X000000FF*/ ) << 24 );
    }
static uint64_t moab::SysUtil::swap_bytes ( uint64_t  value) [inline, static]

Definition at line 89 of file SysUtil.cpp.

References m64b2, m64b3, m64b4, m64b5, m64b6, and m64b7.

    {
        return ( ( value /*& m64b8*/ ) >> 56 ) | ( ( value & m64b7 ) >> 40 ) | ( ( value & m64b6 ) >> 24 ) |
               ( ( value & m64b5 ) >> 8 ) | ( ( value & m64b4 ) << 8 ) | ( ( value & m64b3 ) << 24 ) |
               ( ( value & m64b2 ) << 40 ) | ( ( value /*& m64b1*/ ) << 56 );
    }

Variable Documentation

const uint64_t moab::SysUtil::m64b1 = 0xFF

Definition at line 80 of file SysUtil.cpp.

const uint64_t moab::SysUtil::m64b2 = m64b1 << 8

Definition at line 81 of file SysUtil.cpp.

Referenced by swap_bytes().

const uint64_t moab::SysUtil::m64b3 = m64b1 << 16

Definition at line 82 of file SysUtil.cpp.

Referenced by swap_bytes().

const uint64_t moab::SysUtil::m64b4 = m64b1 << 24

Definition at line 83 of file SysUtil.cpp.

Referenced by swap_bytes().

const uint64_t moab::SysUtil::m64b5 = m64b1 << 32

Definition at line 84 of file SysUtil.cpp.

Referenced by swap_bytes().

const uint64_t moab::SysUtil::m64b6 = m64b1 << 40

Definition at line 85 of file SysUtil.cpp.

Referenced by swap_bytes().

const uint64_t moab::SysUtil::m64b7 = m64b1 << 48

Definition at line 86 of file SysUtil.cpp.

Referenced by swap_bytes().

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines