Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
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. More...
 
long filesize (FILE *filp)
 Get size of file (if it is a regular file) More...
 
long filesize (std::ifstream &str)
 Get size of file (if it is a regular file) More...
 
void byteswap (void *data, unsigned value_size, size_t num_elem)
 Swap byte order (e.g. change from big-endian to little-endian) More...
 
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. More...
 
void byteswap4 (void *data, size_t num_elem)
 Alternate byteswap optimized for 4-byte values. More...
 
void byteswap8 (void *data, size_t num_elem)
 Alternate byteswap optimized for 8-byte values. More...
 
bool little_endian ()
 Check if platform is little-endian. More...
 
bool big_endian ()
 Check if platform is big-endian. More...
 
template<typename T >
void byteswap (T *data, size_t num_elem)
 Type-specific byte swap. More...
 

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

◆ big_endian()

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.

62  {
63  const unsigned one = 1;
64  return !( ( (char*)&one )[sizeof( unsigned ) - 1] );
65  }

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

◆ byteswap() [1/2]

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.

86  {
87  switch( sizeof( T ) )
88  {
89  case 1:
90  break;
91  case 2:
92  byteswap2( data, num_elem );
93  break;
94  case 4:
95  byteswap4( data, num_elem );
96  break;
97  case 8:
98  byteswap8( data, num_elem );
99  break;
100  default:
101  byteswap( data, sizeof( T ), num_elem );
102  break;
103  }
104  }

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

◆ byteswap() [2/2]

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.

58  {
59  char* mem = reinterpret_cast< char* >( data );
60  char* const end = mem + value_size * num_elem;
61  for( ; mem < end; mem += value_size )
62  {
63  unsigned i = 0, j = value_size - 1;
64  while( i < j )
65  std::swap( mem[i++], mem[j--] );
66  }
67  }

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

◆ byteswap2()

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.

129  {
130  uint16_t* mem = reinterpret_cast< uint16_t* >( data );
131  uint16_t* end = mem + num_elem;
132  for( ; mem < end; ++mem )
133  *mem = swap_bytes( *mem );
134  }

References swap_bytes().

Referenced by byteswap().

◆ byteswap4()

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.

137  {
138  uint32_t* mem = reinterpret_cast< uint32_t* >( data );
139  uint32_t* end = mem + num_elem;
140  for( ; mem < end; ++mem )
141  *mem = swap_bytes( *mem );
142  }

References swap_bytes().

Referenced by byteswap().

◆ byteswap8()

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.

145  {
146  if( sizeof( void* ) >= 8 )
147  {
148  uint64_t* mem = reinterpret_cast< uint64_t* >( data );
149  uint64_t* end = mem + num_elem;
150  for( ; mem < end; ++mem )
151  *mem = swap_bytes( *mem );
152  }
153  else
154  {
155  uint32_t* mem = reinterpret_cast< uint32_t* >( data );
156  uint32_t* end = mem + 2 * num_elem;
157  for( ; mem < end; mem += 2 )
158  {
159  uint32_t tmp = swap_bytes( mem[0] );
160  mem[0] = swap_bytes( mem[1] );
161  mem[1] = tmp;
162  }
163  }
164  }

References swap_bytes().

Referenced by byteswap().

◆ filesize() [1/2]

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.

28  {
29  long curr_pos = ftell( filp );
30  if( fseek( filp, 0, SEEK_END ) ) return -1;
31 
32  long length = ftell( filp );
33  if( fseek( filp, curr_pos, SEEK_SET ) )
34  {
35  assert( 0 );
36  return -2;
37  }
38 
39  return length;
40  }

References length().

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

◆ filesize() [2/2]

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.

43  {
44  std::istream::pos_type curr_pos = str.tellg();
45  if( !str.seekg( 0, std::ios_base::end ) ) return -1;
46 
47  long length = static_cast< long >( str.tellg() );
48  if( !str.seekg( curr_pos, std::ios_base::beg ) )
49  {
50  assert( 0 );
51  return -2;
52  }
53 
54  return length;
55  }

References length().

◆ little_endian()

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.

51  {
52  const unsigned one = 1;
53  return !*( (char*)&one );
54  }

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

◆ setmem()

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.

16  {
17  if( !num_elem ) return;
18 
19  char* array = reinterpret_cast< char* >( mem );
20  memcpy( array, value, value_size );
21  size_t count;
22  for( count = 1; count * 2 < num_elem; count *= 2 )
23  memcpy( array + count * value_size, array, count * value_size );
24  memcpy( array + count * value_size, array, ( num_elem - count ) * value_size );
25  }

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

◆ swap_bytes() [1/3]

static uint16_t moab::SysUtil::swap_bytes ( uint16_t  value)
inlinestatic

Definition at line 69 of file SysUtil.cpp.

70  {
71  return ( value >> 8 ) | ( value << 8 );
72  }

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

◆ swap_bytes() [2/3]

static uint32_t moab::SysUtil::swap_bytes ( uint32_t  value)
inlinestatic

Definition at line 74 of file SysUtil.cpp.

75  {
76  return ( ( value /*& (uint32_t)0xFF000000*/ ) >> 24 ) | ( ( value & (uint32_t)0x00FF0000 ) >> 8 ) |
77  ( ( value & (uint32_t)0x0000FF00 ) << 8 ) | ( ( value /*& (uint32_t)0X000000FF*/ ) << 24 );
78  }

◆ swap_bytes() [3/3]

static uint64_t moab::SysUtil::swap_bytes ( uint64_t  value)
inlinestatic

Definition at line 89 of file SysUtil.cpp.

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

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

Variable Documentation

◆ m64b1

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

Definition at line 80 of file SysUtil.cpp.

◆ m64b2

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

Definition at line 81 of file SysUtil.cpp.

Referenced by swap_bytes().

◆ m64b3

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

Definition at line 82 of file SysUtil.cpp.

Referenced by swap_bytes().

◆ m64b4

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

Definition at line 83 of file SysUtil.cpp.

Referenced by swap_bytes().

◆ m64b5

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

Definition at line 84 of file SysUtil.cpp.

Referenced by swap_bytes().

◆ m64b6

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

Definition at line 85 of file SysUtil.cpp.

Referenced by swap_bytes().

◆ m64b7

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

Definition at line 86 of file SysUtil.cpp.

Referenced by swap_bytes().