openTRI 0.1
streams.h
1/*
2STREAMS - General purpose IO streams interface
3Copyright (C) 2007 Raphael
4
5E-mail: raphael@fx-world.org
6homepage: http://wordpress.fx-world.org
7*/
8
9
10#ifndef __streams_h__
11#define __streams_h__
12
13
14#ifdef __PSP__
15#include <pspiofilemgr.h>
16#endif
17
18#include <zlib.h>
19
20
21#define STREAM_TYPE_MEM 0x01
22#define STREAM_TYPE_FILE 0x02
23#define STREAM_TYPE_AFILE 0x03
24#define STREAM_TYPE_BITS 0x04
25#define STREAM_TYPE_URL 0x05
26#define STREAM_TYPE_CON 0x06
27// 0x07 - 0x1F are reserved for future hardcoded streams
28// 0x20 - 0x7F are reserved for custom streams (use stream_get_typeid())
29#define STREAM_TYPE_BUF 0x80
30#define STREAM_TYPE_ZIP 0x100
31
32
33// seek function flags
34#define STREAM_START 0x0
35#define STREAM_SET STREAM_START
36#define STREAM_CUR 0x1
37#define STREAM_END 0x2
38
39
40#ifdef __PSP__
41#define STREAM_RDONLY PSP_O_RDONLY
42#define STREAM_WRONLY PSP_O_WRONLY
43#define STREAM_RDWR PSP_O_RDWR
44#define STREAM_APPEND PSP_O_APPEND
45#define STREAM_CREATE PSP_O_CREAT
46#define STREAM_TRUNC PSP_O_TRUNC
47#else
48#define STREAM_RDONLY _O_RDONLY
49#define STREAM_WRONLY _O_WRONLY
50#define STREAM_RDWR _O_RDWR
51#define STREAM_APPEND _O_APPEND
52#define STREAM_CREATE _O_CREAT
53#define STREAM_TRUNC _O_TRUNC
54#endif
55
56#define STREAM_ALL (STREAM_RDWR|STREAM_APPEND|STREAM_CREATE|STREAM_TRUNC)
57
58typedef struct stream_struct stream;
59
60typedef stream* (*stream_open_func)( char*, long );
61typedef int (*stream_close_func)( stream* );
62typedef long (*stream_read_func)( stream*, void*, unsigned long );
63typedef long (*stream_write_func)( stream*, void*, unsigned long );
64typedef long (*stream_seek_func)( stream*, long, unsigned int );
65typedef long (*stream_tell_func)( stream* );
66
67
68typedef struct stream_base_struct {
69 int type; // Stream type ID
70 int eos; // EOS flag (0 for protocols)
71 unsigned int size; // Stream size in bytes (0 for protocols)
72 unsigned int mode; // Stream open mode (possible open modes for protocols)
73 char name[256]; // Stream name (filename/URL/memory address)
74
75 stream_open_func open;
76 stream_close_func close;
77 stream_read_func read;
78 stream_write_func write;
79 stream_seek_func seek;
80 stream_tell_func tell;
81} stream_base, stream_protocol;
82
83
85 stream_protocol* protocol;
86 struct stream_protocol_list_struct* next;
87 } stream_protocol_list;
88
89
90
91// Memory IO stream
92typedef struct mstream_struct {
93 stream_base s;
94 char* data;
95 char* cur;
96
97 unsigned long size;
98 unsigned long pos;
99} mstream;
100
101
102
103// File IO stream
104typedef struct fstream_struct {
105 stream_base s;
106 #ifdef __PSP__
107 int fd;
108 #else
109 FILE* fd;
110 #endif
111
112 unsigned long fsize;
113} fstream;
114
115
116#define BFILE_WAIT_READ 1
117#define BFILE_WAIT_WRITE 2
118#define BFILE_WAIT_WRITEINCOMPLETE 4
119
120
121#define BFILE_BUFFER_SIZE (1024*128) // 128kb seems best size for MemStick
122// Buffered file IO stream with asynchronous reads/writes
123typedef struct afstream_struct {
124 stream_base s;
125 #ifdef __PSP__
126 int fd;
127 #else
128 FILE* fd;
129 #endif
130
131 char data[BFILE_BUFFER_SIZE]; // Read/Write buffer
132 char* cur;
133 int buf; // Current buffer number
134
135 int wait; // flag to show that an async read operation is pending
136 int async_error; // holding last async read/write error status
137
138 unsigned long bsize; // size of remaining buffered data
139 unsigned long fsize; // size of complete file
140 unsigned long fpos; // current file position (needed for async read/writes)
141} afstream;
142
143
144
145// Bitstream
146typedef struct bstream_struct {
147 stream_base s;
148
149 unsigned int* data;
150 unsigned int* cur;
151
152 unsigned int len; // current number of bits in bitstream
153 unsigned int pos; // current bitposition in bitstream
154 unsigned int size; // size of bitstream in bytes (allocated mem)
155 unsigned int buf1; // current bitbuffer
156 unsigned int buf2; // next bitbuffer (to make sure 32bit minimum can be read at once without problems)
157 unsigned int bpos; // current position in bitbuffer
158} bstream;
159
160
161
162// Buffered IO stream layer
163typedef struct bufstream_struct {
164 stream_base s; // the stream protocol
165
166 stream* _s; // the real opened stream
167
168 char data[BFILE_BUFFER_SIZE];
169 char* cur; // start of read-ahead buffer
170 long wpos; // last real write file position
171
172 int wait;
173 long bsize; // size of remaining buffered data
174 long fpos; // Virtual file position
175} bufstream;
176
177
178// ZLib compressed stream (not a .zip file reader!)
179typedef struct zstream_struct {
180 stream_base s; // the stream protocol
181
182 stream* _s; // the real opened stream
183
184 z_stream z; // the z stream structure
185
186 char inbuf[BFILE_BUFFER_SIZE];
187 char outbuf[BFILE_BUFFER_SIZE];
188 char* cur; // start of read-ahead buffer
189 long wpos; // last real write file position
190 unsigned long crc;
191
192 int wait;
193 long bsize; // size of remaining buffered data
194 long fpos; // Virtual file position
195
196 long spos; // Start of compressed stream data (no header)
197} zstream;
198
199
201 stream_base* _stream;
202};
203
204
205
206stream* stream_fopen( char* name, long flags ); // open local file stream
207stream* stream_afopen( char* name, long flags ); // open local async buffered file stream
208stream* stream_mopen( char* data, long size ); // open memory stream
209stream* stream_bopen( char* data, long size ); // open bitstream
210stream* stream_uopen( char* name, long flags ); // open net URL stream (not implemented yet)
211stream* stream_open( int type, char* param1, long param2 ); // generic stream opener
212
213stream* stream_zopen( int type, char* param1, long param2 );
214stream* stream_bufopen( int type, char* param1, long param2 ); // buffered stream opener (can also use stream_open( STREAM_TYPE_BUF | STREAM_TYPE_*, ... ))
215
216
217int stream_close( stream* s );
218
219// Read size bytes from stream into buf
220long stream_read( stream* s, void* buf, unsigned long size );
221
222// Write size bytes from buf into stream
223long stream_write( stream* s, void* buf, unsigned long size );
224
225// Change position in stream
226long stream_seek( stream* s, long offs, unsigned int dir );
227
228// Get current position in stream
229long stream_tell( stream* s );
230
231// Reset stream position
232long stream_rewind( stream* s );
233
234// Get size of stream in bytes
235long stream_size( stream* s );
236
237// Get EOS (End of stream) flag of stream
238long stream_eos( stream* s );
239
240// Get type ID of stream
241long stream_type( stream* s );
242
243// Get stream name (filename/URL/memory address)
244char* stream_name( stream* s );
245
246
247// Extend the supported protocols at runtime
248int stream_register_protocol( stream_protocol* p );
249int stream_unregister_protocol( stream_protocol* p );
250// Retrieve the next free custom type ID or -1 if no more are available
251int stream_get_typeid();
252
253
254// VFPU optimized memcpy function for usage by protocols (falls back to libc malloc if not on PSP)
255void* memcpy_vfpu( void* dst, void* src, unsigned int size );
256
257
258#endif //__streams_h__
Definition streams.h:123
Definition streams.h:146
Definition streams.h:163
Definition streams.h:104
Definition streams.h:92
Definition streams.h:68
Definition streams.h:84
Definition streams.h:200
Definition streams.h:179