From b6a9ae621e9b4c1507ba8a6e6550740b8f79abe7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 28 Jun 2020 04:38:51 +0200 Subject: stdlib: added strtok() --- src/pOS/arch/x86/libc/string/strtok.cpp | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/pOS/arch/x86/libc/string/strtok.cpp diff --git a/src/pOS/arch/x86/libc/string/strtok.cpp b/src/pOS/arch/x86/libc/string/strtok.cpp new file mode 100644 index 0000000..b6fb4b3 --- /dev/null +++ b/src/pOS/arch/x86/libc/string/strtok.cpp @@ -0,0 +1,48 @@ +#include +#include + +char* strtok(char* str, const char* delim) +{ + static char* lastPos = 0; + static char token[100]; + + if(!str && !lastPos) + return 0; + + if(!delim) + return 0; + + if(str) + lastPos = str; + + int delim_len = strlen(delim); + char* strt_ptr = lastPos; + int count = 0; + + while(*lastPos != '\0') + { + bool is_found = false; + for(int y = 0; y < delim_len; y++) + { + if(*(delim + y) == *lastPos) + is_found = true; + } + lastPos++; + if(is_found) + break; + count++; + } + + if(*lastPos == '\0') + lastPos = NULL; + + if(!count) + return strtok(0, delim); + + for(int x = 0; x < count; x++) + token[x] = *(strt_ptr + x); + token[count] = '\0'; + + return token; +} + -- cgit 1.4.1