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