1 | /* |
---|
2 | Audio File Library |
---|
3 | Copyright (C) 1999, Elliot Lee <sopwith@redhat.com> |
---|
4 | |
---|
5 | This library is free software; you can redistribute it and/or |
---|
6 | modify it under the terms of the GNU Library General Public |
---|
7 | License as published by the Free Software Foundation; either |
---|
8 | version 2 of the License, or (at your option) any later version. |
---|
9 | |
---|
10 | This library is distributed in the hope that it will be useful, |
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | Library General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU Library General Public |
---|
16 | License along with this library; if not, write to the |
---|
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | Boston, MA 02111-1307 USA. |
---|
19 | */ |
---|
20 | |
---|
21 | /* |
---|
22 | af_vfs.c |
---|
23 | |
---|
24 | Virtual file operations for the Audio File Library. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "afinternal.h" |
---|
28 | #include "af_vfs.h" |
---|
29 | |
---|
30 | #include <stdlib.h> |
---|
31 | |
---|
32 | AFvirtualfile * |
---|
33 | af_virtual_file_new(void) |
---|
34 | { |
---|
35 | return (AFvirtualfile *) calloc(sizeof (AFvirtualfile), 1); |
---|
36 | } |
---|
37 | |
---|
38 | void |
---|
39 | af_virtual_file_destroy(AFvirtualfile *vfile) |
---|
40 | { |
---|
41 | vfile->destroy(vfile); |
---|
42 | |
---|
43 | free(vfile); |
---|
44 | } |
---|
45 | |
---|
46 | size_t af_fread (void *data, size_t size, size_t nmemb, AFvirtualfile *vfile) |
---|
47 | { |
---|
48 | if (size == 0 || nmemb == 0) |
---|
49 | return 0; |
---|
50 | |
---|
51 | if (vfile->read) { |
---|
52 | int retval; |
---|
53 | |
---|
54 | retval = (* vfile->read) (vfile, data, size * nmemb); |
---|
55 | |
---|
56 | return retval/size; |
---|
57 | } else |
---|
58 | return 0; |
---|
59 | } |
---|
60 | |
---|
61 | size_t af_fwrite (const void *data, size_t size, size_t nmemb, |
---|
62 | AFvirtualfile *vfile) |
---|
63 | { |
---|
64 | if (size == 0 || nmemb == 0) |
---|
65 | return 0; |
---|
66 | |
---|
67 | if (vfile->write) { |
---|
68 | int retval; |
---|
69 | |
---|
70 | retval = (* vfile->write) (vfile, data, size * nmemb); |
---|
71 | |
---|
72 | return retval/size; |
---|
73 | } else |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | int |
---|
78 | af_fclose(AFvirtualfile *vfile) |
---|
79 | { |
---|
80 | af_virtual_file_destroy(vfile); |
---|
81 | |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | long |
---|
86 | af_flength(AFvirtualfile *vfile) |
---|
87 | { |
---|
88 | if(vfile->length) |
---|
89 | return (* vfile->length)(vfile); |
---|
90 | else |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | int |
---|
95 | af_fseek(AFvirtualfile *vfile, long offset, int whence) |
---|
96 | { |
---|
97 | if(whence == SEEK_CUR) |
---|
98 | (* vfile->seek) (vfile, offset, 1); |
---|
99 | else if(whence == SEEK_SET) |
---|
100 | (* vfile->seek) (vfile, offset, 0); |
---|
101 | else |
---|
102 | return -1; |
---|
103 | |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | long |
---|
108 | af_ftell(AFvirtualfile *vfile) |
---|
109 | { |
---|
110 | if(vfile->tell) |
---|
111 | return (* vfile->tell)(vfile); |
---|
112 | else |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | static ssize_t af_file_read (AFvirtualfile *vfile, void *data, size_t nbytes); |
---|
117 | static long af_file_length (AFvirtualfile *vfile); |
---|
118 | static ssize_t af_file_write (AFvirtualfile *vfile, const void *data, |
---|
119 | size_t nbytes); |
---|
120 | static void af_file_destroy(AFvirtualfile *vfile); |
---|
121 | static long af_file_seek(AFvirtualfile *vfile, long offset, int is_relative); |
---|
122 | static long af_file_tell(AFvirtualfile *vfile); |
---|
123 | |
---|
124 | AFvirtualfile * |
---|
125 | af_virtual_file_new_for_file(FILE *fh) |
---|
126 | { |
---|
127 | AFvirtualfile *vf; |
---|
128 | |
---|
129 | if(!fh) |
---|
130 | return NULL; |
---|
131 | |
---|
132 | vf = af_virtual_file_new(); |
---|
133 | vf->closure = fh; |
---|
134 | vf->read = af_file_read; |
---|
135 | vf->write = af_file_write; |
---|
136 | vf->length = af_file_length; |
---|
137 | vf->destroy = af_file_destroy; |
---|
138 | vf->seek = af_file_seek; |
---|
139 | vf->tell = af_file_tell; |
---|
140 | |
---|
141 | return vf; |
---|
142 | } |
---|
143 | |
---|
144 | static ssize_t af_file_read(AFvirtualfile *vfile, void *data, size_t nbytes) |
---|
145 | { |
---|
146 | return fread(data, 1, nbytes, vfile->closure); |
---|
147 | } |
---|
148 | |
---|
149 | static long |
---|
150 | af_file_length(AFvirtualfile *vfile) |
---|
151 | { |
---|
152 | long curpos, retval; |
---|
153 | |
---|
154 | curpos = ftell(vfile->closure); |
---|
155 | fseek(vfile->closure, 0, SEEK_END); |
---|
156 | retval = ftell(vfile->closure); |
---|
157 | fseek(vfile->closure, curpos, SEEK_SET); |
---|
158 | |
---|
159 | return retval; |
---|
160 | } |
---|
161 | |
---|
162 | static ssize_t af_file_write (AFvirtualfile *vfile, const void *data, |
---|
163 | size_t nbytes) |
---|
164 | { |
---|
165 | return fwrite(data, 1, nbytes, vfile->closure); |
---|
166 | } |
---|
167 | |
---|
168 | static void |
---|
169 | af_file_destroy(AFvirtualfile *vfile) |
---|
170 | { |
---|
171 | fclose(vfile->closure); vfile->closure = NULL; |
---|
172 | } |
---|
173 | |
---|
174 | static long |
---|
175 | af_file_seek(AFvirtualfile *vfile, long offset, int is_relative) |
---|
176 | { |
---|
177 | fseek(vfile->closure, offset, is_relative?SEEK_CUR:SEEK_SET); |
---|
178 | |
---|
179 | return ftell(vfile->closure); |
---|
180 | } |
---|
181 | |
---|
182 | static long |
---|
183 | af_file_tell(AFvirtualfile *vfile) |
---|
184 | { |
---|
185 | return ftell(vfile->closure); |
---|
186 | } |
---|