]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/fastbuf.h
Opt: Make OPT_LAST_ARG actually work and document interface of opt_parse()
[libucw.git] / ucw / fastbuf.h
index fc82a0e5c277c5bc88991a117f8337289b7a7826..9b0f72510fcc793e571a4da995fafd0348ec3702 100644 (file)
 #include <string.h>
 #include <alloca.h>
 
+#ifdef CONFIG_UCW_CLEAN_ABI
+#define bbcopy_slow ucw_bbcopy_slow
+#define bclose ucw_bclose
+#define bclose_file_helper ucw_bclose_file_helper
+#define bconfig ucw_bconfig
+#define beof_slow ucw_beof_slow
+#define bfdopen ucw_bfdopen
+#define bfdopen_internal ucw_bfdopen_internal
+#define bfdopen_shared ucw_bfdopen_shared
+#define bfilesize ucw_bfilesize
+#define bfilesync ucw_bfilesync
+#define bfix_tmp_file ucw_bfix_tmp_file
+#define bflush ucw_bflush
+#define bfmmopen_internal ucw_bfmmopen_internal
+#define bgetc_slow ucw_bgetc_slow
+#define bgets ucw_bgets
+#define bgets0 ucw_bgets0
+#define bgets_bb ucw_bgets_bb
+#define bgets_mp ucw_bgets_mp
+#define bgets_nodie ucw_bgets_nodie
+#define bgets_stk_init ucw_bgets_stk_init
+#define bgets_stk_step ucw_bgets_stk_step
+#define bopen ucw_bopen
+#define bopen_fd_name ucw_bopen_fd_name
+#define bopen_file ucw_bopen_file
+#define bopen_file_try ucw_bopen_file_try
+#define bopen_limited_fd ucw_bopen_limited_fd
+#define bopen_tmp ucw_bopen_tmp
+#define bopen_tmp_file ucw_bopen_tmp_file
+#define bopen_try ucw_bopen_try
+#define bpeekc_slow ucw_bpeekc_slow
+#define bprintf ucw_bprintf
+#define bputc_slow ucw_bputc_slow
+#define bread_slow ucw_bread_slow
+#define brefill ucw_brefill
+#define brewind ucw_brewind
+#define bseek ucw_bseek
+#define bsetpos ucw_bsetpos
+#define bskip_slow ucw_bskip_slow
+#define bspout ucw_bspout
+#define bthrow ucw_bthrow
+#define bwrite_slow ucw_bwrite_slow
+#define fb_tie ucw_fb_tie
+#define fbatomic_internal_write ucw_fbatomic_internal_write
+#define fbatomic_open ucw_fbatomic_open
+#define fbbuf_init_read ucw_fbbuf_init_read
+#define fbbuf_init_write ucw_fbbuf_init_write
+#define fbdir_cheat ucw_fbdir_cheat
+#define fbdir_open_fd_internal ucw_fbdir_open_fd_internal
+#define fbgrow_create ucw_fbgrow_create
+#define fbgrow_create_mp ucw_fbgrow_create_mp
+#define fbgrow_get_buf ucw_fbgrow_get_buf
+#define fbgrow_reset ucw_fbgrow_reset
+#define fbgrow_rewind ucw_fbgrow_rewind
+#define fbmem_clone_read ucw_fbmem_clone_read
+#define fbmem_create ucw_fbmem_create
+#define fbmulti_append ucw_fbmulti_append
+#define fbmulti_create ucw_fbmulti_create
+#define fbmulti_remove ucw_fbmulti_remove
+#define fbpar_cf ucw_fbpar_cf
+#define fbpar_def ucw_fbpar_def
+#define fbpool_end ucw_fbpool_end
+#define fbpool_init ucw_fbpool_init
+#define fbpool_start ucw_fbpool_start
+#define open_tmp ucw_open_tmp
+#define temp_file_name ucw_temp_file_name
+#define vbprintf ucw_vbprintf
+#endif
+
 /***
  * === Internal structure [[internal]]
  *
  * 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) */
