2 * Copyright (C) 2008-2011 by ProFUSION embedded systems
3 * Copyright (C) 2007 by INdT
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * @author Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
23 #include <lightmediascanner_utils.h>
30 * Strips string, in place.
32 * @param str string to be stripped.
33 * @param p_len string length to analyse, also the place where the final size
37 lms_strstrip(char *str, unsigned int *p_len)
53 for (i = len - 1; i >= 0; i--) {
54 if (isspace(*p) || *p == '\0') {
67 for (i = 0; i < len; i++) {
83 for (; len >= 0; len--, str++, p++)
88 * If string exists, strips it, in place, free if *p_len = 0
90 * @param p_str pointer to string to be stripped.
91 * @param p_len string length to analyse, also the place where the final size
94 * @note this will call free() on *p_str if it becomes empty.
97 lms_strstrip_and_free(char **p_str, unsigned int *p_len)
102 lms_strstrip(*p_str, p_len);
110 * lms_string_size version of lms_strstrip_and_free().
112 * @param *p pointer to lms_string_size to be stripped.
114 * @note this will call free() on lms_string_size->str if it becomes empty.
117 lms_string_size_strip_and_free(struct lms_string_size *p)
122 lms_strstrip(p->str, &p->len);
130 * lms_string_size version of strdup().
132 * @param dst where to return the duplicated value.
133 * @param src pointer to lms_string_size to be duplicated.
135 * @return 1 on success, 0 on failure.
138 lms_string_size_dup(struct lms_string_size *dst, const struct lms_string_size *src)
146 dst->str = malloc(src->len + 1);
153 memcpy(dst->str, src->str, dst->len);
154 dst->str[dst->len] = '\0';
159 * Similar to lms_string_size_dup(), but from a simple string.
161 * @param dst where to return the duplicated value.
162 * @param src pointer to string to be duplicated.
163 * @param size size to copy or -1 to auto-calculate.
165 * @return 1 on success, 0 on failure.
168 lms_string_size_strndup(struct lms_string_size *dst, const char *src, int size)
183 dst->str = malloc(size + 1);
190 memcpy(dst->str, src, dst->len);
191 dst->str[dst->len] = '\0';
196 * Find out which of the given extensions matches the given name.
198 * @param name string to analyse.
199 * @param name_len string length.
200 * @param exts array of extensions to be checked.
201 * @param exts_len number of items in array @p exts
203 * @return index in @p exts or -1 if it doesn't match none.
206 lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) {
208 unsigned int *exts_pos;
211 exts_pos = alloca(exts_len * sizeof(*exts_pos));
212 for (i = 0; i < exts_len; i++)
213 exts_pos[i] = exts[i].len;
215 for (s = name + name_len - 1; s >= name; s--) {
226 for (i = 0; i < exts_len; i++) {
227 if (exts_pos[i] > 0) {
230 ce = exts[i].str[exts_pos[i] - 1];
231 if (ce == c1 || ce == c2) {
232 if (exts_pos[i] == 1)