ref: 20ce2adbd3f70fa13b952892d03aea7bc1dc2902
parent: d286fe436eb2725df329da508bb3c1b7caeed875
author: Paul Brossier <piem@piem.org>
date: Mon Dec 17 20:07:13 EST 2018
[utils] move string routines to strutils.c
--- a/src/io/sink.c
+++ b/src/io/sink.c
@@ -53,6 +53,9 @@
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 @@
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 @@
#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);
--- /dev/null
+++ b/src/utils/strutils.c
@@ -1,0 +1,42 @@
+/*
+ Copyright (C) 2018 Paul Brossier <piem@aubio.org>
+
+ 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 <http://www.gnu.org/licenses/>.
+
+*/
+
+#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);
+}