@@ -144,8 +212,6 @@ struct fastbuf {
   struct resource *res;                                /* The fastbuf can be tied to a resource pool */
 };
 
-struct fastbuf *fb_tie(struct fastbuf *b);     /* Tie fastbuf to a resource if there is an active pool */
-
 /**
  * Fastbuf flags
  */
@@ -154,13 +220,14 @@ enum fb_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.
  ***/
 
 /**
@@ -196,7 +263,7 @@ extern struct fb_params fbpar_def;  /** The default `fb_params` **/
  * 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. **/
@@ -307,8 +374,6 @@ void bclose_file_helper(struct fastbuf *f, int fd, int is_temp_file);
  *
  * 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. **/
@@ -323,8 +388,6 @@ struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); /** Create a f
  * 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. **/
@@ -354,7 +417,7 @@ void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrit
 /**
  * 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.
@@ -377,8 +440,6 @@ static inline uns fbbuf_count_written(struct fastbuf *f) /** Calculates, how man
  * 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;
@@ -387,7 +448,13 @@ struct fastbuf *fbgrow_create(unsigned basic_size);        /** Create the growing buffe
 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). **/
-uns fbgrow_read_all(struct fastbuf *b, byte **buf);    /** Read the entire content of a growing fastbuf. Can be called in any state. **/
+
+/**
+ * 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]]
@@ -438,8 +505,6 @@ void *fbpool_end(struct fbpool *fb);
  *
  * 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 {
@@ -448,7 +513,6 @@ struct fb_atomic {
   byte *expected_max_bptr;
   uns slack_size;
 };
-#define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
 
 /**
  * Open an atomic fastbuf.
@@ -478,6 +542,53 @@ static inline void fbatomic_commit(struct fastbuf *b)
     fbatomic_internal_write(b);
 }
 
+/***
+ * === Fastbufs atop other fastbufs [[fbmulti]]
+ *
+ * Imagine some code which does massive string processing. It takes an input
+ * buffer, writes a part of it into an output buffer, then some other string
+ * and then the remaining part of the input buffer. Or anything else where you
+ * copy all the data at each stage of the complicated process.
+ *
+ * This backend takes multiple fastbufs and concatenates them formally into
+ * one. You may then read them consecutively as they were one fastbuf at all.
+ *
+ * This backend is read-only.
+ *
+ * This backend is seekable iff all of the supplied fastbufs are seekable.
+ *
+ * You aren't allowed to do anything with the underlying buffers while these
+ * are connected into fbmulti.
+ *
+ * The fbmulti is inited by @fbmulti_create(). It returns an empty fbmulti.
+ * Then you call @fbmulti_append() for each fbmulti.
+ *
+ * If @bclose() is called on fbmulti, all the underlying buffers get closed
+ * recursively.
+ *
+ * If you want to keep an underlying fastbuf open after @bclose, just remove it
+ * by @fbmulti_remove where the second parameter is a pointer to the removed
+ * fastbuf. If you pass NULL, all the underlying fastbufs are removed.
+ *
+ * After @fbmulti_remove, the state of the fbmulti is undefined. The only allowed
+ * operation is either another @fbmulti_remove or @bclose on the fbmulti.
+ ***/
+
+/**
+ * Create an empty fbmulti
+ **/
+struct fastbuf *fbmulti_create(void);
+
+/**
+ * Append a fb to fbmulti
+ **/
+void fbmulti_append(struct fastbuf *f, struct fastbuf *fb);
+
+/**
+ * Remove a fb from fbmulti
+ **/
+void fbmulti_remove(struct fastbuf *f, struct fastbuf *fb);
+
 /*** === Configuring stream parameters [[bconfig]] ***/
 
 enum bconfig_type {                    /** Parameters that could be configured. **/
@@ -571,7 +682,7 @@ static inline uns bread(struct fastbuf *f, void *b, uns l)
 /**
  * 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)
 {
@@ -600,7 +711,7 @@ static inline void bwrite(struct fastbuf *f, const void *b, uns l) /** Writes bu
 /**
  * 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. **/
@@ -614,7 +725,7 @@ struct mempool;
 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);
 /**