Skip to content

Commit

Permalink
made safe strtok implementation static to music.c for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
sezero committed Nov 18, 2019
1 parent 4378df0 commit 440bca2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
69 changes: 34 additions & 35 deletions music.c
Expand Up @@ -77,38 +77,6 @@ static int num_decoders = 0;
/* Semicolon-separated SoundFont paths */
static char* soundfont_paths = NULL;

/*
* public domain strtok_r() by Charlie Gordon
*
* from comp.lang.c 9/14/2007
*
* http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
*
* (Declaration that it's public domain):
* http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
*/
char *Mix_strtok_safe(char *str, const char *delim, char **nextp)
{
char *ret;
if (str == NULL) {
str = *nextp;
}

str += strspn(str, delim);
if (*str == '\0') {
return NULL;
}
ret = str;

str += strcspn(str, delim);
if (*str) {
*str++ = '\0';
}

*nextp = str;
return ret;
}

/* Interfaces for the various music interfaces, ordered by priority */
static Mix_MusicInterface *s_music_interfaces[] =
{
Expand Down Expand Up @@ -1129,6 +1097,38 @@ const char* Mix_GetSoundFonts(void)
return NULL;
}

/*
* public domain strtok_r() by Charlie Gordon
*
* from comp.lang.c 9/14/2007
*
* http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
*
* (Declaration that it's public domain):
* http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
*/
static char *_strtok_safe(char *str, const char *delim, char **nextp)
{
char *ret;
if (str == NULL) {
str = *nextp;
}

str += strspn(str, delim);
if (*str == '\0') {
return NULL;
}
ret = str;

str += strcspn(str, delim);
if (*str) {
*str++ = '\0';
}

*nextp = str;
return ret;
}

int Mix_EachSoundFont(int (SDLCALL *function)(const char*, void*), void *data)
{
char *context, *path, *paths;
Expand All @@ -1150,9 +1150,8 @@ int Mix_EachSoundFont(int (SDLCALL *function)(const char*, void*), void *data)
#else
#define SEPARATOR ":;"
#endif
for (path = Mix_strtok_safe(paths, SEPARATOR, &context);
path;
path = Mix_strtok_safe(NULL, SEPARATOR, &context))
for (path = _strtok_safe(paths, SEPARATOR, &context); path;
path = _strtok_safe(NULL, SEPARATOR, &context))
{
if (!function(path, data)) {
continue;
Expand Down
3 changes: 0 additions & 3 deletions music.h
Expand Up @@ -119,9 +119,6 @@ extern void unload_music(void);
extern char *music_cmd;
extern SDL_AudioSpec music_spec;

/* Safe strtok implementation */
extern char *Mix_strtok_safe(char *str, const char *delim, char **nextp);

#endif /* MUSIC_H_ */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 440bca2

Please sign in to comment.