简体   繁体   English

stGok_r为MinGW

[英]strtok_r for MinGW

strtok_r is the reentrant variant of strtok. strtok_r是strtok的可重入变体。 It is POSIX-conformant. 符合POSIX标准。 However, it is missing from MinGW, and I'm trying to compile a program that is using it. 但是,MinGW缺少它,我正在尝试编译正在使用它的程序。

Is there any way I could add a standard implementation of this function, perhaps to the project's own code, or to MinGW's standard library functions? 有没有什么办法可以添加这个函数的标准实现,可能是项目自己的代码,还是MinGW的标准库函数?

Since there are some license questions about the code from another answer, here's one that's explicitly public domain: 由于有一些关于来自另一个答案的代码的许可证问题,这里是一个明确的公共领域:

/* 
 * public domain strtok_r() by Charlie Gordon
 *
 *   from comp.lang.c  9/14/2007
 *
 *      http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
 *
 *     (Declaration that it's public domain):
 *      http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
 */

char* strtok_r(
    char *str, 
    const char *delim, 
    char **nextp)
{
    char *ret;

    if (str == NULL)
    {
        str = *nextp;
    }

    str += strspn(str, delim);

    if (*str == '\0')
    {
        return NULL;
    }

    ret = str;

    str += strcspn(str, delim);

    if (*str)
    {
        *str++ = '\0';
    }

    *nextp = str;

    return ret;
}

Here's the source code which you can simply add to your own library/function in your project: 这是您可以简单地添加到项目中自己的库/函数的源代码:

char *strtok_r(char *str, const char *delim, char **save)
{
    char *res, *last;

    if( !save )
        return strtok(str, delim);
    if( !str && !(str = *save) )
        return NULL;
    last = str + strlen(str);
    if( (*save = res = strtok(str, delim)) )
    {
        *save += strlen(res);
        if( *save < last )
            (*save)++;
        else
            *save = NULL;
    }
    return res;
}

Is the FreeBSD implementation any use to you? FreeBSD实现对你有用吗?

Its liberally licensed but integrating it may have some requirements on your project documentation (adding an acknowledgement that the code has been included). 它自由许可但集成它可能对您的项目文档有一些要求(添加一个包含代码的确认)。

MINGW has no implementation of strtok_r . strtok_r没有执行strtok_r However you can find a thread-safe implementation in the link below: 但是,您可以在以下链接中找到线程安全的实现:

http://www.raspberryginger.com/jbailey/minix/html/strtok__r_8c-source.html http://www.raspberryginger.com/jbailey/minix/html/strtok__r_8c-source.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM