diff options
Diffstat (limited to 'src/util/string.h')
| -rw-r--r-- | src/util/string.h | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/util/string.h b/src/util/string.h new file mode 100644 index 0000000..6f71a11 --- /dev/null +++ b/src/util/string.h @@ -0,0 +1,107 @@ +#ifndef _SK_UTIL_STRING_H +#define _SK_UTIL_STRING_H + +#include "array.h" + +#include <stddef.h> + +/** + * stringize: + * Convert value of preprocessor macro to C string literal. + * + * @param x: the name of the macro + */ +#define stringize(x) quote(x) + +/** + * quote: + * Quote literally whatever is passed into this macro as a C string. + * + * @param x: the value to quote + * + */ +#ifdef HAVE_STRINGIZE +#define quote(x) #x +#else +#define quote(x) "x" +#endif + +/** + * boolstr: + * Make string out of bool. + * + * @param b: the boolean variable + * @returns "true" or "false" + */ +#define boolstr(b) (b ? "true" : "false") + +/** + * strdup: + * like strdup(3p). + */ +char *strdup(const char *s); + +/** + * strajoin: + * Joins a C string array using a separator character. + * + * The returned string should be free()d. + * + * @param sep: a char to insert between each element of the array + * @param a: the array to join (NULL terminated) + * @return a newly-allocated C string containing all of the elements joined + */ +#define strajoin(sep, a) stranjoin(sep, a, anullidx((void *)a)) + +/** + * stranjoin: + * Joins a C string array using a separator character. + * + * The returned string should be free()d. + * + * @param sep: a char to insert between each element of the array + * @param a: the array to join + * @param nmemb: the length of the array + * @return a newly-allocated C string containing all of the elements joined + */ +char *stranjoin(char sep, const char *a[], size_t nmemb); + +char *_strjoin(char sep, /* const char * */...); /* internal */ + +/** + * strjoin: + * Joins a number of strings together to form one long string, with the + * @separator inserted between each of them. The returned string + * should be free()d. + * + * @param sep: a char to insert between each of the strings + * @param ...: a list of strings to join + * @return a newly-allocated string containing all of the strings joined + */ +#define strjoin(sep, ...) _strjoin(sep, __VA_ARGS__, NULL) + +/** + * pathjoin: + * Joins a number of path components together to form one single path. + * The returned string should be free()d. + * + * @param ...: a list of path components to join + * @return a newly-allocated string containing all of the path components joined + */ +#define pathjoin(...) strjoin(stringize(DIRSEP)[0], __VA_ARGS__) + +/** + * strsplit: + * Splits a string into a NULL-terminated array by a given separator. + * + * Call this function with a NULL buffer to determine the size of the resulting + * array. + * + * @param str: the input string (null-terminated) + * @param sep: a string containing one or more separator characters + * @param buf: a buffer to put the array into + * @returns the number of bytes written into buf + */ +size_t strsplit(const char *restrict in, const char *restrict sep, void *buf); + +#endif |
