[easytag] Use GIO for reading WavPack headers



commit 71dc1da626f0d32d4d8f0f983d12c11d86900114
Author: David King <amigadave amigadave com>
Date:   Tue Dec 23 18:20:15 2014 +0000

    Use GIO for reading WavPack headers
    
    Split out the WavpackStreamReader functions to a separate file, so that
    they can be shared between the header and tag functions.

 Makefile.am                |    2 +
 src/tags/wavpack_header.c  |   38 +++++++++--
 src/tags/wavpack_private.c |  161 ++++++++++++++++++++++++++++++++++++++++++++
 src/tags/wavpack_private.h |   50 ++++++++++++++
 src/tags/wavpack_tag.c     |  147 +---------------------------------------
 5 files changed, 246 insertions(+), 152 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 9d3ef13..643dfe9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -86,6 +86,7 @@ easytag_SOURCES = \
        src/tags/opus_tag.c \
        src/tags/vcedit.c \
        src/tags/wavpack_header.c \
+       src/tags/wavpack_private.c \
        src/tags/wavpack_tag.c \
        src/win32/win32dep.c
 
@@ -139,6 +140,7 @@ easytag_headers = \
        src/tags/opus_tag.h \
        src/tags/vcedit.h \
        src/tags/wavpack_header.h \
+       src/tags/wavpack_private.h \
        src/tags/wavpack_tag.h \
        src/win32/win32dep.h
 
diff --git a/src/tags/wavpack_header.c b/src/tags/wavpack_header.c
index ecd48dc..801b40e 100644
--- a/src/tags/wavpack_header.c
+++ b/src/tags/wavpack_header.c
@@ -28,6 +28,7 @@
 #include "et_core.h"
 #include "misc.h"
 #include "wavpack_header.h"
+#include "wavpack_private.h"
 
 
 gboolean
@@ -35,21 +36,44 @@ et_wavpack_header_read_file_info (GFile *file,
                                   ET_File_Info *ETFileInfo,
                                   GError **error)
 {
-    gchar *filename;
+    WavpackStreamReader reader = { wavpack_read_bytes, wavpack_get_pos,
+                                   wavpack_set_pos_abs, wavpack_set_pos_rel,
+                                   wavpack_push_back_byte, wavpack_get_length,
+                                   wavpack_can_seek, NULL /* No writing. */ };
+    EtWavpackState state;
     WavpackContext *wpc;
     gchar message[80];
 
     g_return_val_if_fail (file != NULL && ETFileInfo != NULL, FALSE);
     g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-    /* TODO: Use WavpackOpenFileInputEx() instead. */
-    filename = g_file_get_path (file);
-    wpc = WavpackOpenFileInput (filename, message, 0, 0);
-    g_free (filename);
+    state.error = NULL;
+    state.istream = g_file_read (file, NULL, &state.error);
+
+    if (!state.istream)
+    {
+        g_propagate_error (error, state.error);
+        return FALSE;
+    }
+
+    state.seekable = G_SEEKABLE (state.istream);
+
+    /* NULL for the WavPack correction file. */
+    wpc = WavpackOpenFileInputEx (&reader, &state, NULL, message, 0, 0);
 
     if (wpc == NULL)
     {
-        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", message);
+        if (state.error)
+        {
+            g_propagate_error (error, state.error);
+        }
+        else
+        {
+            g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s",
+                         message);
+        }
+
+        g_object_unref (state.istream);
         return FALSE;
     }
 
@@ -64,6 +88,8 @@ et_wavpack_header_read_file_info (GFile *file,
 
     WavpackCloseFile(wpc);
 
+    g_object_unref (state.istream);
+
     return TRUE;
 }
 
