Advertisement
milanmetal

[C] strtok / used in Kernel, this 'reinventing wheel'

Dec 2nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. char* sp = NULL; /* the start position of the string */
  6.  
  7. char* strtok1(char* str, const char* delimiters) {
  8.  
  9.     int i = 0;
  10.     int len = strlen(delimiters);
  11.  
  12.     /* check in the delimiters */
  13.     if(len == 0)
  14.         printf("delimiters are empty\n");
  15.  
  16.     /* if the original string has nothing left */
  17.     if(!str && !sp)
  18.         return NULL;
  19.  
  20.     /* initialize the sp during the first call */
  21.     if(str && !sp)
  22.         sp = str;
  23.  
  24.     /* find the start of the substring, skip delimiters */
  25.     char* p_start = sp;
  26.     while(1) {
  27.         for(i = 0; i < len; i ++) {
  28.             if(*p_start == delimiters[i]) {
  29.                 p_start ++;
  30.                 break;
  31.             }
  32.         }
  33.  
  34.         if(i == len) {
  35.                sp = p_start;
  36.                break;
  37.         }
  38.     }
  39.  
  40.     /* return NULL if nothing left */
  41.     if(*sp == '\0') {
  42.         sp = NULL;
  43.         return sp;
  44.     }
  45.  
  46.     /* find the end of the substring, and
  47.         replace the delimiter with null */
  48.     while(*sp != '\0') {
  49.         for(i = 0; i < len; i ++) {
  50.             if(*sp == delimiters[i]) {
  51.                 *sp = '\0';
  52.                 break;
  53.             }
  54.         }
  55.  
  56.         sp ++;
  57.         if (i < len)
  58.             break;
  59.     }
  60.  
  61.     return p_start;
  62. }
  63.  
  64. /* https://fengl.org/2013/01/01/implement-strtok-in-c-2/ */
  65. /* // ULtra-simple. haha! */
  66. /* // ------------------------------------------- */
  67. /* char* strtok1(char *str, const char* delim) { */
  68. /*   static char* _buffer; */
  69. /*   if(str != NULL) _buffer = str; */
  70. /*   if(_buffer[0] == '\0') return NULL; */
  71.  
  72. /*   char *ret = _buffer, *b; */
  73. /*   const char *d; */
  74.  
  75. /*   for(b = _buffer; *b !='\0'; b++) { */
  76. /*     for(d = delim; *d != '\0'; d++) { */
  77. /*       if(*b == *d) { */
  78. /*         *b = '\0'; */
  79. /*         _buffer = b+1; */
  80.  
  81. /*         // skip the beginning delimiters */
  82. /*         if(b == ret) { */
  83. /*           ret++; */
  84. /*           continue; */
  85. /*         } */
  86. /*         return ret; */
  87. /*       } */
  88. /*     } */
  89. /*   } */
  90.  
  91. /*   return ret; */
  92. /* } */
  93.  
  94.  
  95. int main ()
  96. {
  97.   char str[] ="- This, a sample string.";
  98.   char * pch;
  99.   printf ("Splitting string \"%s\" into tokens:\n",str);
  100.   pch = strtok1(str," ,.-");
  101.   while (pch != NULL)
  102.     {
  103.       printf ("%s\n",pch);
  104.       pch = strtok1(NULL, " ,.-");
  105.     }
  106.   return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement