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>
32 * Strips string, in place.
34 * @param str string to be stripped.
35 * @param p_len string length to analyse, also the place where the final size
39 lms_strstrip(char *str, unsigned int *p_len)
55 for (i = len - 1; i >= 0; i--) {
56 if (isspace(*p) || *p == '\0') {
69 for (i = 0; i < len; i++) {
85 for (; len >= 0; len--, str++, p++)
90 * If string exists, strips it, in place, free if *p_len = 0
92 * @param p_str pointer to string to be stripped.
93 * @param p_len string length to analyse, also the place where the final size
96 * @note this will call free() on *p_str if it becomes empty.
99 lms_strstrip_and_free(char **p_str, unsigned int *p_len)
104 lms_strstrip(*p_str, p_len);
112 * lms_string_size version of lms_strstrip_and_free().
114 * @param *p pointer to lms_string_size to be stripped.
116 * @note this will call free() on lms_string_size->str if it becomes empty.
119 lms_string_size_strip_and_free(struct lms_string_size *p)
124 lms_strstrip(p->str, &p->len);
132 * lms_string_size version of strdup().
134 * @param dst where to return the duplicated value.
135 * @param src pointer to lms_string_size to be duplicated.
137 * @return 1 on success, 0 on failure.
140 lms_string_size_dup(struct lms_string_size *dst, const struct lms_string_size *src)
148 dst->str = malloc(src->len + 1);
155 memcpy(dst->str, src->str, dst->len);
156 dst->str[dst->len] = '\0';
161 * Similar to lms_string_size_dup(), but from a simple string.
163 * @param dst where to return the duplicated value.
164 * @param src pointer to string to be duplicated.
165 * @param size size to copy or -1 to auto-calculate.
167 * @return 1 on success, 0 on failure.
170 lms_string_size_strndup(struct lms_string_size *dst, const char *src, int size)
185 dst->str = malloc(size + 1);
192 memcpy(dst->str, src, dst->len);
193 dst->str[dst->len] = '\0';
197 /* Euclidean algorithm
198 * http://en.wikipedia.org/wiki/Euclidean_algorithm */
200 gcd(unsigned int a, unsigned int b)
214 * Guess aspect ratio from known ratios or Greatest Common Divisor.
216 * @param ret where to store the newly allocated string with ratio.
217 * @param width frame width to guess aspect ratio.
218 * @param height frame height to guess aspect ratio.
219 * @return 1 on success and @c ret->str must be @c free()d, 0 on failure.
222 lms_aspect_ratio_guess(struct lms_string_size *ret, int width, int height)
226 struct lms_string_size str;
227 } *itr, known_ratios[] = {
228 {16.0 / 9.0, LMS_STATIC_STRING_SIZE("16:9")},
229 {4.0 / 3.0, LMS_STATIC_STRING_SIZE("4:3")},
230 {3.0 / 2.0, LMS_STATIC_STRING_SIZE("3:2")},
231 {5.0 / 3.0, LMS_STATIC_STRING_SIZE("5:3")},
232 {8.0 / 5.0, LMS_STATIC_STRING_SIZE("8:5")},
233 {1.85, LMS_STATIC_STRING_SIZE("1.85:1")},
234 {1.4142, LMS_STATIC_STRING_SIZE("1.41:1")},
235 {2.39, LMS_STATIC_STRING_SIZE("2.39:1")},
236 {16.18 / 10.0, LMS_STATIC_STRING_SIZE("16.18:10")},
240 unsigned num, den, f;
242 if (width <= 0 || height <= 0) {
248 ratio = (double)width / (double)height;
249 for (itr = known_ratios; itr->ratio > 0.0; itr++) {
250 if (fabs(ratio - itr->ratio) <= 0.01)
251 return lms_string_size_dup(ret, &itr->str);
254 f = gcd(width, height);
258 ret->len = asprintf(&ret->str, "%u:%u", num, den);
259 if (ret->len == (unsigned int)-1) {
270 * Find out which of the given extensions matches the given name.
272 * @param name string to analyse.
273 * @param name_len string length.
274 * @param exts array of extensions to be checked.
275 * @param exts_len number of items in array @p exts
277 * @return index in @p exts or -1 if it doesn't match none.
280 lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) {
282 unsigned int *exts_pos;
285 exts_pos = alloca(exts_len * sizeof(*exts_pos));
286 for (i = 0; i < exts_len; i++)
287 exts_pos[i] = exts[i].len;
289 for (s = name + name_len - 1; s >= name; s--) {
300 for (i = 0; i < exts_len; i++) {
301 if (exts_pos[i] > 0) {
304 ce = exts[i].str[exts_pos[i] - 1];
305 if (ce == c1 || ce == c2) {
306 if (exts_pos[i] == 1)
322 * Extract name from a path given its path string, length, base and extension.
324 * @param name where to store the result.
325 * @param path the input path to base the name on.
326 * @param pathlen the path size in bytes.
327 * @param baselen where (offset int bytes) in path starts the filename (base name).
328 * @param extlen the extension length in bytes.
329 * @param cs_conv charset conversion to use, if none use @c NULL.
330 * @return 1 on success, 0 on failure.
333 lms_name_from_path(struct lms_string_size *name, const char *path, unsigned int pathlen, unsigned int baselen, unsigned int extlen, struct lms_charset_conv *cs_conv)
335 int size = pathlen - baselen - extlen;
337 name->str = malloc(size + 1);
344 memcpy(name->str, path + baselen, size);
345 name->str[size] = '\0';
348 lms_charset_conv(cs_conv, &name->str, &name->len);