Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
SysUtil.hpp
Go to the documentation of this file.
1 #ifndef MB_SYS_UTIL_HPP
2 #define MB_SYS_UTIL_HPP
3 
4 #include <cstring> // for size_t
5 #include <cstdio>
6 #include <iosfwd>
7 
8 namespace moab
9 {
10 
11 namespace SysUtil
12 {
13 
14  /**\brief Similar to memset, but accepts values larger than 1 char
15  *
16  * Set block of memory to repeating copies of a sequene of bytes.
17  *\param mem Pointer to start of memory block to initialize
18  *\param value Byte sequence to initialize mem with
19  *\param value_size Size of 'value'
20  *\param num_elem Size of 'mem' as a multiple of value_size (the number of
21  * copies of 'value' to write into 'mem'.)
22  */
23  void setmem( void* mem, const void* value, unsigned value_size, size_t num_elem );
24 
25  /**\brief Get size of file (if it is a regular file)
26  *
27  * Get size of regular file.
28  *\return - file size if known
29  * - -1 if file size cannot be determined (e.g. a pipe)
30  * - -2 if an unexpected failure occured (may indicate change
31  * in file position.)
32  */
33  long filesize( FILE* filp );
34 
35  /**\brief Get size of file (if it is a regular file)
36  *
37  * Get size of regular file.
38  *\return - file size if known
39  * - -1 if file size cannot be determined (e.g. a pipe)
40  * - -2 if an unexpected failure occured (may indicate change
41  * in file position.)
42  */
43  long filesize( std::ifstream& str );
44 
45  /**\brief Check if platform is little-endian
46  *
47  * Check if platform is little-endian (least significant
48  * byte at highest memory address.)
49  */
50  inline bool little_endian()
51  {
52  const unsigned one = 1;
53  return !*( (char*)&one );
54  }
55 
56  /**\brief Check if platform is big-endian
57  *
58  * Check if platform is big-endian (least significant
59  * byte at lowest memory address.)
60  */
61  inline bool big_endian()
62  {
63  const unsigned one = 1;
64  return !( ( (char*)&one )[sizeof( unsigned ) - 1] );
65  }
66 
67  /**\brief Swap byte order (e.g. change from big-endian to little-endian)
68  *
69  * Reverse byte order or array of values.
70  *\param data Pointer to beginning of memory block to modify
71  *\param values_size Size of one value
72  *\param num_elem Number of values of size 'value_size' in 'data'
73  */
74  void byteswap( void* data, unsigned value_size, size_t num_elem );
75 
76  /**\brief Alternate byteswap optimized for 2-byte values */
77  void byteswap2( void* data, size_t num_elem );
78  /**\brief Alternate byteswap optimized for 4-byte values */
79  void byteswap4( void* data, size_t num_elem );
80  /**\brief Alternate byteswap optimized for 8-byte values */
81  void byteswap8( void* data, size_t num_elem );
82 
83  /**\brief Type-specific byte swap */
84  template < typename T >
85  inline void byteswap( T* data, size_t num_elem )
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  }
105 
106 } // namespace SysUtil
107 
108 } // namespace moab
109 
110 #endif