[Spice-devel] [usbredir v2 PATCH] usbredirfilter: Win32: implement strtok_r to be used by windows (win_strtok_r)
Hans de Goede
hdegoede at redhat.com
Mon May 28 02:04:54 PDT 2012
Hi,
On 05/24/2012 04:40 PM, Uri Lublin wrote:
> mingw imlpements neigther strtok_r nor strtok_s.
>
> So here is a simple implementation of strtok_r (for windows).
> It assumes the delimiter is a single character, which is
> good enough for usbredirfilter.
If we're going to do this I would prefer to do a full
implementation, without the delimiter is a single char
limit. I know this is not important right now, but we
might start depending on multi-char delimiters in the
future and then this will come back to bite us.
> ---
> usbredirparser/usbredirfilter.c | 47 +++++++++++++++++++++++++++++++++++++++
> 1 files changed, 47 insertions(+), 0 deletions(-)
>
> diff --git a/usbredirparser/usbredirfilter.c b/usbredirparser/usbredirfilter.c
> index b74c921..df48acb 100644
> --- a/usbredirparser/usbredirfilter.c
> +++ b/usbredirparser/usbredirfilter.c
> @@ -23,6 +23,53 @@
> #include<string.h>
> #include<errno.h>
>
> +#ifdef WIN32
> +#define strtok_r win_strtok_r
> +
> +/*
> + * strtok_r like implementation for windows (mingw).
> + *
> + * Only the first character of delim is used as a delimiter
> + */
> +char *win_strtok_r(char *str, const char *delim, char **state)
> +{
> + const char d = delim[0];
> + char *p;
> + int found = 0;
> +
> + /* sanity checks */
> + if ((delim == NULL) || (state == NULL))
> + return NULL;
> +
> + if (str == NULL) {
> + str = *state;
> + }
> +
> + if (str == NULL) {
> + return NULL;
> + }
> +
> + for (p=str; *p ; p++) {
> + if (*p == d) {
> + found = 1;
> + *p++ = '\0';
> + while (*p&& *p==d) /* skip all delimiters */
I suggest adding a:
bool char_matches_delim(char c, const char *delim);
helper function and using that in place of the 2
*p == d checks, making this a fully compatible strtok
replacement drop-in.
> + p++;
> + break;
> + }
> + }
> +
> + *state = found ? p : NULL;
> +
> + /* do not return empty strings */
> + if (!*str)
> + return NULL;
> +
> + return str;
> +}
> +
> +#endif
> +
> #include "usbredirfilter.h"
>
> int usbredirfilter_string_to_rules(
Regards,
Hans
More information about the Spice-devel
mailing list