1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2001 *
9 * by Stan Seibert <volsung@xiph.org> AND OTHER CONTRIBUTORS *
10 * http://www.xiph.org/ *
12 ********************************************************************
16 ********************************************************************/
26 #include "transport.h"
30 typedef struct file_private_t
{
32 data_source_stats_t stats
;
37 transport_t file_transport
; /* Forward declaration */
40 int file_can_transport (char *source_string
)
42 return 1; /* The file transport is tested last, so always try it */
45 data_source_t
* file_open (char *source_string
, ogg123_options_t
*ogg123_opts
)
47 data_source_t
*source
;
48 file_private_t
*private;
50 /* Allocate data source structures */
51 source
= malloc(sizeof(data_source_t
));
52 private = malloc(sizeof(file_private_t
));
54 if (source
!= NULL
&& private != NULL
) {
55 source
->source_string
= strdup(source_string
);
56 source
->transport
= &file_transport
;
57 source
->private = private;
59 private->seekable
= 1;
60 private->stats
.transfer_rate
= 0;
61 private->stats
.bytes_read
= 0;
62 private->stats
.input_buffer_used
= 0;
64 fprintf(stderr
, _("ERROR: Out of memory.\n"));
69 if (strcmp(source_string
, "-") == 0) {
71 private->seekable
= 0;
73 private->fp
= fopen(source_string
, "r");
75 if (private->fp
== NULL
) {
76 free(source
->source_string
);
87 int file_peek (data_source_t
*source
, void *ptr
, size_t size
, size_t nmemb
)
89 file_private_t
*private = source
->private;
90 FILE *fp
= private->fp
;
94 if (!private->seekable
) return 0;
96 /* Record where we are */
99 items
= fread(ptr
, size
, nmemb
, fp
);
101 /* Now go back so we maintain the peek() semantics */
102 if (fseek(fp
, start
, SEEK_SET
) != 0)
103 items
= 0; /* Flag error condition since we couldn't seek back to
110 int file_read (data_source_t
*source
, void *ptr
, size_t size
, size_t nmemb
)
112 file_private_t
*private = source
->private;
113 FILE *fp
= private->fp
;
116 bytes_read
= fread(ptr
, size
, nmemb
, fp
);
119 private->stats
.bytes_read
+= bytes_read
;
125 int file_seek (data_source_t
*source
, long offset
, int whence
)
127 file_private_t
*private = source
->private;
128 FILE *fp
= private->fp
;
130 if (!private->seekable
) return -1;
132 return fseek(fp
, offset
, whence
);
136 data_source_stats_t
* file_statistics (data_source_t
*source
)
138 file_private_t
*private = source
->private;
140 return malloc_data_source_stats(&private->stats
);
144 long file_tell (data_source_t
*source
)
146 file_private_t
*private = source
->private;
147 FILE *fp
= private->fp
;
149 if (!private->seekable
) return -1;
155 void file_close (data_source_t
*source
)
157 file_private_t
*private = source
->private;
158 FILE *fp
= private->fp
;
162 free(source
->source_string
);
163 free(source
->private);
168 transport_t file_transport
= {