diff --git a/src/tags/wavpack_private.c b/src/tags/wavpack_private.c
new file mode 100644
index 0000000..c969baa
--- /dev/null
+++ b/src/tags/wavpack_private.c
@@ -0,0 +1,161 @@
+/* EasyTAG - tag editor for audio files
+ * Copyright (C) 2014 David King <amigadave amigadave com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU 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 General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "wavpack_private.h"
+
+/* For EOF. */
+#include <stdio.h>
+
+int32_t
+wavpack_read_bytes (void *id,
+                    void *data,
+                    int32_t bcount)
+{
+    EtWavpackState *state;
+    gssize bytes_written;
+
+    state = (EtWavpackState *)id;
+
+    bytes_written = g_input_stream_read (G_INPUT_STREAM (state->istream), data,
+                                         bcount, NULL, &state->error);
+
+    if (bytes_written == -1)
+    {
+        return 0;
+    }
+
+    return bytes_written;
+}
+
+uint32_t
+wavpack_get_pos (void *id)
+{
+    EtWavpackState *state;
+
+    state = (EtWavpackState *)id;
+
+    return g_seekable_tell (state->seekable);
+}
+
+int
+wavpack_set_pos_abs (void *id,
+                     uint32_t pos)
+{
+    EtWavpackState *state;
+
+    state = (EtWavpackState *)id;
+
+    if (!g_seekable_seek (state->seekable, pos, G_SEEK_SET, NULL,
+                          &state->error))
+    {
+        return -1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+int
+wavpack_set_pos_rel (void *id,
+                     int32_t delta,
+                     int mode)
+{
+    EtWavpackState *state;
+    GSeekType seek_type;
+
+    state = (EtWavpackState *)id;
+
+    switch (mode)
+    {
+        case SEEK_SET:
+            seek_type = G_SEEK_SET;
+            break;
+        case SEEK_CUR:
+            seek_type = G_SEEK_CUR;
+            break;
+        case SEEK_END:
+            seek_type = G_SEEK_END;
+            break;
+        default:
+            g_assert_not_reached ();
+            break;
+    }
+
+    if (!g_seekable_seek (state->seekable, delta, seek_type, NULL,
+                          &state->error))
+    {
+        return -1;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+int
+wavpack_push_back_byte (void *id,
+                        int c)
+{
+    EtWavpackState *state;
+
+    state = (EtWavpackState *)id;
+
+    if (!g_seekable_seek (state->seekable, -1, G_SEEK_CUR, NULL,
+                          &state->error))
+    {
+        return EOF;
+    }
+
+    /* TODO: Check if c is present in the input stream. */
+    return c;
+}
+
+uint32_t
+wavpack_get_length (void *id)
+{
+    EtWavpackState *state;
+    GFileInfo *info;
+    goffset size;
+
+    state = (EtWavpackState *)id;
+
+    info = g_file_input_stream_query_info (state->istream,
+                                           G_FILE_ATTRIBUTE_STANDARD_SIZE,
+                                           NULL, &state->error);
+
+    if (!info)
+    {
+        return 0;
+    }
+
+    size = g_file_info_get_size (info);
+    g_object_unref (info);
+
+    return size;
+}
+
+int
+wavpack_can_seek (void *id)
+{
+    EtWavpackState *state;
+
+    state = (EtWavpackState *)id;
+
+    return g_seekable_can_seek (state->seekable);
+}
diff --git a/src/tags/wavpack_private.h b/src/tags/wavpack_private.h
new file mode 100644
index 0000000..1f9815d
--- /dev/null
+++ b/src/tags/wavpack_private.h
@@ -0,0 +1,50 @@
+/* EasyTAG - tag editor for audio files
+ * Copyright (C) 2014 David King <amigadave amigadave com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU 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 General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef ET_WAVPACK_PRIVATE_H_
+#define ET_WAVPACK_PRIVATE_H_
+
+#include "config.h"
+
+#ifdef ENABLE_WAVPACK
+
+#include <gio/gio.h>
+#include <wavpack/wavpack.h>
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+    GFileInputStream *istream;
+    GSeekable *seekable;
+    GError *error;
+} EtWavpackState;
+
+int32_t wavpack_read_bytes (void *id, void *data, int32_t bcount);
+uint32_t wavpack_get_pos (void *id);
+int wavpack_set_pos_abs (void *id, uint32_t pos);
+int wavpack_set_pos_rel (void *id, int32_t delta, int mode);
+int wavpack_push_back_byte (void *id, int c);
+uint32_t wavpack_get_length (void *id);
+int wavpack_can_seek (void *id);
+
+G_END_DECLS
+
+#endif /* ENABLE_WAVPACK */
+
+#endif /* ET_WAVPACK_PRIVATE_H_ */
diff --git a/src/tags/wavpack_tag.c b/src/tags/wavpack_tag.c
index a0e0870..f77cf41 100644
--- a/src/tags/wavpack_tag.c
+++ b/src/tags/wavpack_tag.c
@@ -31,6 +31,7 @@
 #include "picture.h"
 #include "charset.h"
 #include "misc.h"
+#include "wavpack_private.h"
 #include "wavpack_tag.h"
 
 #define MAXLEN 1024
@@ -56,152 +57,6 @@
  *     Cover Art (Back)
  */
 
-typedef struct
-{
-    GFileInputStream *istream;
-    GSeekable *seekable;
-    GError *error;
-} EtWavpackState;
-
-static int32_t
-wavpack_read_bytes (void *id,
-                    void *data,
-                    int32_t bcount)
-{
-    EtWavpackState *state;
-    gssize bytes_written;
-
-    state = (EtWavpackState *)id;
-
-    bytes_written = g_input_stream_read (G_INPUT_STREAM (state->istream), data,
-                                         bcount, NULL, &state->error);
-
-    if (bytes_written == -1)
-    {
-        return 0;
-    }
-
-    return bytes_written;
-}
-
-static uint32_t
-wavpack_get_pos (void *id)
-{
-    EtWavpackState *state;
-
-    state = (EtWavpackState *)id;
-
-    return g_seekable_tell (state->seekable);
-}
-
-static int
-wavpack_set_pos_abs (void *id,
-                     uint32_t pos)
-{
-    EtWavpackState *state;
-
-    state = (EtWavpackState *)id;
-
-    if (!g_seekable_seek (state->seekable, pos, G_SEEK_SET, NULL,
-                          &state->error))
-    {
-        return -1;
-    }
-    else
-    {
-        return 0;
-    }
-}
-
-static int
-wavpack_set_pos_rel (void *id,
-                     int32_t delta,
-                     int mode)
-{
-    EtWavpackState *state;
-    GSeekType seek_type;
-
-    state = (EtWavpackState *)id;
-
-    switch (mode)
-    {
-        case SEEK_SET:
-            seek_type = G_SEEK_SET;
-            break;
-        case SEEK_CUR:
-            seek_type = G_SEEK_CUR;
-            break;
-        case SEEK_END:
-            seek_type = G_SEEK_END;
-            break;
-        default:
-            g_assert_not_reached ();
-            break;
-    }
-
-    if (!g_seekable_seek (state->seekable, delta, seek_type, NULL,
-                          &state->error))
-    {
-        return -1;
-    }
-    else
-    {
-        return 0;
-    }
-}
-
-static int
-wavpack_push_back_byte (void *id,
-                        int c)
-{
-    EtWavpackState *state;
-
-    state = (EtWavpackState *)id;
-
-    if (!g_seekable_seek (state->seekable, -1, G_SEEK_CUR, NULL,
-                          &state->error))
-    {
-        return EOF;
-    }
-
-    /* TODO: Check if c is present in the input stream. */
-    return c;
-}
-
-static uint32_t
-wavpack_get_length (void *id)
-{
-    EtWavpackState *state;
-    GFileInfo *info;
-    goffset size;
-
-    state = (EtWavpackState *)id;
-
-    info = g_file_input_stream_query_info (state->istream,
-                                           G_FILE_ATTRIBUTE_STANDARD_SIZE,
-                                           NULL, &state->error);
-
-    if (!info)
-    {
-        return 0;
-    }
-
-    size = g_file_info_get_size (info);
-    g_object_unref (info);
-
-    return size;
-}
-
-static int
-wavpack_can_seek (void *id)
-{
-    EtWavpackState *state;
-
-    state = (EtWavpackState *)id;
-
-    return g_seekable_can_seek (state->seekable);
-}
-
 /*
  * Read tag data from a Wavpack file.
  */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]