From 20ce2adbd3f70fa13b952892d03aea7bc1dc2902 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 18 Dec 2018 01:07:13 +0100 Subject: [PATCH] [utils] move string routines to strutils.c --- src/io/sink.c | 23 +++++------------------ src/utils/strutils.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 src/utils/strutils.c diff --git a/src/io/sink.c b/src/io/sink.c index 6fbd6b4e..0a974374 100644 --- a/src/io/sink.c +++ b/src/io/sink.c @@ -53,6 +53,9 @@ struct _aubio_sink_t { del_aubio_sink_t s_del; }; +extern uint_t aubio_str_path_has_extension(const char_t *filename, + const char_t *pattern); + #ifdef HAVE_VORBISENC typedef struct _aubio_sink_vorbis_t aubio_sink_vorbis_t; extern aubio_sink_vorbis_t * new_aubio_sink_vorbis(const char_t *uri, @@ -91,26 +94,12 @@ extern void aubio_sink_flac_do_multi(aubio_sink_flac_t *s, fmat_t* write_data, uint_t write); #endif /* HAVE_FLAC */ -static const char_t *aubio_get_extension(const char_t *filename) -{ - if (!filename) return NULL; - // find last occurence of dot character - const char_t *ext = strrchr(filename, '.'); - if (!ext || ext == filename) return ""; - else return ext + 1; -} - aubio_sink_t * new_aubio_sink(const char_t * uri, uint_t samplerate) { aubio_sink_t * s = AUBIO_NEW(aubio_sink_t); -#if defined(HAVE_VORBISENC) || defined(HAVE_FLAC) - const char_t * uri_ext = aubio_get_extension(uri); -#endif /* defined(HAVE_VORBISENC) || defined(HAVE_FLAC) */ #ifdef HAVE_VORBISENC // check if this uri could be for us - uint_t match_oggstream = 0; - if (uri_ext && strcmp (uri_ext, "ogg") == 0) match_oggstream = 1; - if (match_oggstream) { + if (aubio_str_path_has_extension(uri, "ogg")) { s->sink = (void *)new_aubio_sink_vorbis(uri, samplerate); if (s->sink) { s->s_do = (aubio_sink_do_t)(aubio_sink_vorbis_do); @@ -128,9 +117,7 @@ aubio_sink_t * new_aubio_sink(const char_t * uri, uint_t samplerate) { #ifdef HAVE_FLAC // check if this uri could be for us - uint_t match_flacstream = 0; - if (uri_ext && strcmp (uri_ext, "flac") == 0) match_flacstream = 1; - if (match_flacstream) { + if (aubio_str_path_has_extension(uri, "flac")) { s->sink = (void *)new_aubio_sink_flac(uri, samplerate); if (s->sink) { s->s_do = (aubio_sink_do_t)(aubio_sink_flac_do); diff --git a/src/utils/strutils.c b/src/utils/strutils.c new file mode 100644 index 00000000..4f61b647 --- /dev/null +++ b/src/utils/strutils.c @@ -0,0 +1,42 @@ +/* + Copyright (C) 2018 Paul Brossier + + This file is part of aubio. + + aubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#include "aubio_priv.h" + +const char_t *aubio_str_get_extension(const char_t *filename) +{ + if (!filename) return NULL; + // find last occurence of dot character + const char_t *ext = strrchr(filename, '.'); + if (!ext || ext == filename) return ""; + else return ext + 1; +} + +uint_t aubio_str_extension_matches(const char_t *ext, const char_t *pattern) +{ + return ext && pattern && (strncasecmp(ext, pattern, PATH_MAX) == 0); +} + +uint_t aubio_str_path_has_extension(const char_t *filename, + const char_t *pattern) +{ + const char_t *ext = aubio_str_get_extension(filename); + return aubio_str_extension_matches(ext, pattern); +} -- 2.11.0