README.md (2182B)
1 finwo/buf 2 ========= 3 4 > Simple byte buffers 5 6 This library makes use of [dep](https://github.com/finwo/dep) to manage it's 7 dependencies and exports. 8 9 Installation 10 ------------ 11 12 ```sh 13 dep add finwo/buf 14 dep install 15 ``` 16 17 After that, simply add `include lib/.dep/config.mk` in your makefile and include 18 the header file by adding `#include "finwo/buf.h`. 19 20 API 21 --- 22 23 ### Structures 24 25 <details> 26 <summary>struct buf</summary> 27 28 The main handle of the buffer library 29 30 ```C 31 struct buf { 32 char *alloc; 33 char *dat; 34 size_t len; 35 size_t cap; 36 }; 37 ``` 38 39 </details> 40 41 ### Methods 42 43 <details> 44 <summary>buf_error(errno)</summary> 45 46 Translates an error code into something human-readable (or null) 47 48 ```C 49 const char *buf_error(int errno); 50 ``` 51 52 </details> 53 <details> 54 <summary>buf_append(subject, source, length)</summary> 55 56 Append `length` bytes to the data in the buffer 57 Copies the data, does not mutate the source 58 59 Returns an error code, or 0 on success 60 61 ```C 62 int buf_append(struct buf *subject, const char *source, size_t length); 63 ``` 64 65 </details> 66 <details> 67 <summary>buf_append_byte(subject, value)</summary> 68 69 Append a single byte to the buffer 70 71 Returns an error code, or 0 on success 72 73 ```C 74 int buf_append_byte(struct buf *subject, const char value); 75 ``` 76 77 </details> 78 <details> 79 <summary>buf_consume(subject, length)</summary> 80 81 Remove `length` bytes from the front of the buffer 82 83 Returns the amount of bytes removed (may differ from directive) 84 85 ```C 86 size_t buf_consume(struct buf *subject, size_t length); 87 ``` 88 89 </details> 90 <details> 91 <summary>buf_clear(subject)</summary> 92 93 Clears out all data from the buffer 94 95 ```C 96 int buf_clear(struct buf *subject); 97 ``` 98 99 </details> 100 <details> 101 <summary>buf_set_allocator(realloc, free)</summary> 102 103 Sets custom allocator instead of libc. This function, if needed, should be 104 called only once at startup and prior to calling buf_* functions 105 106 ```C 107 void buf_set_allocator(void *(*realloc)(void*,size_t), void (*free)(void*)); 108 ``` 109 110 </details> 111 112 Testing 113 ------- 114 115 If you want to run the library's tests, simply run `make tests` to compile 116 and run the testing binaries 117 118 License 119 ------- 120 121 buf.c source code is available under the [FGPL License](License.md)