diff options
author | Max Kellermann <max@duempel.org> | 2009-12-30 23:27:37 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2010-01-01 17:25:07 +0100 |
commit | d3b763a48c09a60a0c0b5ccb6cccd9376875c470 (patch) | |
tree | 83b8794f78ef8941806cf5757888d8abf2eaa126 /src/input/file_input_plugin.c | |
parent | 816b6ad4a71c3ade95e62b62396f2b0415c03f20 (diff) | |
download | mpd-d3b763a48c09a60a0c0b5ccb6cccd9376875c470.tar.gz mpd-d3b763a48c09a60a0c0b5ccb6cccd9376875c470.tar.xz mpd-d3b763a48c09a60a0c0b5ccb6cccd9376875c470.zip |
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).
Diffstat (limited to '')
-rw-r--r-- | src/input/file_input_plugin.c | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/src/input/file_input_plugin.c b/src/input/file_input_plugin.c index 92da42290..1c5813f88 100644 --- a/src/input/file_input_plugin.c +++ b/src/input/file_input_plugin.c @@ -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 |