utils refactor.
[platform/upstream/lightmediascanner.git] / src / lib / lightmediascanner_utils.c
1 /**
2  * Copyright (C) 2008-2011 by ProFUSION embedded systems
3  * Copyright (C) 2007 by INdT
4  *
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.
9  *
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.
14  *
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
18  * 02110-1301 USA
19  *
20  * @author Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
21  */
22
23 #include <lightmediascanner_utils.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <alloca.h>
29 #include <math.h>
30
31 /**
32  * Strips string, in place.
33  *
34  * @param str string to be stripped.
35  * @param p_len string length to analyse, also the place where the final size
36  *        is stored.
37  */
38 void
39 lms_strstrip(char *str, unsigned int *p_len)
40 {
41     int i, len;
42     char *p;
43
44     len = *p_len;
45
46     if (len == 0)
47         return;
48
49     if (*str == '\0') {
50         *p_len = 0;
51         return;
52     }
53
54     p = str + len - 1;
55     for (i = len - 1; i >= 0; i--) {
56         if (isspace(*p) || *p == '\0') {
57             *p = '\0';
58             len--;
59             p--;
60         } else
61             break;
62     }
63     if (len == 0) {
64         *p_len = 0;
65         return;
66     }
67
68     p = str;
69     for (i = 0; i < len; i++) {
70         if (isspace(*p))
71             p++;
72         else
73             break;
74     }
75     len -= i;
76     if (len == 0) {
77         *str = '\0';
78         *p_len = 0;
79         return;
80     }
81
82     *p_len = len;
83
84     if (str < p)
85         for (; len >= 0; len--, str++, p++)
86             *str = *p;
87 }
88
89 /**
90  * If string exists, strips it, in place, free if *p_len = 0
91  *
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
94  *        is stored.
95  *
96  * @note this will call free() on *p_str if it becomes empty.
97  */
98 void
99 lms_strstrip_and_free(char **p_str, unsigned int *p_len)
100 {
101     if (!*p_str)
102         return;
103
104     lms_strstrip(*p_str, p_len);
105     if (*p_len == 0) {
106         free(*p_str);
107         *p_str = NULL;
108     }
109 }
110
111 /**
112  * lms_string_size version of lms_strstrip_and_free().
113  *
114  * @param *p pointer to lms_string_size to be stripped.
115  *
116  * @note this will call free() on lms_string_size->str if it becomes empty.
117  */
118 void
119 lms_string_size_strip_and_free(struct lms_string_size *p)
120 {
121     if (!p->str)
122         return;
123
124     lms_strstrip(p->str, &p->len);
125     if (p->len == 0) {
126         free(p->str);
127         p->str = NULL;
128     }
129 }
130
131 /**
132  * lms_string_size version of strdup().
133  *
134  * @param dst where to return the duplicated value.
135  * @param src pointer to lms_string_size to be duplicated.
136  *
137  * @return 1 on success, 0 on failure.
138  */
139 int
140 lms_string_size_dup(struct lms_string_size *dst, const struct lms_string_size *src)
141 {
142     if (src->len == 0) {
143         dst->str = NULL;
144         dst->len = 0;
145         return 1;
146     }
147
148     dst->str = malloc(src->len + 1);
149     if (!dst->str) {
150         dst->len = 0;
151         return 0;
152     }
153
154     dst->len = src->len;
155     memcpy(dst->str, src->str, dst->len);
156     dst->str[dst->len] = '\0';
157     return 1;
158 }
159
160 /**
161  * Similar to lms_string_size_dup(), but from a simple string.
162  *
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.
166  *
167  * @return 1 on success, 0 on failure.
168  */
169 int
170 lms_string_size_strndup(struct lms_string_size *dst, const char *src, int size)
171 {
172     if (size < 0) {
173         if (!src)
174             size = 0;
175         else
176             size = strlen(src);
177     }
178
179     if (size == 0) {
180         dst->str = NULL;
181         dst->len = 0;
182         return 1;
183     }
184
185     dst->str = malloc(size + 1);
186     if (!dst->str) {
187         dst->len = 0;
188         return 0;
189     }
190
191     dst->len = size;
192     memcpy(dst->str, src, dst->len);
193     dst->str[dst->len] = '\0';
194     return 1;
195 }
196
197 /* Euclidean algorithm
198  * http://en.wikipedia.org/wiki/Euclidean_algorithm */
199 static unsigned int
200 gcd(unsigned int a, unsigned int b)
201 {
202     unsigned int t;
203
204     while (b) {
205         t = b;
206         b = a % t;
207         a = t;
208     }
209
210     return a;
211 }
212
213 /**
214  * Guess aspect ratio from known ratios or Greatest Common Divisor.
215  *
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.
220  */
221 int
222 lms_aspect_ratio_guess(struct lms_string_size *ret, int width, int height)
223 {
224     static struct {
225         double ratio;
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")},
237         {-1.0, {NULL, 0}}
238     };
239     double ratio;
240     unsigned num, den, f;
241
242     if (width <= 0 || height <= 0) {
243         ret->len = 0;
244         ret->str = NULL;
245         return 0;
246     }
247
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);
252     }
253
254     f = gcd(width, height);
255
256     num = width / f;
257     den = height / f;
258     ret->len = asprintf(&ret->str, "%u:%u", num, den);
259     if (ret->len == (unsigned int)-1) {
260         ret->len = 0;
261         ret->str = NULL;
262         return 0;
263     }
264
265     return 1;
266 }
267
268
269 /**
270  * Find out which of the given extensions matches the given name.
271  *
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
276  *
277  * @return index in @p exts or -1 if it doesn't match none.
278  */
279 int
280 lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) {
281     unsigned int i;
282     unsigned int *exts_pos;
283     const char *s;
284
285     exts_pos = alloca(exts_len * sizeof(*exts_pos));
286     for (i = 0; i < exts_len; i++)
287         exts_pos[i] = exts[i].len;
288
289     for (s = name + name_len - 1; s >= name; s--) {
290         int match;
291         char c1, c2;
292
293         c1 = *s;
294         if (c1 >= 'a')
295             c2 = c1;
296         else
297             c2 = 'a' + c1 - 'A';
298
299         match = 0;
300         for (i = 0; i < exts_len; i++) {
301             if (exts_pos[i] > 0) {
302                 char ce;
303
304                 ce = exts[i].str[exts_pos[i] - 1];
305                 if (ce == c1 || ce == c2) {
306                     if (exts_pos[i] == 1)
307                         return i;
308                     exts_pos[i]--;
309                     match = 1;
310                 } else
311                     exts_pos[i] = 0;
312             }
313         }
314         if (!match)
315             return -1;
316     }
317
318     return -1;
319 }
320
321 /**
322  * Extract name from a path given its path string, length, base and extension.
323  *
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.
331  */
332 int
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)
334 {
335     int size = pathlen - baselen - extlen;
336
337     name->str = malloc(size + 1);
338     if (!name->str) {
339         name->len = 0;
340         return 0;
341     }
342
343     name->len = size;
344     memcpy(name->str, path + baselen, size);
345     name->str[size] = '\0';
346
347     if (cs_conv)
348         lms_charset_conv(cs_conv, &name->str, &name->len);
349
350     return 1;
351 }