strbuf is a simple library for creating strings in C.
The strings are stored in a continous buffer, separated by '\0'.
Just add strbuf.c to your project file / Makefile and include strbuf.h where needed.
Create a new string buffer.
- capacity - Initial buffer capacity
Return: A new string buffer handle (to be freed with sbfree)
Resize a string buffer.
- sb - String buffer handle
- capacity - New size of string buffer
Return: The handle of the reallocated string buffer, og NULL for error
Release all allocations related to a string buffer.
- sb - String buffer handle or NULL
Return: 0 => OK, -1 => Error
Make sure the capacity of a string buffer is at least needed_capacity.
- sb - String buffer handle
- needed_capacity - Minimum capacity
Return: The handle of the possibly reallocated string buffer
Append a string to the current string (could be considered a checked strcat).
- sb - String buffer handle
- str - String to be appended
Return: 0 => OK, -1 => Error
Append a character to the current string.
- sb - String buffer handle
- ch - Character to be appended
Return: ch => OK, -1 => Error
Terminate a string, starting a new one. Following calls to sbcat() or sbput() will append to a new string.
- sb - String buffer handle
Return: 0 => OK, -1 => Error
Return the current index in the string buffer.
- sb - String buffer handle
Return: Index
Do something on each string in the string buffer.
- sb - String buffer handle
- ix - Index to where in the string buffer to start
- fn - Function to be called on each string
Return: 0 => OK
Search for a string in the string buffer.
- sb - String buffer handle
- ix - Index to where in the string buffer to start searching
- fn - String comparison function (should return 0 if equal)
- what - Which string to search for
Return: Index of found string, or (size_t)-1 for error
Return a C string.
- sb - String buffer handle
- ix - Index to where in the string buffer
Return: The '\0'-terminated C string
Write a JSON array containing all strings in the string buffer.
- sb - String buffer handle
- ix - Index of where in the string buffer to start printing
- fn - Function that will actually do the printing
Return: 0 => OK
Write a CSV array containing all strings in the string buffer.
- sb - String buffer handle
- ix - Index of where in the string buffer to start printing
- sep - Which separator to use
- quote - Which quote character to use
- fn - Function that will actually do the printing
Return: 0 => OK