Skip to content

ahy1/strbuf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

strbuf

Description

strbuf is a simple library for creating strings in C.

The strings are stored in a continous buffer, separated by '\0'.

Usage

Just add strbuf.c to your project file / Makefile and include strbuf.h where needed.

Functions

STRBUF *sballoc(size_t capacity)

Create a new string buffer.

  • capacity - Initial buffer capacity

Return: A new string buffer handle (to be freed with sbfree)

STRBUF *sbrealloc(STRBUF *sb, size_t capacity)

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

int sbfree(STRBUF *sb)

Release all allocations related to a string buffer.

  • sb - String buffer handle or NULL

Return: 0 => OK, -1 => Error

STRBUF *sbexpand(STRBUF *sb, size_t needed_capacity)

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

int sbcat(STRBUF *sb, const char *str)

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

int sbput(STRBUF *sb, int ch)

Append a character to the current string.

  • sb - String buffer handle
  • ch - Character to be appended

Return: ch => OK, -1 => Error

int sbstop(STRBUF *sb)

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

size_t sbix(STRBUF *sb)

Return the current index in the string buffer.

  • sb - String buffer handle

Return: Index

int sbforeach(STRBUF *sb, size_t ix, int (*fn)(const char *))

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

size_t sbsearch(STRBUF *sb, size_t ix, int (*fn)(const char *, const char *), const char *what)

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

const char *sbcstr(STRBUF *sb, size_t ix)

Return a C string.

  • sb - String buffer handle
  • ix - Index to where in the string buffer

Return: The '\0'-terminated C string

int sbwritejson(STRBUF *sb, size_t ix, int (*fn)(const char *))

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

int sbwritecsv(STRBUF *sb, size_t ix, int sep, int quote, int (*fn)(const char *))

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

About

String buffer library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages