29d76cbd3c5e31094a25c2f8ac66f8557ac60d7a
[lms] / lightmediascanner / src / lib / lightmediascanner_utils.c
1 /**
2  * Copyright (C) 2007 by INdT
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
19  */
20
21 #include <lightmediascanner_utils.h>
22 #include <ctype.h>
23 #include <alloca.h>
24
25 /**
26  * Strips string, in place.
27  *
28  * @param str string to be stripped.
29  * @param p_len string length to analyse, also the place where the final size
30  *        is stored.
31  */
32 void
33 lms_strstrip(char *str, unsigned int *p_len)
34 {
35     int i, len;
36     char *p;
37
38     len = *p_len;
39
40     if (len < 2) /* just '\0'? */
41         return;
42
43     p = str + len - 1;
44     for (i = len - 1; i >= 0; i--) {
45         if (isspace(*p)) {
46             *p = '\0';
47             len--;
48             p--;
49         } else
50             break;
51     }
52     if (len == 0) {
53         *p_len = 0;
54         return;
55     }
56
57     p = str;
58     for (i = 0; i < len; i++) {
59         if (isspace(*p))
60             p++;
61         else
62             break;
63     }
64     len -= i;
65     if (len == 0) {
66         *str = '\0';
67         *p_len = 0;
68         return;
69     }
70
71     *p_len = len;
72
73     if (str < p)
74         for (; len > 0; len--, str++, p++)
75             *str = *p;
76 }
77
78 /**
79  * Find out which of the given extensions matches the given name.
80  *
81  * @param name string to analyse.
82  * @param name_len string length.
83  * @param exts array of extensions to be checked.
84  * @param exts_len number of items in array @p exts
85  *
86  * @return index in @p exts or -1 if it doesn't match none.
87  */
88 int
89 lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) {
90     int i;
91     unsigned int *exts_pos;
92     const char *s;
93
94     exts_pos = alloca(exts_len * sizeof(*exts_pos));
95     for (i = 0; i < exts_len; i++)
96         exts_pos[i] = exts[i].len;
97
98     for (s = name + name_len - 1; s >= name; s--) {
99         int i, match;
100         char c1, c2;
101
102         c1 = *s;
103         if (c1 >= 'a')
104             c2 = c1;
105         else
106             c2 = 'a' + c1 - 'A';
107
108         match = 0;
109         for (i = 0; i < exts_len; i++) {
110             if (exts_pos[i] > 0) {
111                 char ce;
112
113                 ce = exts[i].str[exts_pos[i] - 1];
114                 if (ce == c1 || ce == c2) {
115                     if (exts_pos[i] == 1)
116                         return i;
117                     exts_pos[i]--;
118                     match = 1;
119                 } else
120                     exts_pos[i] = 0;
121             }
122         }
123         if (!match)
124             return -1;
125     }
126
127     return -1;
128 }