Browse code

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.

Stefano Sabatini authored on 2011/10/15 07:14:37
Showing 7 changed files
... ...
@@ -1186,7 +1186,6 @@ HAVE_LIST="
1186 1186
     setrlimit
1187 1187
     strerror_r
1188 1188
     strptime
1189
-    strtok_r
1190 1189
     struct_addrinfo
1191 1190
     struct_ipv6_mreq
1192 1191
     struct_sockaddr_in6
... ...
@@ -1585,17 +1584,14 @@ tcp_protocol_deps="network"
1585 1585
 udp_protocol_deps="network"
1586 1586
 
1587 1587
 # filters
1588
-abuffer_filter_deps="strtok_r"
1589
-aconvert_filter_deps="strtok_r"
1590
-aformat_filter_deps="strtok_r"
1591 1588
 amovie_filter_deps="avcodec avformat"
1592 1589
 blackframe_filter_deps="gpl"
1593 1590
 boxblur_filter_deps="gpl"
1594 1591
 cropdetect_filter_deps="gpl"
1595 1592
 delogo_filter_deps="gpl"
1596 1593
 drawtext_filter_deps="libfreetype"
1597
-frei0r_filter_deps="frei0r dlopen strtok_r"
1598
-frei0r_src_filter_deps="frei0r dlopen strtok_r"
1594
+frei0r_filter_deps="frei0r dlopen"
1595
+frei0r_src_filter_deps="frei0r dlopen"
1599 1596
 hqdn3d_filter_deps="gpl"
1600 1597
 movie_filter_deps="avcodec avformat"
1601 1598
 mp_filter_deps="gpl avcodec"
... ...
@@ -2942,7 +2938,6 @@ check_func  ${malloc_prefix}posix_memalign      && enable posix_memalign
2942 2942
 check_func  setrlimit
2943 2943
 check_func  strerror_r
2944 2944
 check_func  strptime
2945
-check_func  strtok_r
2946 2945
 check_func_headers conio.h kbhit
2947 2946
 check_func_headers windows.h PeekNamedPipe
2948 2947
 check_func_headers io.h setmode
... ...
@@ -28,6 +28,7 @@
28 28
  */
29 29
 
30 30
 #include "libavutil/audioconvert.h"
31
+#include "libavutil/avstring.h"
31 32
 #include "libavcodec/audioconvert.h"
32 33
 #include "avfilter.h"
33 34
 #include "internal.h"
... ...
@@ -125,15 +126,15 @@ static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
125 125
     aconvert->out_chlayout    = 0;
126 126
     aconvert->out_packing_fmt = -1;
127 127
 
128
-    if ((arg = strtok_r(args, ":", &ptr)) && strcmp(arg, "auto")) {
128
+    if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
129 129
         if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
130 130
             goto end;
131 131
     }
