MiniGUI API Reference (MiniGUI-Threads)  v5.0.6
A mature and proven cross-platform GUI system for embedded and smart IoT devices
endianrw.h
Go to the documentation of this file.
1 //
3 // IMPORTANT NOTICE
4 //
5 // The following open source license statement does not apply to any
6 // entity in the Exception List published by FMSoft.
7 //
8 // For more information, please visit:
9 //
10 // https://www.fmsoft.cn/exception-list
11 //
13 
60 /*
61  * $Id: endianrw.h 11349 2009-03-02 05:00:43Z weiym $
62  *
63  * MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks,
64  * pSOS, ThreadX, NuCleus, OSE, and Win32.
65  *
66  * The idea comes from LGPL'ed SDL by Sam Lantinga.
67  */
68 
69 #ifndef _MGUI_ENDIAN_RW_H
70 #define _MGUI_ENDIAN_RW_H
71 
72 /* The macros used to swap values */
73 /* Try to use superfast macros on systems that support them */
74 #ifdef linux
75 #include <endian.h>
76 #ifdef __arch__swab16
77 #define ArchSwap16 __arch__swab16
78 #endif
79 #ifdef __arch__swab32
80 #define ArchSwap32 __arch__swab32
81 #endif
82 #endif /* linux */
83 
84 /* Set up for C function definitions, even when using C++ */
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
89 /************************** General RW operations ****************************/
90 
111 #define RWAREA_TYPE_UNKNOWN 0
112 #define RWAREA_TYPE_STDIO 1
113 #define RWAREA_TYPE_MEM 2
114 
118 typedef struct _MG_RWops {
124  int (*seek)(struct _MG_RWops *context, int offset, int whence);
125 
131  int (*read)(struct _MG_RWops *context, void *ptr, int objsize, int num);
132 
138  int (*write)(struct _MG_RWops *context, const void *ptr, int objsize,
139  int num);
140 
141 #ifdef _MGUSE_OWN_STDIO
142  /* */
143  int (*ungetc)(struct _MG_RWops *context, unsigned char c);
144 #endif
145 
149  int (*close)(struct _MG_RWops *context);
150 
154  int (*eof)(struct _MG_RWops *context);
155 
167 
168  union {
169  struct {
170  int autoclose;
171  FILE *fp;
172  } stdio;
173  struct {
174  Uint8 *base;
175  Uint8 *here;
176  Uint8 *stop;
177  } mem;
178  struct {
179  void *data1;
180  } unknown;
181  } hidden;
182 } MG_RWops;
183 
198 MG_EXPORT MG_RWops* MGUI_RWFromFile(const char *file, const char *mode);
199 
214 MG_EXPORT MG_RWops* MGUI_RWFromFP(FILE *fp, int autoclose);
215 
230 MG_EXPORT MG_RWops* MGUI_RWFromMem(void *mem, size_t size);
231 
247 MG_EXPORT void MGUI_InitMemRW (MG_RWops* area, void *mem, size_t size);
248 
260 MG_EXPORT MG_RWops* MGUI_AllocRW(void);
261 
272 MG_EXPORT void MGUI_FreeRW(MG_RWops *area);
273 
295 #define MGUI_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
296 
310 #define MGUI_RWtell(ctx) (ctx)->seek(ctx, 0, SEEK_CUR)
311 
327 #define MGUI_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
328 
344 #define MGUI_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
345 
359 #define MGUI_RWclose(ctx) (ctx)->close(ctx)
360 
374 #define MGUI_RWeof(ctx) (ctx)->eof(ctx)
375 
390 MG_EXPORT int MGUI_RWgetc (MG_RWops* area);
391 
394 /****************** Endian specific read/write interfaces *********************/
395 
418 /* Use inline functions for compilers that support them, and static
419  functions for those that do not. Because these functions become
420  static for compilers that do not support inline functions, this
421  header should only be included in files that actually use them.
422 */
423 #ifndef ArchSwap16
424 static inline Uint16 ArchSwap16(Uint16 D) {
425  return((D<<8)|(D>>8));
426 }
427 #endif
428 #ifndef ArchSwap32
429 static inline Uint32 ArchSwap32(Uint32 D) {
430  return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
431 }
432 #endif
433 #ifdef MGUI_HAS_64BIT_TYPE
434 #ifndef ArchSwap64
435 static inline Uint64 ArchSwap64(Uint64 val) {
436  Uint32 hi, lo;
437 
438  /* Separate into high and low 32-bit values and swap them */
439  lo = (Uint32)(val&0xFFFFFFFF);
440  val >>= 32;
441  hi = (Uint32)(val&0xFFFFFFFF);
442  val = ArchSwap32(lo);
443  val <<= 32;
444  val |= ArchSwap32(hi);
445  return(val);
446 }
447 #endif
448 #else
449 #ifndef ArchSwap64
450 /* This is mainly to keep compilers from complaining in MGUI code.
451  If there is no real 64-bit datatype, then compilers will complain about
452  the fake 64-bit datatype that MGUI provides when it compiles user code.
453 */
454 #define ArchSwap64(X) (X)
455 #endif
456 #endif /* MGUI_HAS_64BIT_TYPE */
457 
458 /* Byteswap item from the specified endianness to the native endianness */
459 #if MGUI_BYTEORDER == MGUI_LIL_ENDIAN
460 
461 #define ArchSwapLE16(X) (X)
462 
463 #define ArchSwapLE32(X) (X)
464 
465 #define ArchSwapLE64(X) (X)
466 
467 #define ArchSwapBE16(X) ArchSwap16(X)
468 
469 #define ArchSwapBE32(X) ArchSwap32(X)
470 
471 #define ArchSwapBE64(X) ArchSwap64(X)
472 #else
473 #define ArchSwapLE16(X) ArchSwap16(X)
474 #define ArchSwapLE32(X) ArchSwap32(X)
475 #define ArchSwapLE64(X) ArchSwap64(X)
476 #define ArchSwapBE16(X) (X)
477 #define ArchSwapBE32(X) (X)
478 #define ArchSwapBE64(X) (X)
479 #endif
480 
493 extern Uint16 MGUI_ReadLE16(MG_RWops *src);
494 
507 extern Uint16 MGUI_ReadBE16(MG_RWops *src);
508 
521 extern Uint32 MGUI_ReadLE32(MG_RWops *src);
522 
535 extern Uint32 MGUI_ReadBE32(MG_RWops *src);
536 
549 extern Uint64 MGUI_ReadLE64(MG_RWops *src);
550 
563 extern Uint64 MGUI_ReadBE64(MG_RWops *src);
564 
579 extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);
580 
595 extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);
596 
611 extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);
612 
627 extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);
628 
643 extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);
644 
659 extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);
660 
673 extern Uint16 MGUI_ReadLE16FP(FILE *src);
674 
687 extern Uint32 MGUI_ReadLE32FP(FILE *src);
688 
703 extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);
704 
719 extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);
720 
721 static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data)
722 {
723 #if 1
724  Uint16 h1, h2;
725 
726  h1 = *(*data); (*data)++;
727  h2 = *(*data); (*data)++;
728  return ((h2<<8)|h1);
729 #else
730  Uint16 u;
731  memcpy (&u, *data, sizeof (Uint16));
732  u = ArchSwapLE16 (u);
733  *data += sizeof (Uint16);
734  return u;
735 #endif
736 }
737 
738 static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data)
739 {
740 #if 1
741  Uint32 q1, q2, q3, q4;
742 
743  q1 = *(*data); (*data)++;
744  q2 = *(*data); (*data)++;
745  q3 = *(*data); (*data)++;
746  q4 = *(*data); (*data)++;
747  return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));
748 #else
749  Uint32 u;
750  memcpy (&u, *data, sizeof (Uint32));
751  u = ArchSwapLE32 (u);
752  *data += sizeof (Uint32);
753  return u;
754 #endif
755 }
756 
757 static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data)
758 {
759 #if 1
760  Uint16 h1, h2;
761 
762  h1 = *(*data); (*data)++;
763  h2 = *(*data); (*data)++;
764  return ((h1<<8)|h2);
765 #else
766  Uint16 u;
767  memcpy (&u, *data, sizeof (Uint16));
768  u = ArchSwapBE16 (u);
769  *data += sizeof (Uint16);
770  return u;
771 #endif
772 }
773 
774 static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data)
775 {
776 #if 1
777  Uint32 q1, q2, q3, q4;
778 
779  q1 = *(*data); (*data)++;
780  q2 = *(*data); (*data)++;
781  q3 = *(*data); (*data)++;
782  q4 = *(*data); (*data)++;
783  return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));
784 #else
785  Uint32 u;
786  memcpy (&u, *data, sizeof (Uint32));
787  u = ArchSwapBE32 (u);
788  *data += sizeof (Uint32);
789  return u;
790 #endif
791 }
792 
799 /* Ends C function definitions when using C++ */
800 #ifdef __cplusplus
801 }
802 #endif
803 
804 #endif /* _MGUI_ENDIAN_RW_H */
805 
_MG_RWops::type
Uint32 type
Definition: endianrw.h:166
MGUI_WriteBE64
int MGUI_WriteBE64(MG_RWops *dst, Uint64 value)
Writes an 64-bit integer of native format to a MG_RWops object in big endianness.
MGUI_InitMemRW
MG_EXPORT void MGUI_InitMemRW(MG_RWops *area, void *mem, size_t size)
Initializes an MG_RWops object from a block of memory.
MGUI_FreeRW
MG_EXPORT void MGUI_FreeRW(MG_RWops *area)
Frees an MG_RWops object.
MGUI_ReadBE16
Uint16 MGUI_ReadBE16(MG_RWops *src)
Reads a 16-bit big endian integer from a MG_RWops object.
ArchSwapLE16
#define ArchSwapLE16(X)
Definition: endianrw.h:461
MGUI_RWFromMem
MG_EXPORT MG_RWops * MGUI_RWFromMem(void *mem, size_t size)
Creates an MG_RWops object from a block of memory.
MGUI_WriteBE16
int MGUI_WriteBE16(MG_RWops *dst, Uint16 value)
Writes an 16-bit integer of native format to a MG_RWops object in big endianness.
MGUI_ReadLE64
Uint64 MGUI_ReadLE64(MG_RWops *src)
Reads a 64-bit little endian integer from a MG_RWops object.
MGUI_RWFromFile
MG_EXPORT MG_RWops * MGUI_RWFromFile(const char *file, const char *mode)
Creates an MG_RWops object from a file.
MGUI_ReadLE16FP
Uint16 MGUI_ReadLE16FP(FILE *src)
Reads a 16-bit little endian integer from a stdio FILE object.
ArchSwapBE16
#define ArchSwapBE16(X)
Definition: endianrw.h:467
MGUI_ReadLE16
Uint16 MGUI_ReadLE16(MG_RWops *src)
Reads a 16-bit little endian integer from a MG_RWops object.
MGUI_WriteLE64
int MGUI_WriteLE64(MG_RWops *dst, Uint64 value)
Writes an 64-bit integer of native format to a MG_RWops object in littlen endianness.
Uint64
unsigned long long Uint64
A type definition for a 64-bit unsigned integer.
Definition: common.h:206
MGUI_WriteBE32
int MGUI_WriteBE32(MG_RWops *dst, Uint32 value)
Writes an 32-bit integer of native format to a MG_RWops object in big endianness.
Uint16
unsigned short Uint16
A type definition for a 16-bit unsigned integer.
Definition: common.h:167
MGUI_WriteLE16
int MGUI_WriteLE16(MG_RWops *dst, Uint16 value)
Writes an 16-bit integer of native format to a MG_RWops object in littlen endianness.
Uint8
unsigned char Uint8
A type definition for an 8-bit unsigned character.
Definition: common.h:157
MGUI_ReadBE64
Uint64 MGUI_ReadBE64(MG_RWops *src)
Reads a 64-bit big endian integer from a MG_RWops object.
MGUI_ReadBE32
Uint32 MGUI_ReadBE32(MG_RWops *src)
Reads a 32-bit big endian integer from a MG_RWops object.
_MG_RWops::read
int(* read)(struct _MG_RWops *context, void *ptr, int objsize, int num)
Definition: endianrw.h:131
MGUI_WriteLE16FP
int MGUI_WriteLE16FP(FILE *dst, Uint16 value)
Writes an 16-bit integer of native format to a stdio FILE object in littlen endianness.
ArchSwapLE32
#define ArchSwapLE32(X)
Definition: endianrw.h:463
MGUI_ReadLE32FP
Uint32 MGUI_ReadLE32FP(FILE *src)
Reads a 32-bit little endian integer from a stdio FILE object.
MGUI_WriteLE32FP
int MGUI_WriteLE32FP(FILE *dst, Uint32 value)
Writes an 32-bit integer of native format to a stdio FILE object in littlen endianness.
MGUI_ReadLE32
Uint32 MGUI_ReadLE32(MG_RWops *src)
Reads a 32-bit little endian integer from a MG_RWops object.
_MG_RWops::seek
int(* seek)(struct _MG_RWops *context, int offset, int whence)
Definition: endianrw.h:124
_MG_RWops::close
int(* close)(struct _MG_RWops *context)
Definition: endianrw.h:149
_MG_RWops
Definition: endianrw.h:118
Uint32
unsigned int Uint32
A type definition for a 32-bit unsigned integer.
Definition: common.h:177
MGUI_WriteLE32
int MGUI_WriteLE32(MG_RWops *dst, Uint32 value)
Writes an 32-bit integer of native format to a MG_RWops object in littlen endianness.
MGUI_RWFromFP
MG_EXPORT MG_RWops * MGUI_RWFromFP(FILE *fp, int autoclose)
Creates an MG_RWops object from an opened stdio FILE object.
ArchSwapBE32
#define ArchSwapBE32(X)
Definition: endianrw.h:469
MGUI_AllocRW
MG_EXPORT MG_RWops * MGUI_AllocRW(void)
Allocates an uninitialized MG_RWops object.
MG_RWops
struct _MG_RWops MG_RWops
MGUI_RWgetc
MG_EXPORT int MGUI_RWgetc(MG_RWops *area)
Reads the next character from an data source.
_MG_RWops::write
int(* write)(struct _MG_RWops *context, const void *ptr, int objsize, int num)
Definition: endianrw.h:138
_MG_RWops::eof
int(* eof)(struct _MG_RWops *context)
Definition: endianrw.h:154