input_stream: return allocated input_stream objects
Major API redesign: don't let the caller allocate the input_stream object. Let each input plugin allocate its own (derived/extended) input_stream pointer. The "data" attribute can now be removed, and all input plugins simply cast the input_stream pointer to their own structure (with an "struct input_stream base" as the first attribute).
This commit is contained in:
src
archive
archive_plugin.carchive_plugin.hdecoder
decoder_thread.cinput
archive_input_plugin.ccurl_input_plugin.cfile_input_plugin.cmms_input_plugin.crewind_input_plugin.crewind_input_plugin.h
input_plugin.hinput_stream.cinput_stream.hplaylist
playlist_list.cplaylist_list.hplaylist_queue.csong_update.ctest
@@ -32,18 +32,24 @@
|
||||
#undef G_LOG_DOMAIN
|
||||
#define G_LOG_DOMAIN "input_file"
|
||||
|
||||
struct file_input_stream {
|
||||
struct input_stream base;
|
||||
|
||||
int fd;
|
||||
};
|
||||
|
||||
static inline GQuark
|
||||
file_quark(void)
|
||||
{
|
||||
return g_quark_from_static_string("file");
|
||||
}
|
||||
|
||||
static bool
|
||||
input_file_open(struct input_stream *is, const char *filename,
|
||||
GError **error_r)
|
||||
static struct input_stream *
|
||||
input_file_open(const char *filename, GError **error_r)
|
||||
{
|
||||
int fd, ret;
|
||||
struct stat st;
|
||||
struct file_input_stream *fis;
|
||||
|
||||
if (!g_path_is_absolute(filename))
|
||||
return false;
|
||||
@@ -57,8 +63,6 @@ input_file_open(struct input_stream *is, const char *filename,
|
||||
return false;
|
||||
}
|
||||
|
||||
is->seekable = true;
|
||||
|
||||
ret = fstat(fd, &st);
|
||||
if (ret < 0) {
|
||||
g_set_error(error_r, file_quark(), errno,
|
||||
@@ -75,26 +79,29 @@ input_file_open(struct input_stream *is, const char *filename,
|
||||
return false;
|
||||
}
|
||||
|
||||
is->size = st.st_size;
|
||||
|
||||
#ifdef POSIX_FADV_SEQUENTIAL
|
||||
posix_fadvise(fd, (off_t)0, is->size, POSIX_FADV_SEQUENTIAL);
|
||||
posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL);
|
||||
#endif
|
||||
|
||||
is->plugin = &input_plugin_file;
|
||||
is->data = GINT_TO_POINTER(fd);
|
||||
is->ready = true;
|
||||
fis = g_new(struct file_input_stream, 1);
|
||||
input_stream_init(&fis->base, &input_plugin_file);
|
||||
|
||||
return true;
|
||||
fis->base.size = st.st_size;
|
||||
fis->base.seekable = true;
|
||||
fis->base.ready = true;
|
||||
|
||||
fis->fd = fd;
|
||||
|
||||
return &fis->base;
|
||||
}
|
||||
|
||||
static bool
|
||||
input_file_seek(struct input_stream *is, goffset offset, int whence,
|
||||
GError **error_r)
|
||||
{
|
||||
int fd = GPOINTER_TO_INT(is->data);
|
||||
struct file_input_stream *fis = (struct file_input_stream *)is;
|
||||
|
||||
offset = (goffset)lseek(fd, (off_t)offset, whence);
|
||||
offset = (goffset)lseek(fis->fd, (off_t)offset, whence);
|
||||
if (offset < 0) {
|
||||
g_set_error(error_r, file_quark(), errno,
|
||||
"Failed to seek: %s", g_strerror(errno));
|
||||
@@ -109,10 +116,10 @@ static size_t
|
||||
input_file_read(struct input_stream *is, void *ptr, size_t size,
|
||||
GError **error_r)
|
||||
{
|
||||
int fd = GPOINTER_TO_INT(is->data);
|
||||
struct file_input_stream *fis = (struct file_input_stream *)is;
|
||||
ssize_t nbytes;
|
||||
|
||||
nbytes = read(fd, ptr, size);
|
||||
nbytes = read(fis->fd, ptr, size);
|
||||
if (nbytes < 0) {
|
||||
g_set_error(error_r, file_quark(), errno,
|
||||
"Failed to read: %s", g_strerror(errno));
|
||||
@@ -126,9 +133,10 @@ input_file_read(struct input_stream *is, void *ptr, size_t size,
|
||||
static void
|
||||
input_file_close(struct input_stream *is)
|
||||
{
|
||||
int fd = GPOINTER_TO_INT(is->data);
|
||||
struct file_input_stream *fis = (struct file_input_stream *)is;
|
||||
|
||||
close(fd);
|
||||
close(fis->fd);
|
||||
g_free(fis);
|
||||
}
|
||||
|
||||
static bool
|
||||
|
Reference in New Issue
Block a user