132
-    if ((arg = strtok_r(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
132
+    if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
133 133
         if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
134 134
             goto end;
135 135
     }
136
-    if ((arg = strtok_r(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
136
+    if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
137 137
         if ((ret = ff_parse_packing_format((int *)&aconvert->out_packing_fmt, arg, ctx)) < 0)
138 138
             goto end;
139 139
     }
... ...
@@ -50,7 +50,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
50 50
         aformat->fmts_list = all_formats;                               \
51 51
     } else {                                                            \
52 52
         for (fmt_str = fmts_str;                                        \
53
-             fmt_str = strtok_r(fmt_str, ",", &ptr); fmt_str = NULL) {  \
53
+             fmt_str = av_strtok(fmt_str, ",", &ptr); fmt_str = NULL) { \
54 54
             if ((ret = ff_parse_##fmt_name((fmt_type *)&fmt,            \
55 55
                                            fmt_str, ctx)) < 0) {        \
56 56
                 av_freep(&fmts_str);                                    \
... ...
@@ -25,6 +25,7 @@
25 25
  */
26 26
 
27 27
 #include "libavutil/audioconvert.h"
28
+#include "libavutil/avstring.h"
28 29
 #include "libavutil/fifo.h"
29 30
 #include "asrc_abuffer.h"
30 31
 #include "internal.h"
... ...
@@ -256,7 +257,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
256 256
     char *args = av_strdup(args0);
257 257
     int ret;
258 258
 
259
-    arg = strtok_r(args, ":", &ptr);
259
+    arg = av_strtok(args, ":", &ptr);
260 260
 
261 261
 #define ADD_FORMAT(fmt_name)                                            \
262 262
     if (!arg)                                                           \
... ...
@@ -266,7 +267,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
266 266
         return ret;                                                     \
267 267
     }                                                                   \
268 268
     if (*args)                                                          \
269
-        arg = strtok_r(NULL, ":", &ptr)
269
+        arg = av_strtok(NULL, ":", &ptr)
270 270
 
271 271
     ADD_FORMAT(sample_rate);
272 272
     ADD_FORMAT(sample_format);
... ...
@@ -216,7 +216,7 @@ static av_cold int frei0r_init(AVFilterContext *ctx,
216 216
     /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
217 217
     if ((path = av_strdup(getenv("FREI0R_PATH")))) {
218 218
         char *p, *ptr = NULL;
219
-        for (p = path; p = strtok_r(p, ":", &ptr); p = NULL)
219
+        for (p = path; p = av_strtok(p, ":", &ptr); p = NULL)
220 220
             if (frei0r->dl_handle = load_path(ctx, p, dl_name))
221 221
                 break;
222 222
         av_free(path);
... ...
@@ -160,6 +160,35 @@ char *av_get_token(const char **buf, const char *term)
160 160
     return ret;
161 161
 }
162 162
 
163
+char *av_strtok(char *s, const char *delim, char **saveptr)
164
+{
165
+    char *tok;
166
+
167
+    if (!s && !(s = *saveptr))
168
+        return NULL;
169
+
170
+    /* skip leading delimiters */
171
+    s += strspn(s, delim);
172
+
173
+    /* s now points to the first non delimiter char, or to the end of the string */
174
+    if (!*s) {
175
+        *saveptr = NULL;
176
+        return NULL;
177
+    }
178
+    tok = s++;
179
+
180
+    /* skip non delimiters */
181
+    s += strcspn(s, delim);
182
+    if (*s) {
183
+        *s = 0;
184
+        *saveptr = s+1;
185
+    } else {
186
+        *saveptr = NULL;
187
+    }
188
+
189
+    return tok;
190
+}
191
+
163 192
 #ifdef TEST
164 193
 
165 194
 #undef printf
... ...
@@ -141,4 +141,28 @@ char *av_d2str(double d);
141 141
  */
142 142
 char *av_get_token(const char **buf, const char *term);
143 143
 
144
+/**
145
+ * Split the string into several tokens which can be accessed by
146
+ * successive calls to av_strtok().
147
+ *
148
+ * A token is defined as a sequence of characters not belonging to the
149
+ * set specified in delim.
150
+ *
151
+ * On the first call to av_strtok(), s should point to the string to
152
+ * parse, and the value of saveptr is ignored. In subsequent calls, s
153
+ * should be NULL, and saveptr should be unchanged since the previous
154
+ * call.
155
+ *
156
+ * This function is similar to strtok_r() defined in POSIX.1.
157
+ *
158
+ * @param s the string to parse, may be NULL
159
+ * @param delim 0-terminated list of token delimiters, must be non-NULL
160
+ * @param saveptr user-provided pointer which points to stored
161
+ * information necessary for av_strtok() to continue scanning the same
162
+ * string. saveptr is updated to point to the next character after the
163
+ * first delimiter found, or to NULL if the string was terminated
164
+ * @return the found token, or NULL when no token is found
165
+ */
166
+char *av_strtok(char *s, const char *delim, char **saveptr);
167
+
144 168
 #endif /* AVUTIL_AVSTRING_H */