/*
* UCW Library -- Fast Buffered I/O
*
- * (c) 1997--2008 Martin Mares <mj@ucw.cz>
+ * (c) 1997--2011 Martin Mares <mj@ucw.cz>
* (c) 2004 Robert Spalek <robert@ucw.cz>
*
* This software may be freely distributed and used according to the terms
*
* - Initialization:
* * out: `buffer <= bstop <= bptr <= bufend` (flushed).
+ * * @fb_tie() should be called on the newly created fastbuf.
*
* - `refill`:
* * in: `buffer <= bstop <= bptr <= bufend` (reading or flushed).
* for how it works.
**/
struct fastbuf {
- byte is_fastbuf[0]; /* Dummy field for checking of type casts */
byte *bptr, *bstop; /* State of the buffer */
byte *buffer, *bufend; /* Start and end of the buffer */
char *name; /* File name (used for error messages) */
struct resource *res; /* The fastbuf can be tied to a resource pool */
};
-void fb_tie(struct fastbuf *b); /* Tie fastbuf to a resource if there is an active pool */
-
/**
* Fastbuf flags
*/
FB_DIE_ON_EOF = 0x2, /* Most of read operations throw "fb.eof" on EOF */
};
+/** Tie a fastbuf to a resource in the current resource pool. Returns the pointer to the same fastbuf. **/
+struct fastbuf *fb_tie(struct fastbuf *b); /* Tie fastbuf to a resource if there is an active pool */
+
/***
* === Fastbuf on files [[fbparam]]
*
* If you want to use fastbufs to access files, you can choose one of several
* back-ends and set their parameters.
- *
- * All file fastbufs are tied to resources automatically.
***/
/**
* Use @params to select the fastbuf back-end and its parameters or
* pass NULL if you are fine with defaults.
*
- * Dies if the file does not exist.
+ * Raises `ucw.fb.open` if the file does not exist.
**/
struct fastbuf *bopen_file(const char *name, int mode, struct fb_params *params);
struct fastbuf *bopen_file_try(const char *name, int mode, struct fb_params *params); /** Like bopen_file(), but returns NULL on failure. **/
*
* The `fblim` back-end reads from a file handle, but at most a given
* number of bytes. This is frequently used for reading from sockets.
- *
- * All such fastbufs are tied to resources automatically.
***/
struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); /** Create a fastbuf which reads at most @limit bytes from @fd. **/
* First, you use @fbmem_create() to create the stream and the fastbuf
* used for writing to it. Then you can call @fbmem_clone_read() to get
* an arbitrary number of fastbuf for reading from the stream.
- *
- * All in-memory fastbufs are tied to resources automatically.
***/
struct fastbuf *fbmem_create(uns blocksize); /** Create stream and return its writing fastbuf. **/
/**
* Creates a write-only fastbuf which writes into a provided memory buffer.
* The fastbuf structure is allocated by the caller and pointed to by @f.
- * An attempt to write behind the end of the buffer dies.
+ * An attempt to write behind the end of the buffer causes the `ucw.fb.write` exception.
*
* Data are written directly into the buffer, so it is not necessary to call @bflush()
* at any moment.
* size and it is expanded to accomodate all data.
*
* At every moment, you can use `fastbuf->buffer` to gain access to the stream.
- *
- * All fastbufs of this type are tied to resources automatically.
***/
+struct mempool;
+
struct fastbuf *fbgrow_create(unsigned basic_size); /** Create the growing buffer pre-allocated to @basic_size bytes. **/
+struct fastbuf *fbgrow_create_mp(struct mempool *mp, unsigned basic_size); /** Create the growing buffer pre-allocated to @basic_size bytes. **/
void fbgrow_reset(struct fastbuf *b); /** Reset stream and prepare for writing. **/
void fbgrow_rewind(struct fastbuf *b); /** Prepare for reading (of already written data). **/
+/**
+ * Can be used in any state of @b (for example when writing or after
+ * @fbgrow_rewind()) to return the pointer to internal buffer and its length in
+ * bytes. The returned buffer can be invalidated by further requests.
+ **/
+uns fbgrow_get_buf(struct fastbuf *b, byte **buf);
+
/***
* === Fastbuf on memory pools [[fbpool]]
*
* buffer, but this time the buffer is allocated from within a memory pool.
***/
-struct mempool;
struct fbpool { /** Structure for fastbufs & mempools. **/
struct fastbuf fb;
struct mempool *mp;
*
* Please note that initialization of the clones is not thread-safe,
* so you have to serialize it yourself.
- *
- * The atomic fastbufs are tied to resources automatically.
***/
struct fb_atomic {
byte *expected_max_bptr;
uns slack_size;
};
-#define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
/**
* Open an atomic fastbuf.
/**
* Reads exactly @l bytes of data into @b.
* If at the end of file, it returns 0.
- * If there are data, but less than @l, it dies.
+ * If there are data, but less than @l, it raises `ucw.fb.eof`.
*/
static inline uns breadb(struct fastbuf *f, void *b, uns l)
{
/**
* Reads a line into @b and strips trailing `\n`.
* Returns pointer to the terminating 0 or NULL on `EOF`.
- * Dies if the line is longer than @l.
+ * Raises `ucw.fb.toolong` if the line is longer than @l.
**/
char *bgets(struct fastbuf *f, char *b, uns l);
char *bgets0(struct fastbuf *f, char *b, uns l); /** The same as @bgets(), but for 0-terminated strings. **/
struct bb_t;
/**
* Read a string, strip the trailing `\n` and store it into growing buffer @b.
- * Dies if the line is longer than @limit.
+ * Raises `ucw.fb.toolong` if the line is longer than @limit.
**/
uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
/**