[easytag] Simplify the shared FLAC reading code



commit d41c9b8b9b4d0baa2e697fd0592e50a433998b14
Author: David King <amigadave amigadave com>
Date:   Tue Dec 23 20:35:46 2014 +0000

    Simplify the shared FLAC reading code
    
    Make EtFlacWriteState extend EtFlacReadState, and use a single callback
    function for each of the read, seek, tell and EOF callbacks as a result.

 src/tags/flac_header.c  |    8 ++--
 src/tags/flac_private.c |  139 +++++++++++------------------------------------
 src/tags/flac_private.h |   28 ++++++----
 src/tags/flac_tag.c     |   18 +++---
 4 files changed, 60 insertions(+), 133 deletions(-)
---
diff --git a/src/tags/flac_header.c b/src/tags/flac_header.c
index ff08068..ee60fb7 100644
--- a/src/tags/flac_header.c
+++ b/src/tags/flac_header.c
@@ -39,11 +39,10 @@ et_flac_header_read_file_info (GFile *file,
     FLAC__Metadata_Chain *chain;
     EtFlacReadState state;
     GFileInputStream *istream;
-    FLAC__IOCallbacks callbacks = { et_flac_read_read_func,
+    FLAC__IOCallbacks callbacks = { et_flac_read_func,
                                     NULL, /* Do not set a write callback. */
-                                    et_flac_read_seek_func,
-                                    et_flac_read_tell_func,
-                                    et_flac_read_eof_func,
+                                    et_flac_seek_func, et_flac_tell_func,
+                                    et_flac_eof_func,
                                     et_flac_read_close_func };
     FLAC__Metadata_Iterator *iter;
     gsize metadata_len;
@@ -72,6 +71,7 @@ et_flac_header_read_file_info (GFile *file,
     state.eof = FALSE;
     state.error = NULL;
     state.istream = istream;
+    state.seekable = G_SEEKABLE (istream);
 
     if (!FLAC__metadata_chain_read_with_callbacks (chain, &state, callbacks))
     {
diff --git a/src/tags/flac_private.c b/src/tags/flac_private.c
index c29c91f..0732ca3 100644
--- a/src/tags/flac_private.c
+++ b/src/tags/flac_private.c
@@ -20,18 +20,21 @@
 
 #include <errno.h>
 
-static size_t
-read_func (GInputStream *istream,
-           void *buffer,
-           gsize count,
-           gboolean *eof,
-           GError **error)
+size_t
+et_flac_read_func (void *ptr,
+                   size_t size,
+                   size_t nmemb,
+                   FLAC__IOHandle handle)
 {
+    EtFlacReadState *state;
     gssize bytes_read;
 
-    *eof = FALSE;
+    state = (EtFlacReadState *)handle;
+
+    state->eof = FALSE;
 
-    bytes_read = g_input_stream_read (istream, buffer, count, NULL, error);
+    bytes_read = g_input_stream_read (state->istream, ptr, size * nmemb, NULL,
+                                      &state->error);
 
     if (bytes_read == -1)
     {
@@ -40,45 +43,17 @@ read_func (GInputStream *istream,
     }
     else if (bytes_read == 0)
     {
-        *eof = TRUE;
+        state->eof = TRUE;
     }
 
     return bytes_read;
 }
 
 size_t
-et_flac_read_read_func (void *ptr,
-                        size_t size,
-                        size_t nmemb,
-                        FLAC__IOHandle handle)
-{
-    EtFlacReadState *state;
-
-    state = (EtFlacReadState *)handle;
-
-    return read_func (G_INPUT_STREAM (state->istream), ptr, size * nmemb,
-                      &state->eof, &state->error);
-}
-
-size_t
-et_flac_write_read_func (void *ptr,
-                         size_t size,
-                         size_t nmemb,
-                         FLAC__IOHandle handle)
-{
-    EtFlacWriteState *state;
-
-    state = (EtFlacWriteState *)handle;
-
-    return read_func (G_INPUT_STREAM (state->istream), ptr, size * nmemb,
-                      &state->eof, &state->error);
-}
-
-size_t
-et_flac_write_write_func (const void *ptr,
-                          size_t size,
-                          size_t nmemb,
-                          FLAC__IOHandle handle)
+et_flac_write_func (const void *ptr,
+                    size_t size,
+                    size_t nmemb,
+                    FLAC__IOHandle handle)
 {
     EtFlacWriteState *state;
     gsize bytes_written;
@@ -98,15 +73,17 @@ et_flac_write_write_func (const void *ptr,
     return bytes_written;
 }
 
-static gint
-seek_func (GSeekable *seekable,
-           goffset offset,
-           gint whence,
-           GError **error)
+int
+et_flac_seek_func (FLAC__IOHandle handle,
+                   FLAC__int64 offset,
+                   int whence)
 {
     GSeekType seektype;
+    EtFlacReadState *state;
+
+    state = (EtFlacReadState *)handle;
 
-    if (!g_seekable_can_seek (seekable))
+    if (!g_seekable_can_seek (state->seekable))
     {
         errno = EBADF;
         return -1;
@@ -129,7 +106,8 @@ seek_func (GSeekable *seekable,
                 return -1;
         }
 
-        if (g_seekable_seek (seekable, offset, seektype, NULL, error))
+        if (g_seekable_seek (state->seekable, offset, seektype, NULL,
+                             &state->error))
         {
             return 0;
         }
@@ -142,70 +120,26 @@ seek_func (GSeekable *seekable,
     }
 }
 
-int
-et_flac_read_seek_func (FLAC__IOHandle handle,
-                        FLAC__int64 offset,
-                        int whence)
+FLAC__int64
+et_flac_tell_func (FLAC__IOHandle handle)
 {
     EtFlacReadState *state;
 
     state = (EtFlacReadState *)handle;
 
-    return seek_func (G_SEEKABLE (state->istream), offset, whence,
-                      &state->error);
-}
-
-int
-et_flac_write_seek_func (FLAC__IOHandle handle,
-                         FLAC__int64 offset,
-                         int whence)
-{
-    EtFlacWriteState *state;
-
-    state = (EtFlacWriteState *)handle;
-
-    /* Seeking on the IOStream causes both the input and output stream to have
-     * the same offset. */
-    return seek_func (G_SEEKABLE (state->iostream), offset, whence,
-                      &state->error);
-}
-
-static FLAC__int64
-tell_func (GSeekable *seekable)
-{
-    if (!g_seekable_can_seek (seekable))
+    if (!g_seekable_can_seek (state->seekable))
     {
         errno = EBADF;
         return -1;
     }
     else
     {
-        return g_seekable_tell (seekable);
+        return g_seekable_tell (state->seekable);
     }
 }
 
-FLAC__int64
-et_flac_read_tell_func (FLAC__IOHandle handle)
-{
-    EtFlacReadState *state;
-
-    state = (EtFlacReadState *)handle;
-
-    return tell_func (G_SEEKABLE (state->istream));
-}
-
-FLAC__int64
-et_flac_write_tell_func (FLAC__IOHandle handle)
-{
-    EtFlacWriteState *state;
-
-    state = (EtFlacWriteState *)handle;
-
-    return tell_func (G_SEEKABLE (state->iostream));
-}
-
 int
-et_flac_read_eof_func (FLAC__IOHandle handle)
+et_flac_eof_func (FLAC__IOHandle handle)
 {
     EtFlacReadState *state;
 
@@ -216,17 +150,6 @@ et_flac_read_eof_func (FLAC__IOHandle handle)
 }
 
 int
-et_flac_write_eof_func (FLAC__IOHandle handle)
-{
-    EtFlacWriteState *state;
-
-    state = (EtFlacWriteState *)handle;
-
-    /* EOF is not directly supported by GFileInputStream. */
-    return state->eof ? 1 : 0;
-}
-
-int
 et_flac_read_close_func (FLAC__IOHandle handle)
 {
     EtFlacReadState *state;
diff --git a/src/tags/flac_private.h b/src/tags/flac_private.h
index e009495..5c57076 100644
--- a/src/tags/flac_private.h
+++ b/src/tags/flac_private.h
@@ -36,6 +36,7 @@ G_BEGIN_DECLS
 typedef struct
 {
     GFileInputStream *istream;
+    GSeekable *seekable;
     gboolean eof;
     GError *error;
 } EtFlacReadState;
@@ -48,25 +49,28 @@ typedef struct
  */
 typedef struct
 {
-    GFile *file;
+    /* Begin fields copied from EtFlacReadState. */
     GFileInputStream *istream;
-    GFileOutputStream *ostream;
-    GFileIOStream *iostream;
+    GSeekable *seekable;
     gboolean eof;
     GError *error;
+    /* End fields copied from EtFlacReadState. */
+    GFile *file;
+    GFileOutputStream *ostream;
+    GFileIOStream *iostream;
 } EtFlacWriteState;
 
-size_t et_flac_read_read_func (void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);
-int et_flac_read_seek_func (FLAC__IOHandle handle, FLAC__int64 offset, int whence);
-FLAC__int64 et_flac_read_tell_func (FLAC__IOHandle handle);
-int et_flac_read_eof_func (FLAC__IOHandle handle);
+/* Used with both EtFlacReadState and EtFlacWriteState. */
+size_t et_flac_read_func (void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);
+int et_flac_seek_func (FLAC__IOHandle handle, FLAC__int64 offset, int whence);
+FLAC__int64 et_flac_tell_func (FLAC__IOHandle handle);
+int et_flac_eof_func (FLAC__IOHandle handle);
+
+/* Only to be used with EtFlacReadState. */
 int et_flac_read_close_func (FLAC__IOHandle handle);
 
-size_t et_flac_write_read_func (void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);
-size_t et_flac_write_write_func (const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);
-int et_flac_write_seek_func (FLAC__IOHandle handle, FLAC__int64 offset, int whence);
-FLAC__int64 et_flac_write_tell_func (FLAC__IOHandle handle);
-int et_flac_write_eof_func (FLAC__IOHandle handle);
+/* Only to be used with EtFlacWriteState. */
+size_t et_flac_write_func (const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle);
 int et_flac_write_close_func (FLAC__IOHandle handle);
 
 G_END_DECLS
diff --git a/src/tags/flac_tag.c b/src/tags/flac_tag.c
index 303c18e..e07fbc9 100644
--- a/src/tags/flac_tag.c
+++ b/src/tags/flac_tag.c
@@ -81,11 +81,10 @@ flac_tag_read_file_tag (GFile *file,
 {
     FLAC__Metadata_Chain *chain;
     EtFlacReadState state;
-    FLAC__IOCallbacks callbacks = { et_flac_read_read_func,
+    FLAC__IOCallbacks callbacks = { et_flac_read_func,
                                     NULL, /* Do not set a write callback. */
-                                    et_flac_read_seek_func,
-                                    et_flac_read_tell_func,
-                                    et_flac_read_eof_func,
+                                    et_flac_seek_func, et_flac_tell_func,
+                                    et_flac_eof_func,
                                     et_flac_read_close_func };
     FLAC__Metadata_Iterator *iter;
 
@@ -108,6 +107,7 @@ flac_tag_read_file_tag (GFile *file,
 
     state.error = NULL;
     state.istream = g_file_read (file, NULL, &state.error);
+    state.seekable = G_SEEKABLE (state.istream);
 
     if (!FLAC__metadata_chain_read_with_callbacks (chain, &state, callbacks))
     {
@@ -813,11 +813,9 @@ flac_tag_write_file_tag (const ET_File *ETFile,
     GFile *file;
     GFileIOStream *iostream;
     EtFlacWriteState state;
-    FLAC__IOCallbacks callbacks = { et_flac_write_read_func,
-                                    et_flac_write_write_func,
-                                    et_flac_write_seek_func,
-                                    et_flac_write_tell_func,
-                                    et_flac_write_eof_func,
+    FLAC__IOCallbacks callbacks = { et_flac_read_func, et_flac_write_func,
+                                    et_flac_seek_func, et_flac_tell_func,
+                                    et_flac_eof_func,
                                     et_flac_write_close_func };
     const gchar *filename;
     const gchar *filename_utf8;
@@ -867,6 +865,7 @@ flac_tag_write_file_tag (const ET_File *ETFile,
 
     state.istream = G_FILE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (iostream)));
     state.ostream = G_FILE_OUTPUT_STREAM (g_io_stream_get_output_stream (G_IO_STREAM (iostream)));
+    state.seekable = G_SEEKABLE (iostream);
     state.iostream = iostream;
 
     if (!FLAC__metadata_chain_read_with_callbacks (chain, &state, callbacks))
@@ -1165,6 +1164,7 @@ flac_tag_write_file_tag (const ET_File *ETFile,
         temp_state.error = NULL;
         temp_state.istream = G_FILE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM 
(temp_iostream)));
         temp_state.ostream = G_FILE_OUTPUT_STREAM (g_io_stream_get_output_stream (G_IO_STREAM 
(temp_iostream)));
+        temp_state.seekable = G_SEEKABLE (temp_iostream);
         temp_state.iostream = temp_iostream;
 
         if (!FLAC__metadata_chain_write_with_callbacks_and_tempfile (chain,


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