Improve code reusage: string strip and free functions.
authorbarbieri <barbieri@gmail.com>
Fri, 8 Feb 2008 16:05:46 +0000 (16:05 +0000)
committerbarbieri <barbieri@gmail.com>
Fri, 8 Feb 2008 16:05:46 +0000 (16:05 +0000)
Provides two utilities around lms_strstrip():
 * lms_strstrip_and_free()
 * lms_string_size_strip_and_free()

lightmediascanner/src/lib/lightmediascanner_utils.c
lightmediascanner/src/lib/lightmediascanner_utils.h
lightmediascanner/src/plugins/asf/asf.c
lightmediascanner/src/plugins/id3lib/id3lib.cpp
lightmediascanner/src/plugins/jpeg/jpeg.c
lightmediascanner/src/plugins/mp4/mp4.c
lightmediascanner/src/plugins/ogg/ogg.c
lightmediascanner/src/plugins/rm/rm.c

index 2223810..bb14144 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <lightmediascanner_utils.h>
 #include <ctype.h>
+#include <stdlib.h>
 #include <alloca.h>
 
 /**
@@ -81,6 +82,49 @@ lms_strstrip(char *str, unsigned int *p_len)
 }
 
 /**
+ * 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.
index dd52cf5..dc0c773 100644 (file)
@@ -56,6 +56,8 @@ extern "C" {
 
 
     API void lms_strstrip(char *str, unsigned int *p_len) GNUC_NON_NULL(1, 2);
+    API void lms_strstrip_and_free(char **p_str, unsigned int *p_len) GNUC_NON_NULL(1, 2);
+    API void lms_string_size_strip_and_free(struct lms_string_size *p) GNUC_NON_NULL(1);
     API int lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) GNUC_NON_NULL(1, 3);
 
 
index 462bcb9..ae828df 100644 (file)
@@ -362,18 +362,6 @@ _skip_header_extension(int fd)
     }
 }
 
-static void
-_strstrip(char **str, unsigned int *p_len)
-{
-    if (*str)
-        lms_strstrip(*str, p_len);
-
-    if (*p_len == 0 && *str) {
-        free(*str);
-        *str = NULL;
-    }
-}
-
 static void *
 _match(struct plugin *p, const char *path, int len, int base)
 {
@@ -460,10 +448,10 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in
             stream_type = STREAM_TYPE_VIDEO;
     }
 
-    _strstrip(&info.title.str, &info.title.len);
-    _strstrip(&info.artist.str, &info.artist.len);
-    _strstrip(&info.album.str, &info.album.len);
-    _strstrip(&info.genre.str, &info.genre.len);
+    lms_string_size_strip_and_free(&info.title);
+    lms_string_size_strip_and_free(&info.artist);
+    lms_string_size_strip_and_free(&info.album);
+    lms_string_size_strip_and_free(&info.genre);
 
     if (!info.title.str) {
         int ext_idx;
index d71bd1d..e7680e7 100644 (file)
@@ -61,14 +61,7 @@ _id3lib_get_string(const ID3_Frame *frame, ID3_FieldID field_id, struct lms_stri
   size++;
   s->str = (char *)malloc(size * sizeof(char));
   s->len = field->Get(s->str, size);
-  if (s->len > 0)
-    lms_strstrip(s->str, &s->len);
-
-  if (s->len < 1) {
-    free(s->str);
-    s->str = NULL;
-    s->len = 0;
-  }
+  lms_string_size_strip_and_free(s);
 
  done:
   field->SetEncoding(enc);
index ef65c7d..92c60b9 100644 (file)
@@ -108,11 +108,7 @@ _jpeg_com_process(int fd, int len, struct lms_string_size *comment)
         comment->str[len] = '\0';
     comment->len = len;
 
-    lms_strstrip(comment->str, &comment->len);
-    if (comment->len == 0) {
-        free(comment->str);
-        comment->str = NULL;
-    }
+    lms_string_size_strip_and_free(comment);
 
     return 0;
 }
@@ -313,11 +309,7 @@ _exif_text_encoding_get(int fd, unsigned int count, int offset, struct lms_strin
     s->str[count] = '\0';
     s->len = count;
 
-    lms_strstrip(s->str, &s->len);
-    if (s->len == 0) {
-        free(s->str);
-        s->str = NULL;
-    }
+    lms_string_size_strip_and_free(s);
 
     return 0;
 }
@@ -348,11 +340,7 @@ _exif_text_ascii_get(int fd, unsigned int count, int offset, struct lms_string_s
     s->str[count - 1] = '\0';
     s->len = count - 1;
 
-    lms_strstrip(s->str, &s->len);
-    if (s->len == 0) {
-        free(s->str);
-        s->str = NULL;
-    }
+    lms_string_size_strip_and_free(s);
 
     return 0;
 }
index 76c623f..53972a6 100644 (file)
@@ -56,18 +56,6 @@ static const struct lms_string_size _exts[] = {
     LMS_STATIC_STRING_SIZE(".mp4")
 };
 
-static void
-_strstrip(char **str, unsigned int *p_len)
-{
-    if (*str)
-        lms_strstrip(*str, p_len);
-
-    if (*p_len == 0 && *str) {
-        free(*str);
-        *str = NULL;
-    }
-}
-
 static void *
 _match(struct plugin *p, const char *path, int len, int base)
 {
@@ -114,10 +102,10 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in
     if (num_tracks > 0)
         stream_type = STREAM_TYPE_VIDEO;
 
-    _strstrip(&info.title.str, &info.title.len);
-    _strstrip(&info.artist.str, &info.artist.len);
-    _strstrip(&info.album.str, &info.album.len);
-    _strstrip(&info.genre.str, &info.genre.len);
+    lms_string_size_strip_and_free(&info.title);
+    lms_string_size_strip_and_free(&info.artist);
+    lms_string_size_strip_and_free(&info.album);
+    lms_string_size_strip_and_free(&info.genre);
 
     if (!info.title.str) {
         int ext_idx;
index 3425370..0eb0120 100644 (file)
@@ -166,6 +166,7 @@ _parse_ogg (const char *filename, struct lms_audio_info *info)
         info->title.len = size;
         info->title.str = malloc(size * sizeof(char));
         memcpy(info->title.str, tag, size);
+        lms_string_size_strip_and_free(&info->title);
     }
 
     tag = vorbis_comment_query(&vc, "ARTIST", 0);
@@ -173,6 +174,7 @@ _parse_ogg (const char *filename, struct lms_audio_info *info)
         info->artist.len = size;
         info->artist.str = malloc(size * sizeof(char));
         memcpy(info->artist.str, tag, size);
+        lms_string_size_strip_and_free(&info->artist);
     }
 
     tag = vorbis_comment_query(&vc, "ALBUM", 0);
@@ -180,6 +182,7 @@ _parse_ogg (const char *filename, struct lms_audio_info *info)
         info->album.len = size;
         info->album.str = malloc(size * sizeof(char));
         memcpy(info->album.str, tag, size);
+        lms_string_size_strip_and_free(&info->album);
     }
 
     tag = vorbis_comment_query(&vc, "TRACKNUMBER", 0);
@@ -192,6 +195,7 @@ _parse_ogg (const char *filename, struct lms_audio_info *info)
         info->genre.len = size;
         info->genre.str = malloc(size * sizeof(char));
         memcpy(info->genre.str, tag, size);
+        lms_string_size_strip_and_free(&info->genre);
     }
 
     fclose(file);
index 1390a2b..871baf8 100644 (file)
@@ -195,18 +195,6 @@ _parse_cont_header(int fd, struct rm_info *info)
     _read_string(fd, NULL, NULL); /* comment */
 }
 
-static void
-_strstrip(char **str, unsigned int *p_len)
-{
-    if (*str)
-        lms_strstrip(*str, p_len);
-
-    if (*p_len == 0 && *str) {
-        free(*str);
-        *str = NULL;
-    }
-}
-
 static void *
 _match(struct plugin *p, const char *path, int len, int base)
 {
@@ -274,8 +262,8 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in
             stream_type = STREAM_TYPE_VIDEO;
     }
 
-    _strstrip(&info.title.str, &info.title.len);
-    _strstrip(&info.artist.str, &info.artist.len);
+    lms_string_size_strip_and_free(&info.title);
+    lms_string_size_strip_and_free(&info.artist);
 
     if (!info.title.str) {
         int ext_idx;