Commit b35e9e19 authored by Stefano Sabatini's avatar Stefano Sabatini

lavu: add av_strtok()

The function strtok_r() is part of the POSIX.1 specification, but is not
available on some platforms. We provide an internal implementation, so we
do not need to rely on a platform implementation.
parent 88bdf747
......@@ -1186,7 +1186,6 @@ HAVE_LIST="
setrlimit
strerror_r
strptime
strtok_r
struct_addrinfo
struct_ipv6_mreq
struct_sockaddr_in6
......@@ -1585,17 +1584,14 @@ tcp_protocol_deps="network"
udp_protocol_deps="network"
# filters
abuffer_filter_deps="strtok_r"
aconvert_filter_deps="strtok_r"
aformat_filter_deps="strtok_r"
amovie_filter_deps="avcodec avformat"
blackframe_filter_deps="gpl"
boxblur_filter_deps="gpl"
cropdetect_filter_deps="gpl"
delogo_filter_deps="gpl"
drawtext_filter_deps="libfreetype"
frei0r_filter_deps="frei0r dlopen strtok_r"
frei0r_src_filter_deps="frei0r dlopen strtok_r"
frei0r_filter_deps="frei0r dlopen"
frei0r_src_filter_deps="frei0r dlopen"
hqdn3d_filter_deps="gpl"
movie_filter_deps="avcodec avformat"
mp_filter_deps="gpl avcodec"
......@@ -2942,7 +2938,6 @@ check_func ${malloc_prefix}posix_memalign && enable posix_memalign
check_func setrlimit
check_func strerror_r
check_func strptime
check_func strtok_r
check_func_headers conio.h kbhit
check_func_headers windows.h PeekNamedPipe
check_func_headers io.h setmode
......
......@@ -28,6 +28,7 @@
*/
#include "libavutil/audioconvert.h"
#include "libavutil/avstring.h"
#include "libavcodec/audioconvert.h"
#include "avfilter.h"
#include "internal.h"
......@@ -125,15 +126,15 @@ static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
aconvert->out_chlayout = 0;
aconvert->out_packing_fmt = -1;
if ((arg = strtok_r(args, ":", &ptr)) && strcmp(arg, "auto")) {
if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
goto end;
}
if ((arg = strtok_r(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
goto end;
}
if ((arg = strtok_r(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
if ((ret = ff_parse_packing_format((int *)&aconvert->out_packing_fmt, arg, ctx)) < 0)
goto end;
}
......
......@@ -50,7 +50,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
aformat->fmts_list = all_formats; \
} else { \
for (fmt_str = fmts_str; \
fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) { \
fmt_str = av_strtok(fmt_str, ",", &ptr); fmt_str = NULL) { \
if ((ret = ff_parse_##fmt_name((fmt_type *)&fmt, \
fmt_str, ctx)) < 0) { \
av_freep(&fmts_str); \
......
......@@ -25,6 +25,7 @@
*/
#include "libavutil/audioconvert.h"
#include "libavutil/avstring.h"
#include "libavutil/fifo.h"
#include "asrc_abuffer.h"
#include "internal.h"
......@@ -256,7 +257,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
char *args = av_strdup(args0);
int ret;
arg = strtok_r(args, ":", &ptr);
arg = av_strtok(args, ":", &ptr);
#define ADD_FORMAT(fmt_name) \
if (!arg) \
......@@ -266,7 +267,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
return ret; \
} \
if (*args) \
arg = strtok_r(NULL, ":", &ptr)
arg = av_strtok(NULL, ":", &ptr)
ADD_FORMAT(sample_rate);
ADD_FORMAT(sample_format);
......
......@@ -216,7 +216,7 @@ static av_cold int frei0r_init(AVFilterContext *ctx,
/* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
if ((path = av_strdup(getenv("FREI0R_PATH")))) {
char *p, *ptr = NULL;
for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
for (p = path; p = av_strtok(p, ":", &ptr); p = NULL)
if (frei0r->dl_handle = load_path(ctx, p, dl_name))
break;
av_free(path);
......
......@@ -160,6 +160,35 @@ char *av_get_token(const char **buf, const char *term)
return ret;
}
char *av_strtok(char *s, const char *delim, char **saveptr)
{
char *tok;
if (!s && !(s = *saveptr))
return NULL;
/* skip leading delimiters */
s += strspn(s, delim);
/* s now points to the first non delimiter char, or to the end of the string */
if (!*s) {
*saveptr = NULL;
return NULL;
}
tok = s++;
/* skip non delimiters */
s += strcspn(s, delim);
if (*s) {
*s = 0;
*saveptr = s+1;
} else {
*saveptr = NULL;
}
return tok;
}
#ifdef TEST
#undef printf
......
......@@ -141,4 +141,28 @@ char *av_d2str(double d);
*/
char *av_get_token(const char **buf, const char *term);
/**
* Split the string into several tokens which can be accessed by
* successive calls to av_strtok().
*
* A token is defined as a sequence of characters not belonging to the
* set specified in delim.
*
* On the first call to av_strtok(), s should point to the string to
* parse, and the value of saveptr is ignored. In subsequent calls, s
* should be NULL, and saveptr should be unchanged since the previous
* call.
*
* This function is similar to strtok_r() defined in POSIX.1.
*
* @param s the string to parse, may be NULL
* @param delim 0-terminated list of token delimiters, must be non-NULL
* @param saveptr user-provided pointer which points to stored
* information necessary for av_strtok() to continue scanning the same
* string. saveptr is updated to point to the next character after the
* first delimiter found, or to NULL if the string was terminated
* @return the found token, or NULL when no token is found
*/
char *av_strtok(char *s, const char *delim, char **saveptr);
#endif /* AVUTIL_AVSTRING_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment