From f08041f0eb8512304584b583073508629a934c88 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 26 Oct 2008 20:38:44 +0100 Subject: input_stream: added struct input_plugin Instead of managing a set of method pointers in each input_stream struct, move these into the new input_plugin struct. Each input_stream has only a pointer to the plugin struct. Pointers to all implementations are kept in the array "input_plugins". --- src/input_curl.c | 18 +++++++++++------- src/input_curl.h | 6 +----- src/input_file.c | 38 +++++++++++++------------------------- src/input_file.h | 3 +-- src/input_stream.c | 33 ++++++++++++++++++++++----------- src/input_stream.h | 21 +++++++++++++++------ 6 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/input_curl.c b/src/input_curl.c index fc78adca0..09e73c3e2 100644 --- a/src/input_curl.c +++ b/src/input_curl.c @@ -452,7 +452,8 @@ input_curl_seek(struct input_stream *is, mpd_unused long offset, return 0; } -bool input_curl_open(struct input_stream *is, char *url) +static bool +input_curl_open(struct input_stream *is, const char *url) { struct input_curl *c; bool ret; @@ -483,11 +484,14 @@ bool input_curl_open(struct input_stream *is, char *url) return false; } - is->seekFunc = input_curl_seek; - is->closeFunc = input_curl_close; - is->readFunc = input_curl_read; - is->atEOFFunc = input_curl_eof; - is->bufferFunc = input_curl_buffer; - return true; } + +const struct input_plugin input_plugin_curl = { + .open = input_curl_open, + .close = input_curl_close, + .buffer = input_curl_buffer, + .read = input_curl_read, + .eof = input_curl_eof, + .seek = input_curl_seek, +}; diff --git a/src/input_curl.h b/src/input_curl.h index 9ced70f7f..5ae06f065 100644 --- a/src/input_curl.h +++ b/src/input_curl.h @@ -19,14 +19,10 @@ #ifndef MPD_INPUT_CURL_H #define MPD_INPUT_CURL_H -#include - -struct input_stream; +extern const struct input_plugin input_plugin_curl; void input_curl_global_init(void); void input_curl_global_finish(void); -bool input_curl_open(struct input_stream *is, char *url); - #endif diff --git a/src/input_file.c b/src/input_file.c index 6918a1d54..a176d55f2 100644 --- a/src/input_file.c +++ b/src/input_file.c @@ -21,30 +21,15 @@ #include "log.h" #include "os_compat.h" -static int -input_file_seek(struct input_stream *is, long offset, int whence); - -static size_t -input_file_read(struct input_stream *is, void *ptr, size_t size); - -static int -input_file_close(struct input_stream *is); - -static int -input_file_eof(struct input_stream *is); - -static int -input_file_buffer(struct input_stream *is); - -int -input_file_open(struct input_stream *is, char *filename) +static bool +input_file_open(struct input_stream *is, const char *filename) { FILE *fp; fp = fopen(filename, "r"); if (!fp) { is->error = errno; - return -1; + return false; } is->seekable = 1; @@ -58,15 +43,9 @@ input_file_open(struct input_stream *is, char *filename) #endif is->data = fp; - is->seekFunc = input_file_seek; - is->closeFunc = input_file_close; - is->readFunc = input_file_read; - is->atEOFFunc = input_file_eof; - is->bufferFunc = input_file_buffer; - is->ready = 1; - return 0; + return true; } static int @@ -128,3 +107,12 @@ input_file_buffer(mpd_unused struct input_stream *is) { return 0; } + +const struct input_plugin input_plugin_file = { + .open = input_file_open, + .close = input_file_close, + .buffer = input_file_buffer, + .read = input_file_read, + .eof = input_file_eof, + .seek = input_file_seek, +}; diff --git a/src/input_file.h b/src/input_file.h index 8e25ff113..4374f0988 100644 --- a/src/input_file.h +++ b/src/input_file.h @@ -21,7 +21,6 @@ #include "input_stream.h" -int -input_file_open(struct input_stream *is, char *filename); +extern const struct input_plugin input_plugin_file; #endif diff --git a/src/input_stream.c b/src/input_stream.c index db51cbd4c..a5c55fa54 100644 --- a/src/input_stream.c +++ b/src/input_stream.c @@ -27,6 +27,16 @@ #include +static const struct input_plugin *const input_plugins[] = { + &input_plugin_file, +#ifdef HAVE_CURL + &input_plugin_curl, +#endif +}; + +static const unsigned num_input_plugins = + sizeof(input_plugins) / sizeof(input_plugins[0]); + void input_stream_global_init(void) { #ifdef HAVE_CURL @@ -52,26 +62,27 @@ int input_stream_open(struct input_stream *is, char *url) is->meta_name = NULL; is->meta_title = NULL; - if (input_file_open(is, url) == 0) - return 0; + for (unsigned i = 0; i < num_input_plugins; ++i) { + const struct input_plugin *plugin = input_plugins[i]; -#ifdef HAVE_CURL - if (input_curl_open(is, url)) - return 0; -#endif + if (plugin->open(is, url)) { + is->plugin = plugin; + return 0; + } + } return -1; } int input_stream_seek(struct input_stream *is, long offset, int whence) { - return is->seekFunc(is, offset, whence); + return is->plugin->seek(is, offset, whence); } size_t input_stream_read(struct input_stream *is, void *ptr, size_t size) { - return is->readFunc(is, ptr, size); + return is->plugin->read(is, ptr, size); } int input_stream_close(struct input_stream *is) @@ -83,15 +94,15 @@ int input_stream_close(struct input_stream *is) if (is->meta_title) free(is->meta_title); - return is->closeFunc(is); + return is->plugin->close(is); } int input_stream_eof(struct input_stream *is) { - return is->atEOFFunc(is); + return is->plugin->eof(is); } int input_stream_buffer(struct input_stream *is) { - return is->bufferFunc(is); + return is->plugin->buffer(is); } diff --git a/src/input_stream.h b/src/input_stream.h index fbd8eb3fd..b2887e10d 100644 --- a/src/input_stream.h +++ b/src/input_stream.h @@ -20,8 +20,23 @@ #define INPUT_STREAM_H #include +#include + +struct input_stream; + +struct input_plugin { + bool (*open)(struct input_stream *is, const char *url); + int (*close)(struct input_stream *is); + + int (*buffer)(struct input_stream *is); + size_t (*read)(struct input_stream *is, void *ptr, size_t size); + int (*eof)(struct input_stream *is); + int (*seek)(struct input_stream *is, long offset, int whence); +}; struct input_stream { + const struct input_plugin *plugin; + int ready; int error; @@ -30,12 +45,6 @@ struct input_stream { char *mime; int seekable; - int (*seekFunc)(struct input_stream *is, long offset, int whence); - size_t (*readFunc)(struct input_stream *is, void *ptr, size_t size); - int (*closeFunc)(struct input_stream *is); - int (*atEOFFunc)(struct input_stream *is); - int (*bufferFunc)(struct input_stream *is); - void *data; char *meta_name; char *meta_title; -- cgit v1.2.3