MiniGUI API Reference (MiniGUI-Standalone)  v3.2.0
A mature and proven cross-platform GUI system for embedded and smart IoT devices
endianrw.h
Go to the documentation of this file.
1 
48 /*
49  * $Id: endianrw.h 11349 2009-03-02 05:00:43Z weiym $
50  *
51  * MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks,
52  * pSOS, ThreadX, NuCleus, OSE, and Win32.
53  *
54  * The idea comes from LGPL'ed SDL by Sam Lantinga.
55  */
56 
57 #ifndef _MGUI_ENDIAN_RW_H
58 #define _MGUI_ENDIAN_RW_H
59 
60 
61 /* Set up for C function definitions, even when using C++ */
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 /************************** General RW operations ****************************/
67 
88 #define RWAREA_TYPE_UNKNOWN 0
89 #define RWAREA_TYPE_STDIO 1
90 #define RWAREA_TYPE_MEM 2
91 
95 typedef struct _MG_RWops {
101  int (*seek)(struct _MG_RWops *context, int offset, int whence);
102 
108  int (*read)(struct _MG_RWops *context, void *ptr, int objsize, int num);
109 
115  int (*write)(struct _MG_RWops *context, const void *ptr, int objsize,
116  int num);
117 
118 #ifdef _MGUSE_OWN_STDIO
119  /* */
120  int (*ungetc)(struct _MG_RWops *context, unsigned char c);
121 #endif
122 
126  int (*close)(struct _MG_RWops *context);
127 
131  int (*eof)(struct _MG_RWops *context);
132 
144 
145  union {
146  struct {
147  int autoclose;
148  FILE *fp;
149  } stdio;
150  struct {
151  Uint8 *base;
152  Uint8 *here;
153  Uint8 *stop;
154  } mem;
155  struct {
156  void *data1;
157  } unknown;
158  } hidden;
159 } MG_RWops;
160 
175 MG_EXPORT MG_RWops* MGUI_RWFromFile(const char *file, const char *mode);
176 
191 MG_EXPORT MG_RWops* MGUI_RWFromFP(FILE *fp, int autoclose);
192 
207 MG_EXPORT MG_RWops* MGUI_RWFromMem(void *mem, int size);
208 
224 MG_EXPORT void MGUI_InitMemRW (MG_RWops* area, void *mem, int size);
225 
237 MG_EXPORT MG_RWops* MGUI_AllocRW(void);
238 
249 MG_EXPORT void MGUI_FreeRW(MG_RWops *area);
250 
272 #define MGUI_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
273 
287 #define MGUI_RWtell(ctx) (ctx)->seek(ctx, 0, SEEK_CUR)
288 
304 #define MGUI_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
305 
321 #define MGUI_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
322 
336 #define MGUI_RWclose(ctx) (ctx)->close(ctx)
337 
351 #define MGUI_RWeof(ctx) (ctx)->eof(ctx)
352 
367 MG_EXPORT int MGUI_RWgetc (MG_RWops* area);
368 
371 /****************** Endian specific read/write interfaces *********************/
372 
395 /* The macros used to swap values */
396 /* Try to use superfast macros on systems that support them */
397 #ifdef linux
398 #include <endian.h>
399 #ifdef __arch__swab16
400 #define ArchSwap16 __arch__swab16
401 #endif
402 #ifdef __arch__swab32
403 #define ArchSwap32 __arch__swab32
404 #endif
405 #endif /* linux */
406 
407 /* Use inline functions for compilers that support them, and static
408  functions for those that do not. Because these functions become
409  static for compilers that do not support inline functions, this
410  header should only be included in files that actually use them.
411 */
412 #ifndef ArchSwap16
413 static inline Uint16 ArchSwap16(Uint16 D) {
414  return((D<<8)|(D>>8));
415 }
416 #endif
417 #ifndef ArchSwap32
418 static inline Uint32 ArchSwap32(Uint32 D) {
419  return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
420 }
421 #endif
422 #ifdef MGUI_HAS_64BIT_TYPE
423 #ifndef ArchSwap64
424 static inline Uint64 ArchSwap64(Uint64 val) {
425  Uint32 hi, lo;
426 
427  /* Separate into high and low 32-bit values and swap them */
428  lo = (Uint32)(val&0xFFFFFFFF);
429  val >>= 32;
430  hi = (Uint32)(val&0xFFFFFFFF);
431  val = ArchSwap32(lo);
432  val <<= 32;
433  val |= ArchSwap32(hi);
434  return(val);
435 }
436 #endif
437 #else
438 #ifndef ArchSwap64
439 /* This is mainly to keep compilers from complaining in MGUI code.
440  If there is no real 64-bit datatype, then compilers will complain about
441  the fake 64-bit datatype that MGUI provides when it compiles user code.
442 */
443 #define ArchSwap64(X) (X)
444 #endif
445 #endif /* MGUI_HAS_64BIT_TYPE */
446 
447 /* Byteswap item from the specified endianness to the native endianness */
448 #if MGUI_BYTEORDER == MGUI_LIL_ENDIAN
449 
450 #define ArchSwapLE16(X) (X)
451 
452 #define ArchSwapLE32(X) (X)
453 
454 #define ArchSwapLE64(X) (X)
455 
456 #define ArchSwapBE16(X) ArchSwap16(X)
457 
458 #define ArchSwapBE32(X) ArchSwap32(X)
459 
460 #define ArchSwapBE64(X) ArchSwap64(X)
461 #else
462 #define ArchSwapLE16(X) ArchSwap16(X)
463 #define ArchSwapLE32(X) ArchSwap32(X)
464 #define ArchSwapLE64(X) ArchSwap64(X)
465 #define ArchSwapBE16(X) (X)
466 #define ArchSwapBE32(X) (X)
467 #define ArchSwapBE64(X) (X)
468 #endif
469 
482 extern Uint16 MGUI_ReadLE16(MG_RWops *src);
483 
496 extern Uint16 MGUI_ReadBE16(MG_RWops *src);
497 
510 extern Uint32 MGUI_ReadLE32(MG_RWops *src);
511 
524 extern Uint32 MGUI_ReadBE32(MG_RWops *src);
525 
538 extern Uint64 MGUI_ReadLE64(MG_RWops *src);
539 
552 extern Uint64 MGUI_ReadBE64(MG_RWops *src);
553 
568 extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);
569 
584 extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);
585 
600 extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);
601 
616 extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);
617 
632 extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);
633 
648 extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);
649 
662 extern Uint16 MGUI_ReadLE16FP(FILE *src);
663 
676 extern Uint32 MGUI_ReadLE32FP(FILE *src);
677 
692 extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);
693 
708 extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);
709 
710 static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data)
711 {
712 #if 1
713  Uint16 h1, h2;
714 
715  h1 = *(*data); (*data)++;
716  h2 = *(*data); (*data)++;
717  return ((h2<<8)|h1);
718 #else
719  Uint16 u;
720  memcpy (&u, *data, sizeof (Uint16));
721  u = ArchSwapLE16 (u);
722  *data += sizeof (Uint16);
723  return u;
724 #endif
725 }
726 
727 static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data)
728 {
729 #if 1
730  Uint32 q1, q2, q3, q4;
731 
732  q1 = *(*data); (*data)++;
733  q2 = *(*data); (*data)++;
734  q3 = *(*data); (*data)++;
735  q4 = *(*data); (*data)++;
736  return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));
737 #else
738  Uint32 u;
739  memcpy (&u, *data, sizeof (Uint32));
740  u = ArchSwapLE32 (u);
741  *data += sizeof (Uint32);
742  return u;
743 #endif
744 }
745 
746 static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data)
747 {
748 #if 1
749  Uint16 h1, h2;
750 
751  h1 = *(*data); (*data)++;
752  h2 = *(*data); (*data)++;
753  return ((h1<<8)|h2);
754 #else
755  Uint16 u;
756  memcpy (&u, *data, sizeof (Uint16));
757  u = ArchSwapBE16 (u);
758  *data += sizeof (Uint16);
759  return u;
760 #endif
761 }
762 
763 static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data)
764 {
765 #if 1
766  Uint32 q1, q2, q3, q4;
767 
768  q1 = *(*data); (*data)++;
769  q2 = *(*data); (*data)++;
770  q3 = *(*data); (*data)++;
771  q4 = *(*data); (*data)++;
772  return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));
773 #else
774  Uint32 u;
775  memcpy (&u, *data, sizeof (Uint32));
776  u = ArchSwapBE32 (u);
777  *data += sizeof (Uint32);
778  return u;
779 #endif
780 }
781 
788 /* Ends C function definitions when using C++ */
789 #ifdef __cplusplus
790 }
791 #endif
792 
793 #endif /* _MGUI_ENDIAN_RW_H */
794 
int(* seek)(struct _MG_RWops *context, int offset, int whence)
Definition: endianrw.h:101
Uint32 MGUI_ReadLE32FP(FILE *src)
Reads a 32-bit little endian integer from a stdio FILE object.
Uint16 MGUI_ReadLE16FP(FILE *src)
Reads a 16-bit little endian integer from a stdio FILE object.
MG_EXPORT void MGUI_FreeRW(MG_RWops *area)
Frees an MG_RWops object.
int(* write)(struct _MG_RWops *context, const void *ptr, int objsize, int num)
Definition: endianrw.h:115
int MGUI_WriteBE64(MG_RWops *dst, Uint64 value)
Writes an 64-bit integer of native format to a MG_RWops object in big endianness. ...
Uint64 MGUI_ReadBE64(MG_RWops *src)
Reads a 64-bit big endian integer from a MG_RWops object.
unsigned long long Uint64
A type definition for a 64-bit unsigned integer.
Definition: common.h:191
int MGUI_WriteLE16FP(FILE *dst, Uint16 value)
Writes an 16-bit integer of native format to a stdio FILE object in littlen endianness.
MG_EXPORT int MGUI_RWgetc(MG_RWops *area)
Reads the next character from an data source.
unsigned short Uint16
A type definition for a 16-bit unsigned integer.
Definition: common.h:152
int MGUI_WriteLE64(MG_RWops *dst, Uint64 value)
Writes an 64-bit integer of native format to a MG_RWops object in littlen endianness.
Uint32 MGUI_ReadLE32(MG_RWops *src)
Reads a 32-bit little endian integer from a MG_RWops object.
int MGUI_WriteLE16(MG_RWops *dst, Uint16 value)
Writes an 16-bit integer of native format to a MG_RWops object in littlen endianness.
int(* close)(struct _MG_RWops *context)
Definition: endianrw.h:126
MG_EXPORT MG_RWops * MGUI_RWFromMem(void *mem, int size)
Creates an MG_RWops object from a block of memory.
MG_EXPORT MG_RWops * MGUI_RWFromFP(FILE *fp, int autoclose)
Creates an MG_RWops object from an opened stdio FILE object.
Uint16 MGUI_ReadLE16(MG_RWops *src)
Reads a 16-bit little endian integer from a MG_RWops object.
MG_EXPORT void MGUI_InitMemRW(MG_RWops *area, void *mem, int size)
Initializes an MG_RWops object from a block of memory.
#define ArchSwapLE16(X)
Definition: endianrw.h:450
int MGUI_WriteBE32(MG_RWops *dst, Uint32 value)
Writes an 32-bit integer of native format to a MG_RWops object in big endianness. ...
Uint32 type
Definition: endianrw.h:143
Uint16 MGUI_ReadBE16(MG_RWops *src)
Reads a 16-bit big endian integer from a MG_RWops object.
int(* eof)(struct _MG_RWops *context)
Definition: endianrw.h:131
unsigned char Uint8
A type definition for an 8-bit unsigned character.
Definition: common.h:142
#define ArchSwapBE16(X)
Definition: endianrw.h:456
int MGUI_WriteLE32(MG_RWops *dst, Uint32 value)
Writes an 32-bit integer of native format to a MG_RWops object in littlen endianness.
unsigned int Uint32
A type definition for a 32-bit unsigned integer.
Definition: common.h:162
#define ArchSwapLE32(X)
Definition: endianrw.h:452
MG_EXPORT MG_RWops * MGUI_RWFromFile(const char *file, const char *mode)
Creates an MG_RWops object from a file.
int MGUI_WriteBE16(MG_RWops *dst, Uint16 value)
Writes an 16-bit integer of native format to a MG_RWops object in big endianness. ...
Uint64 MGUI_ReadLE64(MG_RWops *src)
Reads a 64-bit little endian integer from a MG_RWops object.
int(* read)(struct _MG_RWops *context, void *ptr, int objsize, int num)
Definition: endianrw.h:108
int MGUI_WriteLE32FP(FILE *dst, Uint32 value)
Writes an 32-bit integer of native format to a stdio FILE object in littlen endianness.
Uint32 MGUI_ReadBE32(MG_RWops *src)
Reads a 32-bit big endian integer from a MG_RWops object.
#define ArchSwapBE32(X)
Definition: endianrw.h:458
MG_EXPORT MG_RWops * MGUI_AllocRW(void)
Allocates an uninitialized MG_RWops object.
struct _MG_RWops MG_RWops