endianrw.h

Go to the documentation of this file.
00001 
00024 /*
00025  * $Id: endianrw.h 11349 2009-03-02 05:00:43Z weiym $
00026  * 
00027  *             MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks, 
00028  *                     pSOS, ThreadX, NuCleus, OSE, and Win32.
00029  *
00030  *             Copyright (C) 2002-2009 Feynman Software.
00031  *             Copyright (C) 1998-2002 Wei Yongming.
00032  *
00033  *             The idea comes from LGPL'ed SDL by Sam Lantinga.
00034  */
00035 
00036 #ifndef _MGUI_ENDIAN_RW_H
00037 #define _MGUI_ENDIAN_RW_H
00038 
00039 
00040 /* Set up for C function definitions, even when using C++ */
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 /************************** General RW operations ****************************/
00046 
00067 #define RWAREA_TYPE_UNKNOWN 0
00068 #define RWAREA_TYPE_STDIO   1
00069 #define RWAREA_TYPE_MEM     2
00070 
00074 typedef struct _MG_RWops {
00080     int (*seek)(struct _MG_RWops *context, int offset, int whence);
00081 
00087     int (*read)(struct _MG_RWops *context, void *ptr, int objsize, int num);
00088 
00094     int (*write)(struct _MG_RWops *context, const void *ptr, int objsize, 
00095                     int num);
00096 
00097 #ifdef _MGUSE_OWN_STDIO
00098     /* */
00099     int (*ungetc)(struct _MG_RWops *context, unsigned char c);
00100 #endif
00101 
00105     int (*close)(struct _MG_RWops *context);
00106 
00110     int (*eof)(struct _MG_RWops *context);
00111 
00122     Uint32 type;
00123 
00124     union {
00125         struct {
00126             int autoclose;
00127             FILE *fp;
00128         } stdio;
00129         struct {
00130             Uint8 *base;
00131             Uint8 *here;
00132             Uint8 *stop;
00133         } mem;
00134         struct {
00135             void *data1;
00136         } unknown;
00137     } hidden;
00138 } MG_RWops;
00139 
00154 MG_EXPORT MG_RWops* MGUI_RWFromFile(const char *file, const char *mode);
00155 
00170 MG_EXPORT MG_RWops* MGUI_RWFromFP(FILE *fp, int autoclose);
00171 
00186 MG_EXPORT MG_RWops* MGUI_RWFromMem(void *mem, int size);
00187 
00203 MG_EXPORT void MGUI_InitMemRW (MG_RWops* area, void *mem, int size);
00204 
00216 MG_EXPORT MG_RWops* MGUI_AllocRW(void);
00217 
00228 MG_EXPORT void MGUI_FreeRW(MG_RWops *area);
00229 
00251 #define MGUI_RWseek(ctx, offset, whence)    (ctx)->seek(ctx, offset, whence)
00252 
00266 #define MGUI_RWtell(ctx)                    (ctx)->seek(ctx, 0, SEEK_CUR)
00267 
00283 #define MGUI_RWread(ctx, ptr, size, n)      (ctx)->read(ctx, ptr, size, n)
00284 
00300 #define MGUI_RWwrite(ctx, ptr, size, n)     (ctx)->write(ctx, ptr, size, n)
00301 
00315 #define MGUI_RWclose(ctx)                   (ctx)->close(ctx)
00316 
00330 #define MGUI_RWeof(ctx)                     (ctx)->eof(ctx)
00331 
00346 MG_EXPORT int MGUI_RWgetc (MG_RWops* area);
00347 
00350 /****************** Endian specific read/write interfaces *********************/
00351 
00374 /* The macros used to swap values */
00375 /* Try to use superfast macros on systems that support them */
00376 #ifdef linux
00377 #include <endian.h>
00378 #ifdef __arch__swab16
00379 #define ArchSwap16  __arch__swab16
00380 #endif
00381 #ifdef __arch__swab32
00382 #define ArchSwap32  __arch__swab32
00383 #endif
00384 #endif /* linux */
00385 
00386 /* Use inline functions for compilers that support them, and static
00387    functions for those that do not.  Because these functions become
00388    static for compilers that do not support inline functions, this
00389    header should only be included in files that actually use them.
00390 */
00391 #ifndef ArchSwap16
00392 static inline Uint16 ArchSwap16(Uint16 D) {
00393         return((D<<8)|(D>>8));
00394 }
00395 #endif
00396 #ifndef ArchSwap32
00397 static inline Uint32 ArchSwap32(Uint32 D) {
00398         return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
00399 }
00400 #endif
00401 #ifdef MGUI_HAS_64BIT_TYPE
00402 #ifndef ArchSwap64
00403 static inline Uint64 ArchSwap64(Uint64 val) {
00404         Uint32 hi, lo;
00405 
00406         /* Separate into high and low 32-bit values and swap them */
00407         lo = (Uint32)(val&0xFFFFFFFF);
00408         val >>= 32;
00409         hi = (Uint32)(val&0xFFFFFFFF);
00410         val = ArchSwap32(lo);
00411         val <<= 32;
00412         val |= ArchSwap32(hi);
00413         return(val);
00414 }
00415 #endif
00416 #else
00417 #ifndef ArchSwap64
00418 /* This is mainly to keep compilers from complaining in MGUI code.
00419    If there is no real 64-bit datatype, then compilers will complain about
00420    the fake 64-bit datatype that MGUI provides when it compiles user code.
00421 */
00422 #define ArchSwap64(X)        (X)
00423 #endif
00424 #endif /* MGUI_HAS_64BIT_TYPE */
00425 
00426 /* Byteswap item from the specified endianness to the native endianness */
00427 #if MGUI_BYTEORDER == MGUI_LIL_ENDIAN
00428 
00429 #define ArchSwapLE16(X)        (X)
00430 
00431 #define ArchSwapLE32(X)        (X)
00432 
00433 #define ArchSwapLE64(X)        (X)
00434 
00435 #define ArchSwapBE16(X)        ArchSwap16(X)
00436 
00437 #define ArchSwapBE32(X)        ArchSwap32(X)
00438 
00439 #define ArchSwapBE64(X)        ArchSwap64(X)
00440 #else
00441 #define ArchSwapLE16(X)        ArchSwap16(X)
00442 #define ArchSwapLE32(X)        ArchSwap32(X)
00443 #define ArchSwapLE64(X)        ArchSwap64(X)
00444 #define ArchSwapBE16(X)        (X)
00445 #define ArchSwapBE32(X)        (X)
00446 #define ArchSwapBE64(X)        (X)
00447 #endif
00448 
00461 extern Uint16 MGUI_ReadLE16(MG_RWops *src);
00462 
00475 extern Uint16 MGUI_ReadBE16(MG_RWops *src);
00476 
00489 extern Uint32 MGUI_ReadLE32(MG_RWops *src);
00490 
00503 extern Uint32 MGUI_ReadBE32(MG_RWops *src);
00504 
00517 extern Uint64 MGUI_ReadLE64(MG_RWops *src);
00518 
00531 extern Uint64 MGUI_ReadBE64(MG_RWops *src);
00532 
00547 extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);
00548 
00563 extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);
00564 
00579 extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);
00580 
00595 extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);
00596 
00611 extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);
00612 
00627 extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);
00628 
00641 extern Uint16 MGUI_ReadLE16FP(FILE *src);
00642 
00655 extern Uint32 MGUI_ReadLE32FP(FILE *src);
00656 
00671 extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);
00672 
00687 extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);
00688 
00689 static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data)
00690 {
00691 #if 1
00692     Uint16 h1, h2;
00693 
00694     h1 = *(*data); (*data)++;
00695     h2 = *(*data); (*data)++;
00696     return ((h2<<8)|h1);
00697 #else
00698     Uint16 u;
00699     memcpy (&u, *data, sizeof (Uint16));
00700     u = ArchSwapLE16 (u);
00701     *data += sizeof (Uint16);
00702     return u;
00703 #endif
00704 }
00705 
00706 static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data)
00707 {
00708 #if 1
00709     Uint32 q1, q2, q3, q4;
00710 
00711     q1 = *(*data); (*data)++;
00712     q2 = *(*data); (*data)++;
00713     q3 = *(*data); (*data)++;
00714     q4 = *(*data); (*data)++;
00715     return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));
00716 #else
00717     Uint32 u;
00718     memcpy (&u, *data, sizeof (Uint32));
00719     u = ArchSwapLE32 (u);
00720     *data += sizeof (Uint32);
00721     return u;
00722 #endif
00723 }
00724 
00725 static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data)
00726 {
00727 #if 1
00728     Uint16 h1, h2;
00729 
00730     h1 = *(*data); (*data)++;
00731     h2 = *(*data); (*data)++;
00732     return ((h1<<8)|h2);
00733 #else
00734     Uint16 u;
00735     memcpy (&u, *data, sizeof (Uint16));
00736     u = ArchSwapBE16 (u);
00737     *data += sizeof (Uint16);
00738     return u;
00739 #endif
00740 }
00741 
00742 static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data)
00743 {
00744 #if 1
00745     Uint32 q1, q2, q3, q4;
00746 
00747     q1 = *(*data); (*data)++;
00748     q2 = *(*data); (*data)++;
00749     q3 = *(*data); (*data)++;
00750     q4 = *(*data); (*data)++;
00751     return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));
00752 #else
00753     Uint32 u;
00754     memcpy (&u, *data, sizeof (Uint32));
00755     u = ArchSwapBE32 (u);
00756     *data += sizeof (Uint32);
00757     return u;
00758 #endif
00759 }
00760 
00767 /* Ends C function definitions when using C++ */
00768 #ifdef __cplusplus
00769 }
00770 #endif
00771 
00772 #endif /* _MGUI_ENDIAN_RW_H */
00773 
Generated on Thu Apr 7 15:55:29 2011 for MiniGUI V3.0.12 API Reference by  doxygen 1.6.3