ALmixer  0.0.5
ALmixer_RWops.h
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
29 #ifndef _ALmixer_rwops_h
30 #define _ALmixer_rwops_h
31 
32 #include "ALmixer.h"
33 /*
34 #include "ALmixer_stdinc.h"
35 */
36 
37 /*
38  I'm probably asking for trouble, but I don't want to have to ship all the SDL_config headers.
39  So I'm going to inline just the parts I need.
40  I need the stdint types, and
41  I need the compiler define for HAVE_STDIO_H.
42  As far as I can tell, all the shipping SDL2 systems have stdio.
43  I also want the pragma structure packing. My hope is my copied implementation is compatible so you can pointer cast between SDL/ALmixer.
44  */
45 #ifndef ALMIXER_SHOULD_DISABLE_STDIO
46 #define ALMIXER_HAVE_STDIO_H 1
47 #include <stdio.h>
48 /*
49 #include <stddef.h>
50 */
51 #endif /* ifndef ALMIXER_SHOULD_DISABLE_STDIO */
52 
53 /* Most everything except Visual Studio 2008 and earlier has stdint.h now */
54 #if defined(_MSC_VER) && (_MSC_VER < 1600)
55 #ifndef ALMIXER_SHOULD_DISABLE_STDINT_REPLACEMENT
56 /* Here are some reasonable defaults */
57 typedef unsigned int size_t;
58 typedef signed char int8_t;
59 typedef unsigned char uint8_t;
60 typedef signed short int16_t;
61 typedef unsigned short uint16_t;
62 typedef signed int int32_t;
63 typedef unsigned int uint32_t;
64 typedef signed long long int64_t;
65 typedef unsigned long long uint64_t;
66 typedef unsigned long uintptr_t;
67 #define ALMIXER_NOW_USING_STDINT_REPLACEMENT 1
68 #endif /* #ifndef ALMIXER_SHOULD_DISABLE_STDINT_REPLACEMENT */
69 #else
70 #include <stdint.h>
71 #endif /* Visual Studio 2008 */
72 typedef enum
73 {
76 } ALmixer_bool;
77 /*
78 #include "ALmixer_error.h"
79 #include "begin_code.h"
80 */
81 /* Force structure packing at 4 byte alignment.
82  This is necessary if the header is included in code which has structure
83  packing set to an alternate value, say for loading structures from disk.
84  The packing is reset to the previous value in close_code.h
85  */
86 #if defined(_MSC_VER) || defined(__MWERKS__) || defined(__BORLANDC__)
87 #ifdef _MSC_VER
88 #pragma warning(disable: 4103)
89 #endif
90 #ifdef __BORLANDC__
91 #pragma nopackwarning
92 #endif
93 #ifdef _M_X64
94 /* Use 8-byte alignment on 64-bit architectures, so pointers are aligned */
95 #pragma pack(push,8)
96 #else
97 #pragma pack(push,4)
98 #endif
99 #endif /* Compiler needs structure packing set */
100 
101 
102 /* Set up for C function definitions, even when using C++ */
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
107 /* RWops Types */
108 #define ALMIXER_RWOPS_UNKNOWN 0 /* Unknown stream type */
109 #define ALMIXER_RWOPS_WINFILE 1 /* Win32 file */
110 #define ALMIXER_RWOPS_STDFILE 2 /* Stdio file */
111 #define ALMIXER_RWOPS_JNIFILE 3 /* Android asset */
112 #define ALMIXER_RWOPS_MEMORY 4 /* Memory stream */
113 #define ALMIXER_RWOPS_MEMORY_RO 5 /* Read-Only memory stream */
114 
118 typedef struct ALmixer_RWops
119 {
123  int64_t (ALMIXER_CALL * size) (struct ALmixer_RWops * context);
124 
131  int64_t (ALMIXER_CALL * seek) (struct ALmixer_RWops * context, int64_t offset,
132  int whence);
133 
140  size_t (ALMIXER_CALL * read) (struct ALmixer_RWops * context, void *ptr,
141  size_t size, size_t maxnum);
142 
149  size_t (ALMIXER_CALL * write) (struct ALmixer_RWops * context, const void *ptr,
150  size_t size, size_t num);
151 
157  int (ALMIXER_CALL * close) (struct ALmixer_RWops * context);
158 
159  uint32_t type;
160  union
161  {
162 #if defined(__ANDROID__)
163  struct
164  {
165  void *fileNameRef;
166  void *inputStreamRef;
167  void *readableByteChannelRef;
168  void *readMethod;
169  void *assetFileDescriptorRef;
170  long position;
171  long size;
172  long offset;
173  int fd;
174  } androidio;
175 #elif defined(__WIN32__)
176  struct
177  {
178  ALmixer_bool append;
179  void *h;
180  struct
181  {
182  void *data;
183  size_t size;
184  size_t left;
185  } buffer;
186  } windowsio;
187 #endif
188 
189 #ifdef ALMIXER_HAVE_STDIO_H
190  struct
191  {
193  FILE *fp;
194  } stdio;
195 #endif
196  struct
197  {
198  uint8_t *base;
199  uint8_t *here;
200  uint8_t *stop;
201  } mem;
202  struct
203  {
204  void *data1;
205  void *data2;
206  } unknown;
207  } hidden;
208 
209 } ALmixer_RWops;
210 
211 
217 /* @{ */
218 
219 extern ALMIXER_DECLSPEC ALmixer_RWops *ALMIXER_CALL ALmixer_RWFromFile(const char *file,
220  const char *mode);
221 
222 #ifdef ALMIXER_HAVE_STDIO_H
223 extern ALMIXER_DECLSPEC ALmixer_RWops *ALMIXER_CALL ALmixer_RWFromFP(FILE * fp,
225 #else
226 extern ALMIXER_DECLSPEC ALmixer_RWops *ALMIXER_CALL ALmixer_RWFromFP(void * fp,
228 #endif
229 
230 extern ALMIXER_DECLSPEC ALmixer_RWops *ALMIXER_CALL ALmixer_RWFromMem(void *mem, int size);
231 extern ALMIXER_DECLSPEC ALmixer_RWops *ALMIXER_CALL ALmixer_RWFromConstMem(const void *mem,
232  int size);
233 
234 /* @} *//* RWFrom functions */
235 
236 
237 extern ALMIXER_DECLSPEC ALmixer_RWops *ALMIXER_CALL ALmixer_AllocRW(void);
238 extern ALMIXER_DECLSPEC void ALMIXER_CALL ALmixer_FreeRW(ALmixer_RWops * area);
239 
240 #define RW_SEEK_SET 0
241 #define RW_SEEK_CUR 1
242 #define RW_SEEK_END 2
249 /* @{ */
250 #define ALmixer_RWsize(ctx) (ctx)->size(ctx)
251 #define ALmixer_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
252 #define ALmixer_RWtell(ctx) (ctx)->seek(ctx, 0, RW_SEEK_CUR)
253 #define ALmixer_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
254 #define ALmixer_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
255 #define ALmixer_RWclose(ctx) (ctx)->close(ctx)
256 /* @} *//* Read/write macros */
257 
258 
264 /* @{ */
265 extern ALMIXER_DECLSPEC uint8_t ALMIXER_CALL ALmixer_ReadU8(ALmixer_RWops * src);
266 extern ALMIXER_DECLSPEC uint16_t ALMIXER_CALL ALmixer_ReadLE16(ALmixer_RWops * src);
267 extern ALMIXER_DECLSPEC uint16_t ALMIXER_CALL ALmixer_ReadBE16(ALmixer_RWops * src);
268 extern ALMIXER_DECLSPEC uint32_t ALMIXER_CALL ALmixer_ReadLE32(ALmixer_RWops * src);
269 extern ALMIXER_DECLSPEC uint32_t ALMIXER_CALL ALmixer_ReadBE32(ALmixer_RWops * src);
270 extern ALMIXER_DECLSPEC uint64_t ALMIXER_CALL ALmixer_ReadLE64(ALmixer_RWops * src);
271 extern ALMIXER_DECLSPEC uint64_t ALMIXER_CALL ALmixer_ReadBE64(ALmixer_RWops * src);
272 /* @} *//* Read endian functions */
273 
279 /* @{ */
280 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteU8(ALmixer_RWops * dst, uint8_t value);
281 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteLE16(ALmixer_RWops * dst, uint16_t value);
282 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteBE16(ALmixer_RWops * dst, uint16_t value);
283 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteLE32(ALmixer_RWops * dst, uint32_t value);
284 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteBE32(ALmixer_RWops * dst, uint32_t value);
285 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteLE64(ALmixer_RWops * dst, uint64_t value);
286 extern ALMIXER_DECLSPEC size_t ALMIXER_CALL ALmixer_WriteBE64(ALmixer_RWops * dst, uint64_t value);
287 /* @} *//* Write endian functions */
288 
289 
290 /* Ends C function definitions when using C++ */
291 #ifdef __cplusplus
292 }
293 #endif
294 /*
295 #include "close_code.h"
296 */
297 #if defined(_MSC_VER) || defined(__MWERKS__) || defined(__WATCOMC__) || defined(__BORLANDC__)
298 #ifdef __BORLANDC__
299 #pragma nopackwarning
300 #endif
301 #pragma pack(pop)
302 #endif /* Compiler needs structure packing set */
303 #endif /* _ALmixer_rwops_h */
304 
305 /* vi: set ts=4 sw=4 expandtab: */
ALmixer_bool
Definition: ALmixer_RWops.h:72
void * data1
Definition: ALmixer_RWops.h:204
size_t ALmixer_WriteLE32(ALmixer_RWops *dst, uint32_t value)
uint64_t ALmixer_ReadBE64(ALmixer_RWops *src)
uint16_t ALmixer_ReadLE16(ALmixer_RWops *src)
uint8_t * base
Definition: ALmixer_RWops.h:198
ALmixer_RWops * ALmixer_RWFromFP(FILE *fp, ALmixer_bool autoclose)
uint8_t ALmixer_ReadU8(ALmixer_RWops *src)
uint16_t ALmixer_ReadBE16(ALmixer_RWops *src)
uint8_t * here
Definition: ALmixer_RWops.h:199
int64_t(* seek)(struct ALmixer_RWops *context, int64_t offset, int whence)
Seek to offset relative to whence, one of stdio&#39;s whence values: RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END.
Definition: ALmixer_RWops.h:131
struct ALmixer_RWops::@0::@1 stdio
int(* close)(struct ALmixer_RWops *context)
Close and free an allocated ALmixer_RWops structure.
Definition: ALmixer_RWops.h:157
union ALmixer_RWops::@0 hidden
struct ALmixer_RWops::@0::@3 unknown
Definition: ALmixer_RWops.h:75
uint32_t ALmixer_ReadLE32(ALmixer_RWops *src)
struct ALmixer_RWops::@0::@2 mem
void * data2
Definition: ALmixer_RWops.h:205
ALmixer_bool autoclose
Definition: ALmixer_RWops.h:192
ALmixer (which I sometimes call "SDL-OpenAL-Mixer" or "SDL_ALmixer") is a cross-platform audio librar...
ALmixer_RWops * ALmixer_RWFromConstMem(const void *mem, int size)
size_t ALmixer_WriteBE16(ALmixer_RWops *dst, uint16_t value)
void ALmixer_FreeRW(ALmixer_RWops *area)
ALmixer_RWops * ALmixer_AllocRW(void)
size_t(* write)(struct ALmixer_RWops *context, const void *ptr, size_t size, size_t num)
Write exactly num objects each of size size from the area pointed at by ptr to data stream...
Definition: ALmixer_RWops.h:149
uint64_t ALmixer_ReadLE64(ALmixer_RWops *src)
FILE * fp
Definition: ALmixer_RWops.h:193
uint32_t type
Definition: ALmixer_RWops.h:159
ALmixer_RWops * ALmixer_RWFromMem(void *mem, int size)
size_t(* read)(struct ALmixer_RWops *context, void *ptr, size_t size, size_t maxnum)
Read up to maxnum objects each of size size from the data stream to the area pointed at by ptr...
Definition: ALmixer_RWops.h:140
uint8_t * stop
Definition: ALmixer_RWops.h:200
size_t ALmixer_WriteU8(ALmixer_RWops *dst, uint8_t value)
int64_t(* size)(struct ALmixer_RWops *context)
Return the size of the file in this rwops, or -1 if unknown.
Definition: ALmixer_RWops.h:123
ALmixer_RWops * ALmixer_RWFromFile(const char *file, const char *mode)
size_t ALmixer_WriteLE64(ALmixer_RWops *dst, uint64_t value)
size_t ALmixer_WriteLE16(ALmixer_RWops *dst, uint16_t value)
size_t ALmixer_WriteBE32(ALmixer_RWops *dst, uint32_t value)
size_t ALmixer_WriteBE64(ALmixer_RWops *dst, uint64_t value)
uint32_t ALmixer_ReadBE32(ALmixer_RWops *src)
Definition: ALmixer_RWops.h:74
This is the read/write operation structure – very basic.
Definition: ALmixer_RWops.h:118