diff options
author | Uoti Urpala <uau@mplayer2.org> | 2011-07-28 11:07:47 +0300 |
---|---|---|
committer | Uoti Urpala <uau@mplayer2.org> | 2011-07-29 05:50:38 +0300 |
commit | e873d703e956d3e2e68b9e18562983b029b5c7a8 (patch) | |
tree | 44a2c48ae4e94bd580ffd5833cd71a037af36043 /bstr.c | |
parent | d8374376c0d46ffab88b96eb32b52621c34f562c (diff) | |
download | mpv-e873d703e956d3e2e68b9e18562983b029b5c7a8.tar.bz2 mpv-e873d703e956d3e2e68b9e18562983b029b5c7a8.tar.xz |
options: change option parsing to use bstr
Using bstr allows simpler parsing code, especially because it avoids
the need to modify or copy strings just to terminate extracted
substrings.
Diffstat (limited to 'bstr.c')
-rw-r--r-- | bstr.c | 49 |
1 files changed, 41 insertions, 8 deletions
@@ -20,6 +20,7 @@ #include <libavutil/avutil.h> #include <assert.h> #include <ctype.h> +#include <stdarg.h> #include "talloc.h" @@ -71,6 +72,15 @@ int bstrrchr(struct bstr str, int c) return -1; } +int bstrcspn(struct bstr str, const char *reject) +{ + int i; + for (i = 0; i < str.len; i++) + if (strchr(reject, str.start[i])) + break; + return i; +} + int bstr_find(struct bstr haystack, struct bstr needle) { for (int i = 0; i < haystack.len; i++) @@ -96,21 +106,18 @@ struct bstr bstr_strip(struct bstr str) return str; } -struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest) +struct bstr bstr_split(struct bstr str, const char *sep, struct bstr *rest) { - int start, end; + int start; for (start = 0; start < str.len; start++) if (!strchr(sep, str.start[start])) break; - for (end = start; end < str.len; end++) - if (strchr(sep, str.start[end])) - break; + str = bstr_cut(str, start); + int end = bstrcspn(str, sep); if (rest) { *rest = bstr_cut(str, end); } - str.start += start; - str.len = end - start; - return str; + return bstr_splice(str, 0, end); } @@ -131,6 +138,7 @@ struct bstr bstr_splice(struct bstr str, int start, int end) long long bstrtoll(struct bstr str, struct bstr *rest, int base) { + str = bstr_lstrip(str); char buf[51]; int len = FFMIN(str.len, 50); memcpy(buf, str.start, len); @@ -142,6 +150,20 @@ long long bstrtoll(struct bstr str, struct bstr *rest, int base) return r; } +double bstrtod(struct bstr str, struct bstr *rest) +{ + str = bstr_lstrip(str); + char buf[101]; + int len = FFMIN(str.len, 100); + memcpy(buf, str.start, len); + buf[len] = 0; + char *endptr; + double r = strtod(buf, &endptr); + if (rest) + *rest = bstr_cut(str, endptr - buf); + return r; +} + struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str) { if (str.len == 0) @@ -169,3 +191,14 @@ void bstr_lower(struct bstr str) for (int i = 0; i < str.len; i++) str.start[i] = tolower(str.start[i]); } + +int bstr_sscanf(struct bstr str, const char *format, ...) +{ + char *ptr = bstrdup0(NULL, str); + va_list va; + va_start(va, format); + int ret = vsscanf(ptr, format, va); + va_end(va); + talloc_free(ptr); + return ret; +} |