about summary refs log tree commit diff
path: root/misc/stream.h
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-01-16 22:23:26 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-01-16 22:23:26 +0000
commita3933c07619a6f9b12e25f8e7d5c68790dff0f29 (patch)
treecb99c33f9c50d8b0b31cb2c40e8c6650bde6dc38 /misc/stream.h
parent66d742a48e2d264b370b8157c78f55a76a90a462 (diff)
downloadbtpd-a3933c07619a6f9b12e25f8e7d5c68790dff0f29.tar.gz
btpd-a3933c07619a6f9b12e25f8e7d5c68790dff0f29.zip
Unify the write and read structs. There was no good reason to keep them
separate. Improve the bts_seek so that it only calls close or lseek if
necessary. Otherwise it's a nop. Hide bts_seek and instead make the offset
explicit in calls to bts_get or bts_put.

Diffstat (limited to 'misc/stream.h')
-rw-r--r--misc/stream.h53
1 files changed, 22 insertions, 31 deletions
diff --git a/misc/stream.h b/misc/stream.h
index 6ed91d5..30ce443 100644
--- a/misc/stream.h
+++ b/misc/stream.h
@@ -1,36 +1,27 @@
 #ifndef BTPD_STREAM_H
 #define BTPD_STREAM_H
 
-typedef int (*F_fdcb)(const char *, int *, void *);
-
-#define def_stream(name) \
-struct name {\
-    struct metainfo *meta;\
-    F_fdcb fd_cb;\
-    void *fd_arg;\
-    unsigned index;\
-    off_t t_off;\
-    off_t f_off;\
-    int fd;\
-}
-
-def_stream(bt_stream_ro);
-
-struct bt_stream_ro *
-bts_open_ro(struct metainfo *meta, off_t off, F_fdcb fd_cb, void *fd_arg);
-int bts_read_ro(struct bt_stream_ro *bts, char *buf, size_t len);
-void bts_seek_ro(struct bt_stream_ro *bts, off_t nbytes);
-void bts_close_ro(struct bt_stream_ro *bts);
-
-def_stream(bt_stream_wo);
-
-struct bt_stream_wo *
-bts_open_wo(struct metainfo *meta, off_t off, F_fdcb fd_cb, void *fd_arg);
-int bts_write_wo(struct bt_stream_wo *bts, const char *buf, size_t len);
-int bts_close_wo(struct bt_stream_wo *bts);
-
-int bts_sha(struct bt_stream_ro *bts, off_t length, uint8_t *hash);
-int bts_hashes(struct metainfo *, F_fdcb fd_cb,
-               void (*cb)(uint32_t, uint8_t *, void *), void *arg);
+typedef int (*fdcb_t)(const char *, int *, void *);
+typedef void (*hashcb_t)(uint32_t, uint8_t *, void *);
+
+struct bt_stream {
+    struct metainfo *meta;
+    fdcb_t fd_cb;
+    void *fd_arg;
+    unsigned index;
+    off_t t_off;
+    off_t f_off;
+    int fd;
+};
+
+int bts_open(struct bt_stream **res, struct metainfo *meta, fdcb_t fd_cb,
+    void *fd_arg);
+int bts_get(struct bt_stream *bts, off_t off, uint8_t *buf, size_t len);
+int bts_put(struct bt_stream *bts, off_t off, const uint8_t *buf, size_t len);
+int bts_close(struct bt_stream *bts);
+
+int bts_sha(struct bt_stream *bts, off_t start, off_t length, uint8_t *hash);
+int bts_hashes(struct metainfo *meta, fdcb_t fd_cb, hashcb_t hash_cb,
+    void *arg);
 
 #endif