source: trunk/third/audiofile/libaudiofile/af_vfs.c @ 17099

Revision 17099, 3.8 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17098, which included commits to RCS files with non-trunk default branches.
Line 
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
32AFvirtualfile *
33af_virtual_file_new(void)
34{
35  return (AFvirtualfile *) calloc(sizeof (AFvirtualfile), 1);
36}
37
38void
39af_virtual_file_destroy(AFvirtualfile *vfile)
40{
41  vfile->destroy(vfile);
42
43  free(vfile);
44}
45
46size_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
61size_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
77int
78af_fclose(AFvirtualfile *vfile)
79{
80  af_virtual_file_destroy(vfile);
81
82  return 0;
83}
84
85long
86af_flength(AFvirtualfile *vfile)
87{
88  if(vfile->length)
89    return (* vfile->length)(vfile);
90  else
91    return 0;
92}
93
94int
95af_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
107long
108af_ftell(AFvirtualfile *vfile)
109{
110  if(vfile->tell)
111    return (* vfile->tell)(vfile);
112  else
113    return 0;
114}
115
116static ssize_t af_file_read (AFvirtualfile *vfile, void *data, size_t nbytes);
117static long af_file_length (AFvirtualfile *vfile);
118static ssize_t af_file_write (AFvirtualfile *vfile, const void *data,
119        size_t nbytes);
120static void af_file_destroy(AFvirtualfile *vfile);
121static long af_file_seek(AFvirtualfile *vfile, long offset, int is_relative);
122static long af_file_tell(AFvirtualfile *vfile);
123
124AFvirtualfile *
125af_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
144static ssize_t af_file_read(AFvirtualfile *vfile, void *data, size_t nbytes)
145{
146        return fread(data, 1, nbytes, vfile->closure);
147}
148
149static long
150af_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
162static 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
168static void
169af_file_destroy(AFvirtualfile *vfile)
170{
171  fclose(vfile->closure); vfile->closure = NULL;
172}
173
174static long
175af_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
182static long
183af_file_tell(AFvirtualfile *vfile)
184{
185  return ftell(vfile->closure);
186}
Note: See TracBrowser for help on using the repository browser.