c语言之strtok_r函数和strtok函数

来源:互联网 发布:北京it外包 编辑:程序博客网 时间:2024/06/05 16:53

     c语言之strtok_r函数是linux平台下的strtok函数的线程安全版本,windows的string.h中并不包含此函数。其函数原型如下:

     char *strtok_r(char *str, const char *delim, char **saveptr);

     参数char **saveptr参数是一个指向char *的指针变量,用来在strtok_r内部保存切分时的上下文,以应对连续调用分解相同源字符串。

     第一次调用strtok_r时,str参数必须指向待提取的字符串,saveptr参数的值可以忽略。连续调用时,str赋值为NULL,saveptr为上次调用后返回的值,不要修改。

     其函数实现如下:

  1. /* 
  2.  * strtok_r.c: 
  3.  * Implementation of strtok_r for systems which don't have it. 
  4.  * 
  5.  * This is taken from the GNU C library and is distributed under the terms of 
  6.  * the LGPL. See copyright notice below. 
  7.  * 
  8.  */  
  9.   
  10. #ifdef HAVE_CONFIG_H   
  11. #include "configuration.h"   
  12. #endif /* HAVE_CONFIG_H */   
  13.   
  14. #ifndef HAVE_STRTOK_R   
  15.   
  16. static const char rcsid[] = "$Id: strtok_r.c,v 1.1 2001/04/24 14:25:34 chris Exp $";  
  17.   
  18. #include <string.h>   
  19.   
  20. #undef strtok_r   
  21.   
  22. /* Parse S into tokens separated by characters in DELIM. 
  23.    If S is NULL, the saved pointer in SAVE_PTR is used as 
  24.    the next starting point.  For example: 
  25.         char s[] = "-abc-=-def"; 
  26.         char *sp; 
  27.         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def" 
  28.         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL 
  29.         x = strtok_r(NULL, "=", &sp);   // x = NULL 
  30.                 // s = "abc/0-def/0" 
  31. */  
  32. char *strtok_r(char *s, const char *delim, char **save_ptr) {  
  33.     char *token;  
  34.   
  35.     if (s == NULL) s = *save_ptr;  
  36.   
  37.     /* Scan leading delimiters.  */  
  38.     s += strspn(s, delim);  
  39.     if (*s == '/0')   
  40.         return NULL;  
  41.   
  42.     /* Find the end of the token.  */  
  43.     token = s;  
  44.     s = strpbrk(token, delim);  
  45.     if (s == NULL)  
  46.         /* This token finishes the string.  */  
  47.         *save_ptr = strchr(token, '/0');  
  48.     else {  
  49.         /* Terminate the token and make *SAVE_PTR point past it.  */  
  50.         *s = '/0';  
  51.         *save_ptr = s + 1;  
  52.     }  
  53.   
  54.     return token;
  55.  } 
  56. 代码整体的流程如下:

    (1)判断参数s是否为NULL,如果是NULL就以传递进来的save_ptr作为起始分解位置;若不是NULL,则以s开始切分。

    (2)跳过待分解字符串开始的所有分界符。

    (3)判断当前待分解的位置是否为'/0',若是则返回NULL(联系到(一)中所说对返回值为NULL的解释);不是则继续。

    (4)保存当前的待分解串的指针token,调用strpbrk在token中找分界符:如果找不到,则将save_ptr赋值为待分解串尾部'/0'所在的位置,token没有发生变化;若找的到则将分界符所在位置赋值为'/0',token相当于被截断了(提取出来),save_ptr指向分界符的下一位。

    (5)函数的最后(无论找到还是没找到)都将返回。