MiniGUI API Reference (MiniGUI-Standalone)  v4.0.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 //
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 
73 /* Set up for C function definitions, even when using C++ */
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 /************************** General RW operations ****************************/
79 
100 #define RWAREA_TYPE_UNKNOWN 0
101 #define RWAREA_TYPE_STDIO 1
102 #define RWAREA_TYPE_MEM 2
103 
107 typedef struct _MG_RWops {
113  int (*seek)(struct _MG_RWops *context, int offset, int whence);
114 
120  int (*read)(struct _MG_RWops *context, void *ptr, int objsize, int num);
121 
127  int (*write)(struct _MG_RWops *context, const void *ptr, int objsize,
128  int num);
129 
130 #ifdef _MGUSE_OWN_STDIO
131  /* */
132  int (*ungetc)(struct _MG_RWops *context, unsigned char c);
133 #endif
134 
138  int (*close)(struct _MG_RWops *context);
139 
143  int (*eof)(struct _MG_RWops *context);
144 
156 
157  union {
158  struct {
159  int autoclose;
160  FILE *fp;
161  } stdio;
162  struct {
163  Uint8 *base;
164  Uint8 *here;
165  Uint8 *stop;
166  } mem;
167  struct {
168  void *data1;
169  } unknown;
170  } hidden;
171 } MG_RWops;
172 
187 MG_EXPORT MG_RWops* MGUI_RWFromFile(const char *file, const char *mode);
188 
203 MG_EXPORT MG_RWops* MGUI_RWFromFP(FILE *fp, int autoclose);
204 
219 MG_EXPORT MG_RWops* MGUI_RWFromMem(void *mem, size_t size);
220 
236 MG_EXPORT void MGUI_InitMemRW (MG_RWops* area, void *mem, size_t size);
237 
249 MG_EXPORT MG_RWops* MGUI_AllocRW(void);
250 
261 MG_EXPORT void MGUI_FreeRW(MG_RWops *area);
262 
284 #define MGUI_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
285 
299 #define MGUI_RWtell(ctx) (ctx)->seek(ctx, 0, SEEK_CUR)
300 
316 #define MGUI_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
317 
333 #define MGUI_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
334 
348 #define MGUI_RWclose(ctx) (ctx)->close(ctx)
349 
363 #define MGUI_RWeof(ctx) (ctx)->eof(ctx)
364 
379 MG_EXPORT int MGUI_RWgetc (MG_RWops* area);
380 
383 /****************** Endian specific read/write interfaces *********************/
384 
407 /* The macros used to swap values */
408 /* Try to use superfast macros on systems that support them */
409 #ifdef linux
410 #include <endian.h>
411 #ifdef __arch__swab16
412 #define ArchSwap16 __arch__swab16
413 #endif
414 #ifdef __arch__swab32
415 #define ArchSwap32 __arch__swab32
416 #endif
417 #endif /* linux */
418 
419 /* Use inline functions for compilers that support them, and static
420  functions for those that do not. Because these functions become
421  static for compilers that do not support inline functions, this
422  header should only be included in files that actually use them.
423 */
424 #ifndef ArchSwap16
425 static inline Uint16 ArchSwap16(Uint16 D) {
426  return((D<<8)|(D>>8));
427 }
428 #endif
429 #ifndef ArchSwap32
430 static inline Uint32 ArchSwap32(Uint32 D) {
431  return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
432 }
433 #endif
434 #ifdef MGUI_HAS_64BIT_TYPE
435 #ifndef ArchSwap64
436 static inline Uint64 ArchSwap64(Uint64 val) {
437  Uint32 hi, lo;
438 
439  /* Separate into high and low 32-bit values and swap them */
440  lo = (Uint32)(val&0xFFFFFFFF);
441  val >>= 32;
442  hi = (Uint32)(val&0xFFFFFFFF);
443  val = ArchSwap32(lo);
444  val <<= 32;
445  val |= ArchSwap32(hi);
446  return(val);
447 }
448 #endif
449 #else
450 #ifndef ArchSwap64
451 /* This is mainly to keep compilers from complaining in MGUI code.
452  If there is no real 64-bit datatype, then compilers will complain about
453  the fake 64-bit datatype that MGUI provides when it compiles user code.
454 */
455 #define ArchSwap64(X) (X)
456 #endif
457 #endif /* MGUI_HAS_64BIT_TYPE */
458 
459 /* Byteswap item from the specified endianness to the native endianness */
460 #if MGUI_BYTEORDER == MGUI_LIL_ENDIAN
461 
462 #define ArchSwapLE16(X) (X)
463 
464 #define ArchSwapLE32(X) (X)
465 
466 #define ArchSwapLE64(X) (X)
467 
468 #define ArchSwapBE16(X) ArchSwap16(X)
469 
470 #define ArchSwapBE32(X) ArchSwap32(X)
471 
472 #define ArchSwapBE64(X) ArchSwap64(X)
473 #else
474 #define ArchSwapLE16(X) ArchSwap16(X)
475 #define ArchSwapLE32(X) ArchSwap32(X)
476 #define ArchSwapLE64(X) ArchSwap64(X)
477 #define ArchSwapBE16(X) (X)
478 #define ArchSwapBE32(X) (X)
479 #define ArchSwapBE64(X) (X)
480 #endif
481 
494 extern Uint16 MGUI_ReadLE16(MG_RWops *src);
495 
508 extern Uint16 MGUI_ReadBE16(MG_RWops *src);
509 
522 extern Uint32 MGUI_ReadLE32(MG_RWops *src);
523 
536 extern Uint32 MGUI_ReadBE32(MG_RWops *src);
537 
550 extern Uint64 MGUI_ReadLE64(MG_RWops *src);
551 
564 extern Uint64 MGUI_ReadBE64(MG_RWops *src);
565 
580 extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);
581 
596 extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);
597 
612 extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);
613 
628 extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);
629 
644 extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);
645 
660 extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);
661 
674 extern Uint16 MGUI_ReadLE16FP(FILE *src);
675 
688 extern Uint32 MGUI_ReadLE32FP(FILE *src);
689 
704 extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);
705 
720 extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);
721 
722 static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data)
723 {
724 #if 1
725  Uint16 h1, h2;
726 
727  h1 = *(*data); (*data)++;
728  h2 = *(*data); (*data)++;
729  return ((h2<<8)|h1);
730 #else
731  Uint16 u;
732  memcpy (&u, *data, sizeof (Uint16));
733  u = ArchSwapLE16 (u);
734  *data += sizeof (Uint16);
735  return u;
736 #endif
737 }
738 
739 static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data)
740 {
741 #if 1
742  Uint32 q1, q2, q3, q4;
743 
744  q1 = *(*data); (*data)++;
745  q2 = *(*data); (*data)++;
746  q3 = *(*data); (*data)++;
747  q4 = *(*data); (*data)++;
748  return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));
749 #else
750  Uint32 u;
751  memcpy (&u, *data, sizeof (Uint32));
752  u = ArchSwapLE32 (u);
753  *data += sizeof (Uint32);
754  return u;
755 #endif
756 }
757 
758 static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data)
759 {
760 #if 1
761  Uint16 h1, h2;
762 
763  h1 = *(*data); (*data)++;
764  h2 = *(*data); (*data)++;
765  return ((h1<<8)|h2);
766 #else
767  Uint16 u;
768  memcpy (&u, *data, sizeof (Uint16));
769  u = ArchSwapBE16 (u);
770  *data += sizeof (Uint16);
771  return u;
772 #endif
773 }
774 
775 static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data)
776 {
777 #if 1
778  Uint32 q1, q2, q3, q4;
779 
780  q1 = *(*data); (*data)++;
781  q2 = *(*data); (*data)++;
782  q3 = *(*data); (*data)++;
783  q4 = *(*data); (*data)++;
784  return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));
785 #else
786  Uint32 u;
787  memcpy (&u, *data, sizeof (Uint32));
788  u = ArchSwapBE32 (u);
789  *data += sizeof (Uint32);
790  return u;
791 #endif
792 }
793 
800 /* Ends C function definitions when using C++ */
801 #ifdef __cplusplus
802 }
803 #endif
804 
805 #endif /* _MGUI_ENDIAN_RW_H */
806 
int(* seek)(struct _MG_RWops *context, int offset, int whence)
Definition: endianrw.h:113
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:127
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:203
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:164
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:138
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.
#define ArchSwapLE16(X)
Definition: endianrw.h:462
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:155
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:143
unsigned char Uint8
A type definition for an 8-bit unsigned character.
Definition: common.h:154
#define ArchSwapBE16(X)
Definition: endianrw.h:468
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:174
#define ArchSwapLE32(X)
Definition: endianrw.h:464
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:120
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:470
MG_EXPORT MG_RWops * MGUI_AllocRW(void)
Allocates an uninitialized MG_RWops object.
struct _MG_RWops MG_RWops