src/compat.c
author Ozkan Sezer
Wed, 18 Dec 2019 18:55:56 +0300
changeset 1098 3be892f72aa7
parent 1012 7642e4258f8e
child 1129 888c7be704ce
permissions -rw-r--r--
fix build.
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2019 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 
    22 #include <stddef.h>
    23 #include "compat.h"
    24 
    25 #if !defined(HAVE_SDL_STRTOKR)
    26 /*
    27  * Adapted from _PDCLIB_strtok() of PDClib library at
    28  * https://github.com/DevSolar/pdclib.git
    29  *
    30  * The code was under CC0 license:
    31  * https://creativecommons.org/publicdomain/zero/1.0/legalcode :
    32  *
    33  *                        No Copyright
    34  *
    35  * The person who associated a work with this deed has dedicated the
    36  * work to the public domain by waiving all of his or her rights to
    37  * the work worldwide under copyright law, including all related and
    38  * neighboring rights, to the extent allowed by law.
    39  *
    40  * You can copy, modify, distribute and perform the work, even for
    41  * commercial purposes, all without asking permission. See Other
    42  * Information below.
    43  */
    44 char *SDL_strtokr(char *s1, const char *s2, char **ptr)
    45 {
    46     const char *p = s2;
    47 
    48     if (!s2 || !ptr || (!s1 && !*ptr)) return NULL;
    49 
    50     if (s1 != NULL) {  /* new string */
    51         *ptr = s1;
    52     } else { /* old string continued */
    53         if (*ptr == NULL) {
    54         /* No old string, no new string, nothing to do */
    55             return NULL;
    56         }
    57         s1 = *ptr;
    58     }
    59 
    60     /* skip leading s2 characters */
    61     while (*p && *s1) {
    62         if (*s1 == *p) {
    63         /* found separator; skip and start over */
    64             ++s1;
    65             p = s2;
    66             continue;
    67         }
    68         ++p;
    69     }
    70 
    71     if (! *s1) { /* no more to parse */
    72         *ptr = s1;
    73         return NULL;
    74     }
    75 
    76     /* skipping non-s2 characters */
    77     *ptr = s1;
    78     while (**ptr) {
    79         p = s2;
    80         while (*p) {
    81             if (**ptr == *p++) {
    82             /* found separator; overwrite with '\0', position *ptr, return */
    83                 *((*ptr)++) = '\0';
    84                 return s1;
    85             }
    86         }
    87         ++(*ptr);
    88     }
    89 
    90     /* parsed to end of string */
    91     return s1;
    92 }
    93 #endif /* HAVE_SDL_STRTOKR */