diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2011-10-28 17:07:11 +0400 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2011-10-28 17:07:11 +0400 |
commit | 2ed7a66653c9c94cb47064cb12092c2e81331b9b (patch) | |
tree | fd74c9e051fb00c941a3d474d48a5655919ab140 /source/blender/blenlib/intern/string.c | |
parent | 0d63bb005ff7c45ca0ebcfbe4eaf1215dfe2d3e2 (diff) |
BLI_string: Adding the BLI_strtok_r function, which mimics stdlib strtok_r (unavailable on some systems).
It allows to iterate over a string, returning an new element each time, using a char as separator. See BLI_String.h's comments for more info and an example.
Needed by the UI template list patch following!
Diffstat (limited to 'source/blender/blenlib/intern/string.c')
-rw-r--r-- | source/blender/blenlib/intern/string.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 3a66425a5de..3ec84e0b593 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -375,6 +375,35 @@ int BLI_natstrcmp(const char *s1, const char *s2) return 0; } +/* As unfortunately strtok_r is not available everywhere... */ +char *BLI_strtok_r(char *str, const char *delimiter, char **ctx) +{ + char *cut = NULL, *ret = NULL; + char *split = str ? str : *ctx; + + if(!split) { + return ret; + } + + cut = strchr(split, *delimiter); + if(cut) { + size_t len_ret = cut - split; + size_t len_ctx = strlen(split) - len_ret - 1; + ret = BLI_strdupn(split, len_ret); + if(len_ctx > 0) { + *ctx = split+len_ret+1; + } + else { + *ctx = NULL; + } + } + else { + ret = BLI_strdup(split); + *ctx = NULL; + } + return ret; +} + void BLI_timestr(double _time, char *str) { /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */ |