File: stream.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (294 lines) | stat: -rw-r--r-- 6,817 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <simage.h>
#include <simage_private.h>
#include <string.h>

#ifdef SIMAGE_OGGVORBIS_SUPPORT
#include "simage_oggvorbis.h"
#endif /* SIMAGE_OGGVORBIS_SUPPORT */

#ifdef SIMAGE_LIBSNDFILE_SUPPORT
#include "simage_libsndfile.h"
#endif /* SIMAGE_LIBSNDFILE_SUPPORT */

struct simage_stream_s {
  char * filename;
  
  s_stream_open_func * open;
  s_stream_create_func * create;
  s_stream_get_func * get;
  s_stream_put_func * put;
  s_stream_close_func * close;
  s_stream_seek_func * seek;
  s_stream_tell_func * tell;
  
  s_params * params;
  void *context;
};

struct simage_stream_importer {
  s_stream_open_func * open;
  s_stream_get_func * get;
  s_stream_seek_func * seek;
  s_stream_tell_func * tell;
  s_stream_close_func * close;  

  struct simage_stream_importer * next;
};

struct simage_stream_exporter {
  s_stream_create_func * create;
  s_stream_put_func * put;
  s_stream_seek_func * seek;
  s_stream_tell_func * tell;
  s_stream_close_func * close;  
  
  struct simage_stream_exporter * next;
};

// FIXME: convert access to these variables into the singleton
// pattern. 20010917 mortene.
static struct simage_stream_importer * importers;
static struct simage_stream_exporter * exporters;

static void 
add_internal_importers(void)
{
  static int first = 1;
  if (first) {
#ifdef SIMAGE_OGGVORBIS_SUPPORT
    s_stream_importer_add_ex(oggvorbis_reader_stream_open,
                             oggvorbis_reader_stream_get, 
                             oggvorbis_reader_stream_seek,
                             oggvorbis_reader_stream_tell,
                             oggvorbis_reader_stream_close);
#endif
#ifdef SIMAGE_LIBSNDFILE_SUPPORT
    s_stream_importer_add_ex(libsndfile_stream_open,
                             libsndfile_stream_get, 
                             libsndfile_stream_seek,
                             libsndfile_stream_tell,
                             libsndfile_stream_close);
#endif
    first = 0;
  }                
}

static void
add_internal_exporters(void)
{
  static int first = 1;
  if (first) {    
    /* none yet */
   first = 0;
  }
}

s_stream * 
s_stream_open(const char * filename, s_params * params)
{
  struct simage_stream_importer * imp;
  s_stream * stream;

  stream = (s_stream*) malloc(sizeof(s_stream));
  stream->params = NULL;
  stream->filename = NULL;
  stream->context = NULL;

  add_internal_importers();

  imp = importers;
  while (imp) {
    if (imp->open(filename, stream, params)) break;
    imp = imp->next;
  }
  if (imp == NULL) {
    free((void*)stream);
    return NULL;
  }

  stream->filename = (char*) malloc(strlen(filename)+1);
  strcpy(stream->filename, filename);
  stream->open = imp->open;
  stream->get = imp->get;
  stream->close = imp->close;
  stream->seek = imp->seek;
  stream->tell = imp->tell;

  return stream;
}

s_stream * 
s_stream_create(const char * filename, s_params * params /* | NULL */)
{
  struct simage_stream_exporter * exp;
  s_stream * stream = (s_stream*) malloc(sizeof(s_stream));
  stream->params = NULL;
  stream->filename = NULL;
  stream->context = NULL;
  
  add_internal_exporters();

  exp = exporters;
  while (exp) {
    if (exp->create(filename, stream, params)) break;
    exp = exp->next;
  }
  if (exp == NULL) {
    free((void*) stream);
    return NULL;
  }

  stream->filename = (char*) malloc(strlen(filename)+1);
  stream->create = exp->create;
  stream->put = exp->put;
  stream->close = exp->close;
  stream->seek = exp->seek;
  stream->tell = exp->tell;
  strcpy(stream->filename, filename);
  return stream;
}

void * 
s_stream_get_buffer(s_stream * stream, void * buffer,
                           int *size, s_params * params)
{
  return stream->get(stream, buffer, size, params);
}

int 
s_stream_put_buffer(s_stream * stream, void * buffer, 
                        int size, s_params * params)
{
  return stream->put(stream, buffer, size, params);
}

void 
s_stream_close(s_stream * stream)
{
  stream->close(stream);
}

void 
s_stream_destroy(s_stream * stream)
{
  if (stream->params) s_params_destroy(stream->params);
  if (stream->filename) free((void*) stream->filename);
  free((void*) stream);
}

s_params * 
s_stream_params(s_stream * stream)
{
  if (stream->params == NULL) {
    stream->params = s_params_create();
  }
  return stream->params;
}

void *
s_stream_context_get(s_stream *stream)
{
  return stream->context;
}

void 
s_stream_context_set(s_stream *stream, void *context)
{
  stream->context = context;
}

void 
s_stream_importer_add(s_stream_open_func * open,
                      s_stream_get_func * get,
                      s_stream_close_func * close)
{
  s_stream_importer_add_ex(open, get, NULL, NULL, close);
}

void 
s_stream_importer_add_ex(s_stream_open_func * open,
                         s_stream_get_func * get,
                         s_stream_seek_func * seek,
                         s_stream_tell_func * tell,
                         s_stream_close_func * close)
{
  struct simage_stream_importer * last, * imp = importers;
  last = NULL;
  while (imp) {
    last = imp;
    imp = imp->next;
  }
  imp = (struct simage_stream_importer*) malloc(sizeof(struct simage_stream_importer));
  imp->open = open;
  imp->get = get;
  imp->close = close;
  imp->seek = seek;
  imp->tell = tell;
  imp->next = NULL;

  if (last == NULL) {
    importers = imp;
  }
  else last->next = imp;
}

void 
s_stream_exporter_add(s_stream_create_func * create,
                      s_stream_put_func * put,
                      s_stream_close_func * close)
{
  s_stream_exporter_add_ex(create, put, NULL, NULL, close);
}

void 
s_stream_exporter_add_ex(s_stream_create_func * create,
                         s_stream_put_func * put,
                         s_stream_seek_func * seek,
                         s_stream_tell_func * tell,
                         s_stream_close_func * close)
{
  struct simage_stream_exporter * last, * exp = exporters;
  last = NULL;
  while (exp) {
    last = exp;
    exp = exp->next;
  }
  exp = (struct simage_stream_exporter*) malloc(sizeof(struct simage_stream_exporter));
  exp->create = create;
  exp->put = put;
  exp->close = close;
  exp->seek = seek;
  exp->tell = tell;
  exp->next = NULL;

  if (last == NULL) {
    exporters = exp;
  }
  else last->next = exp;
}

int 
s_stream_seek(s_stream * stream, int offset, int whence,
              s_params * params /* | NULL */)
{
  if (stream->seek)
    return stream->seek(stream, offset, whence, params);
  else
    return -1;
}

int s_stream_tell(s_stream *stream,
                  s_params * params /* | NULL */)
{
  if (stream->tell)
    return stream->tell(stream, params);
  else
    return -1;
}