Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

endianrw.h

Go to the documentation of this file.
00001 
00024 /*
00025  * $Id: endianrw.h,v 1.34 2006/06/11 04:16:14 weiym Exp $
00026  * 
00027  *             MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks, 
00028  *                     pSOS, ThreadX, NuCleus, OSE, and Win32.
00029  *
00030  *             Copyright (C) 2002-2006 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 /* Set up for C function definitions, even when using C++ */
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 /************************** General RW operations ****************************/
00045 
00066 #define RWAREA_TYPE_UNKNOWN 0
00067 #define RWAREA_TYPE_STDIO   1
00068 #define RWAREA_TYPE_MEM     2
00069 
00073 typedef struct _MG_RWops {
00079     int (*seek)(struct _MG_RWops *context, int offset, int whence);
00080 
00086     int (*read)(struct _MG_RWops *context, void *ptr, int objsize, int num);
00087 
00093     int (*write)(struct _MG_RWops *context, const void *ptr, int objsize, 
00094                     int num);
00095 
00096 #ifdef _USE_OWN_STDIO
00097     /* */
00098     int (*ungetc)(struct _MG_RWops *context, unsigned char c);
00099 #endif
00100 
00104     int (*close)(struct _MG_RWops *context);
00105 
00109     int (*eof)(struct _MG_RWops *context);
00110 
00121     Uint32 type;
00122 
00123     union {
00124         struct {
00125             int autoclose;
00126             FILE *fp;
00127         } stdio;
00128         struct {
00129             Uint8 *base;
00130             Uint8 *here;
00131             Uint8 *stop;
00132         } mem;
00133         struct {
00134             void *data1;
00135         } unknown;
00136     } hidden;
00137 } MG_RWops;
00138 
00153 MG_EXPORT MG_RWops* MGUI_RWFromFile(const char *file, const char *mode);
00154 
00169 MG_EXPORT MG_RWops* MGUI_RWFromFP(FILE *fp, int autoclose);
00170 
00185 MG_EXPORT MG_RWops* MGUI_RWFromMem(void *mem, int size);
00186 
00202 MG_EXPORT void MGUI_InitMemRW (MG_RWops* area, void *mem, int size);
00203 
00215 MG_EXPORT MG_RWops* MGUI_AllocRW(void);
00216 
00227 MG_EXPORT void MGUI_FreeRW(MG_RWops *area);
00228 
00250 #define MGUI_RWseek(ctx, offset, whence)    (ctx)->seek(ctx, offset, whence)
00251 
00265 #define MGUI_RWtell(ctx)                    (ctx)->seek(ctx, 0, SEEK_CUR)
00266 
00282 #define MGUI_RWread(ctx, ptr, size, n)      (ctx)->read(ctx, ptr, size, n)
00283 
00299 #define MGUI_RWwrite(ctx, ptr, size, n)     (ctx)->write(ctx, ptr, size, n)
00300 
00314 #define MGUI_RWclose(ctx)                   (ctx)->close(ctx)
00315 
00329 #define MGUI_RWeof(ctx)                     (ctx)->eof(ctx)
00330 
00345 MG_EXPORT int MGUI_RWgetc (MG_RWops* area);
00346 
00349 /****************** Endian specific read/write interfaces *********************/
00350 
00373 /* The macros used to swap values */
00374 /* Try to use superfast macros on systems that support them */
00375 #ifdef linux
00376 #include <endian.h>
00377 #ifdef __arch__swab16
00378 #define ArchSwap16  __arch__swab16
00379 #endif
00380 #ifdef __arch__swab32
00381 #define ArchSwap32  __arch__swab32
00382 #endif
00383 #endif /* linux */
00384 
00385 /* Use inline functions for compilers that support them, and static
00386    functions for those that do not.  Because these functions become
00387    static for compilers that do not support inline functions, this
00388    header should only be included in files that actually use them.
00389 */
00390 #ifndef ArchSwap16
00391 static inline Uint16 ArchSwap16(Uint16 D) {
00392         return((D<<8)|(D>>8));
00393 }
00394 #endif
00395 #ifndef ArchSwap32
00396 static inline Uint32 ArchSwap32(Uint32 D) {
00397         return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
00398 }
00399 #endif
00400 #ifdef MGUI_HAS_64BIT_TYPE
00401 #ifndef ArchSwap64
00402 static inline Uint64 ArchSwap64(Uint64 val) {
00403         Uint32 hi, lo;
00404 
00405         /* Separate into high and low 32-bit values and swap them */
00406         lo = (Uint32)(val&0xFFFFFFFF);
00407         val >>= 32;
00408         hi = (Uint32)(val&0xFFFFFFFF);
00409         val = ArchSwap32(lo);
00410         val <<= 32;
00411         val |= ArchSwap32(hi);
00412         return(val);
00413 }
00414 #endif
00415 #else
00416 #ifndef ArchSwap64
00417 /* This is mainly to keep compilers from complaining in MGUI code.
00418    If there is no real 64-bit datatype, then compilers will complain about
00419    the fake 64-bit datatype that MGUI provides when it compiles user code.
00420 */
00421 #define ArchSwap64(X)        (X)
00422 #endif
00423 #endif /* MGUI_HAS_64BIT_TYPE */
00424 
00425 /* Byteswap item from the specified endianness to the native endianness */
00426 #if MGUI_BYTEORDER == MGUI_LIL_ENDIAN
00427 
00428 #define ArchSwapLE16(X)        (X)
00429 
00430 #define ArchSwapLE32(X)        (X)
00431 
00432 #define ArchSwapLE64(X)        (X)
00433 
00434 #define ArchSwapBE16(X)        ArchSwap16(X)
00435 
00436 #define ArchSwapBE32(X)        ArchSwap32(X)
00437 
00438 #define ArchSwapBE64(X)        ArchSwap64(X)
00439 #else
00440 #define ArchSwapLE16(X)        ArchSwap16(X)
00441 #define ArchSwapLE32(X)        ArchSwap32(X)
00442 #define ArchSwapLE64(X)        ArchSwap64(X)
00443 #define ArchSwapBE16(X)        (X)
00444 #define ArchSwapBE32(X)        (X)
00445 #define ArchSwapBE64(X)        (X)
00446 #endif
00447 
00460 extern Uint16 MGUI_ReadLE16(MG_RWops *src);
00461 
00474 extern Uint16 MGUI_ReadBE16(MG_RWops *src);
00475 
00488 extern Uint32 MGUI_ReadLE32(MG_RWops *src);
00489 
00502 extern Uint32 MGUI_ReadBE32(MG_RWops *src);
00503 
00516 extern Uint64 MGUI_ReadLE64(MG_RWops *src);
00517 
00530 extern Uint64 MGUI_ReadBE64(MG_RWops *src);
00531 
00546 extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);
00547 
00562 extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);
00563 
00578 extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);
00579 
00594 extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);
00595 
00610 extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);
00611 
00626 extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);
00627 
00640 extern Uint16 MGUI_ReadLE16FP(FILE *src);
00641 
00654 extern Uint32 MGUI_ReadLE32FP(FILE *src);
00655 
00670 extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);
00671 
00686 extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);
00687 
00688 static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data)
00689 {
00690 #if 1
00691     Uint16 h1, h2;
00692 
00693     h1 = *(*data); (*data)++;
00694     h2 = *(*data); (*data)++;
00695     return ((h2<<8)|h1);
00696 #else
00697     Uint16 u;
00698     memcpy (&u, *data, sizeof (Uint16));
00699     u = ArchSwapLE16 (u);
00700     *data += sizeof (Uint16);
00701     return u;
00702 #endif
00703 }
00704 
00705 static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data)
00706 {
00707 #if 1
00708     Uint32 q1, q2, q3, q4;
00709 
00710     q1 = *(*data); (*data)++;
00711     q2 = *(*data); (*data)++;
00712     q3 = *(*data); (*data)++;
00713     q4 = *(*data); (*data)++;
00714     return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));
00715 #else
00716     Uint32 u;
00717     memcpy (&u, *data, sizeof (Uint32));
00718     u = ArchSwapLE32 (u);
00719     *data += sizeof (Uint32);
00720     return u;
00721 #endif
00722 }
00723 
00724 static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data)
00725 {
00726 #if 1
00727     Uint16 h1, h2;
00728 
00729     h1 = *(*data); (*data)++;
00730     h2 = *(*data); (*data)++;
00731     return ((h1<<8)|h2);
00732 #else
00733     Uint16 u;
00734     memcpy (&u, *data, sizeof (Uint16));
00735     u = ArchSwapBE16 (u);
00736     *data += sizeof (Uint16);
00737     return u;
00738 #endif
00739 }
00740 
00741 static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data)
00742 {
00743 #if 1
00744     Uint32 q1, q2, q3, q4;
00745 
00746     q1 = *(*data); (*data)++;
00747     q2 = *(*data); (*data)++;
00748     q3 = *(*data); (*data)++;
00749     q4 = *(*data); (*data)++;
00750     return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));
00751 #else
00752     Uint32 u;
00753     memcpy (&u, *data, sizeof (Uint32));
00754     u = ArchSwapBE32 (u);
00755     *data += sizeof (Uint32);
00756     return u;
00757 #endif
00758 }
00759 
00766 /* Ends C function definitions when using C++ */
00767 #ifdef __cplusplus
00768 }
00769 #endif
00770 
00771 #endif /* _MGUI_ENDIAN_RW_H */
00772 

Generated on Mon Jun 26 14:21:34 2006 for MiniGUI V2.0.3 API Reference by  doxygen 1.4.2