From 962eec0440ad65a71c09a9e206e110c62d9de3c8 Mon Sep 17 00:00:00 2001 From: Uoti Urpala Date: Wed, 23 Feb 2011 16:18:09 +0200 Subject: bstr.[ch], path.[ch]: add string and path handling functions Add some new string and path handling functions to be used in following commits. Use new path handling functions to simplify find_files(). --- bstr.h | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'bstr.h') diff --git a/bstr.h b/bstr.h index 459bf70bef..33a47c0abc 100644 --- a/bstr.h +++ b/bstr.h @@ -22,18 +22,50 @@ #include #include #include +#include +#include "talloc.h" + +/* NOTE: 'len' is size_t, but most string-handling functions below assume + * that input size has been sanity checked and len fits in an int. + */ struct bstr { - const uint8_t *start; + unsigned char *start; size_t len; }; int bstrcmp(struct bstr str1, struct bstr str2); int bstrcasecmp(struct bstr str1, struct bstr str2); +int bstrchr(struct bstr str, int c); +struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str); +struct bstr bstr_strip(struct bstr str); +struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest); +struct bstr bstr_splice(struct bstr str, int start, int end); +long long bstrtoll(struct bstr str, struct bstr *rest, int base); + +static inline struct bstr bstr_cut(struct bstr str, int n) +{ + return (struct bstr){str.start + n, str.len - n}; +} + +static inline bool bstr_startswith(struct bstr str, struct bstr prefix) +{ + if (str.len < prefix.len) + return false; + return !memcmp(str.start, prefix.start, prefix.len); +} + +static inline char *bstrdup0(void *talloc_ctx, struct bstr str) +{ + // cast is live555 C++ compilation workaround + return talloc_strndup(talloc_ctx, (char *)str.start, str.len); +} // Create bstr compound literal from null-terminated string -#define BSTR(s) (struct bstr){(s), (s) ? strlen(s) : 0} +#define BSTR(s) (struct bstr){(char *)(s), (s) ? strlen(s) : 0} // create a pair (not single value!) for "%.*s" printf syntax #define BSTR_P(bstr) (int)((bstr).len), (bstr).start +#define WHITESPACE " \f\n\r\t\v" + #endif /* MPLAYER_BSTR_H */ -- cgit v1.2.3