X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=lightmediascanner%2Fsrc%2Flib%2Flightmediascanner_utils.c;h=c33dca4a68660eda3779fb49f743c4b6a1e274a4;hb=4ccbba7a7e013abf9d2cf42aab2786f02e66c92d;hp=0ee509d9e8b93d2e537d8010b034468707e65a32;hpb=279654dc69aedd700e3a66ffba5557d4f8223b6b;p=lms diff --git a/lightmediascanner/src/lib/lightmediascanner_utils.c b/lightmediascanner/src/lib/lightmediascanner_utils.c index 0ee509d..c33dca4 100644 --- a/lightmediascanner/src/lib/lightmediascanner_utils.c +++ b/lightmediascanner/src/lib/lightmediascanner_utils.c @@ -1,5 +1,26 @@ +/** + * 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 + */ + #include #include +#include #include /** @@ -17,12 +38,17 @@ lms_strstrip(char *str, unsigned int *p_len) len = *p_len; - if (len < 2) /* just '\0'? */ + if (len == 0) + return; + + if (*str == '\0') { + *p_len = 0; return; + } p = str + len - 1; for (i = len - 1; i >= 0; i--) { - if (isspace(*p)) { + if (isspace(*p) || *p == '\0') { *p = '\0'; len--; p--; @@ -51,11 +77,54 @@ lms_strstrip(char *str, unsigned int *p_len) *p_len = len; if (str < p) - for (; len > 0; len--, str++, p++) + for (; len >= 0; len--, str++, p++) *str = *p; } /** + * If string exists, strips it, in place, free if *p_len = 0 + * + * @param p_str pointer to string to be stripped. + * @param p_len string length to analyse, also the place where the final size + * is stored. + * + * @note this will call free() on *p_str if it becomes empty. + */ +void +lms_strstrip_and_free(char **p_str, unsigned int *p_len) +{ + if (!*p_str) + return; + + lms_strstrip(*p_str, p_len); + if (*p_len == 0) { + free(*p_str); + *p_str = NULL; + } +} + +/** + * lms_string_size version of lms_strstrip_and_free(). + * + * @param *p pointer to lms_string_size to be stripped. + * + * @note this will call free() on lms_string_size->str if it becomes empty. + */ +void +lms_string_size_strip_and_free(struct lms_string_size *p) +{ + if (!p->str) + return; + + lms_strstrip(p->str, &p->len); + if (p->len == 0) { + free(p->str); + p->str = NULL; + } +} + + +/** * Find out which of the given extensions matches the given name. * * @param name string to analyse.