"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/buffer.c" (26 Dec 2023, 737 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "buffer.c" see the Fossies "Dox" file reference documentation.

    1 #include <stdio.h>
    2 
    3 #include "imapfilter.h"
    4 #include "buffer.h"
    5 
    6 
    7 /*
    8  * Initialize buffer.
    9  */
   10 void
   11 buffer_init(buffer *buf, size_t n)
   12 {
   13 
   14     buf->data = (char *)xmalloc((n + 1) * sizeof(char));
   15     *buf->data = '\0';
   16     buf->len = 0;
   17     buf->size = n;
   18 }
   19 
   20 
   21 /*
   22  * Free allocated memory of buffer.
   23  */
   24 void
   25 buffer_free(buffer *buf)
   26 {
   27 
   28     if (!buf->data)
   29         return;
   30 
   31     xfree(buf->data);
   32     buf->data = NULL;
   33 }
   34 
   35 
   36 /*
   37  * Reset buffer.
   38  */
   39 void
   40 buffer_reset(buffer *buf)
   41 {
   42 
   43     *buf->data = '\0';
   44     buf->len = 0;
   45 }
   46 
   47 
   48 /*
   49  * Check if the buffer has enough space to store data and reallocate memory if
   50  * needed.
   51  */
   52 void
   53 buffer_check(buffer *buf, size_t n)
   54 {
   55 
   56     while (n > buf->size) {
   57         buf->size *= 2;
   58         buf->data = (char *)xrealloc(buf->data, buf->size + 1);
   59     }
   60 }