MOAB: Mesh Oriented datABase
(version 5.4.1)
|
00001 #ifndef MB_SYS_UTIL_HPP 00002 #define MB_SYS_UTIL_HPP 00003 00004 #include <cstring> // for size_t 00005 #include <cstdio> 00006 #include <iosfwd> 00007 00008 namespace moab 00009 { 00010 00011 namespace SysUtil 00012 { 00013 00014 /**\brief Similar to memset, but accepts values larger than 1 char 00015 * 00016 * Set block of memory to repeating copies of a sequene of bytes. 00017 *\param mem Pointer to start of memory block to initialize 00018 *\param value Byte sequence to initialize mem with 00019 *\param value_size Size of 'value' 00020 *\param num_elem Size of 'mem' as a multiple of value_size (the number of 00021 * copies of 'value' to write into 'mem'.) 00022 */ 00023 void setmem( void* mem, const void* value, unsigned value_size, size_t num_elem ); 00024 00025 /**\brief Get size of file (if it is a regular file) 00026 * 00027 * Get size of regular file. 00028 *\return - file size if known 00029 * - -1 if file size cannot be determined (e.g. a pipe) 00030 * - -2 if an unexpected failure occured (may indicate change 00031 * in file position.) 00032 */ 00033 long filesize( FILE* filp ); 00034 00035 /**\brief Get size of file (if it is a regular file) 00036 * 00037 * Get size of regular file. 00038 *\return - file size if known 00039 * - -1 if file size cannot be determined (e.g. a pipe) 00040 * - -2 if an unexpected failure occured (may indicate change 00041 * in file position.) 00042 */ 00043 long filesize( std::ifstream& str ); 00044 00045 /**\brief Check if platform is little-endian 00046 * 00047 * Check if platform is little-endian (least significant 00048 * byte at highest memory address.) 00049 */ 00050 inline bool little_endian() 00051 { 00052 const unsigned one = 1; 00053 return !*( (char*)&one ); 00054 } 00055 00056 /**\brief Check if platform is big-endian 00057 * 00058 * Check if platform is big-endian (least significant 00059 * byte at lowest memory address.) 00060 */ 00061 inline bool big_endian() 00062 { 00063 const unsigned one = 1; 00064 return !( ( (char*)&one )[sizeof( unsigned ) - 1] ); 00065 } 00066 00067 /**\brief Swap byte order (e.g. change from big-endian to little-endian) 00068 * 00069 * Reverse byte order or array of values. 00070 *\param data Pointer to beginning of memory block to modify 00071 *\param values_size Size of one value 00072 *\param num_elem Number of values of size 'value_size' in 'data' 00073 */ 00074 void byteswap( void* data, unsigned value_size, size_t num_elem ); 00075 00076 /**\brief Alternate byteswap optimized for 2-byte values */ 00077 void byteswap2( void* data, size_t num_elem ); 00078 /**\brief Alternate byteswap optimized for 4-byte values */ 00079 void byteswap4( void* data, size_t num_elem ); 00080 /**\brief Alternate byteswap optimized for 8-byte values */ 00081 void byteswap8( void* data, size_t num_elem ); 00082 00083 /**\brief Type-specific byte swap */ 00084 template < typename T > 00085 inline void byteswap( T* data, size_t num_elem ) 00086 { 00087 switch( sizeof( T ) ) 00088 { 00089 case 1: 00090 break; 00091 case 2: 00092 byteswap2( data, num_elem ); 00093 break; 00094 case 4: 00095 byteswap4( data, num_elem ); 00096 break; 00097 case 8: 00098 byteswap8( data, num_elem ); 00099 break; 00100 default: 00101 byteswap( data, sizeof( T ), num_elem ); 00102 break; 00103 } 00104 } 00105 00106 } // namespace SysUtil 00107 00108 } // namespace moab 00109 00110 #endif