From 217bed5131baa258398e51c6aa5143a66fe16ba7 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Mon, 25 Apr 2011 23:43:23 +0200 Subject: [PATCH] player: implement io-callbacks for loading tracks Some demos use datafiles; let's allow these to easily work by adding a callback-interface for loading tracks. The callbacks are only present in player-mode, because tracks aren't loaded from disk in client-mode. --- sync/device.c | 26 ++++++++++++++++++++------ sync/device.h | 3 +++ sync/sync.h | 9 ++++++++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/sync/device.c b/sync/device.c index 4f9c30d..2f78f1b 100644 --- a/sync/device.c +++ b/sync/device.c @@ -81,6 +81,16 @@ static SOCKET server_connect(const char *host, unsigned short nport) closesocket(sock); return INVALID_SOCKET; } + +#else + +void sync_set_io_cb(struct sync_device *d, struct sync_io_cb *cb) +{ + d->io_cb.open = cb->open; + d->io_cb.read = cb->read; + d->io_cb.close = cb->close; +} + #endif struct sync_device *sync_create_device(const char *base) @@ -101,6 +111,10 @@ struct sync_device *sync_create_device(const char *base) #ifndef SYNC_PLAYER d->row = -1; d->sock = INVALID_SOCKET; +#else + d->io_cb.open = fopen; + d->io_cb.read = fread; + d->io_cb.close = fclose; #endif return d; @@ -125,11 +139,11 @@ void sync_destroy_device(struct sync_device *d) static int get_track_data(struct sync_device *d, struct sync_track *t) { int i; - FILE *fp = fopen(sync_track_path(d->base, t->name), "rb"); + void *fp = d->io_cb.open(sync_track_path(d->base, t->name), "rb"); if (!fp) return -1; - fread(&t->num_keys, sizeof(size_t), 1, fp); + d->io_cb.read(&t->num_keys, sizeof(size_t), 1, fp); t->keys = malloc(sizeof(struct track_key) * t->num_keys); if (!t->keys) return -1; @@ -137,13 +151,13 @@ static int get_track_data(struct sync_device *d, struct sync_track *t) for (i = 0; i < (int)t->num_keys; ++i) { struct track_key *key = t->keys + i; char type; - fread(&key->row, sizeof(size_t), 1, fp); - fread(&key->value, sizeof(float), 1, fp); - fread(&type, sizeof(char), 1, fp); + d->io_cb.read(&key->row, sizeof(size_t), 1, fp); + d->io_cb.read(&key->value, sizeof(float), 1, fp); + d->io_cb.read(&type, sizeof(char), 1, fp); key->type = (enum key_type)type; } - fclose(fp); + d->io_cb.close(fp); return 0; } diff --git a/sync/device.h b/sync/device.h index c08ffbd..4dca4bf 100644 --- a/sync/device.h +++ b/sync/device.h @@ -6,6 +6,7 @@ #define SYNC_DEVICE_H #include "data.h" +#include "sync.h" struct sync_device { char *base; @@ -14,6 +15,8 @@ struct sync_device { #ifndef SYNC_PLAYER int row; SOCKET sock; +#else + struct sync_io_cb io_cb; #endif }; diff --git a/sync/sync.h b/sync/sync.h index 99e24ac..790e967 100644 --- a/sync/sync.h +++ b/sync/sync.h @@ -25,7 +25,14 @@ struct sync_cb { int sync_connect(struct sync_device *, const char *, unsigned short); int sync_update(struct sync_device *, int, struct sync_cb *, void *); void sync_save_tracks(const struct sync_device *); -#endif /* !defined(SYNC_PLAYER) */ +#else /* defined(SYNC_PLAYER) */ +struct sync_io_cb { + void *(*open)(const char *filename, const char *mode); + size_t (*read)(void *ptr, size_t size, size_t nitems, void *stream); + int (*close)(void *stream); +}; +void sync_set_io_cb(struct sync_device *d, struct sync_io_cb *cb); +#endif /* defined(SYNC_PLAYER) */ const struct sync_track *sync_get_track(struct sync_device *, const char *); float sync_get_val(const struct sync_track *, double);