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 | #ifdef HAVE_CONFIG_H |
---|
28 | #include <config.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include "afinternal.h" |
---|
32 | #include "af_vfs.h" |
---|
33 | |
---|
34 | #include <stdlib.h> |
---|
35 | |
---|
36 | AFvirtualfile * |
---|
37 | af_virtual_file_new(void) |
---|
38 | { |
---|
39 | return (AFvirtualfile *) calloc(sizeof (AFvirtualfile), 1); |
---|
40 | } |
---|
41 | |
---|
42 | void |
---|
43 | af_virtual_file_destroy(AFvirtualfile *vfile) |
---|
44 | { |
---|
45 | vfile->destroy(vfile); |
---|
46 | |
---|
47 | free(vfile); |
---|
48 | } |
---|
49 | |
---|
50 | size_t af_fread (void *data, size_t size, size_t nmemb, AFvirtualfile *vfile) |
---|
51 | { |
---|
52 | if (size == 0 || nmemb == 0) |
---|
53 | return 0; |
---|
54 | |
---|
55 | if (vfile->read) { |
---|
56 | int retval; |
---|
57 | |
---|
58 | retval = (* vfile->read) (vfile, data, size * nmemb); |
---|
59 | |
---|
60 | return retval/size; |
---|
61 | } else |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | size_t af_fwrite (const void *data, size_t size, size_t nmemb, |
---|
66 | AFvirtualfile *vfile) |
---|
67 | { |
---|
68 | if (size == 0 || nmemb == 0) |
---|
69 | return 0; |
---|
70 | |
---|
71 | if (vfile->write) { |
---|
72 | int retval; |
---|
73 | |
---|
74 | retval = (* vfile->write) (vfile, data, size * nmemb); |
---|
75 | |
---|
76 | return retval/size; |
---|
77 | } else |
---|
78 | return 0; |
---|
79 | } |
---|
80 | |
---|
81 | int |
---|
82 | af_fclose(AFvirtualfile *vfile) |
---|
83 | { |
---|
84 | af_virtual_file_destroy(vfile); |
---|
85 | |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | long |
---|
90 | af_flength(AFvirtualfile *vfile) |
---|
91 | { |
---|
92 | if(vfile->length) |
---|
93 | return (* vfile->length)(vfile); |
---|
94 | else |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | int |
---|
99 | af_fseek(AFvirtualfile *vfile, long offset, int whence) |
---|
100 | { |
---|
101 | if(whence == SEEK_CUR) |
---|
102 | (* vfile->seek) (vfile, offset, 1); |
---|
103 | else if(whence == SEEK_SET) |
---|
104 | (* vfile->seek) (vfile, offset, 0); |
---|
105 | else |
---|
106 | return -1; |
---|
107 | |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | long |
---|
112 | af_ftell(AFvirtualfile *vfile) |
---|
113 | { |
---|
114 | if(vfile->tell) |
---|
115 | return (* vfile->tell)(vfile); |
---|
116 | else |
---|
117 | return 0; |
---|
118 | } |
---|
119 | |
---|
120 | static ssize_t af_file_read (AFvirtualfile *vfile, void *data, size_t nbytes); |
---|
121 | static long af_file_length (AFvirtualfile *vfile); |
---|
122 | static ssize_t af_file_write (AFvirtualfile *vfile, const void *data, |
---|
123 | size_t nbytes); |
---|
124 | static void af_file_destroy(AFvirtualfile *vfile); |
---|
125 | static long af_file_seek(AFvirtualfile *vfile, long offset, int is_relative); |
---|
126 | static long af_file_tell(AFvirtualfile *vfile); |
---|
127 | |
---|
128 | AFvirtualfile * |
---|
129 | af_virtual_file_new_for_file(FILE *fh) |
---|
130 | { |
---|
131 | AFvirtualfile *vf; |
---|
132 | |
---|
133 | if(!fh) |
---|
134 | return NULL; |
---|
135 | |
---|
136 | vf = af_virtual_file_new(); |
---|
137 | vf->closure = fh; |
---|
138 | vf->read = af_file_read; |
---|
139 | vf->write = af_file_write; |
---|
140 | vf->length = af_file_length; |
---|
141 | vf->destroy = af_file_destroy; |
---|
142 | vf->seek = af_file_seek; |
---|
143 | vf->tell = af_file_tell; |
---|
144 | |
---|
145 | return vf; |
---|
146 | } |
---|
147 | |
---|
148 | static ssize_t af_file_read(AFvirtualfile *vfile, void *data, size_t nbytes) |
---|
149 | { |
---|
150 | return fread(data, 1, nbytes, vfile->closure); |
---|
151 | } |
---|
152 | |
---|
153 | static long |
---|
154 | af_file_length(AFvirtualfile *vfile) |
---|
155 | { |
---|
156 | long curpos, retval; |
---|
157 | |
---|
158 | curpos = ftell(vfile->closure); |
---|
159 | fseek(vfile->closure, 0, SEEK_END); |
---|
160 | retval = ftell(vfile->closure); |
---|
161 | fseek(vfile->closure, curpos, SEEK_SET); |
---|
162 | |
---|
163 | return retval; |
---|
164 | } |
---|
165 | |
---|
166 | static ssize_t af_file_write (AFvirtualfile *vfile, const void *data, |
---|
167 | size_t nbytes) |
---|
168 | { |
---|
169 | return fwrite(data, 1, nbytes, vfile->closure); |
---|
170 | } |
---|
171 | |
---|
172 | static void |
---|
173 | af_file_destroy(AFvirtualfile *vfile) |
---|
174 | { |
---|
175 | fclose(vfile->closure); vfile->closure = NULL; |
---|
176 | } |
---|
177 | |
---|
178 | static long |
---|
179 | af_file_seek(AFvirtualfile *vfile, long offset, int is_relative) |
---|
180 | { |
---|
181 | fseek(vfile->closure, offset, is_relative?SEEK_CUR:SEEK_SET); |
---|
182 | |
---|
183 | return ftell(vfile->closure); |
---|
184 | } |
---|
185 | |
---|
186 | static long |
---|
187 | af_file_tell(AFvirtualfile *vfile) |
---|
188 | { |
---|
189 | return ftell(vfile->closure); |
---|
190 | } |
---|