libbufLinux

libbuf is a small and lightweight library that allows you to pass data between software components. Familiar functions such as read(), write() and printf() are provided allowing intuitive use. libbuf also supports blocking and non-blocking methodologies.

Links

Overview

Using libbuf is incredibly straightforward. See for yourself!

buf.h

This is a stripped down version of the header file, detailing just the important aspects.
  1. #define BUF_NONBLOCK 0x1
  2. #define BUF_MORE     0x2
  3. #define BUF_GIFT     0x4
  4.  
  5. struct buf;
  6. typedef struct buf buf_t;
  7.  
  8. buf_t *buf_alloc(void);
  9. void buf_purge(buf_t *buf);
  10. void buf_free(buf_t *buf);
  11.  
  12. size_t buf_getSize(buf_t *buf);
  13. size_t buf_getLength(buf_t *buf);
  14.  
  15. int buf_putc(buf_t *buf, int flags, int c);
  16. int buf_getc(buf_t *buf, int flags);
  17.  
  18. size_t buf_write(buf_t *buf, int flags, const uint8_t *data, size_t count);
  19. size_t buf_read(buf_t *buf, int flags, uint8_t *data, size_t count);
  20.  
  21. int buf_printf(buf_t *buf, int flags, const char *format, ...);
  22. int buf_nprintf(buf_t *buf, int flags, size_t size, const char *format, ...);
  23. int buf_vprintf(buf_t *buf, int flags, const char *format, va_list ap);
  24. int buf_vnprintf(buf_t *buf, int flags, size_t size, const char *format, va_list ap);
  25.  
  26. size_t buf_splice(buf_t *dest, buf_t *src, int flags);

Example

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <buf.h>
  5.  
  6. int main(int argc, char *argv[]) {
  7.   buf_t *b;
  8.   char t[10];
  9.   size_t len;
  10.  
  11.   if ((b = buf_alloc()) == NULL) {
  12.     return 1;
  13.   }
  14.  
  15.   printf("Writing 'Hello'...\n");
  16.   if (buf_write(b, 0, "Hello", 5) != 5) {
  17.     return 2;
  18.   }
  19.  
  20.   if ((len = buf_read(b, 0, t, sizeof(t))) < 0) {
  21.     return 3;
  22.   }
  23.  
  24.   printf("Read '%.*s'...\n", len, t);
  25.   if (strncmp(t, "Hello", 5)) {
  26.     return 4;
  27.   }
  28.  
  29.   buf_free(b);
  30.  
  31.   return 0;
  32. }