1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#ifndef MB_SYS_UTIL_HPP
#define MB_SYS_UTIL_HPP

#include <cstring>  // for size_t
#include <cstdio>
#include <iosfwd>

namespace moab
{

namespace SysUtil
{

    /**\brief Similar to memset, but accepts values larger than 1 char
     *
     * Set block of memory to repeating copies of a sequene of bytes.
     *\param mem   Pointer to start of memory block to initialize
     *\param value Byte sequence to initialize mem with
     *\param value_size Size of 'value'
     *\param num_elem Size of 'mem' as a multiple of value_size (the number of
     *             copies of 'value' to write into 'mem'.)
     */
    void setmem( void* mem, const void* value, unsigned value_size, size_t num_elem );

    /**\brief Get size of file (if it is a regular file)
     *
     * Get size of regular file.
     *\return - 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.)
     */
    long filesize( FILE* filp );

    /**\brief Get size of file (if it is a regular file)
     *
     * Get size of regular file.
     *\return - 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.)
     */
    long filesize( std::ifstream& str );

    /**\brief Check if platform is little-endian
     *
     * Check if platform is little-endian (least significant
     * byte at highest memory address.)
     */
    inline bool little_endian()
    {
        const unsigned one = 1;
        return !*( (char*)&one );
    }

    /**\brief Check if platform is big-endian
     *
     * Check if platform is big-endian (least significant
     * byte at lowest memory address.)
     */
    inline bool big_endian()
    {
        const unsigned one = 1;
        return !( ( (char*)&one )[sizeof( unsigned ) - 1] );<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.<--- The address of local variable 'one' is accessed at non-zero index.<--- Address of variable taken here.
    }

    /**\brief Swap byte order (e.g. change from big-endian to little-endian)
     *
     * Reverse byte order or array of values.
     *\param data        Pointer to beginning of memory block to modify
     *\param values_size Size of one value
     *\param num_elem    Number of values of size 'value_size' in 'data'
     */
    void byteswap( void* data, unsigned value_size, size_t num_elem );

    /**\brief Alternate byteswap optimized for 2-byte values */
    void byteswap2( void* data, size_t num_elem );
    /**\brief Alternate byteswap optimized for 4-byte values */
    void byteswap4( void* data, size_t num_elem );
    /**\brief Alternate byteswap optimized for 8-byte values */
    void byteswap8( void* data, size_t num_elem );

    /**\brief Type-specific byte swap */
    template < typename T >
    inline void byteswap( T* data, size_t num_elem )
    {
        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;
        }
    }

}  // namespace SysUtil

}  // namespace moab

#endif