Improve code reusage: string strip and free functions.
[lms] / lightmediascanner / src / plugins / rm / rm.c
index a39de4c..871baf8 100644 (file)
@@ -28,6 +28,7 @@
 #include "config.h"
 #endif
 
+#define _XOPEN_SOURCE 600
 #include <lightmediascanner_plugin.h>
 #include <lightmediascanner_db.h>
 #include <sys/types.h>
 #include <string.h>
 #include <unistd.h>
 
+#define BE_4BYTE(a) ((((unsigned char*)a)[0] << 24) |                   \
+                     (((unsigned char*)a)[1] << 16) |                   \
+                     (((unsigned char*)a)[2] << 8) |                    \
+                     ((unsigned char*)a)[3])
+#define BE_2BYTE(a) ((((unsigned char*)a)[0] << 8) | ((unsigned char*)a)[1])
+
 enum StreamTypes {
     STREAM_TYPE_UNKNOWN = 0,
     STREAM_TYPE_AUDIO,
@@ -93,7 +100,7 @@ _parse_file_header(int fd, struct rm_file_header *file_header)
     }
 
     /* convert to host byte order */
-    file_header->size = ntohl(file_header->size);
+    file_header->size = BE_4BYTE(&file_header->size);
 
 #if 0
     fprintf(stderr, "file_header type=%.*s\n", 4, file_header->type);
@@ -120,7 +127,7 @@ _read_header_type_and_size(int fd, char *type, uint32_t *size)
     if (read(fd, size, 4) != 4)
         return -1;
 
-    *size = ntohl(*size);
+    *size = BE_4BYTE(size);
 
 #if 0
     fprintf(stderr, "header type=%.*s\n", 4, type);
@@ -131,7 +138,7 @@ _read_header_type_and_size(int fd, char *type, uint32_t *size)
 }
 
 static int
-_read_string(int fd, char **out, int *out_len)
+_read_string(int fd, char **out, unsigned int *out_len)
 {
     char *s;
     uint16_t len;
@@ -139,27 +146,25 @@ _read_string(int fd, char **out, int *out_len)
     if (read(fd, &len, 2) == -1)
         return -1;
 
-    len = ntohs(len);
+    len = BE_2BYTE(&len);
 
     if (out) {
         if (len > 0) {
-            s = malloc(sizeof(char) * len + 1);
+            s = malloc(sizeof(char) * (len + 1));
             if (read(fd, s, len) == -1) {
                 free(s);
                 return -1;
             }
             s[len] = '\0';
             *out = s;
-        }
-        else {
+        } else
             *out = NULL;
-        }
 
         *out_len = len;
-    }
-    else {
+    } else
         lseek(fd, len, SEEK_CUR);
-    }
+
+    return 0;
 }
 
 /*
@@ -176,13 +181,9 @@ _read_string(int fd, char **out, int *out_len)
  * word    Comment string length
  * byte[]  Comment string
  */
-static int
+static void
 _parse_cont_header(int fd, struct rm_info *info)
 {
-    uint16_t version;
-    uint16_t title_len;
-    char *title;
-
     /* Ps.: type and size were already read */
 
     /* ignore version */
@@ -194,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)
 {
@@ -273,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;
@@ -283,8 +272,11 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in
         info.title.str = malloc((info.title.len + 1) * sizeof(char));
         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
         info.title.str[info.title.len] = '\0';
-        lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
     }
+    lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
+
+    if (info.artist.str)
+        lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
 
 #if 0
     fprintf(stderr, "file %s info\n", finfo->path);