⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strtok.c

📁 Nucleus Common Library for Integrator using ARM Developer Suite
💻 C
字号:
/*************************************************************************
*
*               Copyright Mentor Graphics Corporation 2002
*                         All Rights Reserved.
*
* THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS
* THE PROPERTY OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS
* SUBJECT TO LICENSE TERMS.
*
*************************************************************************/

/*************************************************************************
* FILE NAME                                     VERSION
*
*       strtok.c                               Nucleus Common Library 1.1
*
* DESCRIPTION
*
*       This file contains the implementation of NCL_strtok.
*
* DATA STRUCTURES
*
*       None.
*
* FUNCTIONS
*
*       NCL_strtok
*
* DEPENDENCIES
*
*       ncl.h
*       string.h
*       nucleus.h
*
************************************************************************/

#define NU_NCL_SOURCE_FILE

#include "ncl\inc\ncl.h"
#include "ncl\inc\string.h"
#include "plus\nucleus.h"                                /* MMU Support */

#ifdef NU_NO_ANSI

/*************************************************************************
*
*   FUNCTION
*
*       NCL_strtoul
*
*   DESCRIPTION
*
*       This function is NOT an ANSI C library function.  It is reentrant.
*       Strtok considers s1 to be the address of a string pointer,
*       pointing to a sequence of zero or more text "tokens", separated
*       by spans of one or more characters from the separator string s2.
*       The first call should pass the address of a pointer to a token
*       string s1, and strtok returns a pointer to the first character
*       of the first token. S1 gets updated to point to the remaining
*       portion of the token string. Subsequent calls to strtok should
*       pass the address of s1, just as with the first call. Since the
*       string comes back modified each time, a copy of the original
*       string should be made before the first call using the strcpy
*       function. The separator string s2 may be different from call to
*       call. When there are no more tokens in s1, a zero is returned.
*
*   INPUTS
*
*       s1                  string with tokens and delimiters
*       s2                  delimiters
*
*   OUTPUTS
*
*       *s1                 updated to point to remaining string - the
*                           token returned as char*
*                             (passed by reference)
*       char*               first char of first token
*
*************************************************************************/

char *NCL_strtok(register char **s1, register const char *s2)
{
    register const char *delim=s2;             /* pointer to delimiters */
    register char *str=*s1;                    /* working pointer to s1 */
    register char *tok=*s1;     /* ptr to s1 (returned as ptr to token) */

    str += NCL_strspn(*s1, s2);        /* strip off leading delimiters */
    tok=str;                      /* move token pointer to new position */

    while (*str)                       /* while str pointer is not null */
    {
        while (*delim)               /* while delim pointer is not null */
        {
            if (*str==*delim++)     /* if char in str matches delimiter */
            {
                *str=0;                 /* replace token with null char */
                str++;                   /* increment str for next call */
                *s1=str;            /* store new pointer position in s1 */

                return tok;                  /* return pointer to token */
            }
        }
        delim=s2;                   /* reset delimiter pointer to front */
        str++;                          /* increment str for while loop */
    }

    *s1=str;                                      /* set string to null */

    return tok;                          /* return last token in string */
}

#else /* NU_NO_ANSI */

/*************************************************************************
*
*   FUNCTION
*
*       NCL_strtoul
*
*   DESCRIPTION
*
*       Considers s1 to be a string of tokens possibly
*       separated by characters from s2.  The first time
*       this routine is called (with s1 pointing to a string)
*       a pointer to the first token in returned.
*       The delimiter itself is replaced by a '\0' before
*       returning the result.  The next time that this routine
*       is called (with s1 being 0),  the comparison begins
*       where the last token ended.  A 0 is returned when
*       there are no more tokens in string s1.
*
*       It will strip off any leading 'token separators'
*       from the token string.
*
*       This function is an ANSI C library function.  It is NOT reentrant.
*
*   INPUTS
*
*       s1                  The string of tokens
*       s2                  Characters that seperate tokens in s1
*
*   OUTPUTS
*
*       char*               pointer to the first char of first token.
*                           If there are no more tokens then return 0.
*
*************************************************************************/

char *NCL_strtok(register char *s1, register const char *s2)
{
    static char *tok;
    register char *p;
    register const char *s;
    register char c;

    if (s1 == 0)
        s1 = tok;

    s1 += NCL_strspn(s1, s2);

    if (*s1 == '\0')
    {
        return (char *)0;
    }

    for (p = s1; (c = *p) != 0; ++p)
    {
        s = s2;
        do
        {
            if (*s == c)
            {
                *p = '\0';
                tok = p+1;

                return s1;
            }
        } while (*s++);
    }

    tok = p;

    return s1;
}

#endif /* NU_NO_ANSI */


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -