Mercurial > audlegacy-plugins
comparison src/Input/timidity/libtimidity/stream.c @ 0:13389e613d67 trunk
[svn] - initial import of audacious-plugins tree (lots to do)
| author | nenolod |
|---|---|
| date | Mon, 18 Sep 2006 01:11:49 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:13389e613d67 |
|---|---|
| 1 #if HAVE_CONFIG_H | |
| 2 # include <config.h> | |
| 3 #endif | |
| 4 | |
| 5 #include "string.h" | |
| 6 | |
| 7 #include "timidity.h" | |
| 8 #include "timidity_internal.h" | |
| 9 #include "common.h" | |
| 10 | |
| 11 struct _MidIStream | |
| 12 { | |
| 13 MidIStreamReadFunc read; | |
| 14 MidIStreamCloseFunc close; | |
| 15 void *ctx; | |
| 16 }; | |
| 17 | |
| 18 typedef struct StdIOContext | |
| 19 { | |
| 20 VFSFile *fp; | |
| 21 int autoclose; | |
| 22 } StdIOContext; | |
| 23 | |
| 24 size_t | |
| 25 stdio_istream_read (void *ctx, void *ptr, size_t size, size_t nmemb) | |
| 26 { | |
| 27 return vfs_fread (ptr, size, nmemb, ((StdIOContext *) ctx)->fp); | |
| 28 } | |
| 29 | |
| 30 int | |
| 31 stdio_istream_close (void *ctx) | |
| 32 { | |
| 33 int ret = 0; | |
| 34 if (((StdIOContext *) ctx)->autoclose) | |
| 35 ret = vfs_fclose (((StdIOContext *) ctx)->fp); | |
| 36 free (ctx); | |
| 37 return ret; | |
| 38 } | |
| 39 | |
| 40 typedef struct MemContext | |
| 41 { | |
| 42 sint8 *base; | |
| 43 sint8 *current; | |
| 44 sint8 *end; | |
| 45 int autofree; | |
| 46 } MemContext; | |
| 47 | |
| 48 size_t | |
| 49 mem_istream_read (void *ctx, void *ptr, size_t size, size_t nmemb) | |
| 50 { | |
| 51 MemContext *c; | |
| 52 size_t count; | |
| 53 | |
| 54 c = (MemContext *) ctx; | |
| 55 count = nmemb; | |
| 56 | |
| 57 if (c->current + count * size > c->end) | |
| 58 count = (c->end - c->current) / size; | |
| 59 | |
| 60 memcpy (ptr, c->current, count * size); | |
| 61 c->current += count * size; | |
| 62 | |
| 63 return count; | |
| 64 } | |
| 65 | |
| 66 int | |
| 67 mem_istream_close (void *ctx) | |
| 68 { | |
| 69 if (((MemContext *) ctx)->autofree) | |
| 70 free (((MemContext *) ctx)->base); | |
| 71 free (ctx); | |
| 72 return 0; | |
| 73 } | |
| 74 | |
| 75 MidIStream * | |
| 76 mid_istream_open_fp (VFSFile * fp, int autoclose) | |
| 77 { | |
| 78 StdIOContext *ctx; | |
| 79 MidIStream *stream; | |
| 80 | |
| 81 stream = safe_malloc (sizeof (MidIStream)); | |
| 82 if (stream == NULL) | |
| 83 return NULL; | |
| 84 | |
| 85 ctx = safe_malloc (sizeof (StdIOContext)); | |
| 86 if (ctx == NULL) | |
| 87 { | |
| 88 free (stream); | |
| 89 return NULL; | |
| 90 } | |
| 91 ctx->fp = fp; | |
| 92 ctx->autoclose = autoclose; | |
| 93 | |
| 94 stream->ctx = ctx; | |
| 95 stream->read = stdio_istream_read; | |
| 96 stream->close = stdio_istream_close; | |
| 97 | |
| 98 return stream; | |
| 99 } | |
| 100 | |
| 101 MidIStream * | |
| 102 mid_istream_open_file (const char *file) | |
| 103 { | |
| 104 VFSFile *fp; | |
| 105 | |
| 106 fp = vfs_fopen (file, "rb"); | |
| 107 if (fp == NULL) | |
| 108 return NULL; | |
| 109 | |
| 110 return mid_istream_open_fp (fp, 1); | |
| 111 } | |
| 112 | |
| 113 MidIStream * | |
| 114 mid_istream_open_mem (void *mem, size_t size, int autofree) | |
| 115 { | |
| 116 MemContext *ctx; | |
| 117 MidIStream *stream; | |
| 118 | |
| 119 stream = safe_malloc (sizeof (MidIStream)); | |
| 120 if (stream == NULL) | |
| 121 return NULL; | |
| 122 | |
| 123 ctx = safe_malloc (sizeof (MemContext)); | |
| 124 if (ctx == NULL) | |
| 125 { | |
| 126 free (stream); | |
| 127 return NULL; | |
| 128 } | |
| 129 ctx->base = mem; | |
| 130 ctx->current = mem; | |
| 131 ctx->end = ((sint8 *) mem) + size; | |
| 132 ctx->autofree = autofree; | |
| 133 | |
| 134 stream->ctx = ctx; | |
| 135 stream->read = mem_istream_read; | |
| 136 stream->close = mem_istream_close; | |
| 137 | |
| 138 return stream; | |
| 139 } | |
| 140 | |
| 141 MidIStream * | |
| 142 mid_istream_open_callbacks (MidIStreamReadFunc read, | |
| 143 MidIStreamCloseFunc close, void *context) | |
| 144 { | |
| 145 MidIStream *stream; | |
| 146 | |
| 147 stream = safe_malloc (sizeof (MidIStream)); | |
| 148 if (stream == NULL) | |
| 149 return NULL; | |
| 150 | |
| 151 stream->ctx = context; | |
| 152 stream->read = read; | |
| 153 stream->close = close; | |
| 154 | |
| 155 return stream; | |
| 156 } | |
| 157 | |
| 158 size_t | |
| 159 mid_istream_read (MidIStream * stream, void *ptr, size_t size, size_t nmemb) | |
| 160 { | |
| 161 return stream->read (stream->ctx, ptr, size, nmemb); | |
| 162 } | |
| 163 | |
| 164 void | |
| 165 mid_istream_skip (MidIStream * stream, size_t len) | |
| 166 { | |
| 167 size_t c; | |
| 168 char tmp[1024]; | |
| 169 while (len > 0) | |
| 170 { | |
| 171 c = len; | |
| 172 if (c > 1024) | |
| 173 c = 1024; | |
| 174 len -= c; | |
| 175 if (c != mid_istream_read (stream, tmp, 1, c)) | |
| 176 { | |
| 177 DEBUG_MSG ("mid_istream_skip error\n"); | |
| 178 } | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 int | |
| 183 mid_istream_close (MidIStream * stream) | |
| 184 { | |
| 185 int ret = stream->close (stream->ctx); | |
| 186 free (stream); | |
| 187 return ret; | |
| 188 } |
