From f5b4d3aa7865c1baaecb9039209cd18dc1874618 Mon Sep 17 00:00:00 2001 From: Gustavo Sverzut Barbieri Date: Mon, 19 Nov 2007 13:41:16 -0300 Subject: [PATCH] Add lightmediascanner_utils. It provides: * lms_strstrip(): remove spaces from string begin and end, accounts length. * lms_which_extension(): check which of the given extension the name ends, returns it index. --- src/lib/Makefile.am | 9 +++- src/lib/lightmediascanner_utils.c | 91 +++++++++++++++++++++++++++++++++++++++ src/lib/lightmediascanner_utils.h | 66 ++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 src/lib/lightmediascanner_utils.c create mode 100644 src/lib/lightmediascanner_utils.h diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index edad179..4ca6845 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -2,10 +2,15 @@ MAINTAINERCLEANFILES = Makefile.in AM_CPPFLAGS = -I$(top_srcdir)/src/lib -include_HEADERS = lightmediascanner.h lightmediascanner_plugin.h +include_HEADERS = \ + lightmediascanner.h \ + lightmediascanner_plugin.h \ + lightmediascanner_utils.h lib_LTLIBRARIES = liblightmediascanner.la -liblightmediascanner_la_SOURCES = lightmediascanner.c +liblightmediascanner_la_SOURCES = \ + lightmediascanner.c \ + lightmediascanner_utils.c liblightmediascanner_la_LIBADD = -ldl @SQLITE3_LIBS@ liblightmediascanner_la_LDFLAGS = -version-info @version_info@ diff --git a/src/lib/lightmediascanner_utils.c b/src/lib/lightmediascanner_utils.c new file mode 100644 index 0000000..4024ca1 --- /dev/null +++ b/src/lib/lightmediascanner_utils.c @@ -0,0 +1,91 @@ +#include +#include +#include + +void +lms_strstrip(char *str, unsigned int *p_len) +{ + int i, len; + char *p; + + len = *p_len; + + if (len < 2) /* just '\0'? */ + return; + + p = str + len - 1; + for (i = len - 1; i >= 0; i--) { + if (isspace(*p)) { + *p = '\0'; + len--; + p--; + } else + break; + } + if (len == 0) { + *p_len = 0; + return; + } + + p = str; + for (i = 0; i < len; i++) { + if (isspace(*p)) + p++; + else + break; + } + len -= i; + if (len == 0) { + *str = '\0'; + *p_len = 0; + return; + } + + *p_len = len; + + if (str < p) + for (; len > 0; len--, str++, p++) + *str = *p; +} + +int +lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) { + int i; + unsigned int *exts_pos; + const char *s; + + exts_pos = alloca(exts_len * sizeof(*exts_pos)); + for (i = 0; i < exts_len; i++) + exts_pos[i] = exts[i].len; + + for (s = name + name_len - 1; s >= name; s--) { + int i, match; + char c1, c2; + + c1 = *s; + if (c1 >= 'a') + c2 = c1; + else + c2 = 'a' + c1 - 'A'; + + match = 0; + for (i = 0; i < exts_len; i++) { + if (exts_pos[i] > 0) { + char ce; + + ce = exts[i].str[exts_pos[i] - 1]; + if (ce == c1 || ce == c2) { + if (exts_pos[i] == 1) + return i; + exts_pos[i]--; + match = 1; + } else + exts_pos[i] = 0; + } + } + if (!match) + return -1; + } + + return -1; +} diff --git a/src/lib/lightmediascanner_utils.h b/src/lib/lightmediascanner_utils.h new file mode 100644 index 0000000..dd52cf5 --- /dev/null +++ b/src/lib/lightmediascanner_utils.h @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2007 by INdT + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * @author Gustavo Sverzut Barbieri + */ + +#ifndef _LIGHTMEDIASCANNER_UTILS_H_ +#define _LIGHTMEDIASCANNER_UTILS_H_ 1 + +#ifdef API +#undef API +#endif + +#ifdef __GNUC__ +# if __GNUC__ >= 4 +# define API __attribute__ ((visibility("default"))) +# else +# define API +# endif +# if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) +# define GNUC_NON_NULL(...) __attribute__((nonnull(__VA_ARGS__))) +# else +# define GNUC_NON_NULL(...) +# endif +#else +# define API +# define GNUC_NON_NULL(...) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + + struct lms_string_size { + char *str; + unsigned int len; + }; + +#define LMS_STATIC_STRING_SIZE(s) {s, sizeof(s) - 1} +#define LMS_ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) + + + API void lms_strstrip(char *str, unsigned int *p_len) GNUC_NON_NULL(1, 2); + API int lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) GNUC_NON_NULL(1, 3); + + + +#ifdef __cplusplus +} +#endif +#endif /* _LIGHTMEDIASCANNER_UTILS_H_ */ -- 2.7.4