PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // byte_order.h 00004 // Copyright (C) 1998-2002 Pascal Vincent 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 00035 #ifndef byte_order_INC 00036 #define byte_order_INC 00037 00038 // norman: 00039 // If I don't add a definition of the namespace std .NET it does not compile 00040 // (weird..)! 00041 #ifdef WIN32 00042 #include <string> 00043 #endif 00044 00045 #include <stdlib.h> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00052 00053 #define LITTLE_ENDIAN_ORDER 'L' 00054 #define BIG_ENDIAN_ORDER 'B' 00055 00056 inline char byte_order() 00057 { 00058 #ifdef BIGENDIAN 00059 return BIG_ENDIAN_ORDER; 00060 #else 00061 return LITTLE_ENDIAN_ORDER; 00062 #endif 00063 } 00064 00065 00067 void endianswap2(void* ptr, int n); 00069 void endianswap4(void* ptr, int n); 00071 void endianswap8(void* ptr, int n); 00072 00075 inline void endianswap(void* ptr, int nelems, int elemsize) 00076 { 00077 switch(elemsize) 00078 { 00079 case 1: 00080 break; 00081 case 2: 00082 endianswap2(ptr,nelems); 00083 break; 00084 case 4: 00085 endianswap4(ptr,nelems); 00086 break; 00087 case 8: 00088 endianswap8(ptr,nelems); 00089 break; 00090 default: 00091 abort(); 00092 } 00093 } 00094 00095 00096 template<class T> 00097 inline void endianswap(T* ptr, int n=1) 00098 { endianswap(ptr,n,sizeof(T)); } 00099 00100 } 00101 00102 #endif 00103 00104 00105 /* 00106 Local Variables: 00107 mode:c++ 00108 c-basic-offset:4 00109 c-file-style:"stroustrup" 00110 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00111 indent-tabs-mode:nil 00112 fill-column:79 00113 End: 00114 */ 00115 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :