aboutsummaryrefslogtreecommitdiff
path: root/lib/timidity/libarc/url_cache.c
blob: dfcc422d71bdf1db9b02910c17a5d405d33831eb (plain)
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
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>

#include "timidity.h"
#include "url.h"
#include "memb.h"

typedef struct _URL_cache
{
    char common[sizeof(struct _URL)];
    URL reader;
    int memb_ok;
    MemBuffer b;
    long pos;
    int autoclose;
} URL_cache;

static long url_cache_read(URL url, void *buff, long n);
static int url_cache_fgetc(URL url);
static long url_cache_seek(URL url, long offset, int whence);
static long url_cache_tell(URL url);
static void url_cache_close(URL url);

URL url_cache_open(URL url, int autoclose)
{
    URL_cache *urlp;

    if(url->type == URL_cache_t && autoclose)
    {
	if(((URL_cache *)url)->memb_ok)
	    delete_memb(&((URL_cache *)url)->b);
	urlp = (URL_cache *)url;
	url = urlp->reader;
    }
    else
    {
	if((urlp = (URL_cache *)alloc_url(sizeof(URL_cache))) == NULL)
	{
	    if(autoclose)
		url_close(url);
	    return NULL;
	}
    }

    /* common members */
    URLm(urlp, type)	  = URL_cache_t;
    URLm(urlp, url_read)  = url_cache_read;
    URLm(urlp, url_gets)  = NULL;
    URLm(urlp, url_fgetc) = url_cache_fgetc;
    URLm(urlp, url_seek)  = url_cache_seek;
    URLm(urlp, url_tell)  = url_cache_tell;
    URLm(urlp, url_close) = url_cache_close;

    /* private members */
    urlp->reader = url;
    urlp->memb_ok = 1;
    init_memb(&urlp->b);
    urlp->pos = 0;
    urlp->autoclose = autoclose;

    return (URL)urlp;
}

void url_cache_disable(URL url)
{
    if(url->type == URL_cache_t)
	url->url_seek = NULL;
}

void url_cache_detach(URL url)
{
    if(url != NULL && url->type == URL_cache_t)
    {
	URL_cache *urlp = (URL_cache *)url;
	if(urlp->autoclose && urlp->reader != NULL)
	    url_close(urlp->reader);
	urlp->reader = NULL;
    }
}

static long url_cache_read(URL url, void *buff, long n)
{
    URL_cache *urlp = (URL_cache *)url;
    MemBuffer *b = &urlp->b;

    if(!urlp->memb_ok)
    {
	if(urlp->reader == NULL)
	    return 0;
	n = url_read(urlp->reader, buff, n);
	if(n > 0)
	    urlp->pos += n;
	return n;
    }

    if(urlp->pos < b->total_size)
    {
	if(n > b->total_size - urlp->pos)
	    n = b->total_size - urlp->pos;
	urlp->pos += read_memb(b, buff, n);
	return n;
    }

    if(url->url_seek == NULL)
    {
	delete_memb(b);
	urlp->memb_ok = 0;
	if(urlp->reader == NULL)
	    return 0;
	n = url_read(urlp->reader, buff, n);
	if(n > 0)
	    urlp->pos += n;
	return n;
    }

    if(urlp->reader == NULL)
	return 0;
    n = url_read(urlp->reader, buff, n);
    if(n > 0)
    {
	push_memb(b, buff, n);
	b->cur = b->tail;
	b->cur->pos = b->cur->size;
	urlp->pos += n;
    }
    return n;
}

static int url_cache_fgetc(URL url)
{
    URL_cache *urlp = (URL_cache *)url;
    MemBuffer *b = &urlp->b;
    char c;
    int i;

    if(!urlp->memb_ok)
    {
	if(urlp->reader == NULL)
	    return EOF;
	if((i = url_getc(urlp->reader)) == EOF)
	    return EOF;
	urlp->pos++;
	return i;
    }

    if(urlp->pos < b->total_size)
    {
	read_memb(b, &c, 1);
	urlp->pos++;
	return (int)(unsigned char)c;
    }

    if(url->url_seek == NULL)
    {
	delete_memb(b);
	urlp->memb_ok = 0;
	if(urlp->reader == NULL)
	    return EOF;
	if((i = url_getc(urlp->reader)) == EOF)
	    return EOF;
	urlp->pos++;
	return i;
    }

    if(urlp->reader == NULL)
	return EOF;
    if((i = url_getc(urlp->reader)) == EOF)
	return EOF;
    c = (char)i;
    push_memb(b, &c, 1);
    b->cur = b->tail;
    b->cur->pos = b->cur->size;
    urlp->pos++;
    return i;
}

static long url_cache_seek(URL url, long offset, int whence)
{
    URL_cache *urlp = (URL_cache *)url;
    MemBuffer *b = &urlp->b;
    long ret, newpos, n, s;

    ret = urlp->pos;
    switch(whence)
    {
      case SEEK_SET:
	newpos = offset;
	break;
      case SEEK_CUR:
	newpos = ret + offset;
	break;
      case SEEK_END:
	while(url_cache_fgetc(url) != EOF)
	    ;
	newpos = b->total_size + whence;
	break;
      default:
	url_errno = errno = EPERM;
	return -1;
    }
    if(newpos < 0)
	newpos = 0;
    n = newpos - ret;

    if(n < 0)
    {
	rewind_memb(b);
	n = newpos;
	urlp->pos = 0;
    }

    s = skip_read_memb(b, n);
    urlp->pos += s;
    while(s++ < n && url_cache_fgetc(url) != EOF)
	;
    return ret;
}

static long url_cache_tell(URL url)
{
    return ((URL_cache *)url)->pos;
}

static void url_cache_close(URL url)
{
    URL_cache *urlp = (URL_cache *)url;
    if(urlp->autoclose && urlp->reader != NULL)
	url_close(urlp->reader);
    if(urlp->memb_ok)
	delete_memb(&urlp->b);
    free(urlp);
}