Utils.h Source File

Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members

# Utils.h

Go to the documentation of this file.

    00001 // ZenLib::Utils - Very small utilities
    00002 // Copyright (C) 2002-2006 Jerome Martinez, Zen@MediaArea.net
    00003 //
    00004 // This software is provided 'as-is', without any express or implied
    00005 // warranty.  In no event will the authors be held liable for any damages
    00006 // arising from the use of this software.
    00007 //
    00008 // Permission is granted to anyone to use this software for any purpose,
    00009 // including commercial applications, and to alter it and redistribute it
    00010 // freely, subject to the following restrictions:
    00011 //
    00012 // 1. The origin of this software must not be misrepresented; you must not
    00013 //    claim that you wrote the original software. If you use this software
    00014 //    in a product, an acknowledgment in the product documentation would be
    00015 //    appreciated but is not required.
    00016 // 2. Altered source versions must be plainly marked as such, and must not be
    00017 //    misrepresented as being the original software.
    00018 // 3. This notice may not be removed or altered from any source distribution.
    00019 //
    00020 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    00021 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    00022 // ZenUtils
    00023 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    00024 //
    00025 // Integer and float manipulation
    00026 //
    00027 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    00028
    00029 //---------------------------------------------------------------------------
    00030 #ifndef ZenUtilsH
    00031 #define ZenUtilsH
    00032 //---------------------------------------------------------------------------
    00033
    00034 //---------------------------------------------------------------------------
    00035 #include <ZenLib/Conf.h>
    00036 //---------------------------------------------------------------------------
    00037
    00038 namespace ZenLib
    00039 {
    00040
    00041 //***************************************************************************
    00042 // Integer transformations
    00043 //***************************************************************************
    00044
    00045 //---------------------------------------------------------------------------
    00046 //Little Endians
    00047 int8s  LittleEndian2int8s  (const char* List);
    00048 int8u  LittleEndian2int8u  (const char* List);
    00049 int16s LittleEndian2int16s (const char* List);
    00050 int16u LittleEndian2int16u (const char* List);
    00051 int32s LittleEndian2int32s (const char* List);
    00052 int32u LittleEndian2int32u (const char* List);
    00053 #if (MAXTYPE_INT >= 64)
    00054 int64s LittleEndian2int64s (const char* List);
    00055 int64u LittleEndian2int64u (const char* List);
    00056 #endif
    00057 #ifndef __BORLANDC__
    00058 inline int8s  LittleEndian2int8s  (const int8u* List) {return LittleEndian2int8s  ((const char*)List);}
    00059 inline int8u  LittleEndian2int8u  (const int8u* List) {return LittleEndian2int8u  ((const char*)List);}
    00060 inline int16s LittleEndian2int16s (const int8u* List) {return LittleEndian2int16s ((const char*)List);}
    00061 inline int16u LittleEndian2int16u (const int8u* List) {return LittleEndian2int16u ((const char*)List);}
    00062 inline int32s LittleEndian2int32s (const int8u* List) {return LittleEndian2int32s ((const char*)List);}
    00063 inline int32u LittleEndian2int32u (const int8u* List) {return LittleEndian2int32u ((const char*)List);}
    00064 #if (MAXTYPE_INT >= 64)
    00065 inline int64s LittleEndian2int64s (const int8u* List) {return LittleEndian2int64s ((const char*)List);}
    00066 inline int64u LittleEndian2int64u (const int8u* List) {return LittleEndian2int64u ((const char*)List);}
    00067 #endif
    00068 #endif //__BORLANDC__
    00069 //---------------------------------------------------------------------------
    00070 //Big Endians
    00071 int8s  BigEndian2int8s     (const char* List);
    00072 int8u  BigEndian2int8u     (const char* List);
    00073 int16s BigEndian2int16s    (const char* List);
    00074 int16u BigEndian2int16u    (const char* List);
    00075 int32s BigEndian2int32s    (const char* List);
    00076 int32u BigEndian2int32u    (const char* List);
    00077 #if (MAXTYPE_INT >= 64)
    00078 int64s BigEndian2int64s    (const char* List);
    00079 int64u BigEndian2int64u    (const char* List);
    00080 #endif
    00081 #ifndef __BORLANDC__
    00082 inline int8s  BigEndian2int8s     (const int8u* List) {return BigEndian2int8s     ((const char*)List);}
    00083 inline int8u  BigEndian2int8u     (const int8u* List) {return BigEndian2int8u     ((const char*)List);}
    00084 inline int16s BigEndian2int16s    (const int8u* List) {return BigEndian2int16s    ((const char*)List);}
    00085 inline int16u BigEndian2int16u    (const int8u* List) {return BigEndian2int16u    ((const char*)List);}
    00086 inline int32s BigEndian2int32s    (const int8u* List) {return BigEndian2int32s    ((const char*)List);}
    00087 inline int32u BigEndian2int32u    (const int8u* List) {return BigEndian2int32u    ((const char*)List);}
    00088 #if (MAXTYPE_INT >= 64)
    00089 inline int64s BigEndian2int64s    (const int8u* List) {return BigEndian2int64s    ((const char*)List);}
    00090 inline int64u BigEndian2int64u    (const int8u* List) {return BigEndian2int64u    ((const char*)List);}
    00091 #endif
    00092 #endif //__BORLANDC__
    00093
    00094 //---------------------------------------------------------------------------
    00095 // int32 - int64
    00096 #if (MAXTYPE_INT >= 64)
    00097     int64s int32s_int64s (                int32s  High, int32u  Low);
    00098     int64u int32u_int64u (                int32u  High, int32u  Low);
    00099     void   int32s_int64s (int64s &BigInt, int32s  High, int32u  Low);
    00100     void   int32u_int64u (int64s &BigInt, int32u  High, int32u  Low);
    00101     void   int64s_int32s (int64s  BigInt, int32s &High, int32u &Low);
    00102     void   int64u_int32u (int64u  BigInt, int32u &High, int32u &Low);
    00103 #endif
    00104
    00105 //---------------------------------------------------------------------------
    00106 // Floats and ints
    00107 int32s float32_int32s  (float32 F, bool Rounded=true);
    00108 int64s float32_int64s  (float32 F, bool Rounded=true);
    00109 int32s float64_int32s  (float64 F, bool Rounded=true);
    00110 int64s float64_int64s  (float64 F, bool Rounded=true);
    00111
    00112 // These functions are used because MSVC6 isn't able to convert an unsigned int64 to a floating-point value, and I couldn't think of a cleaner way to handle it.
    00113 #if _MSC_VER <= 1200
    00114     inline float32 int64u_float32 (int64u v) {return static_cast<float32>(static_cast<int64s>(v>>1))*2.0f + static_cast<float32>(static_cast<int64s>(v & 1));}
    00115     inline float64 int64u_float64 (int64u v) {return static_cast<float64>(static_cast<int64s>(v>>1))*2.0f + static_cast<float32>(static_cast<int64s>(v & 1));}
    00116 #else
    00117     #pragma
    00118     #pragma warning( disable : 4244 )
    00119     inline float32 int64u_float32 (int64u v) {return v;}
    00120     inline float64 int64u_float64 (int64u v) {return v;}
    00121     #pragma warning( default : 4244 )
    00122 #endif // _MSC_VER <= 1200
    00123
    00124 //---------------------------------------------------------------------------
    00125 // CC (often used in all containers to identify a stream
    00126 #if (MAXTYPE_INT >= 64)
    00127 inline int64u CC8(const char*  C) {return C[7]<<0 | C[6]<<8 | C[5]<<16 | C[4]<<24 | ((int64u)C[3])<<32 | ((int64u)C[2])<<40 | ((int64u)C[1])<<48 | ((int64u)C[0])<<56;}
    00128 inline int64u CC7(const char*  C) {return C[6]<<0 | C[5]<<8 | C[4]<<16 | C[3]<<24 | ((int64u)C[2])<<32 | ((int64u)C[1])<<40 | ((int64u)C[0])<<48 | ((int64u)   0)<<56;}
    00129 inline int64u CC6(const char*  C) {return C[5]<<0 | C[4]<<8 | C[3]<<16 | C[2]<<24 | ((int64u)C[1])<<32 | ((int64u)C[0])<<40 | ((int64u)   0)<<48 | ((int64u)   0)<<56;}
    00130 inline int64u CC5(const char*  C) {return C[4]<<0 | C[3]<<8 | C[2]<<16 | C[1]<<24 | ((int64u)C[0])<<32 | ((int64u)   0)<<40 | ((int64u)   0)<<48 | ((int64u)   0)<<56;}
    00131 #endif
    00132 inline int32u CC4(const char*  C) {return C[3]<<0 | C[2]<<8 | C[1]<<16 | C[0]<<24;}
    00133 inline int32u CC3(const char*  C) {return C[2]<<0 | C[1]<<8 | C[0]<<16 |    0<<24;}
    00134 inline int16u CC2(const char*  C) {return C[1]<<0 | C[0]<<8;}
    00135 inline int8u  CC1(const char*  C) {return C[0]<<0;}
    00136 #ifndef __BORLANDC__
    00137 #if (MAXTYPE_INT >= 64)
    00138 inline int64u CC8(const int8u* C) {return C[7]<<0 | C[6]<<8 | C[5]<<16 | C[4]<<24 | ((int64u)C[3])<<32 | ((int64u)C[2])<<40 | ((int64u)C[1])<<48 | ((int64u)C[0])<<56;}
    00139 inline int64u CC7(const int8u* C) {return C[6]<<0 | C[5]<<8 | C[4]<<16 | C[3]<<24 | ((int64u)C[2])<<32 | ((int64u)C[1])<<40 | ((int64u)C[0])<<48 | ((int64u)   0)<<56;}
    00140 inline int64u CC6(const int8u* C) {return C[5]<<0 | C[4]<<8 | C[3]<<16 | C[2]<<24 | ((int64u)C[1])<<32 | ((int64u)C[0])<<40 | ((int64u)   0)<<48 | ((int64u)   0)<<56;}
    00141 inline int64u CC5(const int8u* C) {return C[4]<<0 | C[3]<<8 | C[2]<<16 | C[1]<<24 | ((int64u)C[0])<<32 | ((int64u)   0)<<40 | ((int64u)   0)<<48 | ((int64u)   0)<<56;}
    00142 #endif
    00143 inline int32u CC4(const int8u* C) {return C[3]<<0 | C[2]<<8 | C[1]<<16 | C[0]<<24;}
    00144 inline int32u CC3(const int8u* C) {return C[2]<<0 | C[1]<<8 | C[0]<<16 |    0<<24;}
    00145 inline int16u CC2(const int8u* C) {return C[1]<<0 | C[0]<<8;}
    00146 inline int8u  CC1(const int8u* C) {return C[0]<<0;}
    00147 #endif // __BORLANDC__
    00148
    00149 } //namespace ZenLib
    00150 #endif