lib.[ch], dvd-sector-copy.c: Publish the `buf' machinery as inline functions.
[dvdrip] / lib.h
diff --git a/lib.h b/lib.h
index 2b127e3..2b8eb6b 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -143,6 +143,42 @@ extern PRINTF_LIKE(2, 3) NORETURN
         * exit with status code 2.
         */
 
+/*----- Resizing buffers --------------------------------------------------*/
+
+struct buf {
+  /* A buffer for a string which can grow automatically. */
+
+  char *p;                             /* pointer to the buffer */
+  size_t n, sz;                                /* string length, buffer size */
+};
+#define BUF_INIT { 0, 0, 0 }
+
+static inline void buf_rewind(struct buf *b) { b->n = 0; }
+       /* Throw away the current contents of B so that new stuff gets added
+        * to the beginning.
+        */
+
+static inline void buf_free(struct buf *b)
+  { free(b->p); b->p = 0; b->n = b->sz = 0; }
+       /* Release the memory allocated for B.  The buffer can be reused
+        * immediately and/or freed again safely.
+        */
+
+extern void buf__grow(struct buf *b);
+       /* Make B's buffer larger, so that (at least) one extra byte can be
+        * written to it.  (Internal to `buf_putc'.)
+        */
+
+static inline void buf_putc(struct buf *b, int ch)
+  { if (b->n >= b->sz) buf__grow(b); b->p[b->n++] = ch; }
+       /* Append the character CH to the buffer B. */
+
+static inline void buf_putz(struct buf *b)
+  { if (b->n >= b->sz) buf__grow(b); b->p[b->n] = 0; }
+       /* Append a zero byte to B without increasing the string length, so
+        * that a future `buf_putc' will overwrite it.
+        */
+
 /*----- Resizing vectors --------------------------------------------------*/
 
 #define DEFVEC(vtype, etype)                                           \