summaryrefslogtreecommitdiff
path: root/libaudiofile/af_vfs.c
blob: 2c32d8fbfd1044732fe3daa6a551db0bbf71195c (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
/*
	Audio File Library
	Copyright (C) 1999, Elliot Lee <sopwith@redhat.com>

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

	This library 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
	Library General Public License for more details.

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

/*
	af_vfs.c

	Virtual file operations for the Audio File Library.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "afinternal.h"
#include "af_vfs.h"

#include <stdlib.h>

AFvirtualfile *
af_virtual_file_new(void)
{
  return (AFvirtualfile *) calloc(sizeof (AFvirtualfile), 1);
}

void
af_virtual_file_destroy(AFvirtualfile *vfile)
{
  vfile->destroy(vfile);

  free(vfile);
}

size_t af_fread (void *data, size_t size, size_t nmemb, AFvirtualfile *vfile)
{
  if (size == 0 || nmemb == 0)
    return 0;

  if (vfile->read) {
    int retval;

    retval = (* vfile->read) (vfile, data, size * nmemb);

    return retval/size;
  } else
    return 0;
}

size_t af_fwrite (const void *data, size_t size, size_t nmemb,
	AFvirtualfile *vfile)
{
  if (size == 0 || nmemb == 0)
    return 0;

  if (vfile->write) {
    int retval;

    retval = (* vfile->write) (vfile, data, size * nmemb);

    return retval/size;
  } else
    return 0;
}

int
af_fclose(AFvirtualfile *vfile)
{
  af_virtual_file_destroy(vfile);

  return 0;
}

long
af_flength(AFvirtualfile *vfile)
{
  if(vfile->length)
    return (* vfile->length)(vfile);
  else
    return 0;
}

int
af_fseek(AFvirtualfile *vfile, long offset, int whence)
{
  if(whence == SEEK_CUR)
    (* vfile->seek) (vfile, offset, 1);
  else if(whence == SEEK_SET)
    (* vfile->seek) (vfile, offset, 0);
  else
    return -1;

  return 0;
}

long
af_ftell(AFvirtualfile *vfile)
{
  if(vfile->tell)
    return (* vfile->tell)(vfile);
  else
    return 0;
}

static ssize_t af_file_read (AFvirtualfile *vfile, void *data, size_t nbytes);
static long af_file_length (AFvirtualfile *vfile);
static ssize_t af_file_write (AFvirtualfile *vfile, const void *data,
	size_t nbytes);
static void af_file_destroy(AFvirtualfile *vfile);
static long af_file_seek(AFvirtualfile *vfile, long offset, int is_relative);
static long af_file_tell(AFvirtualfile *vfile);

AFvirtualfile *
af_virtual_file_new_for_file(FILE *fh)
{
  AFvirtualfile *vf;

  if(!fh)
    return NULL;

  vf = af_virtual_file_new();
  vf->closure = fh;
  vf->read = af_file_read;
  vf->write = af_file_write;
  vf->length = af_file_length;
  vf->destroy = af_file_destroy;
  vf->seek = af_file_seek;
  vf->tell = af_file_tell;

  return vf;
}

static ssize_t af_file_read(AFvirtualfile *vfile, void *data, size_t nbytes)
{
	return fread(data, 1, nbytes, vfile->closure);
}

static long
af_file_length(AFvirtualfile *vfile)
{
  long curpos, retval;

  curpos = ftell(vfile->closure);
  fseek(vfile->closure, 0, SEEK_END);
  retval = ftell(vfile->closure);
  fseek(vfile->closure, curpos, SEEK_SET);

  return retval;
}

static ssize_t af_file_write (AFvirtualfile *vfile, const void *data,
	size_t nbytes)
{
	return fwrite(data, 1, nbytes, vfile->closure);
}

static void
af_file_destroy(AFvirtualfile *vfile)
{
  fclose(vfile->closure); vfile->closure = NULL;
}

static long
af_file_seek(AFvirtualfile *vfile, long offset, int is_relative)
{
  fseek(vfile->closure, offset, is_relative?SEEK_CUR:SEEK_SET);

  return ftell(vfile->closure);
}

static long
af_file_tell(AFvirtualfile *vfile)
{
  return ftell(vfile->closure);
}