SDL_strtokr.c (3002B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20 */ 21 #if defined(__clang_analyzer__) 22 #define SDL_DISABLE_ANALYZE_MACROS 1 23 #endif 24 25 #include "../SDL_internal.h" 26 27 #include "SDL_stdinc.h" 28 29 char *SDL_strtokr(char *s1, const char *s2, char **ptr) 30 { 31 #if defined(HAVE_STRTOK_R) 32 return strtok_r(s1, s2, ptr); 33 34 #elif defined(_MSC_VER) && defined(HAVE_STRTOK_S) 35 return strtok_s(s1, s2, ptr); 36 37 #else /* SDL implementation */ 38 /* 39 * Adapted from _PDCLIB_strtok() of PDClib library at 40 * https://github.com/DevSolar/pdclib.git 41 * 42 * The code was under CC0 license: 43 * https://creativecommons.org/publicdomain/zero/1.0/legalcode : 44 * 45 * No Copyright 46 * 47 * The person who associated a work with this deed has dedicated the 48 * work to the public domain by waiving all of his or her rights to 49 * the work worldwide under copyright law, including all related and 50 * neighboring rights, to the extent allowed by law. 51 * 52 * You can copy, modify, distribute and perform the work, even for 53 * commercial purposes, all without asking permission. See Other 54 * Information below. 55 */ 56 const char *p = s2; 57 58 if (!s2 || !ptr || (!s1 && !*ptr)) return NULL; 59 60 if (s1 != NULL) { /* new string */ 61 *ptr = s1; 62 } else { /* old string continued */ 63 if (*ptr == NULL) { 64 /* No old string, no new string, nothing to do */ 65 return NULL; 66 } 67 s1 = *ptr; 68 } 69 70 /* skip leading s2 characters */ 71 while (*p && *s1) { 72 if (*s1 == *p) { 73 /* found separator; skip and start over */ 74 ++s1; 75 p = s2; 76 continue; 77 } 78 ++p; 79 } 80 81 if (! *s1) { /* no more to parse */ 82 *ptr = s1; 83 return NULL; 84 } 85 86 /* skipping non-s2 characters */ 87 *ptr = s1; 88 while (**ptr) { 89 p = s2; 90 while (*p) { 91 if (**ptr == *p++) { 92 /* found separator; overwrite with '\0', position *ptr, return */ 93 *((*ptr)++) = '\0'; 94 return s1; 95 } 96 } 97 ++(*ptr); 98 } 99 100 /* parsed to end of string */ 101 return s1; 102 #endif 103 }